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
157 lines (133 loc) · 4.56 KB
/
ChildNode.js
File metadata and controls
157 lines (133 loc) · 4.56 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
/**
* @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 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 /** @this {!Node} */ 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);
}
}
}
};
}
if (builtIn.before !== undefined) {
Utilities.setPropertyUnchecked(destination, 'before', beforeAfterPatch(builtIn.before));
}
if (builtIn.after !== undefined) {
Utilities.setPropertyUnchecked(destination, 'after', beforeAfterPatch(builtIn.after));
}
if (builtIn.replaceWith !== undefined) {
Utilities.setPropertyUnchecked(destination, 'replaceWith',
/**
* @param {...(!Node|string)} nodes
* @this {!Node}
*/
function(...nodes) {
/**
* A copy of `nodes`, with any DocumentFragment replaced by its children.
* @type {!Array<!Node|string>}
*/
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);
}
}
const wasConnected = Utilities.isConnected(this);
builtIn.replaceWith.apply(this, nodes);
for (let i = 0; i < connectedElements.length; i++) {
internals.disconnectTree(connectedElements[i]);
}
if (wasConnected) {
internals.disconnectTree(this);
for (let i = 0; i < flattenedNodes.length; i++) {
const node = flattenedNodes[i];
if (node instanceof Element) {
internals.connectTree(node);
}
}
}
});
}
if (builtIn.remove !== undefined) {
Utilities.setPropertyUnchecked(destination, 'remove',
/** @this {!Node} */
function() {
const wasConnected = Utilities.isConnected(this);
builtIn.remove.call(this);
if (wasConnected) {
internals.disconnectTree(this);
}
});
}
};