Element-impl.js
16.1 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
"use strict";
const { addNwsapi } = require("../helpers/selectors");
const { HTML_NS } = require("../helpers/namespaces");
const { mixin, memoizeQuery } = require("../../utils");
const idlUtils = require("../generated/utils");
const NodeImpl = require("./Node-impl").implementation;
const ParentNodeImpl = require("./ParentNode-impl").implementation;
const ChildNodeImpl = require("./ChildNode-impl").implementation;
const attributes = require("../attributes");
const namedPropertiesWindow = require("../named-properties-window");
const NODE_TYPE = require("../node-type");
const { parseFragment } = require("../../browser/parser");
const { fragmentSerialization } = require("../domparsing/serialization");
const { domSymbolTree } = require("../helpers/internal-constants");
const DOMException = require("domexception/webidl2js-wrapper");
const DOMTokenList = require("../generated/DOMTokenList");
const NamedNodeMap = require("../generated/NamedNodeMap");
const validateNames = require("../helpers/validate-names");
const { asciiLowercase, asciiUppercase } = require("../helpers/strings");
const { listOfElementsWithQualifiedName, listOfElementsWithNamespaceAndLocalName,
listOfElementsWithClassNames } = require("../node");
const SlotableMixinImpl = require("./Slotable-impl").implementation;
const NonDocumentTypeChildNode = require("./NonDocumentTypeChildNode-impl").implementation;
const ShadowRoot = require("../generated/ShadowRoot");
const Text = require("../generated/Text");
const { isValidHostElementName } = require("../helpers/shadow-dom");
const { isValidCustomElementName, lookupCEDefinition } = require("../helpers/custom-elements");
function attachId(id, elm, doc) {
if (id && elm && doc) {
if (!doc._ids[id]) {
doc._ids[id] = [];
}
doc._ids[id].push(elm);
}
}
function detachId(id, elm, doc) {
if (id && elm && doc) {
if (doc._ids && doc._ids[id]) {
const elms = doc._ids[id];
for (let i = 0; i < elms.length; i++) {
if (elms[i] === elm) {
elms.splice(i, 1);
--i;
}
}
if (elms.length === 0) {
delete doc._ids[id];
}
}
}
}
class ElementImpl extends NodeImpl {
constructor(globalObject, args, privateData) {
super(globalObject, args, privateData);
this._initSlotableMixin();
this._namespaceURI = privateData.namespace;
this._prefix = privateData.prefix;
this._localName = privateData.localName;
this._ceState = privateData.ceState;
this._ceDefinition = privateData.ceDefinition;
this._isValue = privateData.isValue;
this._shadowRoot = null;
this._ceReactionQueue = [];
this.nodeType = NODE_TYPE.ELEMENT_NODE;
this.scrollTop = 0;
this.scrollLeft = 0;
this._attributeList = [];
// Used for caching.
this._attributesByNameMap = new Map();
this._attributes = NamedNodeMap.createImpl(this._globalObject, [], {
element: this
});
}
_attach() {
namedPropertiesWindow.nodeAttachedToDocument(this);
const id = this.getAttributeNS(null, "id");
if (id) {
attachId(id, this, this._ownerDocument);
}
super._attach();
}
_detach() {
super._detach();
namedPropertiesWindow.nodeDetachedFromDocument(this);
const id = this.getAttributeNS(null, "id");
if (id) {
detachId(id, this, this._ownerDocument);
}
}
_attrModified(name, value, oldValue) {
this._modified();
namedPropertiesWindow.elementAttributeModified(this, name, value, oldValue);
if (name === "id" && this._attached) {
const doc = this._ownerDocument;
detachId(oldValue, this, doc);
attachId(value, this, doc);
}
// update classList
if (name === "class" && this._classList !== undefined) {
this._classList.attrModified();
}
this._attrModifiedSlotableMixin(name, value, oldValue);
}
get namespaceURI() {
return this._namespaceURI;
}
get prefix() {
return this._prefix;
}
get localName() {
return this._localName;
}
get _qualifiedName() {
return this._prefix !== null ? this._prefix + ":" + this._localName : this._localName;
}
get tagName() {
let qualifiedName = this._qualifiedName;
if (this.namespaceURI === HTML_NS && this._ownerDocument._parsingMode === "html") {
qualifiedName = asciiUppercase(qualifiedName);
}
return qualifiedName;
}
get attributes() {
return this._attributes;
}
// https://w3c.github.io/DOM-Parsing/#dom-element-outerhtml
get outerHTML() {
// TODO: maybe parse5 can give us a hook where it serializes the node itself too:
// https://github.com/inikulin/parse5/issues/230
// Alternatively, if we can create a virtual node in domSymbolTree, that'd also work.
// It's currently prevented by the fact that a node can't be duplicated in the same tree.
// Then we could get rid of all the code for childNodesForSerializing.
return fragmentSerialization({ childNodesForSerializing: [this], _ownerDocument: this._ownerDocument }, {
requireWellFormed: true,
globalObject: this._globalObject
});
}
set outerHTML(markup) {
let parent = domSymbolTree.parent(this);
const document = this._ownerDocument;
if (!parent) {
return;
}
if (parent.nodeType === NODE_TYPE.DOCUMENT_NODE) {
throw DOMException.create(this._globalObject, [
"Modifications are not allowed for this document",
"NoModificationAllowedError"
]);
}
if (parent.nodeType === NODE_TYPE.DOCUMENT_FRAGMENT_NODE) {
parent = document.createElementNS(HTML_NS, "body");
}
const fragment = parseFragment(markup, parent);
const contextObjectParent = domSymbolTree.parent(this);
contextObjectParent._replace(fragment, this);
}
// https://w3c.github.io/DOM-Parsing/#dfn-innerhtml
get innerHTML() {
return fragmentSerialization(this, {
requireWellFormed: true,
globalObject: this._globalObject
});
}
set innerHTML(markup) {
const fragment = parseFragment(markup, this);
let contextObject = this;
if (this.localName === "template" && this.namespaceURI === HTML_NS) {
contextObject = contextObject._templateContents;
}
contextObject._replaceAll(fragment);
}
get classList() {
if (this._classList === undefined) {
this._classList = DOMTokenList.createImpl(this._globalObject, [], {
element: this,
attributeLocalName: "class"
});
}
return this._classList;
}
hasAttributes() {
return attributes.hasAttributes(this);
}
getAttributeNames() {
return attributes.attributeNames(this);
}
getAttribute(name) {
const attr = attributes.getAttributeByName(this, name);
if (!attr) {
return null;
}
return attr._value;
}
getAttributeNS(namespace, localName) {
const attr = attributes.getAttributeByNameNS(this, namespace, localName);
if (!attr) {
return null;
}
return attr._value;
}
setAttribute(name, value) {
validateNames.name(this._globalObject, name);
if (this._namespaceURI === HTML_NS && this._ownerDocument._parsingMode === "html") {
name = asciiLowercase(name);
}
const attribute = attributes.getAttributeByName(this, name);
if (attribute === null) {
const newAttr = this._ownerDocument._createAttribute({
localName: name,
value
});
attributes.appendAttribute(this, newAttr);
return;
}
attributes.changeAttribute(this, attribute, value);
}
setAttributeNS(namespace, name, value) {
const extracted = validateNames.validateAndExtract(this._globalObject, namespace, name);
// Because of widespread use of this method internally, e.g. to manually implement attribute/content reflection, we
// centralize the conversion to a string here, so that all call sites don't have to do it.
value = `${value}`;
attributes.setAttributeValue(this, extracted.localName, value, extracted.prefix, extracted.namespace);
}
removeAttribute(name) {
attributes.removeAttributeByName(this, name);
}
removeAttributeNS(namespace, localName) {
attributes.removeAttributeByNameNS(this, namespace, localName);
}
toggleAttribute(qualifiedName, force) {
validateNames.name(this._globalObject, qualifiedName);
if (this._namespaceURI === HTML_NS && this._ownerDocument._parsingMode === "html") {
qualifiedName = asciiLowercase(qualifiedName);
}
const attribute = attributes.getAttributeByName(this, qualifiedName);
if (attribute === null) {
if (force === undefined || force === true) {
const newAttr = this._ownerDocument._createAttribute({
localName: qualifiedName,
value: ""
});
attributes.appendAttribute(this, newAttr);
return true;
}
return false;
}
if (force === undefined || force === false) {
attributes.removeAttributeByName(this, qualifiedName);
return false;
}
return true;
}
hasAttribute(name) {
if (this._namespaceURI === HTML_NS && this._ownerDocument._parsingMode === "html") {
name = asciiLowercase(name);
}
return attributes.hasAttributeByName(this, name);
}
hasAttributeNS(namespace, localName) {
if (namespace === "") {
namespace = null;
}
return attributes.hasAttributeByNameNS(this, namespace, localName);
}
getAttributeNode(name) {
return attributes.getAttributeByName(this, name);
}
getAttributeNodeNS(namespace, localName) {
return attributes.getAttributeByNameNS(this, namespace, localName);
}
setAttributeNode(attr) {
// eslint-disable-next-line no-restricted-properties
return attributes.setAttribute(this, attr);
}
setAttributeNodeNS(attr) {
// eslint-disable-next-line no-restricted-properties
return attributes.setAttribute(this, attr);
}
removeAttributeNode(attr) {
// eslint-disable-next-line no-restricted-properties
if (!attributes.hasAttribute(this, attr)) {
throw DOMException.create(this._globalObject, [
"Tried to remove an attribute that was not present",
"NotFoundError"
]);
}
// eslint-disable-next-line no-restricted-properties
attributes.removeAttribute(this, attr);
return attr;
}
getBoundingClientRect() {
return {
bottom: 0,
height: 0,
left: 0,
right: 0,
top: 0,
width: 0
};
}
getClientRects() {
return [];
}
get scrollWidth() {
return 0;
}
get scrollHeight() {
return 0;
}
get clientTop() {
return 0;
}
get clientLeft() {
return 0;
}
get clientWidth() {
return 0;
}
get clientHeight() {
return 0;
}
// https://dom.spec.whatwg.org/#dom-element-attachshadow
attachShadow(init) {
const { _ownerDocument, _namespaceURI, _localName, _isValue } = this;
if (this.namespaceURI !== HTML_NS) {
throw DOMException.create(this._globalObject, [
"This element does not support attachShadow. This element is not part of the HTML namespace.",
"NotSupportedError"
]);
}
if (!isValidHostElementName(_localName) && !isValidCustomElementName(_localName)) {
const message = "This element does not support attachShadow. This element is not a custom element nor " +
"a standard element supporting a shadow root.";
throw DOMException.create(this._globalObject, [message, "NotSupportedError"]);
}
if (isValidCustomElementName(_localName) || _isValue) {
const definition = lookupCEDefinition(_ownerDocument, _namespaceURI, _localName, _isValue);
if (definition && definition.disableShadow) {
throw DOMException.create(this._globalObject, [
"Shadow root cannot be create on a custom element with disabled shadow",
"NotSupportedError"
]);
}
}
if (this._shadowRoot !== null) {
throw DOMException.create(this._globalObject, [
"Shadow root cannot be created on a host which already hosts a shadow tree.",
"NotSupportedError"
]);
}
const shadow = ShadowRoot.createImpl(this._globalObject, [], {
ownerDocument: this.ownerDocument,
mode: init.mode,
host: this
});
this._shadowRoot = shadow;
return shadow;
}
// https://dom.spec.whatwg.org/#dom-element-shadowroot
get shadowRoot() {
const shadow = this._shadowRoot;
if (shadow === null || shadow.mode === "closed") {
return null;
}
return shadow;
}
// https://dom.spec.whatwg.org/#insert-adjacent
_insertAdjacent(element, where, node) {
where = asciiLowercase(where);
if (where === "beforebegin") {
if (element.parentNode === null) {
return null;
}
return element.parentNode._preInsert(node, element);
}
if (where === "afterbegin") {
return element._preInsert(node, element.firstChild);
}
if (where === "beforeend") {
return element._preInsert(node, null);
}
if (where === "afterend") {
if (element.parentNode === null) {
return null;
}
return element.parentNode._preInsert(node, element.nextSibling);
}
throw DOMException.create(this._globalObject, [
'Must provide one of "beforebegin", "afterbegin", "beforeend", or "afterend".',
"SyntaxError"
]);
}
insertAdjacentElement(where, element) {
return this._insertAdjacent(this, where, element);
}
insertAdjacentText(where, data) {
const text = Text.createImpl(this._globalObject, [], { data, ownerDocument: this._ownerDocument });
this._insertAdjacent(this, where, text);
}
// https://w3c.github.io/DOM-Parsing/#dom-element-insertadjacenthtml
insertAdjacentHTML(position, text) {
position = asciiLowercase(position);
let context;
switch (position) {
case "beforebegin":
case "afterend": {
context = this.parentNode;
if (context === null || context.nodeType === NODE_TYPE.DOCUMENT_NODE) {
throw DOMException.create(this._globalObject, [
"Cannot insert HTML adjacent to parent-less nodes or children of document nodes.",
"NoModificationAllowedError"
]);
}
break;
}
case "afterbegin":
case "beforeend": {
context = this;
break;
}
default: {
throw DOMException.create(this._globalObject, [
'Must provide one of "beforebegin", "afterbegin", "beforeend", or "afterend".',
"SyntaxError"
]);
}
}
if (
context.nodeType !== NODE_TYPE.ELEMENT_NODE ||
(
context._ownerDocument._parsingMode === "html" &&
context._localName === "html" &&
context._namespaceURI === HTML_NS
)
) {
context = context._ownerDocument.createElement("body");
}
const fragment = parseFragment(text, context);
switch (position) {
case "beforebegin": {
this.parentNode._insert(fragment, this);
break;
}
case "afterbegin": {
this._insert(fragment, this.firstChild);
break;
}
case "beforeend": {
this._append(fragment);
break;
}
case "afterend": {
this.parentNode._insert(fragment, this.nextSibling);
break;
}
}
}
closest(selectors) {
const matcher = addNwsapi(this);
return matcher.closest(selectors, idlUtils.wrapperForImpl(this));
}
}
mixin(ElementImpl.prototype, NonDocumentTypeChildNode.prototype);
mixin(ElementImpl.prototype, ParentNodeImpl.prototype);
mixin(ElementImpl.prototype, ChildNodeImpl.prototype);
mixin(ElementImpl.prototype, SlotableMixinImpl.prototype);
ElementImpl.prototype.getElementsByTagName = memoizeQuery(function (qualifiedName) {
return listOfElementsWithQualifiedName(qualifiedName, this);
});
ElementImpl.prototype.getElementsByTagNameNS = memoizeQuery(function (namespace, localName) {
return listOfElementsWithNamespaceAndLocalName(namespace, localName, this);
});
ElementImpl.prototype.getElementsByClassName = memoizeQuery(function (classNames) {
return listOfElementsWithClassNames(classNames, this);
});
ElementImpl.prototype.matches = function (selectors) {
const matcher = addNwsapi(this);
return matcher.match(selectors, idlUtils.wrapperForImpl(this));
};
ElementImpl.prototype.webkitMatchesSelector = ElementImpl.prototype.matches;
module.exports = {
implementation: ElementImpl
};