|
0
|
1 |
/* |
|
|
2 |
Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
|
3 |
Code licensed under the BSD License: |
|
|
4 |
http://developer.yahoo.net/yui/license.txt |
|
|
5 |
version: 3.0.0 |
|
|
6 |
build: 1549 |
|
|
7 |
*/ |
|
|
8 |
YUI.add('selector-native', function(Y) { |
|
|
9 |
|
|
|
10 |
(function(Y) { |
|
|
11 |
/** |
|
|
12 |
* The selector-native module provides support for native querySelector |
|
|
13 |
* @module dom |
|
|
14 |
* @submodule selector-native |
|
|
15 |
* @for Selector |
|
|
16 |
*/ |
|
|
17 |
|
|
|
18 |
/** |
|
|
19 |
* Provides support for using CSS selectors to query the DOM |
|
|
20 |
* @class Selector |
|
|
21 |
* @static |
|
|
22 |
* @for Selector |
|
|
23 |
*/ |
|
|
24 |
|
|
|
25 |
Y.namespace('Selector'); // allow native module to standalone |
|
|
26 |
|
|
|
27 |
var COMPARE_DOCUMENT_POSITION = 'compareDocumentPosition', |
|
|
28 |
OWNER_DOCUMENT = 'ownerDocument', |
|
|
29 |
TMP_PREFIX = 'yui-tmp-', |
|
|
30 |
g_counter = 0; |
|
|
31 |
|
|
|
32 |
var Selector = { |
|
|
33 |
_foundCache: [], |
|
|
34 |
|
|
|
35 |
useNative: true, |
|
|
36 |
|
|
|
37 |
_compare: ('sourceIndex' in document.documentElement) ? |
|
|
38 |
function(nodeA, nodeB) { |
|
|
39 |
var a = nodeA.sourceIndex, |
|
|
40 |
b = nodeB.sourceIndex; |
|
|
41 |
|
|
|
42 |
if (a === b) { |
|
|
43 |
return 0; |
|
|
44 |
} else if (a > b) { |
|
|
45 |
return 1; |
|
|
46 |
} |
|
|
47 |
|
|
|
48 |
return -1; |
|
|
49 |
|
|
|
50 |
} : (document.documentElement[COMPARE_DOCUMENT_POSITION] ? |
|
|
51 |
function(nodeA, nodeB) { |
|
|
52 |
if (nodeA[COMPARE_DOCUMENT_POSITION](nodeB) & 4) { |
|
|
53 |
return -1; |
|
|
54 |
} else { |
|
|
55 |
return 1; |
|
|
56 |
} |
|
|
57 |
} : |
|
|
58 |
function(nodeA, nodeB) { |
|
|
59 |
var rangeA, rangeB, compare; |
|
|
60 |
if (nodeA && nodeB) { |
|
|
61 |
rangeA = nodeA[OWNER_DOCUMENT].createRange(); |
|
|
62 |
rangeA.setStart(nodeA, 0); |
|
|
63 |
rangeB = nodeB[OWNER_DOCUMENT].createRange(); |
|
|
64 |
rangeB.setStart(nodeB, 0); |
|
|
65 |
compare = rangeA.compareBoundaryPoints(1, rangeB); // 1 === Range.START_TO_END |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
return compare; |
|
|
69 |
|
|
|
70 |
}), |
|
|
71 |
|
|
|
72 |
_sort: function(nodes) { |
|
|
73 |
if (nodes) { |
|
|
74 |
nodes = Y.Array(nodes, 0, true); |
|
|
75 |
if (nodes.sort) { |
|
|
76 |
nodes.sort(Selector._compare); |
|
|
77 |
} |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
return nodes; |
|
|
81 |
}, |
|
|
82 |
|
|
|
83 |
_deDupe: function(nodes) { |
|
|
84 |
var ret = [], |
|
|
85 |
i, node; |
|
|
86 |
|
|
|
87 |
for (i = 0; (node = nodes[i++]);) { |
|
|
88 |
if (!node._found) { |
|
|
89 |
ret[ret.length] = node; |
|
|
90 |
node._found = true; |
|
|
91 |
} |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
for (i = 0; (node = ret[i++]);) { |
|
|
95 |
node._found = null; |
|
|
96 |
node.removeAttribute('_found'); |
|
|
97 |
} |
|
|
98 |
|
|
|
99 |
return ret; |
|
|
100 |
}, |
|
|
101 |
|
|
|
102 |
/** |
|
|
103 |
* Retrieves a set of nodes based on a given CSS selector. |
|
|
104 |
* @method query |
|
|
105 |
* |
|
|
106 |
* @param {string} selector The CSS Selector to test the node against. |
|
|
107 |
* @param {HTMLElement} root optional An HTMLElement to start the query from. Defaults to Y.config.doc |
|
|
108 |
* @param {Boolean} firstOnly optional Whether or not to return only the first match. |
|
|
109 |
* @return {Array} An array of nodes that match the given selector. |
|
|
110 |
* @static |
|
|
111 |
*/ |
|
|
112 |
query: function(selector, root, firstOnly, skipNative) { |
|
|
113 |
root = root || Y.config.doc; |
|
|
114 |
var ret = [], |
|
|
115 |
useNative = (Y.Selector.useNative && document.querySelector && !skipNative), |
|
|
116 |
queries = [[selector, root]], |
|
|
117 |
query, |
|
|
118 |
result, |
|
|
119 |
i, |
|
|
120 |
fn = (useNative) ? Y.Selector._nativeQuery : Y.Selector._bruteQuery; |
|
|
121 |
|
|
|
122 |
if (selector && fn) { |
|
|
123 |
// split group into seperate queries |
|
|
124 |
if (!skipNative && // already done if skipping |
|
|
125 |
(!useNative || root.tagName)) { // split native when element scoping is needed |
|
|
126 |
queries = Selector._splitQueries(selector, root); |
|
|
127 |
} |
|
|
128 |
|
|
|
129 |
for (i = 0; (query = queries[i++]);) { |
|
|
130 |
result = fn(query[0], query[1], firstOnly); |
|
|
131 |
if (!firstOnly) { // coerce DOM Collection to Array |
|
|
132 |
result = Y.Array(result, 0, true); |
|
|
133 |
} |
|
|
134 |
if (result) { |
|
|
135 |
ret = ret.concat(result); |
|
|
136 |
} |
|
|
137 |
} |
|
|
138 |
|
|
|
139 |
if (queries.length > 1) { // remove dupes and sort by doc order |
|
|
140 |
ret = Selector._sort(Selector._deDupe(ret)); |
|
|
141 |
} |
|
|
142 |
} |
|
|
143 |
|
|
|
144 |
Y.log('query: ' + selector + ' returning: ' + ret.length, 'info', 'Selector'); |
|
|
145 |
return (firstOnly) ? (ret[0] || null) : ret; |
|
|
146 |
|
|
|
147 |
}, |
|
|
148 |
|
|
|
149 |
// allows element scoped queries to begin with combinator |
|
|
150 |
// e.g. query('> p', document.body) === query('body > p') |
|
|
151 |
_splitQueries: function(selector, node) { |
|
|
152 |
var groups = selector.split(','), |
|
|
153 |
queries = [], |
|
|
154 |
prefix = '', |
|
|
155 |
i, len; |
|
|
156 |
|
|
|
157 |
if (node) { |
|
|
158 |
// enforce for element scoping |
|
|
159 |
if (node.tagName) { |
|
|
160 |
node.id = node.id || Y.guid(); |
|
|
161 |
prefix = '#' + node.id + ' '; |
|
|
162 |
} |
|
|
163 |
|
|
|
164 |
for (i = 0, len = groups.length; i < len; ++i) { |
|
|
165 |
selector = prefix + groups[i]; |
|
|
166 |
queries.push([selector, node]); |
|
|
167 |
} |
|
|
168 |
} |
|
|
169 |
|
|
|
170 |
return queries; |
|
|
171 |
}, |
|
|
172 |
|
|
|
173 |
_nativeQuery: function(selector, root, one) { |
|
|
174 |
try { |
|
|
175 |
//Y.log('trying native query with: ' + selector, 'info', 'selector-native'); |
|
|
176 |
return root['querySelector' + (one ? '' : 'All')](selector); |
|
|
177 |
} catch(e) { // fallback to brute if available |
|
|
178 |
//Y.log('native query error; reverting to brute query with: ' + selector, 'info', 'selector-native'); |
|
|
179 |
return Y.Selector.query(selector, root, one, true); // redo with skipNative true |
|
|
180 |
} |
|
|
181 |
}, |
|
|
182 |
|
|
|
183 |
filter: function(nodes, selector) { |
|
|
184 |
var ret = [], |
|
|
185 |
i, node; |
|
|
186 |
|
|
|
187 |
if (nodes && selector) { |
|
|
188 |
for (i = 0; (node = nodes[i++]);) { |
|
|
189 |
if (Y.Selector.test(node, selector)) { |
|
|
190 |
ret[ret.length] = node; |
|
|
191 |
} |
|
|
192 |
} |
|
|
193 |
} else { |
|
|
194 |
Y.log('invalid filter input (nodes: ' + nodes + |
|
|
195 |
', selector: ' + selector + ')', 'warn', 'Selector'); |
|
|
196 |
} |
|
|
197 |
|
|
|
198 |
return ret; |
|
|
199 |
}, |
|
|
200 |
|
|
|
201 |
test: function(node, selector, root) { |
|
|
202 |
var ret = false, |
|
|
203 |
groups = selector.split(','), |
|
|
204 |
item, |
|
|
205 |
i, group; |
|
|
206 |
|
|
|
207 |
if (node && node.tagName) { // only test HTMLElements |
|
|
208 |
root = root || node.ownerDocument; |
|
|
209 |
|
|
|
210 |
if (!node.id) { |
|
|
211 |
node.id = TMP_PREFIX + g_counter++; |
|
|
212 |
} |
|
|
213 |
for (i = 0; (group = groups[i++]);) { // TODO: off-dom test |
|
|
214 |
group += '#' + node.id; // add ID for uniqueness |
|
|
215 |
item = Y.Selector.query(group, root, true); |
|
|
216 |
ret = (item === node); |
|
|
217 |
if (ret) { |
|
|
218 |
break; |
|
|
219 |
} |
|
|
220 |
} |
|
|
221 |
} |
|
|
222 |
|
|
|
223 |
return ret; |
|
|
224 |
} |
|
|
225 |
}; |
|
|
226 |
|
|
|
227 |
Y.mix(Y.Selector, Selector, true); |
|
|
228 |
|
|
|
229 |
})(Y); |
|
|
230 |
|
|
|
231 |
|
|
|
232 |
}, '3.0.0' ,{requires:['dom-base']}); |
|
|
233 |
YUI.add('selector-css2', function(Y) { |
|
|
234 |
|
|
|
235 |
/** |
|
|
236 |
* The selector module provides helper methods allowing CSS2 Selectors to be used with DOM elements. |
|
|
237 |
* @module dom |
|
|
238 |
* @submodule selector-css2 |
|
|
239 |
* @for Selector |
|
|
240 |
*/ |
|
|
241 |
|
|
|
242 |
/** |
|
|
243 |
* Provides helper methods for collecting and filtering DOM elements. |
|
|
244 |
*/ |
|
|
245 |
|
|
|
246 |
var PARENT_NODE = 'parentNode', |
|
|
247 |
TAG_NAME = 'tagName', |
|
|
248 |
ATTRIBUTES = 'attributes', |
|
|
249 |
COMBINATOR = 'combinator', |
|
|
250 |
PSEUDOS = 'pseudos', |
|
|
251 |
|
|
|
252 |
Selector = Y.Selector, |
|
|
253 |
|
|
|
254 |
SelectorCSS2 = { |
|
|
255 |
SORT_RESULTS: true, |
|
|
256 |
_children: function(node, tag) { |
|
|
257 |
var ret = node.children, |
|
|
258 |
i, |
|
|
259 |
children = [], |
|
|
260 |
childNodes, |
|
|
261 |
child; |
|
|
262 |
|
|
|
263 |
if (node.children && tag && node.children.tags) { |
|
|
264 |
children = node.children.tags(tag); |
|
|
265 |
} else if ((!ret && node[TAG_NAME]) || (ret && tag)) { // only HTMLElements have children |
|
|
266 |
childNodes = ret || node.childNodes; |
|
|
267 |
ret = []; |
|
|
268 |
for (i = 0; (child = childNodes[i++]);) { |
|
|
269 |
if (child.tagName) { |
|
|
270 |
if (!tag || tag === child.tagName) { |
|
|
271 |
ret.push(child); |
|
|
272 |
} |
|
|
273 |
} |
|
|
274 |
} |
|
|
275 |
} |
|
|
276 |
|
|
|
277 |
return ret || []; |
|
|
278 |
}, |
|
|
279 |
|
|
|
280 |
_regexCache: {}, |
|
|
281 |
|
|
|
282 |
_re: { |
|
|
283 |
attr: /(\[.*\])/g, |
|
|
284 |
pseudos: /:([\-\w]+(?:\(?:['"]?(.+)['"]?\)))*/i |
|
|
285 |
}, |
|
|
286 |
|
|
|
287 |
/** |
|
|
288 |
* Mapping of shorthand tokens to corresponding attribute selector |
|
|
289 |
* @property shorthand |
|
|
290 |
* @type object |
|
|
291 |
*/ |
|
|
292 |
shorthand: { |
|
|
293 |
'\\#(-?[_a-z]+[-\\w]*)': '[id=$1]', |
|
|
294 |
'\\.(-?[_a-z]+[-\\w]*)': '[className~=$1]' |
|
|
295 |
}, |
|
|
296 |
|
|
|
297 |
/** |
|
|
298 |
* List of operators and corresponding boolean functions. |
|
|
299 |
* These functions are passed the attribute and the current node's value of the attribute. |
|
|
300 |
* @property operators |
|
|
301 |
* @type object |
|
|
302 |
*/ |
|
|
303 |
operators: { |
|
|
304 |
'': function(node, attr) { return Y.DOM.getAttribute(node, attr) !== ''; }, // Just test for existence of attribute |
|
|
305 |
//'': '.+', |
|
|
306 |
//'=': '^{val}$', // equality |
|
|
307 |
'~=': '(?:^|\\s+){val}(?:\\s+|$)', // space-delimited |
|
|
308 |
'|=': '^{val}-?' // optional hyphen-delimited |
|
|
309 |
}, |
|
|
310 |
|
|
|
311 |
pseudos: { |
|
|
312 |
'first-child': function(node) { |
|
|
313 |
return Y.Selector._children(node[PARENT_NODE])[0] === node; |
|
|
314 |
} |
|
|
315 |
}, |
|
|
316 |
|
|
|
317 |
_bruteQuery: function(selector, root, firstOnly) { |
|
|
318 |
var ret = [], |
|
|
319 |
nodes = [], |
|
|
320 |
tokens = Selector._tokenize(selector), |
|
|
321 |
token = tokens[tokens.length - 1], |
|
|
322 |
rootDoc = Y.DOM._getDoc(root), |
|
|
323 |
id, |
|
|
324 |
className, |
|
|
325 |
tagName; |
|
|
326 |
|
|
|
327 |
|
|
|
328 |
// if we have an initial ID, set to root when in document |
|
|
329 |
if (tokens[0] && rootDoc === root && |
|
|
330 |
(id = tokens[0].id) && |
|
|
331 |
rootDoc.getElementById(id)) { |
|
|
332 |
root = rootDoc.getElementById(id); |
|
|
333 |
} |
|
|
334 |
|
|
|
335 |
if (token) { |
|
|
336 |
// prefilter nodes |
|
|
337 |
id = token.id; |
|
|
338 |
className = token.className; |
|
|
339 |
tagName = token.tagName || '*'; |
|
|
340 |
|
|
|
341 |
// try ID first |
|
|
342 |
if (id) { |
|
|
343 |
if (rootDoc.getElementById(id)) { // if in document |
|
|
344 |
nodes = [rootDoc.getElementById(id)]; // TODO: DOM.byId? |
|
|
345 |
} |
|
|
346 |
// try className if supported |
|
|
347 |
} else if (className) { |
|
|
348 |
nodes = root.getElementsByClassName(className); |
|
|
349 |
} else if (tagName) { // default to tagName |
|
|
350 |
nodes = root.getElementsByTagName(tagName || '*'); |
|
|
351 |
} |
|
|
352 |
|
|
|
353 |
if (nodes.length) { |
|
|
354 |
ret = Selector._filterNodes(nodes, tokens, firstOnly); |
|
|
355 |
} |
|
|
356 |
} |
|
|
357 |
|
|
|
358 |
return ret; |
|
|
359 |
}, |
|
|
360 |
|
|
|
361 |
_filterNodes: function(nodes, tokens, firstOnly) { |
|
|
362 |
var i = 0, |
|
|
363 |
j, |
|
|
364 |
len = tokens.length, |
|
|
365 |
n = len - 1, |
|
|
366 |
result = [], |
|
|
367 |
node = nodes[0], |
|
|
368 |
tmpNode = node, |
|
|
369 |
getters = Y.Selector.getters, |
|
|
370 |
operator, |
|
|
371 |
combinator, |
|
|
372 |
token, |
|
|
373 |
path, |
|
|
374 |
pass, |
|
|
375 |
//FUNCTION = 'function', |
|
|
376 |
value, |
|
|
377 |
tests, |
|
|
378 |
test; |
|
|
379 |
|
|
|
380 |
//do { |
|
|
381 |
for (i = 0; (tmpNode = node = nodes[i++]);) { |
|
|
382 |
n = len - 1; |
|
|
383 |
path = null; |
|
|
384 |
|
|
|
385 |
testLoop: |
|
|
386 |
while (tmpNode && tmpNode.tagName) { |
|
|
387 |
token = tokens[n]; |
|
|
388 |
tests = token.tests; |
|
|
389 |
j = tests.length; |
|
|
390 |
if (j && !pass) { |
|
|
391 |
while ((test = tests[--j])) { |
|
|
392 |
operator = test[1]; |
|
|
393 |
if (getters[test[0]]) { |
|
|
394 |
value = getters[test[0]](tmpNode, test[0]); |
|
|
395 |
} else { |
|
|
396 |
value = tmpNode[test[0]]; |
|
|
397 |
// use getAttribute for non-standard attributes |
|
|
398 |
if (value === undefined && tmpNode.getAttribute) { |
|
|
399 |
value = tmpNode.getAttribute(test[0]); |
|
|
400 |
} |
|
|
401 |
} |
|
|
402 |
|
|
|
403 |
if ((operator === '=' && value !== test[2]) || // fast path for equality |
|
|
404 |
(operator.test && !operator.test(value)) || // regex test |
|
|
405 |
(operator.call && !operator(tmpNode, test[0]))) { // function test |
|
|
406 |
|
|
|
407 |
// skip non element nodes or non-matching tags |
|
|
408 |
if ((tmpNode = tmpNode[path])) { |
|
|
409 |
while (tmpNode && |
|
|
410 |
(!tmpNode.tagName || |
|
|
411 |
(token.tagName && token.tagName !== tmpNode.tagName)) |
|
|
412 |
) { |
|
|
413 |
tmpNode = tmpNode[path]; |
|
|
414 |
} |
|
|
415 |
} |
|
|
416 |
continue testLoop; |
|
|
417 |
} |
|
|
418 |
} |
|
|
419 |
} |
|
|
420 |
|
|
|
421 |
n--; // move to next token |
|
|
422 |
// now that we've passed the test, move up the tree by combinator |
|
|
423 |
if (!pass && (combinator = token.combinator)) { |
|
|
424 |
path = combinator.axis; |
|
|
425 |
tmpNode = tmpNode[path]; |
|
|
426 |
|
|
|
427 |
// skip non element nodes |
|
|
428 |
while (tmpNode && !tmpNode.tagName) { |
|
|
429 |
tmpNode = tmpNode[path]; |
|
|
430 |
} |
|
|
431 |
|
|
|
432 |
if (combinator.direct) { // one pass only |
|
|
433 |
path = null; |
|
|
434 |
} |
|
|
435 |
|
|
|
436 |
} else { // success if we made it this far |
|
|
437 |
result.push(node); |
|
|
438 |
if (firstOnly) { |
|
|
439 |
return result; |
|
|
440 |
} |
|
|
441 |
break; |
|
|
442 |
} |
|
|
443 |
} |
|
|
444 |
}// while (tmpNode = node = nodes[++i]); |
|
|
445 |
node = tmpNode = null; |
|
|
446 |
return result; |
|
|
447 |
}, |
|
|
448 |
|
|
|
449 |
_getRegExp: function(str, flags) { |
|
|
450 |
var regexCache = Selector._regexCache; |
|
|
451 |
flags = flags || ''; |
|
|
452 |
if (!regexCache[str + flags]) { |
|
|
453 |
regexCache[str + flags] = new RegExp(str, flags); |
|
|
454 |
} |
|
|
455 |
return regexCache[str + flags]; |
|
|
456 |
}, |
|
|
457 |
|
|
|
458 |
combinators: { |
|
|
459 |
' ': { |
|
|
460 |
axis: 'parentNode' |
|
|
461 |
}, |
|
|
462 |
|
|
|
463 |
'>': { |
|
|
464 |
axis: 'parentNode', |
|
|
465 |
direct: true |
|
|
466 |
}, |
|
|
467 |
|
|
|
468 |
|
|
|
469 |
'+': { |
|
|
470 |
axis: 'previousSibling', |
|
|
471 |
direct: true |
|
|
472 |
} |
|
|
473 |
}, |
|
|
474 |
|
|
|
475 |
_parsers: [ |
|
|
476 |
{ |
|
|
477 |
name: ATTRIBUTES, |
|
|
478 |
re: /^\[([a-z]+\w*)+([~\|\^\$\*!=]=?)?['"]?([^\]]*?)['"]?\]/i, |
|
|
479 |
fn: function(match, token) { |
|
|
480 |
var operator = match[2] || '', |
|
|
481 |
operators = Y.Selector.operators, |
|
|
482 |
test; |
|
|
483 |
|
|
|
484 |
// add prefiltering for ID and CLASS |
|
|
485 |
if ((match[1] === 'id' && operator === '=') || |
|
|
486 |
(match[1] === 'className' && |
|
|
487 |
document.getElementsByClassName && |
|
|
488 |
(operator === '~=' || operator === '='))) { |
|
|
489 |
token.prefilter = match[1]; |
|
|
490 |
token[match[1]] = match[3]; |
|
|
491 |
} |
|
|
492 |
|
|
|
493 |
// add tests |
|
|
494 |
if (operator in operators) { |
|
|
495 |
test = operators[operator]; |
|
|
496 |
if (typeof test === 'string') { |
|
|
497 |
test = Y.Selector._getRegExp(test.replace('{val}', match[3])); |
|
|
498 |
} |
|
|
499 |
match[2] = test; |
|
|
500 |
} |
|
|
501 |
if (!token.last || token.prefilter !== match[1]) { |
|
|
502 |
return match.slice(1); |
|
|
503 |
} |
|
|
504 |
} |
|
|
505 |
|
|
|
506 |
}, |
|
|
507 |
{ |
|
|
508 |
name: TAG_NAME, |
|
|
509 |
re: /^((?:-?[_a-z]+[\w-]*)|\*)/i, |
|
|
510 |
fn: function(match, token) { |
|
|
511 |
var tag = match[1].toUpperCase(); |
|
|
512 |
token.tagName = tag; |
|
|
513 |
|
|
|
514 |
if (tag !== '*' && (!token.last || token.prefilter)) { |
|
|
515 |
return [TAG_NAME, '=', tag]; |
|
|
516 |
} |
|
|
517 |
if (!token.prefilter) { |
|
|
518 |
token.prefilter = 'tagName'; |
|
|
519 |
} |
|
|
520 |
} |
|
|
521 |
}, |
|
|
522 |
{ |
|
|
523 |
name: COMBINATOR, |
|
|
524 |
re: /^\s*([>+~]|\s)\s*/, |
|
|
525 |
fn: function(match, token) { |
|
|
526 |
} |
|
|
527 |
}, |
|
|
528 |
{ |
|
|
529 |
name: PSEUDOS, |
|
|
530 |
re: /^:([\-\w]+)(?:\(['"]?(.+)['"]?\))*/i, |
|
|
531 |
fn: function(match, token) { |
|
|
532 |
var test = Selector[PSEUDOS][match[1]]; |
|
|
533 |
if (test) { // reorder match array |
|
|
534 |
return [match[2], test]; |
|
|
535 |
} else { // selector token not supported (possibly missing CSS3 module) |
|
|
536 |
return false; |
|
|
537 |
} |
|
|
538 |
} |
|
|
539 |
} |
|
|
540 |
], |
|
|
541 |
|
|
|
542 |
_getToken: function(token) { |
|
|
543 |
return { |
|
|
544 |
tagName: null, |
|
|
545 |
id: null, |
|
|
546 |
className: null, |
|
|
547 |
attributes: {}, |
|
|
548 |
combinator: null, |
|
|
549 |
tests: [] |
|
|
550 |
}; |
|
|
551 |
}, |
|
|
552 |
|
|
|
553 |
/** |
|
|
554 |
Break selector into token units per simple selector. |
|
|
555 |
Combinator is attached to the previous token. |
|
|
556 |
*/ |
|
|
557 |
_tokenize: function(selector) { |
|
|
558 |
selector = selector || ''; |
|
|
559 |
selector = Selector._replaceShorthand(Y.Lang.trim(selector)); |
|
|
560 |
var token = Selector._getToken(), // one token per simple selector (left selector holds combinator) |
|
|
561 |
query = selector, // original query for debug report |
|
|
562 |
tokens = [], // array of tokens |
|
|
563 |
found = false, // whether or not any matches were found this pass |
|
|
564 |
match, // the regex match |
|
|
565 |
test, |
|
|
566 |
i, parser; |
|
|
567 |
|
|
|
568 |
/* |
|
|
569 |
Search for selector patterns, store, and strip them from the selector string |
|
|
570 |
until no patterns match (invalid selector) or we run out of chars. |
|
|
571 |
|
|
|
572 |
Multiple attributes and pseudos are allowed, in any order. |
|
|
573 |
for example: |
|
|
574 |
'form:first-child[type=button]:not(button)[lang|=en]' |
|
|
575 |
*/ |
|
|
576 |
outer: |
|
|
577 |
do { |
|
|
578 |
found = false; // reset after full pass |
|
|
579 |
for (i = 0; (parser = Selector._parsers[i++]);) { |
|
|
580 |
if ( (match = parser.re.exec(selector)) ) { // note assignment |
|
|
581 |
if (parser !== COMBINATOR ) { |
|
|
582 |
token.selector = selector; |
|
|
583 |
} |
|
|
584 |
selector = selector.replace(match[0], ''); // strip current match from selector |
|
|
585 |
if (!selector.length) { |
|
|
586 |
token.last = true; |
|
|
587 |
} |
|
|
588 |
|
|
|
589 |
if (Selector._attrFilters[match[1]]) { // convert class to className, etc. |
|
|
590 |
match[1] = Selector._attrFilters[match[1]]; |
|
|
591 |
} |
|
|
592 |
|
|
|
593 |
test = parser.fn(match, token); |
|
|
594 |
if (test === false) { // selector not supported |
|
|
595 |
found = false; |
|
|
596 |
break outer; |
|
|
597 |
} else if (test) { |
|
|
598 |
token.tests.push(test); |
|
|
599 |
} |
|
|
600 |
|
|
|
601 |
if (!selector.length || parser.name === COMBINATOR) { |
|
|
602 |
tokens.push(token); |
|
|
603 |
token = Selector._getToken(token); |
|
|
604 |
if (parser.name === COMBINATOR) { |
|
|
605 |
token.combinator = Y.Selector.combinators[match[1]]; |
|
|
606 |
} |
|
|
607 |
} |
|
|
608 |
found = true; |
|
|
609 |
} |
|
|
610 |
} |
|
|
611 |
} while (found && selector.length); |
|
|
612 |
|
|
|
613 |
if (!found || selector.length) { // not fully parsed |
|
|
614 |
Y.log('query: ' + query + ' contains unsupported token in: ' + selector, 'warn', 'Selector'); |
|
|
615 |
tokens = []; |
|
|
616 |
} |
|
|
617 |
return tokens; |
|
|
618 |
}, |
|
|
619 |
|
|
|
620 |
_replaceShorthand: function(selector) { |
|
|
621 |
var shorthand = Selector.shorthand, |
|
|
622 |
attrs = selector.match(Selector._re.attr), // pull attributes to avoid false pos on "." and "#" |
|
|
623 |
pseudos = selector.match(Selector._re.pseudos), // pull attributes to avoid false pos on "." and "#" |
|
|
624 |
re, i, len; |
|
|
625 |
|
|
|
626 |
if (pseudos) { |
|
|
627 |
selector = selector.replace(Selector._re.pseudos, '!!REPLACED_PSEUDO!!'); |
|
|
628 |
} |
|
|
629 |
|
|
|
630 |
if (attrs) { |
|
|
631 |
selector = selector.replace(Selector._re.attr, '!!REPLACED_ATTRIBUTE!!'); |
|
|
632 |
} |
|
|
633 |
|
|
|
634 |
for (re in shorthand) { |
|
|
635 |
if (shorthand.hasOwnProperty(re)) { |
|
|
636 |
selector = selector.replace(Selector._getRegExp(re, 'gi'), shorthand[re]); |
|
|
637 |
} |
|
|
638 |
} |
|
|
639 |
|
|
|
640 |
if (attrs) { |
|
|
641 |
for (i = 0, len = attrs.length; i < len; ++i) { |
|
|
642 |
selector = selector.replace('!!REPLACED_ATTRIBUTE!!', attrs[i]); |
|
|
643 |
} |
|
|
644 |
} |
|
|
645 |
if (pseudos) { |
|
|
646 |
for (i = 0, len = pseudos.length; i < len; ++i) { |
|
|
647 |
selector = selector.replace('!!REPLACED_PSEUDO!!', pseudos[i]); |
|
|
648 |
} |
|
|
649 |
} |
|
|
650 |
return selector; |
|
|
651 |
}, |
|
|
652 |
|
|
|
653 |
_attrFilters: { |
|
|
654 |
'class': 'className', |
|
|
655 |
'for': 'htmlFor' |
|
|
656 |
}, |
|
|
657 |
|
|
|
658 |
getters: { |
|
|
659 |
href: function(node, attr) { |
|
|
660 |
return Y.DOM.getAttribute(node, attr); |
|
|
661 |
} |
|
|
662 |
} |
|
|
663 |
}; |
|
|
664 |
|
|
|
665 |
Y.mix(Y.Selector, SelectorCSS2, true); |
|
|
666 |
Y.Selector.getters.src = Y.Selector.getters.rel = Y.Selector.getters.href; |
|
|
667 |
|
|
|
668 |
// IE wants class with native queries |
|
|
669 |
if (Y.Selector.useNative && document.querySelector) { |
|
|
670 |
Y.Selector.shorthand['\\.(-?[_a-z]+[-\\w]*)'] = '[class~=$1]'; |
|
|
671 |
} |
|
|
672 |
|
|
|
673 |
|
|
|
674 |
|
|
|
675 |
}, '3.0.0' ,{requires:['selector-native']}); |
|
|
676 |
|
|
|
677 |
|
|
|
678 |
YUI.add('selector', function(Y){}, '3.0.0' ,{use:['selector-native', 'selector-css2']}); |
|
|
679 |
|