-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.js
236 lines (216 loc) · 6.54 KB
/
scan.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*!
* scan 0.9.0+201410070420
* https://github.com/ryanve/scan
* MIT License (c) 2014 Ryan Van Etten
*/
!function(root, name, make) {
if (typeof module != 'undefined' && module['exports']) module['exports'] = make();
else root[name] = make();
}(this, 'scan', function() {
var effin = scan.prototype = Scan.prototype = []
, push = effin.push
, doc = document
, docElem = doc.documentElement
, domL4 = !!doc.contains && !!docElem.contains
, byAll = 'querySelectorAll'
, byTag = 'getElementsByTagName'
, query = doc[byAll] ? byAll : byTag
, matcher = docElem.matches || detect(['webkit', 'moz', 'o', 'ms'], function(prefix) {
return docElem[prefix + 'MatchesSelector'];
})
/**
* @param {Element} e
* @param {string} selector
* @return {boolean} true if element matches selector
*/
, matches = typeof matcher == 'function' ? function(e, selector) {
return !!selector && !!matcher.call(e, selector);
} : function(e, selector) {
return include(qsa(selector, e.ownerDocument), e);
};
/**
* @param {(string|Node|{length:number}|*)=} item
* @param {(string|Node|{length:number})=} root
* @return {Scan}
*/
function scan(item, root) {
return new Scan(item, root);
}
/**
* @constructor
* @this {Scan} instance
* @param {?(string|Node|{length:number})=} item
* @param {?(string|Node|{length:number})=} root
*/
function Scan(item, root) {
this.length = 0;
this.constructor = Scan;
if (item) push.apply(this, typeof item == 'string' ? qsa(item, root) : collect(item));
}
/**
* @param {string=} selector
* @param {(string|Node|{length:number})=} root
* @return {Array}
*/
function qsa(selector, root) {
if (!selector) return [];
root = null == root ? doc : typeof root == 'string' ? qsa(root) : root;
return typeof root != 'object' ? [] : root.nodeType ? (
root[query] ? ary(root[query](selector)) : []
) : amass(selector, root); // root was collection
}
/**
* @param {{length:number}} list
* @return {Array}
*/
function ary(list) {
for (var pure = [], l = list.length, i = 0; i < l;) pure[i] = list[i++];
return pure;
}
/**
* @param {{length:number}|Node|Window} o
* @return {Array}
*/
function collect(o) {
return o.nodeType || o.window == o ? [o] : ary(o);
}
/**
* @param {string} selector
* @param {{length:number}} roots nodes from which to base queries
* @return {Array} unique matches that descend from any `roots` item
*/
function amass(selector, roots) {
for (var u, j, group, els = [], e = 0, l = roots.length, i = 0; i < l;) {
group = qsa(selector, roots[i++]);
label:for (u = 0; group[u]; u++) {
for (j = e; j--;) if (els[j] === group[u]) continue label;
els[e++] = group[u];
}
}
return els;
}
/**
* @param {{length:number}} stack
* @param {*} needle
* @param {number=} start
* @return {boolean}
*/
function include(stack, needle, start) {
var l = stack.length, i = start >> 0;
for (0 > i && (i += l); i < l; i++) if (stack[i] === needle && i in stack) return true;
return false;
}
/**
* @param {Node|*} a element or document to search in
* @param {Node|*} b element to search for
* @return {boolean} true if A contains B
*/
function wraps(a, b) {
// Use parent b/c Node.contains is inclusive
if (domL4 && a.contains) return (b = b.parentNode) === a || a.contains(b);
while (b = b.parentNode) if (b === a) break;
return !!b;
}
/**
* combines jQuery.contains, _.contains, string.contains
* @param {Node|{length:number}|string} ob
* @param {*} needle
* @param {?number=} start
* @return {boolean}
*/
function contains(ob, needle, start) {
if (typeof ob == 'string') return !!~ob.indexOf(needle, start >> 0);
return ob.nodeType ? wraps(ob, needle) : include(ob, needle, start);
}
/**
* @param {{length:number}} nodes
* @param {{length:number}|Element} needles
* @return {Array} descendant needles
*/
function contained(nodes, needles) {
var j, l, ret = [], i = 0, h = nodes.length;
needles = needles.nodeType ? [needles] : needles;
for (l = needles.length; i < l; i++) {
for (j = 0; j < h;) {
if (wraps(nodes[j++], needles[i])) {
ret.push(needles[i]);
break;
}
}
}
return ret;
}
/**
* @param {string} str
* @return {Element|boolean}
*/
function id(str) {
return doc.getElementById(str) || false;
}
/**
* @param {{length:number}} o
* @param {Function} fn
* @param {*=} scope
*/
function detect(o, fn, scope) {
for (var v, i = 0, l = o.length; i < l;) if (fn.call(scope, v = o[i], i++, o)) return v;
}
/**
* @param {string|Node|{length:number}|*} o
* @param {(string|Node|{length:number}|Function|null)=} fn
* @param {*=} scope
*/
function find(o, fn, scope) {
return typeof fn == 'function' ? detect(o, fn, scope) : qsa(o, fn);
}
/**
* @param {{length:number}} before
* @param {{length:number}} after
* @return {{length:number}}
*/
function chain(before, after) {
return before.pushStack ? before.pushStack(after) : after;
}
/**
* @param {{length:number}} items
* @return {{length:number}}
*/
effin['pushStack'] = function(items) {
for (var o = new this.constructor, i = 0, l = items.length; i < l;) push.call(o, items[i++]);
return o;
};
/**
* @this {{length:number}}
* @param {{length:number}|Element|Function|string|*} needle
* @param {*=} scope
* @return {*}
*/
effin['find'] = function(needle, scope) {
var found;
if (typeof needle == 'string') found = amass(needle, this);
else if (typeof needle == 'object') found = contained(this, needle);
else return detect(this, needle, scope);
return chain(this, found);
};
// Comply w/ api.jquery.com/filter + api.jquery.com/not
detect(['not', 'filter'], function(key, yes) {
effin[key] = function(q) {
var kept = [], isF = typeof q == 'function';
if (!q) kept = yes ? kept : ary(this);
else detect(this, function(v, j) {
var keep = isF ? q.call(v, j) : include(this, v);
if (keep == yes) kept.push(v);
}, typeof q == 'string' ? qsa(q) : q.nodeType ? [q] : q);
return chain(this, kept);
};
});
scan['scan'] = scan;
scan['qsa'] = qsa;
scan['id'] = id;
scan['wraps'] = wraps;
scan['contains'] = contains;
scan['matches'] = matches;
scan['find'] = find;
scan['fn'] = effin;
return scan;
});