-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathnode.js
More file actions
76 lines (68 loc) · 2.22 KB
/
node.js
File metadata and controls
76 lines (68 loc) · 2.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
'use strict';
const createContent = (m => m.__esModule ? /* istanbul ignore next */ m.default : /* istanbul ignore next */ m)(require('@ungap/create-content'));
const {indexOf, slice} = require('./array.js');
const getNode = (node, i) => node.childNodes[i];
exports.getNode = getNode;
const getPath = node => {
const path = [];
let {parentNode} = node;
while (parentNode) {
path.unshift(indexOf.call(parentNode.childNodes, node));
node = parentNode;
parentNode = node.parentNode;
}
return path;
};
exports.getPath = getPath;
const {defineProperties} = Object;
const getWire = fragment => {
const {childNodes} = fragment;
const {length} = childNodes;
if (length === 1)
return childNodes[0];
const nodes = slice.call(childNodes, 0);
return defineProperties(fragment, {
remove: {
value() {
const range = document.createRange();
range.setStartBefore(nodes[1]);
range.setEndAfter(nodes[length - 1]);
range.deleteContents();
return nodes[0];
}
},
valueOf: {
value() {
if (childNodes.length !== length) {
const range = document.createRange();
range.setStartBefore(nodes[0]);
range.setEndAfter(nodes[length - 1]);
fragment.appendChild(range.extractContents());
}
return fragment;
}
}
});
};
exports.getWire = getWire;
const {createTreeWalker, importNode} = document;
exports.createTreeWalker = createTreeWalker;
exports.importNode = importNode;
const IE = importNode.length != 1;
const createFragment = IE ?
(text, type) => importNode.call(
document,
createContent(text, type),
true
) :
createContent;
exports.createFragment = createFragment;
// to support IE10 and IE9 I could pass a callback instead
// with an `acceptNode` mode that's the callback itself
// function acceptNode() { return 1; } acceptNode.acceptNode = acceptNode;
// however, I really don't care about IE10 and IE9, as these would require
// also a WeakMap polyfill, and have no reason to exist.
const createWalker = IE ?
fragment => createTreeWalker.call(document, fragment, 1 | 128, null, false) :
fragment => createTreeWalker.call(document, fragment, 1 | 128);
exports.createWalker = createWalker;