selector-css2.js revision d59c625bc91380e7d3458db5e7e9a581d670684a
/**
* The selector module provides helper methods allowing CSS2 Selectors to be used with DOM elements.
* @module dom
* @submodule selector-css2
* @for Selector
*/
/**
* Provides helper methods for collecting and filtering DOM elements.
*/
var PARENT_NODE = 'parentNode',
TAG_NAME = 'tagName',
ATTRIBUTES = 'attributes',
COMBINATOR = 'combinator',
PSEUDOS = 'pseudos',
PREVIOUS = 'previous',
PREVIOUS_SIBLING = 'previousSibling',
_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, attr) { return Y.DOM.getAttribute(node, attr) !== ''; }, // Just test for existence of attribute
//'': '.+',
//'=': '^{val}$', // equality
'~=': '(?:^|\\s+){val}(?:\\s+|$)', // space-delimited
'|=': '^{val}-?' // optional hyphen-delimited
},
pseudos: {
'first-child': function(node) {
}
},
/**
* 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 = [],
id,
i, len;
}
} else {
}
// fast-path ID when possible
// TODO: no prefilter for off-dom id
}
}
if (token) {
if (deDupe) {
}
if (tokens[0] &&
}
// prefilter nodes
// try ID first
// try className if supported
} else if (className) {
} else if (tagName) { // default to tagName
}
}
}
}
return ret;
},
var i = 0,
j,
n = len - 1,
result = [],
path,
FUNCTION = 'function',
test;
//do {
n = len - 1;
path = null;
if (j) {
//(typeof operator === FUNCTION && !operator(tmpNode, test[0])) ||
//(operator.test && !operator.test(tmpNode[test[0]]))) {
continue testLoop;
}
}
}
n--; // move to next token
if (combinator.direct) {
path = null; // one pass only
}
continue;
} else { // success if we made it this far
if (firstOnly) {
return result;
}
break;
}
}
}// while (tmpNode = node = nodes[++i]);
return result;
},
}
},
combinators: {
' ': {
axis: 'parentNode'
},
'>': {
axis: 'parentNode',
direct: true
},
'+': {
axis: 'previousSibling',
direct: true
}
},
_parsers: [
{
fn: function(match, token) {
var operator = match[2];
if (match[1] === 'id' ||
(match[1] === 'className' &&
document.getElementsByClassName &&
(operator === '~=' || operator === '='))) {
token.prefilter = match[1];
token[match[1]] = match[3];
}
if (operator in Y.Selector.operators) {
match[2] = Y.Selector._getRegExp(Y.Selector.operators[operator].replace('{val}', match[3]));
}
if (!token.last || token.prefilter !== match[1]) {
return match.slice(1);
}
}
},
{
name: TAG_NAME,
re: /^((?:-?[_a-z]+[\w-]*)|\*)/i,
fn: function(match, token) {
var tag = match[1].toUpperCase();
token.tagName = tag;
if (!token.last || token.prefilter) {
return [TAG_NAME, '=', tag];
}
if (!token.prefilter) {
token.prefilter = 'tagName';
}
}
},
{
name: COMBINATOR,
re: /^\s*([>+~]|\s)\s*/,
fn: function(match, token) {
}
},
{
name: PSEUDOS,
re: /^:([\-\w]+)(?:\(['"]?(.+)['"]?\))*/i,
fn: function(match, token) {
return [
match[2],
Selector[PSEUDOS][match[1]]
];
}
}
],
_getToken: function(token) {
return {
tagName: null,
id: null,
className: null,
attributes: {},
combinator: null,
tests: []
};
},
/**
Break selector into token units per simple selector.
Combinator is attached to the previous token.
*/
_tokenize: function(selector) {
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
match, // the regex match
test,
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
if (parser !== COMBINATOR) {
token.selector = match[0];
}
selector = selector.replace(match[0], ''); // strip current match from selector
if (!selector.length) {
token.last = true;
}
test = parser.fn(match, token);
if (test) {
token.tests.push(test);
}
if (!selector.length || parser.name === COMBINATOR) {
tokens.push(token);
token = Selector._getToken(token);
if (parser.name === COMBINATOR) {
token.combinator = Y.Selector.combinators[match[1]];
}
}
found = true;
}
}
} while (found && selector.length);
if (!found || selector.length) { // not fully parsed
tokens = [];
}
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);