selector.js revision e3fa31b41d9fb77561d888486cfbb728d490c5df
/**
* Provides helper methods for collecting and filtering DOM elements.
* @module dom
* @submodule selector
*/
/**
* Provides helper methods for collecting and filtering DOM elements.
* @class Selector
* @static
*/
var TAG = 'tag',
PARENT_NODE = 'parentNode',
PREVIOUS_SIBLING = 'previousSibling',
LENGTH = 'length',
NODE_TYPE = 'nodeType',
TAG_NAME = 'tagName',
ATTRIBUTES = 'attributes',
PSEUDOS = 'pseudos',
COMBINATOR = 'combinator';
var patterns = {
pseudos: /^:([\-\w]+)(?:\(['"]?(.+)['"]?\))*/i,
combinator: /^\s*([>+~]|\s)\s*/
};
var Selector = {
/**
* Default document for use queries
* @property document
* @type object
* @default window.document
*/
document: Y.config.doc,
/**
* Mapping of attributes to aliases, normally to work around HTMLAttributes
* that conflict with JS reserved words.
* @property attrAliases
* @type object
*/
attrAliases: {},
/**
* Mapping of shorthand tokens to corresponding attribute selector
* @property shorthand
* @type object
*/
shorthand: {
},
/**
* List of operators and corresponding boolean functions.
*/
operators: {
var s = ' ';
},
'|=': function(attr, val) { return Y.DOM._getRegExp('^' + val + '[-]?').test(attr); }, // Match start with value followed by optional hyphen
'$=': function(attr, val) { return attr.lastIndexOf(val) === attr[LENGTH] - val[LENGTH]; }, // Match ends with value
},
/**
* List of pseudo-classes and corresponding boolean functions.
* These functions are called with the current node, and any value that was parsed with the pseudo regex.
* @property pseudos
* @type object
*/
pseudos: {
'root': function(node) {
},
},
},
},
},
'first-child': function(node) {
},
'last-child': function(node) {
},
},
},
'only-child': function(node) {
},
'only-of-type': function(node) {
},
'empty': function(node) {
},
},
},
'checked': function(node) {
}
},
/**
* Test if the supplied node matches the supplied selector.
* @method test
*
* @param {HTMLElement | String} node An id or node reference to the HTMLElement being tested.
* @param {string} selector The CSS Selector to test the node against.
* @return{boolean} Whether or not the node matches the selector.
* @static
*/
if (!node) {
return false;
}
return true;
}
}
return false;
}
},
/**
* Filters a set of nodes based on a given CSS selector.
* @method filter
*
* @param {string} selector The selector used to test each node.
* @return{array} An array of nodes from the supplied array that match the given selector.
* @static
*/
return result;
},
/**
* 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 | String} root optional An id or HTMLElement to start the query from. Defaults to Selector.document.
* @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
*/
return result;
},
if (!selector) {
return result;
}
var found;
}
return result;
}
nodes = [],
node,
id,
if (idToken) {
}
// use id shortcut when possible
if (id) {
} else {
}
}
} else {
return result;
}
}
}
}
return result;
},
return false;
}
if (deDupe) {
return false;
}
}
return true;
}, firstOnly);
return result;
},
i, len;
return false;
}
var attribute;
return false;
}
return false;
}
}
}
return false;
}
}
}
true;
},
_foundCache: [],
_regexCache: {},
_clearFoundCache: function() {
try { // IE no like delete
} catch(e) {
}
}
Selector._foundCache = [];
},
combinators: {
return true;
}
}
return false;
},
},
}
return true;
}
return false;
},
while (sib) {
return true;
}
}
return false;
}
},
/*
an+b = get every _a_th node starting at the _b_th
0n+b = no repeat ("0" and "n" may both be omitted (together) , e.g. "0n+1" or "1", not "0+1"), return only the _b_th element
1n+b = get every element starting from b ("1" may may be omitted, e.g. "1n+0" or "n+0" or "n")
an+0 = get every _a_th element, "0" may be omitted
*/
var a = parseInt(RegExp.$1, 10), // include every _a_ elements (zero means no repeat, just first _a_)
n = RegExp.$2, // "n"
if (tag) {
} else {
}
if (oddeven) {
a = 2; // always every other
op = '+';
n = 'n';
} else if ( isNaN(a) ) {
a = (n) ? 1 : 0; // start from the first or no repeat
}
if (a === 0) { // just the first
if (reverse) {
}
return true;
} else {
return false;
}
} else if (a < 0) {
a = Math.abs(a);
}
if (!reverse) {
return true;
}
}
} else {
return true;
}
}
}
return false;
},
return attr[i][2];
}
}
},
_getIdTokenIndex: function(tokens) {
return i;
}
}
return -1;
},
/**
Break selector into token units per simple selector.
Combinator is attached to left-hand selector.
*/
var token = {}, // one token per simple selector (left selector holds combinator)
tokens = [], // array of tokens
found = false, // whether or not any matches were found this pass
match; // the regex match
/*
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:
'form:first-child[type=button]:not(button)[lang|=en]'
*/
do {
found = false; // reset after full pass
}
found = true;
//token[re] = token[re] || [];
// capture ID for fast path to element
}
} else { // single selector (tag, combinator)
}
token = { // prep next token
};
}
}
}
}
} while (found);
return tokens;
},
_fixAttributes: function(attr) {
}
}
}
return attr;
},
_replaceShorthand: function(selector) {
var attrs = selector.match(patterns[ATTRIBUTES]); // pull attributes to avoid false pos on "." and "#"
if (attrs) {
}
}
}
if (attrs) {
}
}
return selector;
}
};
}