selector.js revision 7f301ced05415668ede239d3d16fe4a4199754e6
(function(Y) {
/**
* The selector-native module provides support for native querySelector
* @module selector-native
*/
/**
* Provides a wrapper for native querySelectorAll
* @for Selector
*/
var NativeSelector = {
_reUnSupported: /!./,
_foundCache: [],
_supportsNative: function() {
// whitelist and feature detection to manage
// future implementations manually
},
i, len;
try {
} catch(e) { // IE: requires manual copy
ret = [];
}
}
}
return ret;
},
_clearFoundCache: function() {
i, len;
try { // IE no like delete
delete foundCache[i]._found;
} catch(e) {
}
}
foundCache = [];
},
if (nodes) {
});
}
}
return nodes;
},
var ret = [],
i, node;
}
}
return ret;
},
// allows element scoped queries to begin with combinator
// e.g. query('> p', document.body) === query('body > p')
queries = [],
i, len;
if (root) {
if (!isDocRoot) {
// break into separate queries for element scoping
}
} else {
}
}
return queries;
},
}
i, query;
if (selector) {
ret = [];
try {
}
} catch(e) {
}
}
}
}
return ret;
},
var ret = [],
i, node;
}
}
} else {
}
return ret;
},
var ret = false,
id,
item,
i, group;
if (ret) {
break;
}
}
}
return ret;
}
};
}
// allow standalone selector-native module
if (NativeSelector._supportsNative()) {
//Y.Selector.filter = NativeSelector._filter;
//Y.Selector.test = NativeSelector._test;
}
})(Y);
/**
* The selector module provides helper methods allowing CSS2 Selectors to be used with DOM elements.
* @module selector
* @title Selector Utility
* @requires yahoo, dom
*/
/**
* Provides helper methods for collecting and filtering DOM elements.
* @class Selector
* @static
*/
var PARENT_NODE = 'parentNode',
TAG_NAME = 'tagName',
ATTRIBUTES = 'attributes',
COMBINATOR = 'combinator',
PSEUDOS = 'pseudos',
PREVIOUS = 'previous',
PREVIOUS_SIBLING = 'previousSibling',
LENGTH = 'length',
_childCache = [], // cache to cleanup expando node.children
SelectorCSS2 = {
SORT_RESULTS: true,
i, n;
ret = [];
if (n.tagName) {
}
}
}
return ret || [];
},
_regexCache: {},
_re: {
attr: /(\[.*\])/g,
},
/**
* Mapping of shorthand tokens to corresponding attribute selector
* @property shorthand
* @type object
*/
shorthand: {
'\\#(-?[_a-z]+[-\\w]*)': '[id=$1]',
'\\.(-?[_a-z]+[-\\w]*)': '[className~=$1]'
},
/**
* List of operators and corresponding boolean functions.
* These functions are passed the attribute and the current node's value of the attribute.
* @property operators
* @type object
*/
operators: {
'': function(node, m) { return Y.DOM.getAttribute(node, m[0]) !== ''; }, // Just test for existence of attribute
//'': '.+',
'=': '^{val}$', // equality
'~=': '(?:^|\\s+){val}(?:\\s+|$)', // space-delimited
'|=': '^{val}-?' // optional hyphen-delimited
},
pseudos: {
'first-child': function(node) {
}
},
_brute: {
/**
* Retrieves a set of nodes based on a given CSS selector.
* @method query
*
* @param {string} selector The CSS Selector to test the node against.
* @param {HTMLElement} root optional An HTMLElement to start the query from. Defaults to Y.config.doc
* @param {Boolean} firstOnly optional Whether or not to return only the first match.
* @return {Array} An array of nodes that match the given selector.
* @static
*/
var ret = [];
if (selector) {
}
}
},
// TODO: make extensible? events?
_cleanup: function() {
}
_childCache = [];
},
var ret = [],
nodes = [],
i, len;
}
} else {
}
// fast-path ID when possible
}
}
if (token) {
if (deDupe) {
}
}
// TODO: no prefilter for off-dom id
}
if (firstOnly) {
} else {
}
}
}
}
return ret;
},
i = 0,
null,
test,
attr;
if (//node[TAG_NAME] && // tagName limits to HTMLElements
i++;
return false;
}
return false;
}
}
return false;
}
return false;
}
}
return true;
}
return false;
},
}
},
combinators: {
return true;
}
}
return false;
},
},
}
return true;
}
return false;
}
},
_parsers: [
{
};
return true;
}
},
{
fn: function(token, match) {
var val = match[3],
operator = !(match[2] && val) ? '' : match[2],
test = Selector.operators[operator];
// might be a function
if (typeof test === 'string') {
test = Selector._getRegExp(test.replace('{val}', val));
}
if (match[1] === 'id' && val) { // store ID for fast-path match
token.id = val;
token.prefilter = function(root) {
var doc = root.nodeType === 9 ? root : root.ownerDocument,
node = doc.getElementById(val);
return node ? [node] : [];
};
} else if (document.documentElement.getElementsByClassName &&
match[1].indexOf('class') === 0) {
if (!token.prefilter) {
token.prefilter = function(root) {
return root.getElementsByClassName(val);
};
test = true; // skip class test
}
}
return test;
}
},
{
name: COMBINATOR,
re: /^\s*([>+~]|\s)\s*/,
fn: function(token, match) {
token[COMBINATOR] = match[1];
return !! Selector.combinators[token[COMBINATOR]];
}
},
{
name: PSEUDOS,
re: /^:([\-\w]+)(?:\(['"]?(.+)['"]?\))*/i,
fn: function(token, match) {
return Selector[PSEUDOS][match[1]];
}
}
],
_getToken: function(token) {
return {
previous: token,
combinator: ' ',
tag: '*',
prefilter: function(root) {
return root.getElementsByTagName('*');
},
tests: [],
result: []
};
},
/**
Break selector into token units per simple selector.
Combinator is attached to the previous token.
*/
_tokenize: function(selector, root) {
selector = selector || '';
selector = Selector._replaceShorthand(Y.Lang.trim(selector));
var token = Selector._getToken(), // one token per simple selector (left selector holds combinator)
query = selector, // original query for debug report
tokens = [], // array of tokens
found = false, // whether or not any matches were found this pass
test,
match, // the regex match
i, parser;
/*
Search for selector patterns, store, and strip them from the selector string
until no patterns match (invalid selector) or we run out of chars.
Multiple attributes and pseudos are allowed, in any order.
for example:
*/
outer:
do {
found = false; // reset after full pass
for (i = 0, parser; parser = Selector._parsers[i++];) {
if ( (match = parser.re.exec(selector)) ) { // note assignment
test = parser.fn(token, match);
if (test) {
if (test !== true) { // auto-pass
token.tests.push({
name: match[1],
test: test,
match: match.slice(1)
});
}
found = true;
selector = selector.replace(match[0], ''); // strip current match from selector
if (!selector.length || parser.name === COMBINATOR) {
token.root = root;
tokens.push(token);
token = Selector._getToken(token);
}
} else {
found = false;
break outer;
}
}
}
} while (found && selector.length);
if (!found || selector.length) { // not fully parsed
tokens = [];
} else if (tokens.length) {
tokens[tokens.length - 1].last = true;
}
return tokens;
},
_replaceShorthand: function(selector) {
var shorthand = Selector.shorthand,
attrs = selector.match(Selector._re.attr), // pull attributes to avoid false pos on "." and "#"
re, i, len;
if (attrs) {
}
for (re in shorthand) {
if (shorthand.hasOwnProperty(re)) {
}
}
if (attrs) {
for (i = 0, len = attrs.length; i < len; ++i) {
}
}
return selector;
}
};
Y.mix(Y.Selector, SelectorCSS2, true);
// only override native when not supported
if (!Y.Selector._supportsNative()) {
Y.Selector.query = Selector._brute.query;
}