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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
"use strict";
const DOMException = require("domexception/webidl2js-wrapper");
const interfaces = require("../interfaces");
const { implForWrapper } = require("../generated/utils");
const { HTML_NS, SVG_NS } = require("./namespaces");
const { domSymbolTree } = require("./internal-constants");
const { validateAndExtract } = require("./validate-names");
const reportException = require("./runtime-script-errors");
const {
isValidCustomElementName, upgradeElement, lookupCEDefinition, enqueueCEUpgradeReaction
} = require("./custom-elements");
const INTERFACE_TAG_MAPPING = {
// https://html.spec.whatwg.org/multipage/dom.html#elements-in-the-dom%3Aelement-interface
// https://html.spec.whatwg.org/multipage/indices.html#elements-3
[HTML_NS]: {
HTMLElement: [
"abbr", "address", "article", "aside", "b", "bdi", "bdo", "cite", "code", "dd", "dfn", "dt", "em", "figcaption",
"figure", "footer", "header", "hgroup", "i", "kbd", "main", "mark", "nav", "noscript", "rp", "rt", "ruby", "s",
"samp", "section", "small", "strong", "sub", "summary", "sup", "u", "var", "wbr"
],
HTMLAnchorElement: ["a"],
HTMLAreaElement: ["area"],
HTMLAudioElement: ["audio"],
HTMLBaseElement: ["base"],
HTMLBodyElement: ["body"],
HTMLBRElement: ["br"],
HTMLButtonElement: ["button"],
HTMLCanvasElement: ["canvas"],
HTMLDataElement: ["data"],
HTMLDataListElement: ["datalist"],
HTMLDetailsElement: ["details"],
HTMLDialogElement: ["dialog"],
HTMLDirectoryElement: ["dir"],
HTMLDivElement: ["div"],
HTMLDListElement: ["dl"],
HTMLEmbedElement: ["embed"],
HTMLFieldSetElement: ["fieldset"],
HTMLFontElement: ["font"],
HTMLFormElement: ["form"],
HTMLFrameElement: ["frame"],
HTMLFrameSetElement: ["frameset"],
HTMLHeadingElement: ["h1", "h2", "h3", "h4", "h5", "h6"],
HTMLHeadElement: ["head"],
HTMLHRElement: ["hr"],
HTMLHtmlElement: ["html"],
HTMLIFrameElement: ["iframe"],
HTMLImageElement: ["img"],
HTMLInputElement: ["input"],
HTMLLabelElement: ["label"],
HTMLLegendElement: ["legend"],
HTMLLIElement: ["li"],
HTMLLinkElement: ["link"],
HTMLMapElement: ["map"],
HTMLMarqueeElement: ["marquee"],
HTMLMediaElement: [],
HTMLMenuElement: ["menu"],
HTMLMetaElement: ["meta"],
HTMLMeterElement: ["meter"],
HTMLModElement: ["del", "ins"],
HTMLObjectElement: ["object"],
HTMLOListElement: ["ol"],
HTMLOptGroupElement: ["optgroup"],
HTMLOptionElement: ["option"],
HTMLOutputElement: ["output"],
HTMLParagraphElement: ["p"],
HTMLParamElement: ["param"],
HTMLPictureElement: ["picture"],
HTMLPreElement: ["listing", "pre", "xmp"],
HTMLProgressElement: ["progress"],
HTMLQuoteElement: ["blockquote", "q"],
HTMLScriptElement: ["script"],
HTMLSelectElement: ["select"],
HTMLSlotElement: ["slot"],
HTMLSourceElement: ["source"],
HTMLSpanElement: ["span"],
HTMLStyleElement: ["style"],
HTMLTableCaptionElement: ["caption"],
HTMLTableCellElement: ["th", "td"],
HTMLTableColElement: ["col", "colgroup"],
HTMLTableElement: ["table"],
HTMLTimeElement: ["time"],
HTMLTitleElement: ["title"],
HTMLTableRowElement: ["tr"],
HTMLTableSectionElement: ["thead", "tbody", "tfoot"],
HTMLTemplateElement: ["template"],
HTMLTextAreaElement: ["textarea"],
HTMLTrackElement: ["track"],
HTMLUListElement: ["ul"],
HTMLUnknownElement: [],
HTMLVideoElement: ["video"]
},
[SVG_NS]: {
SVGElement: [],
SVGGraphicsElement: [],
SVGSVGElement: ["svg"],
SVGTitleElement: ["title"]
}
};
const TAG_INTERFACE_LOOKUP = {};
for (const namespace of [HTML_NS, SVG_NS]) {
TAG_INTERFACE_LOOKUP[namespace] = {};
const interfaceNames = Object.keys(INTERFACE_TAG_MAPPING[namespace]);
for (const interfaceName of interfaceNames) {
const tagNames = INTERFACE_TAG_MAPPING[namespace][interfaceName];
for (const tagName of tagNames) {
TAG_INTERFACE_LOOKUP[namespace][tagName] = interfaceName;
}
}
}
const UNKNOWN_HTML_ELEMENTS_NAMES = ["applet", "bgsound", "blink", "isindex", "keygen", "multicol", "nextid", "spacer"];
const HTML_ELEMENTS_NAMES = [
"acronym", "basefont", "big", "center", "nobr", "noembed", "noframes", "plaintext", "rb", "rtc",
"strike", "tt"
];
// https://html.spec.whatwg.org/multipage/dom.html#elements-in-the-dom:element-interface
function getHTMLElementInterface(name) {
if (UNKNOWN_HTML_ELEMENTS_NAMES.includes(name)) {
return interfaces.getInterfaceWrapper("HTMLUnknownElement");
}
if (HTML_ELEMENTS_NAMES.includes(name)) {
return interfaces.getInterfaceWrapper("HTMLElement");
}
const specDefinedInterface = TAG_INTERFACE_LOOKUP[HTML_NS][name];
if (specDefinedInterface !== undefined) {
return interfaces.getInterfaceWrapper(specDefinedInterface);
}
if (isValidCustomElementName(name)) {
return interfaces.getInterfaceWrapper("HTMLElement");
}
return interfaces.getInterfaceWrapper("HTMLUnknownElement");
}
// https://svgwg.org/svg2-draft/types.html#ElementsInTheSVGDOM
function getSVGInterface(name) {
const specDefinedInterface = TAG_INTERFACE_LOOKUP[SVG_NS][name];
if (specDefinedInterface !== undefined) {
return interfaces.getInterfaceWrapper(specDefinedInterface);
}
return interfaces.getInterfaceWrapper("SVGElement");
}
// Returns the list of valid tag names that can bo associated with a element given its namespace and name.
function getValidTagNames(namespace, name) {
if (INTERFACE_TAG_MAPPING[namespace] && INTERFACE_TAG_MAPPING[namespace][name]) {
return INTERFACE_TAG_MAPPING[namespace][name];
}
return [];
}
// https://dom.spec.whatwg.org/#concept-create-element
function createElement(
document,
localName,
namespace,
prefix = null,
isValue = null,
synchronousCE = false
) {
let result = null;
const { _globalObject } = document;
const definition = lookupCEDefinition(document, namespace, localName, isValue);
if (definition !== null && definition.name !== localName) {
const elementInterface = getHTMLElementInterface(localName);
result = elementInterface.createImpl(_globalObject, [], {
ownerDocument: document,
localName,
namespace: HTML_NS,
prefix,
ceState: "undefined",
ceDefinition: null,
isValue
});
if (synchronousCE) {
upgradeElement(definition, result);
} else {
enqueueCEUpgradeReaction(result, definition);
}
} else if (definition !== null) {
if (synchronousCE) {
try {
const C = definition.constructor;
const resultWrapper = C.construct();
result = implForWrapper(resultWrapper);
if (!result._ceState || !result._ceDefinition || result._namespaceURI !== HTML_NS) {
throw new TypeError("Internal error: Invalid custom element.");
}
if (result._attributeList.length !== 0) {
throw DOMException.create(_globalObject, ["Unexpected attributes.", "NotSupportedError"]);
}
if (domSymbolTree.hasChildren(result)) {
throw DOMException.create(_globalObject, ["Unexpected child nodes.", "NotSupportedError"]);
}
if (domSymbolTree.parent(result)) {
throw DOMException.create(_globalObject, ["Unexpected element parent.", "NotSupportedError"]);
}
if (result._ownerDocument !== document) {
throw DOMException.create(_globalObject, ["Unexpected element owner document.", "NotSupportedError"]);
}
if (result._namespaceURI !== namespace) {
throw DOMException.create(_globalObject, ["Unexpected element namespace URI.", "NotSupportedError"]);
}
if (result._localName !== localName) {
throw DOMException.create(_globalObject, ["Unexpected element local name.", "NotSupportedError"]);
}
result._prefix = prefix;
result._isValue = isValue;
} catch (error) {
reportException(document._defaultView, error);
const interfaceWrapper = interfaces.getInterfaceWrapper("HTMLUnknownElement");
result = interfaceWrapper.createImpl(_globalObject, [], {
ownerDocument: document,
localName,
namespace: HTML_NS,
prefix,
ceState: "failed",
ceDefinition: null,
isValue: null
});
}
} else {
const interfaceWrapper = interfaces.getInterfaceWrapper("HTMLElement");
result = interfaceWrapper.createImpl(_globalObject, [], {
ownerDocument: document,
localName,
namespace: HTML_NS,
prefix,
ceState: "undefined",
ceDefinition: null,
isValue: null
});
enqueueCEUpgradeReaction(result, definition);
}
} else {
let elementInterface;
switch (namespace) {
case HTML_NS:
elementInterface = getHTMLElementInterface(localName);
break;
case SVG_NS:
elementInterface = getSVGInterface(localName);
break;
default:
elementInterface = interfaces.getInterfaceWrapper("Element");
break;
}
result = elementInterface.createImpl(_globalObject, [], {
ownerDocument: document,
localName,
namespace,
prefix,
ceState: "uncustomized",
ceDefinition: null,
isValue
});
if (namespace === HTML_NS && (isValidCustomElementName(localName) || isValue !== null)) {
result._ceState = "undefined";
}
}
return result;
}
// https://dom.spec.whatwg.org/#internal-createelementns-steps
function internalCreateElementNSSteps(document, namespace, qualifiedName, options) {
const extracted = validateAndExtract(document._globalObject, namespace, qualifiedName);
let isValue = null;
if (options && options.is !== undefined) {
isValue = options.is;
}
return createElement(
document,
extracted.localName,
extracted.namespace,
extracted.prefix,
isValue,
true
);
}
module.exports = {
createElement,
internalCreateElementNSSteps,
getValidTagNames,
getHTMLElementInterface
};
|