/
qwery.js
208 lines (190 loc) · 5.66 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
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
!function (context, doc) {
var id = /#([\w\-]+)/,
clas = /\.[\w\-]+/g,
idOnly = /^#([\w\-]+$)/,
tagOnly = /^([\w\-]+)$/,
html = doc.getElementsByTagName('html')[0],
simple = /^([a-z0-9]+)?(?:([\.\#]+[\w\-\.#]+)?)/,
attr = /\[([\w\-]+)(?:([\^\$\*]?\=)['"]?([ \w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^]+)["']?)?\]/;
function array(ar) {
var i, len, r = [];
for (i = 0, len = ar.length; i < len; i++) {
r[i] = ar[i];
}
return r;
}
function iter(obj) {
this.obj = array(obj);
}
iter.prototype = {
each: function (fn) {
for (var i = 0, l = this.obj.length; i < l; i ++) {
fn.call(this.obj[i], this.obj[i], i, this.obj);
}
return this;
},
map: function (fn) {
var collection = [], i, l;
for (i = 0, l = this.obj.length; i < l; i ++) {
collection[i] = fn.call(this.obj[i], this.obj[i], i, this.obj);
}
return collection;
}
};
function _(obj) {
return new iter(obj);
}
function q(query) {
return query.match(new RegExp(simple.source + '(' + attr.source + ')?'));
}
function interpret(whole, tag, idsAndClasses, wholeAttribute, attribute, qualifier, value) {
var v, c, i, m, classes;
if (tag && this.tagName.toLowerCase() !== tag) {
return false;
}
if (idsAndClasses && (m = idsAndClasses.match(id)) && m[1] !== this.id) {
return false;
}
if (idsAndClasses && (classes = idsAndClasses.match(clas))) {
for (i = classes.length; i--;) {
if (!(new RegExp('(^|\\s+)' + classes[i].slice(1) + '(\\s+|$)')).test(this.className)) {
return false;
}
}
}
if (wholeAttribute && !value) {
var o = this.attributes, k;
for (k in o) {
if (o.hasOwnProperty(k) && o[k].name == attribute) {
return this;
}
}
}
if (wholeAttribute && !checkAttr(qualifier, this.getAttribute(attribute) || '', value)) {
return false;
}
return this;
}
function loopAll(token) {
var i, l, item, r = [], intr = q(token), tag = intr[1] || '*',
els = doc.getElementsByTagName(tag);
for (i = 0, l = els.length; i < l; i++) {
el = els[i];
if (item = interpret.apply(el, intr)) {
r.push(item);
}
}
return r;
}
function clean(s) {
return s.replace(/([\.\*\+\?\^\$\{\}\(\)\|\[\]\/\\])/g, '\\$1');
}
function checkAttr(qualify, actual, val) {
switch (qualify) {
case '=':
return actual == val;
case '^=':
return actual.match(new RegExp('^' + clean(val)));
case '$=':
return actual.match(new RegExp(clean(val) + '$'));
case '*=':
return actual.match(new RegExp(clean(val)));
}
return false;
}
function _qwery(selector) {
var r = [], context, token, i, j, k, p, ret = [],
el, node, found = false,
tokens = selector.split(/\s(?![\s\w\-\/\?\&\=\:\.\(\)\!,@#%<>\{\}\$\*\^'"]*\])/);
if (!tokens.length) {
return r;
}
r = loopAll(tokens.pop());
if (!tokens.length) {
return r;
}
// loop through all found base elements
for (j = r.length; j--;) {
node = r[j];
p = node;
// loop through each token
for (i = tokens.length; i--;) {
parents:
while (p !== html && (p = p.parentNode)) { // loop through parent nodes
if (found = interpret.apply(p, q(tokens[i]))) {
break parents;
}
}
}
found && ret.push(node);
}
return ret;
}
function isAncestor(child, parent) {
if (!parent || !child || parent == child) {
return false;
}
if (parent.contains && child.nodeType) {
return parent.contains(child);
}
else if (parent.compareDocumentPosition && child.nodeType) {
return !!(parent.compareDocumentPosition(child) & 16);
}
return false;
}
var qwery = function () {
// exception for pure classname selectors (it's faster)
var clas = /^\.([\w\-]+)$/, m;
function qsa(selector, root) {
root = (typeof root == 'string') ? doc.querySelector(root) : root;
var i;
if (i = selector.match(idOnly)) {
return [doc.getElementById(i[1])];
}
// taking for granted that every browser that supports qsa, also supports getElsByClsName
if (doc.getElementsByClassName && (m = selector.match(clas))) {
return array((root || doc).getElementsByClassName(m[1]), 0);
}
return array((root || doc).querySelectorAll(selector), 0);
}
// return fast
if (doc.querySelector && doc.querySelectorAll) {
return qsa;
}
return function (selector, root) {
root = (typeof root == 'string') ? qwery(root)[0] : (root || doc);
var i;
if (i = selector.match(idOnly)) {
return [doc.getElementById(i[1])];
}
if (i = selector.match(tagOnly)) {
return [root.getElementsByTagName(i[1])];
}
var result = [],
// here we allow combinator selectors: $('div,span');
collections = _(selector.split(',')).map(function (selector) {
return _qwery(selector);
});
_(collections).each(function (collection) {
var ret = collection;
// allow contexts
if (root !== doc) {
ret = [];
_(collection).each(function (element) {
// make sure element is a descendent of root
isAncestor(element, root) && ret.push(element);
});
}
result = result.concat(ret);
});
return result;
};
}();
// being nice
var oldQwery = context.qwery;
qwery.noConflict = function () {
context.qwery = oldQwery;
return this;
};
context.qwery = qwery;
}(this, document);