-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.js
178 lines (173 loc) · 4.89 KB
/
dom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// 🐈🌑 lune :: dom.js
// ===================================================================
//
// Copyright © 2021 Margaret KIBI.
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can
// obtain one at <https://mozilla.org/MPL/2.0/>.
import { MATHML, NULL, SVG, XHTML, XML } from "./symbols.js";
/**
* A `String` object with an associated `prefix`.
*
* Please note the following:—
*
* + This is a `String` object, not a string primative.
* Use `==` not `===` for comparing with other strings.
*
* + The value of `name` *should be*, but is not required to be, a
* (LE)IRI.
* It may be something entirely different.
*
* + The value of `prefix` *should be*, but is not required to be, a
* `NCName`.
* If it isn’t, an error will be thrown should you actually
* attempt to *use* this `Namespace`.
*/
export class Namespace extends String {
static [MATHML] = new Namespace(
"math",
"http://www.w3.org/1998/Math/MathML",
);
static [NULL] = new Namespace("", "");
static [SVG] = new Namespace("svg", "http://www.w3.org/2000/svg");
static [XHTML] = new Namespace(
"html",
"http://www.w3.org/1999/xhtml",
);
static [XML] = new Namespace(
"xml",
"http://www.w3.org/XML/1998/namespace",
);
/**
* Makes a new `Namespace` object.
*
* @argument {any} prefix
* @argument {any} name
*/
constructor(prefix, name) {
super(name);
/** @readonly */
this.prefix = String(prefix);
return Object.freeze(this);
}
}
/**
* A `String` object with an associated `namespace`.
*
* Please note the following:—
*
* + This is a `String` object, not a string primative.
* Use `==` not `===` for comparing with other strings.
*
* + The value of `namespace` *should be* a `Namespace`.
* It will attempt to create one otherwise.
*/
export class QualifiedName extends String {
/**
* Makes a new `QualifiedName` object.
*
* @argument {any} namespace
* @argument {any} localName
*/
constructor(namespace, localName) {
const prefix = namespace instanceof Namespace
? namespace.prefix
: "";
super(prefix == "" ? localName : `${prefix}:${localName}`);
/** @readonly */
this.localName = String(localName);
/** @readonly */
this.namespace = new Namespace(prefix, namespace);
return Object.freeze(this);
}
}
/**
* @this {?{document: Document}=}
* @argument {QualifiedName} name
* @argument {{[index: string]: string} | Map<string | QualifiedName, string>} attributes
* @argument {TemplateStringsArray} strings
* @argument {...(string | Node |(string | Node)[])} substitutions
* @returns {Element}
*/
function makeElement(name, attributes, strings, ...substitutions) {
const document = this?.document ?? globalThis.document;
const namespace = name.namespace;
const element = document.createElementNS(
String(namespace),
String(name),
);
const attributeEntries = attributes instanceof Map
? attributes.entries()
: Object.entries(attributes);
for (const [key, value] of attributeEntries) {
if (key instanceof QualifiedName) {
const keyNamespace = key.namespace;
element.setAttributeNS(
String(keyNamespace),
String(key),
value,
);
} else {
element.setAttribute(key, value);
}
}
/** @type {(string | Node)[]} */
const children = [];
for (
let i = 0;
i < Math.max(strings.length, substitutions.length);
i++
) {
if (Object.prototype.hasOwnProperty.call(strings, i)) {
const child = strings[i];
if (child != "") {
children.push(child);
}
}
if (Object.prototype.hasOwnProperty.call(substitutions, i)) {
const child = substitutions[i];
if (Array.isArray(child)) {
for (const subChild of child) {
if (subChild != "") {
children.push(subChild);
}
}
} else if (child != "") {
children.push(child);
}
}
}
element.append(...children);
return element;
}
/**
* Used to make tags for a template literal to return an element.
*
* Examples:—
*
* ```js
* const divElement = makeTag("div")`content`;
* const withAttrs = makeTag("div", {class: "foo"})`content`;
* const withNamespace = makeTag(
* new QualifiedName(Namespace[SVG], "svg"),
* )`content`;
* ```
*
* The content may contain substitutions, which may be used to provide
* child elements.
*
* @this {?{document: Document}=}
* @argument {string | QualifiedName} name
* @argument {({[index: string]: string} | Map<string | QualifiedName, string>)=} attributes
*/
export function makeTag(name, attributes = {}) {
return makeElement.bind(
this,
name instanceof QualifiedName
? name
: new QualifiedName(Namespace[XHTML], name),
attributes,
);
}