-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path-util.js
170 lines (147 loc) · 3.85 KB
/
-util.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
"use strict";
var getDocument = require("can-globals/document/document");
function eliminate(array, item) {
var index = array.indexOf(item);
if (index >= 0) {
array.splice(index, 1);
}
}
function wasNotInSet(item, set) {
var inSet = set.has(item);
if(inSet === false) {
set.add(item);
}
return !inSet;
}
function contains(parent, child){
if(child && child.nodeType === Node.TEXT_NODE) {
return contains(parent, child.parentNode);
}
if(parent.contains) {
return parent.contains(child);
}
if(parent.nodeType === Node.DOCUMENT_NODE && parent.documentElement) {
return contains(parent.documentElement, child);
} else {
child = child.parentNode;
if(child === parent) {
return true;
}
return false;
}
}
function isDocumentElement (node) {
return getDocument().documentElement === node;
}
function isFragment (node) {
return !!(node && node.nodeType === 11);
}
function isElementNode (node) {
return !!(node && node.nodeType === 1);
}
function getChildren (parentNode) {
var nodes = [];
var node = parentNode.firstChild;
while (node) {
nodes.push(node);
node = node.nextSibling;
}
return nodes;
}
function getParents (node) {
var nodes;
if (isFragment(node)) {
nodes = getChildren(node);
} else {
nodes = [node];
}
return nodes;
}
function getNodesLegacyB(node) {
var skip, tmp;
var depth = 0;
var items = isFragment(node) ? [] : [node];
if(node.firstChild == null) {
return items;
}
// Always start with the initial element.
do {
if ( !skip && (tmp = node.firstChild) ) {
depth++;
items.push(tmp);
} else if ( tmp = node.nextSibling ) {
skip = false;
items.push(tmp);
} else {
// Skipped or no first child and no next sibling, so traverse upwards,
tmp = node.parentNode;
// and decrement the depth.
depth--;
// Enable skipping, so that in the next loop iteration, the children of
// the now-current node (parent node) aren't processed again.
skip = true;
}
// Instead of setting node explicitly in each conditional block, use the
// tmp var and set it here.
node = tmp;
// Stop if depth comes back to 0 (or goes below zero, in conditions where
// the passed node has neither children nore next siblings).
} while ( depth > 0 );
return items;
}
// IE11 requires a filter parameter for createTreeWalker
// it also must be an object with an `acceptNode` property
function treeWalkerFilterFunction() {
return NodeFilter.FILTER_ACCEPT;
}
var treeWalkerFilter = treeWalkerFilterFunction;
treeWalkerFilter.acceptNode = treeWalkerFilterFunction;
function getNodesWithTreeWalker(rootNode) {
var result = isFragment(rootNode) ? [] : [rootNode];
// IE11 throws if createTreeWalker is called on a non-ElementNode
var walker = isElementNode(rootNode) && getDocument().createTreeWalker(
rootNode,
NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT,
treeWalkerFilter,
false
);
var node;
while(node = walker && walker.nextNode()) {
result.push(node);
}
return result;
}
function getAllNodes (node) {
if( getDocument().createTreeWalker !== undefined ) {
return getNodesWithTreeWalker(node);
} else {
return getNodesLegacyB(node);
}
}
function subscription (fn) {
return function _subscription () {
var disposal = fn.apply(this, arguments);
var isDisposed = false;
return function _disposal () {
if (isDisposed) {
var fnName = fn.name || fn.displayName || 'an anonymous function';
var message = 'Disposal function returned by ' + fnName + ' called more than once.';
throw new Error(message);
}
disposal.apply(this, arguments);
isDisposed = true;
};
};
}
module.exports = {
eliminate: eliminate,
getDocument: getDocument,
isDocumentElement: isDocumentElement,
isFragment: isFragment,
getParents: getParents,
getAllNodes: getAllNodes,
getChildren: getChildren,
subscription: subscription,
wasNotInSet: wasNotInSet,
contains: contains
};