forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom.js
More file actions
executable file
·77 lines (68 loc) · 1.93 KB
/
dom.js
File metadata and controls
executable file
·77 lines (68 loc) · 1.93 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
//require('./casperjs');
// http://dom.spec.whatwg.org/#mutation-method-macro
function mutation(nodes) {
if (!nodes.length) {
throw new Error('DOM Exception 8');
} else if (nodes.length === 1) {
return typeof nodes[0] === 'string' ? document.createTextNode(nodes[0]) : nodes[0];
} else {
var
fragment = document.createDocumentFragment(),
length = nodes.length,
index = -1,
node;
while (++index < length) {
node = nodes[index];
fragment.appendChild(typeof node === 'string' ? document.createTextNode(node) : node);
}
return fragment;
}
}
let methods = {
// safari = webkitMatchesSelector
matches: Element.prototype.matchesSelector || Element.prototype.webkitMatchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.oMatchesSelector,
replace() {
if (this.parentNode) {
this.parentNode.replaceChild(mutation(arguments), this);
}
},
prepend() {
this.insertBefore(mutation(arguments), this.firstChild);
},
append() {
this.appendChild(mutation(arguments));
},
remove() {
let parentNode = this.parentNode;
if (parentNode) {
return parentNode.removeChild(this);
}
},
before: function before() {
if (this.parentNode) {
this.parentNode.insertBefore(mutation(arguments), this);
}
},
after: function after() {
if (this.parentNode) {
this.parentNode.insertBefore(mutation(arguments), this.nextSibling);
}
},
closest: function(selector) {
let node = this;
while (node) {
if (node.matches && node.matches(selector)) return node;
else node = node.parentElement;
}
return null;
}
};
for (let methodName in methods) {
if (!Element.prototype[methodName]) {
Element.prototype[methodName] = methods[methodName];
}
}
require('./customEvent');
require('./dataset');
require('./hidden');
require('./requestAnimationFrame');