-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathqwery.js
94 lines (80 loc) · 2.7 KB
/
qwery.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
/*!
* @preserve Qwery - A selector engine
* https://github.com/ded/qwery
* (c) Dustin Diaz 2014 | License MIT
*/
(function (name, context, definition) {
if (typeof module != 'undefined' && module.exports) module.exports = definition()
else if (typeof define == 'function' && define.amd) define(definition)
else context[name] = definition()
})('qwery', this, function () {
var classOnly = /^\.([\w\-]+)$/
, doc = document
, win = window
, html = doc.documentElement
, nodeType = 'nodeType'
var isAncestor = 'compareDocumentPosition' in html ?
function (element, container) {
return (container.compareDocumentPosition(element) & 16) == 16
} :
function (element, container) {
container = container == doc || container == window ? html : container
return container !== element && container.contains(element)
}
function toArray(ar) {
return [].slice.call(ar, 0)
}
function isNode(el) {
var t
return el && typeof el === 'object' && (t = el.nodeType) && (t == 1 || t == 9)
}
function arrayLike(o) {
return (typeof o === 'object' && isFinite(o.length))
}
function flatten(ar) {
for (var r = [], i = 0, l = ar.length; i < l; ++i) arrayLike(ar[i]) ? (r = r.concat(ar[i])) : (r[r.length] = ar[i])
return r
}
function uniq(ar) {
var a = [], i, j
label:
for (i = 0; i < ar.length; i++) {
for (j = 0; j < a.length; j++) {
if (a[j] == ar[i]) {
continue label
}
}
a[a.length] = ar[i]
}
return a
}
function normalizeRoot(root) {
if (!root) return doc
if (typeof root == 'string') return qwery(root)[0]
if (!root[nodeType] && arrayLike(root)) return root[0]
return root
}
/**
* @param {string|Array.<Element>|Element|Node} selector
* @param {string|Array.<Element>|Element|Node=} opt_root
* @return {Array.<Element>}
*/
function qwery(selector, opt_root) {
var m, root = normalizeRoot(opt_root)
if (!root || !selector) return []
if (selector === win || isNode(selector)) {
return !opt_root || (selector !== win && isNode(root) && isAncestor(selector, root)) ? [selector] : []
}
if (selector && arrayLike(selector)) return flatten(selector)
if (doc.getElementsByClassName && selector == 'string' && (m = selector.match(classOnly))) {
return toArray((root).getElementsByClassName(m[1]))
}
// using duck typing for 'a' window or 'a' document (not 'the' window || document)
if (selector && (selector.document || (selector.nodeType && selector.nodeType == 9))) {
return !opt_root ? [selector] : []
}
return toArray((root).querySelectorAll(selector))
}
qwery.uniq = uniq
return qwery
}, this);