-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathPath.js
More file actions
58 lines (55 loc) · 1.65 KB
/
Path.js
File metadata and controls
58 lines (55 loc) · 1.65 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
'use strict';
const {
COMMENT_NODE,
DOCUMENT_FRAGMENT_NODE,
ELEMENT_NODE
} = require('../shared/constants.js');
// every template literal interpolation indicates
// a precise target in the DOM the template is representing.
// `<p id=${'attribute'}>some ${'content'}</p>`
// hyperHTML finds only once per template literal,
// hence once per entire application life-cycle,
// all nodes that are related to interpolations.
// These nodes are stored as indexes used to retrieve,
// once per upgrade, nodes that will change on each future update.
// A path example is [2, 0, 1] representing the operation:
// node.childNodes[2].childNodes[0].childNodes[1]
// Attributes are addressed via their owner node and their name.
const createPath = node => {
const path = [];
let parentNode;
switch (node.nodeType) {
case ELEMENT_NODE:
case DOCUMENT_FRAGMENT_NODE:
parentNode = node;
break;
case COMMENT_NODE:
parentNode = node.parentNode;
prepend(path, parentNode, node);
break;
default:
parentNode = node.ownerElement;
break;
}
for (
node = parentNode;
(parentNode = parentNode.parentNode);
node = parentNode
) {
prepend(path, parentNode, node);
}
return path;
};
const prepend = (path, parent, node) => {
path.unshift(path.indexOf.call(parent.childNodes, node));
};
Object.defineProperty(exports, '__esModule', {value: true}).default = {
create: (type, node, name) => ({type, name, node, path: createPath(node)}),
find: (node, path) => {
const length = path.length;
for (let i = 0; i < length; i++) {
node = node.childNodes[path[i]];
}
return {node, childNodes: []};
}
}