-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathNode.js
528 lines (498 loc) · 19.2 KB
/
Node.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
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
/**
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
import * as utils from '../utils.js';
import {getScopingShim, removeShadyScoping, replaceShadyScoping,
treeVisitor, currentScopeForNode, currentScopeIsCorrect } from '../style-scoping.js';
import {shadyDataForNode, ensureShadyDataForNode} from '../shady-data.js';
import {recordInsertBefore, recordRemoveChild} from '../link-nodes.js';
import {ownerShadyRootForNode} from '../attach-shadow.js';
const doc = window.document;
const preferPerformance = utils.settings.preferPerformance;
const nativeIsConnectedAccessors =
/** @type {ObjectPropertyDescriptor} */(
Object.getOwnPropertyDescriptor(Node.prototype, 'isConnected')
);
const nativeIsConnected = nativeIsConnectedAccessors && nativeIsConnectedAccessors.get;
export function clearNode(node) {
let firstChild;
while ((firstChild = node[utils.SHADY_PREFIX + 'firstChild'])) {
node[utils.SHADY_PREFIX + 'removeChild'](firstChild);
}
}
function removeOwnerShadyRoot(node) {
// optimization: only reset the tree if node is actually in a root
if (hasCachedOwnerRoot(node)) {
for (let n=node[utils.SHADY_PREFIX + 'firstChild']; n; n = n[utils.SHADY_PREFIX + 'nextSibling']) {
removeOwnerShadyRoot(n);
}
}
const nodeData = shadyDataForNode(node);
if (nodeData) {
nodeData.ownerShadyRoot = undefined;
}
}
function hasCachedOwnerRoot(node) {
const nodeData = shadyDataForNode(node);
return Boolean(nodeData && nodeData.ownerShadyRoot !== undefined);
}
/**
* Finds the first flattened node that is composed in the node's parent.
* If the given node is a slot, then the first flattened node is returned
* if it exists, otherwise advance to the node's nextSibling.
* @param {Node} node within which to find first composed node
* @returns {Node} first composed node
*/
function firstComposedNode(node) {
let composed = node;
if (node && node.localName === 'slot') {
const nodeData = shadyDataForNode(node);
const flattened = nodeData && nodeData.flattenedNodes;
// Note, if `flattened` is falsey, it means that the containing shadowRoot
// has not rendered and therefore the `<slot>` is still in the composed
// DOM. If that's the case the `<slot>` is the first composed node.
if (flattened) {
composed = flattened.length ? flattened[0] :
firstComposedNode(node[utils.SHADY_PREFIX + 'nextSibling']);
}
}
return composed;
}
/**
* @param {Node} node
* @param {Node=} addedNode
* @param {Node=} removedNode
*/
function scheduleObserver(node, addedNode, removedNode) {
const nodeData = shadyDataForNode(node);
const observer = nodeData && nodeData.observer;
if (observer) {
if (addedNode) {
if (addedNode.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
for (let i = 0, l = addedNode.childNodes.length; i < l; i++) {
observer.addedNodes.push(addedNode.childNodes[i]);
}
} else {
observer.addedNodes.push(addedNode);
}
}
if (removedNode) {
observer.removedNodes.push(removedNode);
}
observer.schedule();
}
}
export const NodePatches = utils.getOwnPropertyDescriptors({
/** @this {Node} */
get parentNode() {
const nodeData = shadyDataForNode(this);
const l = nodeData && nodeData.parentNode;
return l !== undefined ? l : this[utils.NATIVE_PREFIX + 'parentNode'];
},
/** @this {Node} */
get firstChild() {
const nodeData = shadyDataForNode(this);
const l = nodeData && nodeData.firstChild;
return l !== undefined ? l : this[utils.NATIVE_PREFIX + 'firstChild'];
},
/** @this {Node} */
get lastChild() {
const nodeData = shadyDataForNode(this);
const l = nodeData && nodeData.lastChild;
return l !== undefined ? l : this[utils.NATIVE_PREFIX + 'lastChild'];
},
/** @this {Node} */
get nextSibling() {
const nodeData = shadyDataForNode(this);
const l = nodeData && nodeData.nextSibling;
return l !== undefined ? l : this[utils.NATIVE_PREFIX + 'nextSibling'];
},
/** @this {Node} */
get previousSibling() {
const nodeData = shadyDataForNode(this);
const l = nodeData && nodeData.previousSibling;
return l !== undefined ? l : this[utils.NATIVE_PREFIX + 'previousSibling'];
},
/** @this {Node} */
get childNodes() {
let childNodes;
if (utils.isTrackingLogicalChildNodes(this)) {
const nodeData = shadyDataForNode(this);
if (!nodeData.childNodes) {
nodeData.childNodes = [];
for (let n=this[utils.SHADY_PREFIX + 'firstChild']; n; n=n[utils.SHADY_PREFIX + 'nextSibling']) {
nodeData.childNodes.push(n);
}
}
childNodes = nodeData.childNodes;
} else {
childNodes = this[utils.NATIVE_PREFIX + 'childNodes'];
}
childNodes.item = function(index) {
return childNodes[index];
}
return childNodes;
},
/** @this {Node} */
get parentElement() {
const nodeData = shadyDataForNode(this);
let l = nodeData && nodeData.parentNode;
if (l && l.nodeType !== Node.ELEMENT_NODE) {
l = null;
}
return l !== undefined ? l : this[utils.NATIVE_PREFIX + 'parentElement'];
},
/** @this {Node} */
get isConnected() {
if (nativeIsConnected && nativeIsConnected.call(this)) {
return true;
}
if (this.nodeType == Node.DOCUMENT_FRAGMENT_NODE) {
return false;
}
// Fast path for distributed nodes.
const ownerDocument = this.ownerDocument;
if (utils.hasDocumentContains) {
if (ownerDocument[utils.NATIVE_PREFIX + 'contains'](this)) {
return true;
}
} else if (ownerDocument.documentElement &&
ownerDocument.documentElement[utils.NATIVE_PREFIX + 'contains'](this)) {
return true;
}
// Slow path for non-distributed nodes.
let node = this;
while (node && !(node instanceof Document)) {
node = node[utils.SHADY_PREFIX + 'parentNode'] || (utils.isShadyRoot(node) ? /** @type {ShadowRoot} */(node).host : undefined);
}
return !!(node && node instanceof Document);
},
/** @this {Node} */
get textContent() {
if (utils.isTrackingLogicalChildNodes(this)) {
let tc = [];
for (let n=this[utils.SHADY_PREFIX + 'firstChild']; n; n = n[utils.SHADY_PREFIX + 'nextSibling']) {
if (n.nodeType !== Node.COMMENT_NODE) {
tc.push(n[utils.SHADY_PREFIX + 'textContent']);
}
}
return tc.join('');
} else {
return this[utils.NATIVE_PREFIX + 'textContent'];
}
},
/**
* @this {Node}
* @param {string} value
*/
set textContent(value) {
if (typeof value === 'undefined' || value === null) {
value = ''
}
switch (this.nodeType) {
case Node.ELEMENT_NODE:
case Node.DOCUMENT_FRAGMENT_NODE:
if (!utils.isTrackingLogicalChildNodes(this) && utils.settings.hasDescriptors) {
// may be removing a nested slot but fast path if we know we are not.
const firstChild = this[utils.SHADY_PREFIX + 'firstChild'];
if (firstChild != this[utils.SHADY_PREFIX + 'lastChild'] ||
(firstChild && firstChild.nodeType != Node.TEXT_NODE)) {
clearNode(this);
}
this[utils.NATIVE_PREFIX + 'textContent'] = value;
} else {
clearNode(this);
// Document fragments must have no childNodes if setting a blank string
if (value.length > 0 || this.nodeType === Node.ELEMENT_NODE) {
this[utils.SHADY_PREFIX + 'insertBefore'](document.createTextNode(value))
}
}
break;
default:
// Note, be wary of patching `nodeValue`.
this.nodeValue = value;
break;
}
},
// Patched `insertBefore`. Note that all mutations that add nodes are routed
// here. When a <slot> is added or a node is added to a host with a shadowRoot
// with a slot, a standard dom `insert` call is aborted and `_asyncRender`
// is called on the relevant shadowRoot. In all other cases, a standard dom
// `insert` can be made, but the location and ref_node may need to be changed.
/**
* @this {Node}
* @param {Node} node
* @param {Node=} ref_node
*/
insertBefore(node, ref_node) {
// optimization: assume native insertBefore is ok if the nodes are not in the document.
if (this.ownerDocument !== doc && node.ownerDocument !== doc) {
this[utils.NATIVE_PREFIX + 'insertBefore'](node, ref_node);
return node;
}
if (node === this) {
throw Error(`Failed to execute 'appendChild' on 'Node': The new child element contains the parent.`);
}
if (ref_node) {
const refData = shadyDataForNode(ref_node);
const p = refData && refData.parentNode;
if ((p !== undefined && p !== this) ||
(p === undefined && ref_node[utils.NATIVE_PREFIX + 'parentNode'] !== this)) {
throw Error(`Failed to execute 'insertBefore' on 'Node': The node ` +
`before which the new node is to be inserted is not a child of this node.`);
}
}
if (ref_node === node) {
return node;
}
scheduleObserver(this, node);
/** @type {!Array<!HTMLSlotElement>} */
const slotsAdded = [];
const ownerRoot = ownerShadyRootForNode(this);
/** @type {string} */
const newScopeName = ownerRoot ? ownerRoot.host.localName : currentScopeForNode(this);
/** @type {string} */
let oldScopeName;
// remove from existing location
const parentNode = node[utils.SHADY_PREFIX + 'parentNode'];
if (parentNode) {
oldScopeName = currentScopeForNode(node);
const skipUnscoping =
// Don't remove scoping if we're inserting into another shadowRoot;
// this would be unnecessary since it will be re-scoped below
Boolean(ownerRoot) ||
// Don't remove scoping if we're being moved between non-shadowRoot
// locations (the likely case is when moving pre-scoped nodes in a template)
!ownerShadyRootForNode(node) ||
// Under preferPerformance, don't remove scoping when moving back into
// a document fragment that was previously scoped; the assumption is
// that the user should only move correctly-scoped DOM back into it
(preferPerformance && this['__noInsertionPoint'] !== undefined);
parentNode[utils.SHADY_PREFIX + 'removeChild'](node, skipUnscoping);
}
// add to new parent
let allowNativeInsert = true;
const needsScoping = (!preferPerformance ||
// Under preferPerformance, only re-scope if we're not coming from a
// pre-scoped doc fragment or back into a pre-scoped doc fragment
(node['__noInsertionPoint'] === undefined &&
this['__noInsertionPoint'] === undefined)) &&
!currentScopeIsCorrect(node, newScopeName);
const needsSlotFinding = ownerRoot && !node['__noInsertionPoint'] &&
(!preferPerformance || node.nodeType === Node.DOCUMENT_FRAGMENT_NODE);
if (needsSlotFinding || needsScoping) {
// NOTE: avoid node.removeChild as this *can* trigger another patched
// method (e.g. custom elements) and we want only the shady method to run.
// The following table describes what style scoping actions should happen as a result of this insertion.
// document -> shadowRoot: replace
// shadowRoot -> shadowRoot: replace
// shadowRoot -> shadowRoot of same type: do nothing
// shadowRoot -> document: allow unscoping
// document -> document: do nothing
// The "same type of shadowRoot" and "document to document cases rely on `currentScopeIsCorrect` returning true
if (needsScoping) {
// in a document or disconnected tree, replace scoping if necessary
oldScopeName = oldScopeName || currentScopeForNode(node);
}
treeVisitor(node, (node) => {
if (needsSlotFinding && node.localName === 'slot') {
slotsAdded.push(/** @type {!HTMLSlotElement} */(node));
}
if (needsScoping) {
replaceShadyScoping(node, newScopeName, oldScopeName);
}
});
}
// if a slot is added, must render containing root.
if (slotsAdded.length) {
ownerRoot._addSlots(slotsAdded);
ownerRoot._asyncRender();
}
if (utils.isTrackingLogicalChildNodes(this)) {
recordInsertBefore(node, this, ref_node);
const parentData = shadyDataForNode(this);
// if the node being inserted into has a shadowRoot, do not perform
// a native insertion
if (parentData.root) {
allowNativeInsert = false;
// when inserting into a host with a shadowRoot with slot, use
// `shadowRoot._asyncRender()` via `attach-shadow` module
// when inserting into a host with shadowRoot with NO slot, do nothing
// as the node should not be added to composed DOM anywhere.
if (utils.hasShadowRootWithSlot(this)) {
parentData.root._asyncRender();
}
// when inserting into a slot inside a shadowRoot, render the
// containing shadowRoot to update fallback content.
} else if (ownerRoot && this.localName === 'slot') {
allowNativeInsert = false;
ownerRoot._asyncRender();
}
}
if (allowNativeInsert) {
// if adding to a shadyRoot, add to host instead
let container = utils.isShadyRoot(this) ?
/** @type {ShadowRoot} */(this).host : this;
// if ref_node, get the ref_node that's actually in composed dom.
if (ref_node) {
ref_node = firstComposedNode(ref_node);
container[utils.NATIVE_PREFIX + 'insertBefore'](node, ref_node);
} else {
container[utils.NATIVE_PREFIX + 'appendChild'](node);
}
// Since ownerDocument is not patched, it can be incorrect after this call
// if the node is physically appended via distribution. This can result
// in the custom elements polyfill not upgrading the node if it's in an inert doc.
// We correct this by calling `adoptNode`.
} else if (node.ownerDocument !== this.ownerDocument) {
this.ownerDocument.adoptNode(node);
}
return node;
},
/**
* @this {Node}
* @param {Node} node
*/
appendChild(node) {
// if this is a shadowRoot and the shadowRoot is passed as `node`
// then an optimized append has already been performed, so do nothing.
if (!(this == node && utils.isShadyRoot(node))) {
return this[utils.SHADY_PREFIX + 'insertBefore'](node);
}
},
/**
* Patched `removeChild`. Note that all dom "removals" are routed here.
* Removes the given `node` from the element's `children`.
* This method also performs dom composition.
* @this {Node}
* @param {Node} node
* @param {boolean=} skipUnscoping
*/
removeChild(node, skipUnscoping = false) {
if (this.ownerDocument !== doc) {
return this[utils.NATIVE_PREFIX + 'removeChild'](node);
}
if (node[utils.SHADY_PREFIX + 'parentNode'] !== this) {
throw Error('The node to be removed is not a child of this node: ' +
node);
}
scheduleObserver(this, null, node);
let preventNativeRemove;
let ownerRoot = ownerShadyRootForNode(node);
const removingInsertionPoint = ownerRoot && ownerRoot._removeContainedSlots(node);
const parentData = shadyDataForNode(this);
if (utils.isTrackingLogicalChildNodes(this)) {
recordRemoveChild(node, this);
if (utils.hasShadowRootWithSlot(this)) {
parentData.root._asyncRender();
preventNativeRemove = true;
}
}
// unscope a node leaving a ShadowRoot if ShadyCSS is present, and this node
// is not going to be rescoped in `insertBefore`
if (getScopingShim() && !skipUnscoping && ownerRoot
&& node.nodeType !== Node.TEXT_NODE) {
const oldScopeName = currentScopeForNode(node);
treeVisitor(node, (node) => {
removeShadyScoping(node, oldScopeName);
});
}
removeOwnerShadyRoot(node);
// if removing slot, must render containing root
if (ownerRoot) {
let changeSlotContent = this.localName === 'slot';
if (changeSlotContent) {
preventNativeRemove = true;
}
if (removingInsertionPoint || changeSlotContent) {
ownerRoot._asyncRender();
}
}
if (!preventNativeRemove) {
// if removing from a shadyRoot, remove from host instead
let container = utils.isShadyRoot(this) ?
/** @type {ShadowRoot} */(this).host :
this;
// not guaranteed to physically be in container; e.g.
// (1) if parent has a shadyRoot, element may or may not at distributed
// location (could be undistributed)
// (2) if parent is a slot, element may not ben in composed dom
if (!(parentData.root || node.localName === 'slot') ||
(container === node[utils.NATIVE_PREFIX + 'parentNode'])) {
container[utils.NATIVE_PREFIX + 'removeChild'](node);
}
}
return node;
},
/**
* @this {Node}
* @param {Node} node
* @param {Node=} ref_node
*/
replaceChild(node, ref_node) {
this[utils.SHADY_PREFIX + 'insertBefore'](node, ref_node);
this[utils.SHADY_PREFIX + 'removeChild'](ref_node);
return node;
},
/**
* @this {Node}
* @param {boolean=} deep
*/
cloneNode(deep) {
if (this.localName == 'template') {
return this[utils.NATIVE_PREFIX + 'cloneNode'](deep);
} else {
const n = this[utils.NATIVE_PREFIX + 'cloneNode'](false);
// Attribute nodes historically had childNodes, but they have later
// been removed from the spec.
// Make sure we do not do a deep clone on them for old browsers (IE11)
if (deep && n.nodeType !== Node.ATTRIBUTE_NODE) {
for (let c=this[utils.SHADY_PREFIX + 'firstChild'], nc; c; c = c[utils.SHADY_PREFIX + 'nextSibling']) {
nc = c[utils.SHADY_PREFIX + 'cloneNode'](true);
n[utils.SHADY_PREFIX + 'appendChild'](nc);
}
}
return n;
}
},
/**
* @this {Node}
* @param {Object=} options
*/
// TODO(sorvell): implement `options` e.g. `{ composed: boolean }`
getRootNode(options) { // eslint-disable-line no-unused-vars
if (!this || !this.nodeType) {
return;
}
const nodeData = ensureShadyDataForNode(this);
let root = nodeData.ownerShadyRoot;
if (root === undefined) {
if (utils.isShadyRoot(this)) {
root = this;
nodeData.ownerShadyRoot = root;
} else {
let parent = this[utils.SHADY_PREFIX + 'parentNode'];
root = parent ? parent[utils.SHADY_PREFIX + 'getRootNode'](options) : this;
// memo-ize result for performance but only memo-ize
// result if node is in the document. This avoids a problem where a root
// can be cached while an element is inside a fragment.
// If this happens and we cache the result, the value can become stale
// because for perf we avoid processing the subtree of added fragments.
if (document.documentElement[utils.NATIVE_PREFIX + 'contains'](this)) {
nodeData.ownerShadyRoot = root;
}
}
}
return root;
},
/** @this {Node} */
contains(node) {
return utils.contains(this, node);
}
});