-
Notifications
You must be signed in to change notification settings - Fork 0
/
childList.js
74 lines (67 loc) · 1.9 KB
/
childList.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
var childNodes = require("can-child-nodes");
var onChildListSymbol = Symbol.for("done.onChildList");
module.exports = function(Node) {
var appendChild = Node.prototype.appendChild;
Node.prototype.appendChild = function(node) {
var nodes = collectNodes(node);
var res = appendChild.apply(this, arguments);
var doc = getDocument(this);
if(doc && doc[onChildListSymbol] !== undefined) {
doc[onChildListSymbol](this, nodes);
}
return res;
};
var insertBefore = Node.prototype.insertBefore;
Node.prototype.insertBefore = function(node) {
var nodes = collectNodes(node);
var res = insertBefore.apply(this, arguments);
var doc = getDocument(this);
if(doc && doc[onChildListSymbol] !== undefined) {
doc[onChildListSymbol](this, nodes);
}
return res;
};
var removeChild = Node.prototype.removeChild;
Node.prototype.removeChild = function(node) {
var res = removeChild.apply(this, arguments);
var doc = getDocument(this);
if(doc && doc[onChildListSymbol] !== undefined) {
doc[onChildListSymbol](this, null, node);
}
return res;
};
var replaceChild = Node.prototype.replaceChild;
Node.prototype.replaceChild = function(newNode, oldNode) {
var nodes = collectNodes(newNode);
var res = replaceChild.apply(this, arguments);
var doc = getDocument(this);
if(doc && doc[onChildListSymbol] !== undefined) {
doc[onChildListSymbol](this, nodes, oldNode);
}
return res;
};
return function() {
Node.prototype.appendChild = appendChild;
Node.prototype.insertBefore = insertBefore;
Node.prototype.removeChild = removeChild;
Node.prototype.replaceChild = replaceChild;
};
};
function collectNodes(node) {
switch(node.nodeType) {
// DocumentFragment
case 11:
return Array.from(childNodes(node));
default:
return [node];
}
}
function getDocument(node) {
switch(node.nodeType) {
// Document node
case 9:
return node;
default:
return node.ownerDocument;
}
}