This repository was archived by the owner on Oct 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathChildNode.js
More file actions
114 lines (96 loc) · 3.22 KB
/
ChildNode.js
File metadata and controls
114 lines (96 loc) · 3.22 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
import CustomElementInternals from '../../CustomElementInternals.js';
import * as Utilities from '../../Utilities.js';
/**
* @typedef {{
* before: !function(...(!Node|string)),
* after: !function(...(!Node|string)),
* replaceWith: !function(...(!Node|string)),
* remove: !function(),
* }}
*/
let ChildNodeNativeMethods;
/**
* @param {!CustomElementInternals} internals
* @param {!Object} destination
* @param {!ChildNodeNativeMethods} builtIn
*/
export default function(internals, destination, builtIn) {
/**
* @param {!function(...(!Node|string))} builtInMethod
* @return {!function(...(!Node|string))}
*/
function beforeAfterPatch(builtInMethod) {
return function(...nodes) {
/**
* A copy of `nodes`, with any DocumentFragment replaced by its children.
* @type {!Array<!Node>}
*/
const flattenedNodes = [];
/**
* Elements in `nodes` that were connected before this call.
* @type {!Array<!Node>}
*/
const connectedElements = [];
for (var i = 0; i < nodes.length; i++) {
const node = nodes[i];
if (node instanceof Element && Utilities.isConnected(node)) {
connectedElements.push(node);
}
if (node instanceof DocumentFragment) {
for (let child = node.firstChild; child; child = child.nextSibling) {
flattenedNodes.push(child);
}
} else {
flattenedNodes.push(node);
}
}
builtInMethod.apply(this, nodes);
for (let i = 0; i < connectedElements.length; i++) {
internals.disconnectTree(connectedElements[i]);
}
if (Utilities.isConnected(this)) {
for (let i = 0; i < flattenedNodes.length; i++) {
const node = flattenedNodes[i];
if (node instanceof Element) {
internals.connectTree(node);
}
}
}
};
}
Utilities.setPropertyUnchecked(destination, 'before', beforeAfterPatch(builtIn.before));
Utilities.setPropertyUnchecked(destination, 'after', beforeAfterPatch(builtIn.after));
Utilities.setPropertyUnchecked(destination, 'replaceWith',
/**
* @param {...(!Node|string)} nodes
*/
function(...nodes) {
// TODO: Fix this for when one of `nodes` is a DocumentFragment!
const connectedBefore = /** @type {!Array<!Node>} */ (nodes.filter(node => {
// DocumentFragments are not connected and will not be added to the list.
return node instanceof Node && Utilities.isConnected(node);
}));
const wasConnected = Utilities.isConnected(this);
builtIn.replaceWith.apply(this, nodes);
for (let i = 0; i < connectedBefore.length; i++) {
internals.disconnectTree(connectedBefore[i]);
}
if (wasConnected) {
internals.disconnectTree(this);
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
if (node instanceof Element) {
internals.connectTree(node);
}
}
}
});
Utilities.setPropertyUnchecked(destination, 'remove',
function() {
const wasConnected = Utilities.isConnected(this);
builtIn.remove.call(this);
if (wasConnected) {
internals.disconnectTree(this);
}
});
};