selector-debug.js revision 3c77f2a2371e25d231bbf46ef6d4afc6e947598d
(function(Y) {
/**
* The selector-native module provides support for native querySelector
* @module dom
* @submodule selector-native
* @for Selector
*/
/**
* Provides support for using CSS selectors to query the DOM
* @class Selector
* @static
* @for Selector
*/
var COMPARE_DOCUMENT_POSITION = 'compareDocumentPosition',
OWNER_DOCUMENT = 'ownerDocument',
TMP_PREFIX = 'yui-tmp-',
g_counter = 0;
var Selector = {
_reUnSupported: /!./g,
_foundCache: [],
_supportsNative: function() {
// whitelist and feature detection to manage
// future implementations manually
},
useNative: true,
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 = [];
},
var a = nodeA.sourceIndex,
b = nodeB.sourceIndex;
if (a === b) {
return 0;
} else if (a > b) {
return 1;
}
return -1;
return -1;
} else {
return 1;
}
} :
}
return compare;
}),
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 = [],
inDoc = true,
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,
item,
i, group;
}
if (ret) {
break;
}
}
}
return ret;
}
};
}
// allow standalone selector-native module
}
})(Y);
/**
* 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',
//TMP_PREFIX = 'yui-tmp-',
//g_counter = 0,
//g_idCache = [],
//g_passCache = {},
g_childCache = [], // cache to cleanup expando node.children
SelectorCSS2 = {
SORT_RESULTS: true,
i,
children = [],
ret = [];
}
}
}
}
}
}
return ret || [];
},
_regexCache: {},
_re: {
attr: /(\[.*\])/g,
urls: /^(?:href|src)/
},
/**
* 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) {
return Y.Selector._children(node[PARENT_NODE])[0] === 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
*/
query: function(selector, root, firstOnly) {
var ret = [];
if (Selector.useNative && Selector._supportsNative() && // use native
!Selector._reUnSupported.test(selector)) { // unless selector is not supported
return Selector._nativeQuery.apply(Selector, arguments);
}
if (selector) {
ret = Selector._query.apply(Selector, arguments);
}
Y.log('query: ' + selector + ' returning: ' + ret.length, 'info', 'Selector');
Selector._cleanup();
return (firstOnly) ? (ret[0] || null) : ret;
},
// TODO: make extensible? events?
_cleanup: function() {
for (var i = 0, node; node = g_childCache[i++];) {
delete node._children;
}
/*
for (i = 0, node; node = g_idCache[i++];) {
node.removeAttribute('id');
}
*/
g_childCache = [];
//g_passCache = {};
//g_idCache = [];
},
_query: function(selector, root, firstOnly, deDupe) {
var ret = [],
nodes = [],
rootDoc,
tokens,
token,
id,
className,
tagName,
i, len;
if (groups.length > 1) {
for (i = 0, len = groups.length; i < len; ++i) {
ret = ret.concat(arguments.callee(groups[i],
root, firstOnly, true));
}
ret = Selector._deDupe(ret);
ret = Selector.SORT_RESULTS ? Selector._sort(ret) : ret;
} else {
root = root || Y.config.doc;
tokens = Selector._tokenize(selector);
if (root.tagName) { // enforce element scope
if (!root.id) { // by prepending ID
root.id = Y.guid();
}
selector = '#' + root.id + ' ' + selector;
rootDoc = root.ownerDocument;
tokens = Selector._tokenize(selector); // regenerate tokens
} else {
rootDoc = root;
}
// if we have an initial ID, set to root when in document
if (tokens[0] && rootDoc === root &&
(id = tokens[0].id) &&
rootDoc.getElementById(id)) {
root = rootDoc.getElementById(id);
}
token = tokens[tokens.length - 1];
if (token) {
// prefilter nodes
id = token.id;
className = token.className;
tagName = token.tagName || '*';
// try ID first
if (id) {
if (rootDoc.getElementById(id)) { // if in document
nodes = [rootDoc.getElementById(id)]; // TODO: DOM.byId?
}
// try className if supported
} else if (className) {
nodes = root.getElementsByClassName(className);
} else if (tagName) { // default to tagName
nodes = root.getElementsByTagName(tagName || '*');
}
if (nodes.length) {
ret = Selector._filterNodes(nodes, tokens, firstOnly);
}
}
}
return ret;
},
_filterNodes: function(nodes, tokens, firstOnly) {
var i = 0,
j,
len = tokens.length,
n = len - 1,
result = [],
node = nodes[0],
tmpNode = node,
getters = Y.Selector.getters,
operator,
combinator,
token,
path,
pass,
//FUNCTION = 'function',
value,
tests,
test;
//do {
for (i = 0; tmpNode = node = nodes[i++];) {
n = len - 1;
path = null;
testLoop:
while (tmpNode && tmpNode.tagName) {
token = tokens[n];
//pass = g_passCache[tmpNode.id];
tests = token.tests;
j = tests.length;
if (j && !pass) {
while ((test = tests[--j])) {
operator = test[1];
value = tmpNode[test[0]];
// skip node as soon as a test fails
if (getters[test[0]]) {
value = getters[test[0]](tmpNode, test[0]);
}
if ((operator === '=' && value !== test[2]) || // fast path for equality
(operator.test && !operator.test(value)) || // regex test
(operator.call && !operator(tmpNode, test[0]))) { // function test
// skip non element nodes or non-matching tags
if ((tmpNode = tmpNode[path])) {
while (tmpNode &&
(!tmpNode.tagName ||
(token.tagName && token.tagName !== tmpNode.tagName))
) {
tmpNode = tmpNode[path];
}
}
continue testLoop;
}
}
}
n--; // move to next token
// now that we've passed the test, move up the tree by combinator
if (!pass && (combinator = token.combinator)) {
path = combinator.axis;
tmpNode = tmpNode[path];
// skip non element nodes
while (tmpNode && !tmpNode.tagName) {
tmpNode = tmpNode[path];
}
if (combinator.direct) { // one pass only
path = null;
}
} else { // success if we made it this far
result.push(node);
/*
if (!pass) {
if (!tmpNode.id) {
tmpNode.id = TMP_PREFIX + g_counter++;
g_idCache.push(tmpNode);
}
//g_passCache[tmpNode.id] = 1;
}
*/
if (firstOnly) {
return result;
}
break;
}
}
}// while (tmpNode = node = nodes[++i]);
node = tmpNode = null;
return result;
},
_getRegExp: function(str, flags) {
var regexCache = Selector._regexCache;
flags = flags || '';
if (!regexCache[str + flags]) {
regexCache[str + flags] = new RegExp(str, flags);
}
return regexCache[str + flags];
},
combinators: {
' ': {
axis: 'parentNode'
},
'>': {
axis: 'parentNode',
direct: true
},
'+': {
axis: 'previousSibling',
direct: true
}
},
_parsers: [
{
name: ATTRIBUTES,
re: /^\[([a-z]+\w*)+([~\|\^\$\*!=]=?)?['"]?([^\]]*?)['"]?\]/i,
fn: function(match, token) {
var operator = match[2] || '',
operators = Y.Selector.operators,
test;
// add prefiltering for ID and CLASS
document.getElementsByClassName &&
(operator === '~=' || operator === '='))) {
token.prefilter = match[1];
token[match[1]] = match[3];
}
// add tests
if (operator in operators) {
test = operators[operator];
}
match[2] = test;
}
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 (tag !== '*' && (!token.last || token.prefilter)) {
return [TAG_NAME, '=', tag];
}
if (!token.prefilter) {
}
}
},
{
name: COMBINATOR,
re: /^\s*([>+~]|\s)\s*/,
fn: function(match, token) {
}
},
{
name: PSEUDOS,
re: /^:([\-\w]+)(?:\(['"]?(.+)['"]?\))*/i,
if (test) { // reorder match array
} else { // selector token not supported (possibly missing CSS3 module)
return false;
}
}
}
],
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.
*/
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:
'form:first-child[type=button]:not(button)[lang|=en]'
*/
do {
found = false; // reset after full pass
if (parser !== COMBINATOR ) {
}
}
}
if (test === false) { // selector not supported
found = false;
break outer;
} else if (test) {
}
}
}
found = true;
}
}
tokens = [];
}
return tokens;
},
_replaceShorthand: function(selector) {
pseudos = selector.match(Selector._re.pseudos), // pull attributes to avoid false pos on "." and "#"
if (pseudos) {
}
if (attrs) {
}
}
}
if (attrs) {
}
}
if (pseudos) {
}
}
return selector;
},
_attrFilters: {
'class': 'className',
'for': 'htmlFor'
},
getters: {
}
}
};
// IE wants class with native queries
}