2008-03-17 15:59:58 +00:00
|
|
|
/* Portions of the Selector class are derived from Jack Slocum's DomQuery,
|
2007-03-12 20:27:19 +00:00
|
|
|
* part of YUI-Ext version 0.40, distributed under the terms of an MIT-style
|
|
|
|
* license. Please see http://www.yui-ext.com/ for more information. */
|
|
|
|
|
2007-08-18 23:21:29 +00:00
|
|
|
var Selector = Class.create({
|
2007-01-18 22:24:27 +00:00
|
|
|
initialize: function(expression) {
|
2007-03-09 04:12:13 +00:00
|
|
|
this.expression = expression.strip();
|
2008-03-27 06:18:15 +00:00
|
|
|
|
|
|
|
if (this.shouldUseSelectorsAPI()) {
|
|
|
|
this.mode = 'selectorsAPI';
|
|
|
|
} else if (this.shouldUseXPath()) {
|
|
|
|
this.mode = 'xpath';
|
|
|
|
this.compileXPathMatcher();
|
|
|
|
} else {
|
|
|
|
this.mode = "normal";
|
|
|
|
this.compileMatcher();
|
|
|
|
}
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2007-03-09 04:12:13 +00:00
|
|
|
|
2007-11-27 18:21:50 +00:00
|
|
|
shouldUseXPath: function() {
|
|
|
|
if (!Prototype.BrowserFeatures.XPath) return false;
|
|
|
|
|
|
|
|
var e = this.expression;
|
|
|
|
|
|
|
|
// Safari 3 chokes on :*-of-type and :empty
|
|
|
|
if (Prototype.Browser.WebKit &&
|
|
|
|
(e.include("-of-type") || e.include(":empty")))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// XPath can't do namespaced attributes, nor can it read
|
|
|
|
// the "checked" property from DOM nodes
|
2008-03-27 06:18:15 +00:00
|
|
|
if ((/(\[[\w-]*?:|:checked)/).test(e))
|
2007-11-27 18:21:50 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2008-03-27 06:18:15 +00:00
|
|
|
shouldUseSelectorsAPI: function() {
|
|
|
|
if (!Prototype.BrowserFeatures.SelectorsAPI) return false;
|
2007-03-09 04:12:13 +00:00
|
|
|
|
2008-03-27 06:18:15 +00:00
|
|
|
if (!Selector._div) Selector._div = new Element('div');
|
|
|
|
|
|
|
|
// Make sure the browser treats the selector as valid. Test on an
|
|
|
|
// isolated element to minimize cost of this check.
|
|
|
|
|
|
|
|
try {
|
|
|
|
Selector._div.querySelector(this.expression);
|
|
|
|
} catch(e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
compileMatcher: function() {
|
2007-03-09 04:12:13 +00:00
|
|
|
var e = this.expression, ps = Selector.patterns, h = Selector.handlers,
|
|
|
|
c = Selector.criteria, le, p, m;
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
if (Selector._cache[e]) {
|
2007-08-08 23:19:45 +00:00
|
|
|
this.matcher = Selector._cache[e];
|
|
|
|
return;
|
2007-03-09 04:12:13 +00:00
|
|
|
}
|
2007-08-08 23:19:45 +00:00
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
this.matcher = ["this.matcher = function(root) {",
|
|
|
|
"var r = root, h = Selector.handlers, c = false, n;"];
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
while (e && le != e && (/\S/).test(e)) {
|
|
|
|
le = e;
|
|
|
|
for (var i in ps) {
|
|
|
|
p = ps[i];
|
|
|
|
if (m = e.match(p)) {
|
2007-08-08 23:19:45 +00:00
|
|
|
this.matcher.push(Object.isFunction(c[i]) ? c[i](m) :
|
2008-03-09 07:27:02 +00:00
|
|
|
new Template(c[i]).evaluate(m));
|
2007-03-09 04:12:13 +00:00
|
|
|
e = e.replace(m[0], '');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.matcher.push("return h.unique(n);\n}");
|
|
|
|
eval(this.matcher.join('\n'));
|
|
|
|
Selector._cache[this.expression] = this.matcher;
|
|
|
|
},
|
|
|
|
|
|
|
|
compileXPathMatcher: function() {
|
|
|
|
var e = this.expression, ps = Selector.patterns,
|
2007-08-07 19:41:13 +00:00
|
|
|
x = Selector.xpath, le, m;
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
if (Selector._cache[e]) {
|
|
|
|
this.xpath = Selector._cache[e]; return;
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
this.matcher = ['.//*'];
|
|
|
|
while (e && le != e && (/\S/).test(e)) {
|
|
|
|
le = e;
|
|
|
|
for (var i in ps) {
|
|
|
|
if (m = e.match(ps[i])) {
|
2007-08-08 23:19:45 +00:00
|
|
|
this.matcher.push(Object.isFunction(x[i]) ? x[i](m) :
|
2007-03-09 04:12:13 +00:00
|
|
|
new Template(x[i]).evaluate(m));
|
|
|
|
e = e.replace(m[0], '');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
this.xpath = this.matcher.join('');
|
|
|
|
Selector._cache[this.expression] = this.xpath;
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2007-03-09 04:12:13 +00:00
|
|
|
|
|
|
|
findElements: function(root) {
|
|
|
|
root = root || document;
|
2008-03-27 06:18:15 +00:00
|
|
|
var results;
|
|
|
|
|
|
|
|
switch (this.mode) {
|
|
|
|
case 'selectorsAPI':
|
|
|
|
return $A(root.querySelectorAll(this.expression));
|
|
|
|
case 'xpath':
|
|
|
|
return document._getElementsByXPath(this.xpath, root);
|
|
|
|
default:
|
|
|
|
return this.matcher(root);
|
|
|
|
}
|
2007-03-09 04:12:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
match: function(element) {
|
2007-07-24 20:49:55 +00:00
|
|
|
this.tokens = [];
|
|
|
|
|
|
|
|
var e = this.expression, ps = Selector.patterns, as = Selector.assertions;
|
|
|
|
var le, p, m;
|
|
|
|
|
|
|
|
while (e && le !== e && (/\S/).test(e)) {
|
|
|
|
le = e;
|
|
|
|
for (var i in ps) {
|
|
|
|
p = ps[i];
|
|
|
|
if (m = e.match(p)) {
|
|
|
|
// use the Selector.assertions methods unless the selector
|
|
|
|
// is too complex.
|
|
|
|
if (as[i]) {
|
|
|
|
this.tokens.push([i, Object.clone(m)]);
|
|
|
|
e = e.replace(m[0], '');
|
|
|
|
} else {
|
|
|
|
// reluctantly do a document-wide search
|
|
|
|
// and look for a match in the array
|
|
|
|
return this.findElements(document).include(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var match = true, name, matches;
|
|
|
|
for (var i = 0, token; token = this.tokens[i]; i++) {
|
|
|
|
name = token[0], matches = token[1];
|
|
|
|
if (!Selector.assertions[name](element, matches)) {
|
|
|
|
match = false; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return match;
|
2007-03-09 04:12:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
toString: function() {
|
|
|
|
return this.expression;
|
|
|
|
},
|
|
|
|
|
|
|
|
inspect: function() {
|
|
|
|
return "#<Selector:" + this.expression.inspect() + ">";
|
|
|
|
}
|
2007-08-18 23:21:29 +00:00
|
|
|
});
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
Object.extend(Selector, {
|
2007-08-08 23:19:45 +00:00
|
|
|
_cache: { },
|
2007-03-09 04:12:13 +00:00
|
|
|
|
|
|
|
xpath: {
|
|
|
|
descendant: "//*",
|
|
|
|
child: "/*",
|
|
|
|
adjacent: "/following-sibling::*[1]",
|
|
|
|
laterSibling: '/following-sibling::*',
|
|
|
|
tagName: function(m) {
|
|
|
|
if (m[1] == '*') return '';
|
|
|
|
return "[local-name()='" + m[1].toLowerCase() +
|
|
|
|
"' or local-name()='" + m[1].toUpperCase() + "']";
|
|
|
|
},
|
|
|
|
className: "[contains(concat(' ', @class, ' '), ' #{1} ')]",
|
|
|
|
id: "[@id='#{1}']",
|
2007-11-27 18:21:50 +00:00
|
|
|
attrPresence: function(m) {
|
|
|
|
m[1] = m[1].toLowerCase();
|
|
|
|
return new Template("[@#{1}]").evaluate(m);
|
|
|
|
},
|
2007-03-09 04:12:13 +00:00
|
|
|
attr: function(m) {
|
2007-11-27 18:21:50 +00:00
|
|
|
m[1] = m[1].toLowerCase();
|
2007-03-09 04:12:13 +00:00
|
|
|
m[3] = m[5] || m[6];
|
|
|
|
return new Template(Selector.xpath.operators[m[2]]).evaluate(m);
|
|
|
|
},
|
|
|
|
pseudo: function(m) {
|
|
|
|
var h = Selector.xpath.pseudos[m[1]];
|
|
|
|
if (!h) return '';
|
2007-08-08 23:19:45 +00:00
|
|
|
if (Object.isFunction(h)) return h(m);
|
2007-03-09 04:12:13 +00:00
|
|
|
return new Template(Selector.xpath.pseudos[m[1]]).evaluate(m);
|
|
|
|
},
|
|
|
|
operators: {
|
|
|
|
'=': "[@#{1}='#{3}']",
|
|
|
|
'!=': "[@#{1}!='#{3}']",
|
|
|
|
'^=': "[starts-with(@#{1}, '#{3}')]",
|
|
|
|
'$=': "[substring(@#{1}, (string-length(@#{1}) - string-length('#{3}') + 1))='#{3}']",
|
|
|
|
'*=': "[contains(@#{1}, '#{3}')]",
|
|
|
|
'~=': "[contains(concat(' ', @#{1}, ' '), ' #{3} ')]",
|
|
|
|
'|=': "[contains(concat('-', @#{1}, '-'), '-#{3}-')]"
|
|
|
|
},
|
|
|
|
pseudos: {
|
|
|
|
'first-child': '[not(preceding-sibling::*)]',
|
|
|
|
'last-child': '[not(following-sibling::*)]',
|
|
|
|
'only-child': '[not(preceding-sibling::* or following-sibling::*)]',
|
2008-03-27 06:18:15 +00:00
|
|
|
'empty': "[count(*) = 0 and (count(text()) = 0)]",
|
2007-03-09 04:12:13 +00:00
|
|
|
'checked': "[@checked]",
|
2008-03-27 06:18:15 +00:00
|
|
|
'disabled': "[(@disabled) and (@type!='hidden')]",
|
|
|
|
'enabled': "[not(@disabled) and (@type!='hidden')]",
|
2007-03-09 04:12:13 +00:00
|
|
|
'not': function(m) {
|
2007-03-27 18:20:35 +00:00
|
|
|
var e = m[6], p = Selector.patterns,
|
2007-11-15 22:15:24 +00:00
|
|
|
x = Selector.xpath, le, v;
|
2007-03-27 18:20:35 +00:00
|
|
|
|
|
|
|
var exclusion = [];
|
|
|
|
while (e && le != e && (/\S/).test(e)) {
|
|
|
|
le = e;
|
|
|
|
for (var i in p) {
|
|
|
|
if (m = e.match(p[i])) {
|
2007-08-08 23:19:45 +00:00
|
|
|
v = Object.isFunction(x[i]) ? x[i](m) : new Template(x[i]).evaluate(m);
|
2007-03-27 18:20:35 +00:00
|
|
|
exclusion.push("(" + v.substring(1, v.length - 1) + ")");
|
|
|
|
e = e.replace(m[0], '');
|
|
|
|
break;
|
|
|
|
}
|
2007-03-09 04:12:13 +00:00
|
|
|
}
|
2007-03-27 18:20:35 +00:00
|
|
|
}
|
|
|
|
return "[not(" + exclusion.join(" and ") + ")]";
|
2007-03-09 04:12:13 +00:00
|
|
|
},
|
|
|
|
'nth-child': function(m) {
|
|
|
|
return Selector.xpath.pseudos.nth("(count(./preceding-sibling::*) + 1) ", m);
|
|
|
|
},
|
|
|
|
'nth-last-child': function(m) {
|
|
|
|
return Selector.xpath.pseudos.nth("(count(./following-sibling::*) + 1) ", m);
|
|
|
|
},
|
|
|
|
'nth-of-type': function(m) {
|
|
|
|
return Selector.xpath.pseudos.nth("position() ", m);
|
|
|
|
},
|
|
|
|
'nth-last-of-type': function(m) {
|
|
|
|
return Selector.xpath.pseudos.nth("(last() + 1 - position()) ", m);
|
|
|
|
},
|
|
|
|
'first-of-type': function(m) {
|
|
|
|
m[6] = "1"; return Selector.xpath.pseudos['nth-of-type'](m);
|
|
|
|
},
|
|
|
|
'last-of-type': function(m) {
|
|
|
|
m[6] = "1"; return Selector.xpath.pseudos['nth-last-of-type'](m);
|
|
|
|
},
|
|
|
|
'only-of-type': function(m) {
|
|
|
|
var p = Selector.xpath.pseudos; return p['first-of-type'](m) + p['last-of-type'](m);
|
|
|
|
},
|
2007-03-27 18:14:53 +00:00
|
|
|
nth: function(fragment, m) {
|
|
|
|
var mm, formula = m[6], predicate;
|
2007-03-09 04:12:13 +00:00
|
|
|
if (formula == 'even') formula = '2n+0';
|
|
|
|
if (formula == 'odd') formula = '2n+1';
|
|
|
|
if (mm = formula.match(/^(\d+)$/)) // digit only
|
2007-03-27 18:14:53 +00:00
|
|
|
return '[' + fragment + "= " + mm[1] + ']';
|
|
|
|
if (mm = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b
|
|
|
|
if (mm[1] == "-") mm[1] = -1;
|
2007-03-09 04:12:13 +00:00
|
|
|
var a = mm[1] ? Number(mm[1]) : 1;
|
2007-03-27 18:14:53 +00:00
|
|
|
var b = mm[2] ? Number(mm[2]) : 0;
|
|
|
|
predicate = "[((#{fragment} - #{b}) mod #{a} = 0) and " +
|
|
|
|
"((#{fragment} - #{b}) div #{a} >= 0)]";
|
|
|
|
return new Template(predicate).evaluate({
|
|
|
|
fragment: fragment, a: a, b: b });
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
2007-03-09 04:12:13 +00:00
|
|
|
}
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
},
|
2007-03-09 04:12:13 +00:00
|
|
|
|
|
|
|
criteria: {
|
2008-01-18 04:52:38 +00:00
|
|
|
tagName: 'n = h.tagName(n, r, "#{1}", c); c = false;',
|
|
|
|
className: 'n = h.className(n, r, "#{1}", c); c = false;',
|
|
|
|
id: 'n = h.id(n, r, "#{1}", c); c = false;',
|
|
|
|
attrPresence: 'n = h.attrPresence(n, r, "#{1}", c); c = false;',
|
2007-03-09 04:12:13 +00:00
|
|
|
attr: function(m) {
|
2007-03-27 18:20:35 +00:00
|
|
|
m[3] = (m[5] || m[6]);
|
2008-01-18 04:52:38 +00:00
|
|
|
return new Template('n = h.attr(n, r, "#{1}", "#{3}", "#{2}", c); c = false;').evaluate(m);
|
2007-03-09 04:12:13 +00:00
|
|
|
},
|
2007-08-07 19:41:13 +00:00
|
|
|
pseudo: function(m) {
|
2007-03-27 18:20:35 +00:00
|
|
|
if (m[6]) m[6] = m[6].replace(/"/g, '\\"');
|
|
|
|
return new Template('n = h.pseudo(n, "#{1}", "#{6}", r, c); c = false;').evaluate(m);
|
|
|
|
},
|
2007-03-09 04:12:13 +00:00
|
|
|
descendant: 'c = "descendant";',
|
|
|
|
child: 'c = "child";',
|
|
|
|
adjacent: 'c = "adjacent";',
|
|
|
|
laterSibling: 'c = "laterSibling";'
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
patterns: {
|
|
|
|
// combinators must be listed first
|
|
|
|
// (and descendant needs to be last combinator)
|
|
|
|
laterSibling: /^\s*~\s*/,
|
|
|
|
child: /^\s*>\s*/,
|
|
|
|
adjacent: /^\s*\+\s*/,
|
|
|
|
descendant: /^\s/,
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
// selectors follow
|
|
|
|
tagName: /^\s*(\*|[\w\-]+)(\b|$)?/,
|
|
|
|
id: /^#([\w\-\*]+)(\b|$)/,
|
|
|
|
className: /^\.([\w\-\*]+)(\b|$)/,
|
2007-12-20 00:17:24 +00:00
|
|
|
pseudo:
|
|
|
|
/^:((first|last|nth|nth-last|only)(-child|-of-type)|empty|checked|(en|dis)abled|not)(\((.*?)\))?(\b|$|(?=\s|[:+~>]))/,
|
2008-02-03 19:45:32 +00:00
|
|
|
attrPresence: /^\[((?:[\w]+:)?[\w]+)\]/,
|
2007-08-07 20:33:53 +00:00
|
|
|
attr: /\[((?:[\w-]*:)?[\w-]+)\s*(?:([!^$*~|]?=)\s*((['"])([^\4]*?)\4|([^'"][^\]]*?)))?\]/
|
2007-03-09 04:12:13 +00:00
|
|
|
},
|
|
|
|
|
2007-07-24 20:49:55 +00:00
|
|
|
// for Selector.match and Element#match
|
|
|
|
assertions: {
|
|
|
|
tagName: function(element, matches) {
|
|
|
|
return matches[1].toUpperCase() == element.tagName.toUpperCase();
|
|
|
|
},
|
|
|
|
|
|
|
|
className: function(element, matches) {
|
|
|
|
return Element.hasClassName(element, matches[1]);
|
|
|
|
},
|
|
|
|
|
|
|
|
id: function(element, matches) {
|
|
|
|
return element.id === matches[1];
|
|
|
|
},
|
|
|
|
|
|
|
|
attrPresence: function(element, matches) {
|
|
|
|
return Element.hasAttribute(element, matches[1]);
|
|
|
|
},
|
|
|
|
|
|
|
|
attr: function(element, matches) {
|
|
|
|
var nodeValue = Element.readAttribute(element, matches[1]);
|
2008-01-23 20:05:17 +00:00
|
|
|
return nodeValue && Selector.operators[matches[2]](nodeValue, matches[5] || matches[6]);
|
2007-07-24 20:49:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
handlers: {
|
|
|
|
// UTILITY FUNCTIONS
|
|
|
|
// joins two collections
|
|
|
|
concat: function(a, b) {
|
|
|
|
for (var i = 0, node; node = b[i]; i++)
|
|
|
|
a.push(node);
|
|
|
|
return a;
|
|
|
|
},
|
|
|
|
|
|
|
|
// marks an array of nodes for counting
|
|
|
|
mark: function(nodes) {
|
2008-01-25 18:49:40 +00:00
|
|
|
var _true = Prototype.emptyFunction;
|
2007-03-09 04:12:13 +00:00
|
|
|
for (var i = 0, node; node = nodes[i]; i++)
|
2008-01-25 18:49:40 +00:00
|
|
|
node._countedByPrototype = _true;
|
2007-03-09 04:12:13 +00:00
|
|
|
return nodes;
|
|
|
|
},
|
|
|
|
|
|
|
|
unmark: function(nodes) {
|
|
|
|
for (var i = 0, node; node = nodes[i]; i++)
|
2008-01-25 18:49:40 +00:00
|
|
|
node._countedByPrototype = undefined;
|
2007-03-09 04:12:13 +00:00
|
|
|
return nodes;
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
// mark each child node with its position (for nth calls)
|
|
|
|
// "ofType" flag indicates whether we're indexing for nth-of-type
|
|
|
|
// rather than nth-child
|
|
|
|
index: function(parentNode, reverse, ofType) {
|
2008-01-25 18:49:40 +00:00
|
|
|
parentNode._countedByPrototype = Prototype.emptyFunction;
|
2007-03-09 04:12:13 +00:00
|
|
|
if (reverse) {
|
|
|
|
for (var nodes = parentNode.childNodes, i = nodes.length - 1, j = 1; i >= 0; i--) {
|
2007-10-08 21:15:46 +00:00
|
|
|
var node = nodes[i];
|
2008-01-25 18:49:40 +00:00
|
|
|
if (node.nodeType == 1 && (!ofType || node._countedByPrototype)) node.nodeIndex = j++;
|
2007-03-09 04:12:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (var i = 0, j = 1, nodes = parentNode.childNodes; node = nodes[i]; i++)
|
2008-01-25 18:49:40 +00:00
|
|
|
if (node.nodeType == 1 && (!ofType || node._countedByPrototype)) node.nodeIndex = j++;
|
2007-03-09 04:12:13 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// filters out duplicates and extends all nodes
|
|
|
|
unique: function(nodes) {
|
|
|
|
if (nodes.length == 0) return nodes;
|
2007-03-11 16:19:02 +00:00
|
|
|
var results = [], n;
|
|
|
|
for (var i = 0, l = nodes.length; i < l; i++)
|
2008-01-25 18:49:40 +00:00
|
|
|
if (!(n = nodes[i])._countedByPrototype) {
|
|
|
|
n._countedByPrototype = Prototype.emptyFunction;
|
2007-03-09 04:12:13 +00:00
|
|
|
results.push(Element.extend(n));
|
|
|
|
}
|
|
|
|
return Selector.handlers.unmark(results);
|
|
|
|
},
|
|
|
|
|
|
|
|
// COMBINATOR FUNCTIONS
|
|
|
|
descendant: function(nodes) {
|
|
|
|
var h = Selector.handlers;
|
|
|
|
for (var i = 0, results = [], node; node = nodes[i]; i++)
|
2007-03-27 18:14:53 +00:00
|
|
|
h.concat(results, node.getElementsByTagName('*'));
|
2007-03-09 04:12:13 +00:00
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
|
|
|
child: function(nodes) {
|
|
|
|
var h = Selector.handlers;
|
2007-03-27 18:14:53 +00:00
|
|
|
for (var i = 0, results = [], node; node = nodes[i]; i++) {
|
2007-11-15 22:21:08 +00:00
|
|
|
for (var j = 0, child; child = node.childNodes[j]; j++)
|
2007-03-27 18:14:53 +00:00
|
|
|
if (child.nodeType == 1 && child.tagName != '!') results.push(child);
|
|
|
|
}
|
2007-03-09 04:12:13 +00:00
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
|
|
|
adjacent: function(nodes) {
|
|
|
|
for (var i = 0, results = [], node; node = nodes[i]; i++) {
|
|
|
|
var next = this.nextElementSibling(node);
|
|
|
|
if (next) results.push(next);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
|
|
|
laterSibling: function(nodes) {
|
|
|
|
var h = Selector.handlers;
|
|
|
|
for (var i = 0, results = [], node; node = nodes[i]; i++)
|
|
|
|
h.concat(results, Element.nextSiblings(node));
|
|
|
|
return results;
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
nextElementSibling: function(node) {
|
|
|
|
while (node = node.nextSibling)
|
2008-03-09 07:27:02 +00:00
|
|
|
if (node.nodeType == 1) return node;
|
2007-03-09 04:12:13 +00:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
previousElementSibling: function(node) {
|
|
|
|
while (node = node.previousSibling)
|
|
|
|
if (node.nodeType == 1) return node;
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// TOKEN FUNCTIONS
|
|
|
|
tagName: function(nodes, root, tagName, combinator) {
|
2007-12-19 23:32:21 +00:00
|
|
|
var uTagName = tagName.toUpperCase();
|
2007-03-09 04:12:13 +00:00
|
|
|
var results = [], h = Selector.handlers;
|
|
|
|
if (nodes) {
|
|
|
|
if (combinator) {
|
|
|
|
// fastlane for ordinary descendant combinators
|
|
|
|
if (combinator == "descendant") {
|
|
|
|
for (var i = 0, node; node = nodes[i]; i++)
|
|
|
|
h.concat(results, node.getElementsByTagName(tagName));
|
|
|
|
return results;
|
|
|
|
} else nodes = this[combinator](nodes);
|
|
|
|
if (tagName == "*") return nodes;
|
|
|
|
}
|
|
|
|
for (var i = 0, node; node = nodes[i]; i++)
|
2007-12-19 23:32:21 +00:00
|
|
|
if (node.tagName.toUpperCase() === uTagName) results.push(node);
|
2007-03-09 04:12:13 +00:00
|
|
|
return results;
|
|
|
|
} else return root.getElementsByTagName(tagName);
|
|
|
|
},
|
|
|
|
|
|
|
|
id: function(nodes, root, id, combinator) {
|
|
|
|
var targetNode = $(id), h = Selector.handlers;
|
2007-05-18 00:51:34 +00:00
|
|
|
if (!targetNode) return [];
|
|
|
|
if (!nodes && root == document) return [targetNode];
|
2007-03-09 04:12:13 +00:00
|
|
|
if (nodes) {
|
|
|
|
if (combinator) {
|
|
|
|
if (combinator == 'child') {
|
|
|
|
for (var i = 0, node; node = nodes[i]; i++)
|
|
|
|
if (targetNode.parentNode == node) return [targetNode];
|
|
|
|
} else if (combinator == 'descendant') {
|
|
|
|
for (var i = 0, node; node = nodes[i]; i++)
|
|
|
|
if (Element.descendantOf(targetNode, node)) return [targetNode];
|
|
|
|
} else if (combinator == 'adjacent') {
|
|
|
|
for (var i = 0, node; node = nodes[i]; i++)
|
|
|
|
if (Selector.handlers.previousElementSibling(targetNode) == node)
|
|
|
|
return [targetNode];
|
|
|
|
} else nodes = h[combinator](nodes);
|
|
|
|
}
|
|
|
|
for (var i = 0, node; node = nodes[i]; i++)
|
|
|
|
if (node == targetNode) return [targetNode];
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return (targetNode && Element.descendantOf(targetNode, root)) ? [targetNode] : [];
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
className: function(nodes, root, className, combinator) {
|
|
|
|
if (nodes && combinator) nodes = this[combinator](nodes);
|
|
|
|
return Selector.handlers.byClassName(nodes, root, className);
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
byClassName: function(nodes, root, className) {
|
|
|
|
if (!nodes) nodes = Selector.handlers.descendant([root]);
|
|
|
|
var needle = ' ' + className + ' ';
|
|
|
|
for (var i = 0, results = [], node, nodeClassName; node = nodes[i]; i++) {
|
|
|
|
nodeClassName = node.className;
|
|
|
|
if (nodeClassName.length == 0) continue;
|
|
|
|
if (nodeClassName == className || (' ' + nodeClassName + ' ').include(needle))
|
|
|
|
results.push(node);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
2008-01-18 04:52:38 +00:00
|
|
|
attrPresence: function(nodes, root, attr, combinator) {
|
2007-11-05 17:11:41 +00:00
|
|
|
if (!nodes) nodes = root.getElementsByTagName("*");
|
2008-01-18 04:52:38 +00:00
|
|
|
if (nodes && combinator) nodes = this[combinator](nodes);
|
2007-03-09 04:12:13 +00:00
|
|
|
var results = [];
|
|
|
|
for (var i = 0, node; node = nodes[i]; i++)
|
|
|
|
if (Element.hasAttribute(node, attr)) results.push(node);
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
2008-01-18 04:52:38 +00:00
|
|
|
attr: function(nodes, root, attr, value, operator, combinator) {
|
2007-03-27 18:20:35 +00:00
|
|
|
if (!nodes) nodes = root.getElementsByTagName("*");
|
2008-01-18 04:52:38 +00:00
|
|
|
if (nodes && combinator) nodes = this[combinator](nodes);
|
2007-03-09 04:12:13 +00:00
|
|
|
var handler = Selector.operators[operator], results = [];
|
|
|
|
for (var i = 0, node; node = nodes[i]; i++) {
|
|
|
|
var nodeValue = Element.readAttribute(node, attr);
|
|
|
|
if (nodeValue === null) continue;
|
|
|
|
if (handler(nodeValue, value)) results.push(node);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
|
|
|
pseudo: function(nodes, name, value, root, combinator) {
|
2007-03-27 18:20:35 +00:00
|
|
|
if (nodes && combinator) nodes = this[combinator](nodes);
|
|
|
|
if (!nodes) nodes = root.getElementsByTagName("*");
|
2007-03-09 04:12:13 +00:00
|
|
|
return Selector.pseudos[name](nodes, value, root);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
pseudos: {
|
|
|
|
'first-child': function(nodes, value, root) {
|
|
|
|
for (var i = 0, results = [], node; node = nodes[i]; i++) {
|
|
|
|
if (Selector.handlers.previousElementSibling(node)) continue;
|
|
|
|
results.push(node);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
'last-child': function(nodes, value, root) {
|
|
|
|
for (var i = 0, results = [], node; node = nodes[i]; i++) {
|
|
|
|
if (Selector.handlers.nextElementSibling(node)) continue;
|
|
|
|
results.push(node);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
'only-child': function(nodes, value, root) {
|
|
|
|
var h = Selector.handlers;
|
|
|
|
for (var i = 0, results = [], node; node = nodes[i]; i++)
|
|
|
|
if (!h.previousElementSibling(node) && !h.nextElementSibling(node))
|
|
|
|
results.push(node);
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
'nth-child': function(nodes, formula, root) {
|
|
|
|
return Selector.pseudos.nth(nodes, formula, root);
|
|
|
|
},
|
|
|
|
'nth-last-child': function(nodes, formula, root) {
|
|
|
|
return Selector.pseudos.nth(nodes, formula, root, true);
|
|
|
|
},
|
|
|
|
'nth-of-type': function(nodes, formula, root) {
|
|
|
|
return Selector.pseudos.nth(nodes, formula, root, false, true);
|
|
|
|
},
|
|
|
|
'nth-last-of-type': function(nodes, formula, root) {
|
|
|
|
return Selector.pseudos.nth(nodes, formula, root, true, true);
|
|
|
|
},
|
|
|
|
'first-of-type': function(nodes, formula, root) {
|
|
|
|
return Selector.pseudos.nth(nodes, "1", root, false, true);
|
|
|
|
},
|
|
|
|
'last-of-type': function(nodes, formula, root) {
|
|
|
|
return Selector.pseudos.nth(nodes, "1", root, true, true);
|
|
|
|
},
|
|
|
|
'only-of-type': function(nodes, formula, root) {
|
|
|
|
var p = Selector.pseudos;
|
|
|
|
return p['last-of-type'](p['first-of-type'](nodes, formula, root), formula, root);
|
|
|
|
},
|
|
|
|
|
2007-03-27 18:14:53 +00:00
|
|
|
// handles the an+b logic
|
|
|
|
getIndices: function(a, b, total) {
|
|
|
|
if (a == 0) return b > 0 ? [b] : [];
|
|
|
|
return $R(1, total).inject([], function(memo, i) {
|
|
|
|
if (0 == (i - b) % a && (i - b) / a >= 0) memo.push(i);
|
|
|
|
return memo;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2007-03-09 04:12:13 +00:00
|
|
|
// handles nth(-last)-child, nth(-last)-of-type, and (first|last)-of-type
|
|
|
|
nth: function(nodes, formula, root, reverse, ofType) {
|
2007-03-27 18:14:53 +00:00
|
|
|
if (nodes.length == 0) return [];
|
2007-03-09 04:12:13 +00:00
|
|
|
if (formula == 'even') formula = '2n+0';
|
|
|
|
if (formula == 'odd') formula = '2n+1';
|
|
|
|
var h = Selector.handlers, results = [], indexed = [], m;
|
|
|
|
h.mark(nodes);
|
|
|
|
for (var i = 0, node; node = nodes[i]; i++) {
|
2008-01-25 18:49:40 +00:00
|
|
|
if (!node.parentNode._countedByPrototype) {
|
2007-03-09 04:12:13 +00:00
|
|
|
h.index(node.parentNode, reverse, ofType);
|
|
|
|
indexed.push(node.parentNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (formula.match(/^\d+$/)) { // just a number
|
|
|
|
formula = Number(formula);
|
|
|
|
for (var i = 0, node; node = nodes[i]; i++)
|
|
|
|
if (node.nodeIndex == formula) results.push(node);
|
2007-03-27 18:14:53 +00:00
|
|
|
} else if (m = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b
|
|
|
|
if (m[1] == "-") m[1] = -1;
|
2007-03-09 04:12:13 +00:00
|
|
|
var a = m[1] ? Number(m[1]) : 1;
|
2007-03-27 18:14:53 +00:00
|
|
|
var b = m[2] ? Number(m[2]) : 0;
|
|
|
|
var indices = Selector.pseudos.getIndices(a, b, nodes.length);
|
|
|
|
for (var i = 0, node, l = indices.length; node = nodes[i]; i++) {
|
|
|
|
for (var j = 0; j < l; j++)
|
|
|
|
if (node.nodeIndex == indices[j]) results.push(node);
|
|
|
|
}
|
2007-03-09 04:12:13 +00:00
|
|
|
}
|
|
|
|
h.unmark(nodes);
|
|
|
|
h.unmark(indexed);
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
|
|
|
'empty': function(nodes, value, root) {
|
|
|
|
for (var i = 0, results = [], node; node = nodes[i]; i++) {
|
|
|
|
// IE treats comments as element nodes
|
2008-03-27 06:18:15 +00:00
|
|
|
if (node.tagName == '!' || node.firstChild) continue;
|
2007-03-09 04:12:13 +00:00
|
|
|
results.push(node);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
|
|
|
'not': function(nodes, selector, root) {
|
2007-03-27 18:20:35 +00:00
|
|
|
var h = Selector.handlers, selectorType, m;
|
|
|
|
var exclusions = new Selector(selector).findElements(root);
|
2007-03-09 04:12:13 +00:00
|
|
|
h.mark(exclusions);
|
|
|
|
for (var i = 0, results = [], node; node = nodes[i]; i++)
|
2008-01-25 18:49:40 +00:00
|
|
|
if (!node._countedByPrototype) results.push(node);
|
2007-03-09 04:12:13 +00:00
|
|
|
h.unmark(exclusions);
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
|
|
|
'enabled': function(nodes, value, root) {
|
|
|
|
for (var i = 0, results = [], node; node = nodes[i]; i++)
|
2008-03-27 06:18:15 +00:00
|
|
|
if (!node.disabled && (!node.type || node.type !== 'hidden'))
|
|
|
|
results.push(node);
|
2007-03-09 04:12:13 +00:00
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
|
|
|
'disabled': function(nodes, value, root) {
|
|
|
|
for (var i = 0, results = [], node; node = nodes[i]; i++)
|
|
|
|
if (node.disabled) results.push(node);
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
|
|
|
'checked': function(nodes, value, root) {
|
|
|
|
for (var i = 0, results = [], node; node = nodes[i]; i++)
|
|
|
|
if (node.checked) results.push(node);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
operators: {
|
|
|
|
'=': function(nv, v) { return nv == v; },
|
|
|
|
'!=': function(nv, v) { return nv != v; },
|
|
|
|
'^=': function(nv, v) { return nv.startsWith(v); },
|
|
|
|
'$=': function(nv, v) { return nv.endsWith(v); },
|
|
|
|
'*=': function(nv, v) { return nv.include(v); },
|
|
|
|
'~=': function(nv, v) { return (' ' + nv + ' ').include(' ' + v + ' '); },
|
|
|
|
'|=': function(nv, v) { return ('-' + nv.toUpperCase() + '-').include('-' + v.toUpperCase() + '-'); }
|
|
|
|
},
|
2008-01-25 18:49:40 +00:00
|
|
|
|
|
|
|
split: function(expression) {
|
|
|
|
var expressions = [];
|
|
|
|
expression.scan(/(([\w#:.~>+()\s-]+|\*|\[.*?\])+)\s*(,|$)/, function(m) {
|
|
|
|
expressions.push(m[1].strip());
|
|
|
|
});
|
|
|
|
return expressions;
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
matchElements: function(elements, expression) {
|
2008-01-06 21:49:16 +00:00
|
|
|
var matches = $$(expression), h = Selector.handlers;
|
2007-03-09 04:12:13 +00:00
|
|
|
h.mark(matches);
|
|
|
|
for (var i = 0, results = [], element; element = elements[i]; i++)
|
2008-01-25 18:49:40 +00:00
|
|
|
if (element._countedByPrototype) results.push(element);
|
2007-03-09 04:12:13 +00:00
|
|
|
h.unmark(matches);
|
|
|
|
return results;
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
findElement: function(elements, expression, index) {
|
2007-08-08 23:19:45 +00:00
|
|
|
if (Object.isNumber(expression)) {
|
2007-03-09 04:12:13 +00:00
|
|
|
index = expression; expression = false;
|
|
|
|
}
|
2007-01-18 22:24:27 +00:00
|
|
|
return Selector.matchElements(elements, expression || '*')[index || 0];
|
|
|
|
},
|
|
|
|
|
|
|
|
findChildElements: function(element, expressions) {
|
2008-01-25 18:49:40 +00:00
|
|
|
expressions = Selector.split(expressions.join(','));
|
2007-03-09 04:12:13 +00:00
|
|
|
var results = [], h = Selector.handlers;
|
|
|
|
for (var i = 0, l = expressions.length, selector; i < l; i++) {
|
|
|
|
selector = new Selector(expressions[i].strip());
|
|
|
|
h.concat(results, selector.findElements(element));
|
|
|
|
}
|
|
|
|
return (l > 1) ? h.unique(results) : results;
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2007-11-27 18:43:56 +00:00
|
|
|
if (Prototype.Browser.IE) {
|
2008-01-25 18:49:40 +00:00
|
|
|
Object.extend(Selector.handlers, {
|
|
|
|
// IE returns comment nodes on getElementsByTagName("*").
|
|
|
|
// Filter them out.
|
|
|
|
concat: function(a, b) {
|
|
|
|
for (var i = 0, node; node = b[i]; i++)
|
|
|
|
if (node.tagName !== "!") a.push(node);
|
|
|
|
return a;
|
|
|
|
},
|
|
|
|
|
|
|
|
// IE improperly serializes _countedByPrototype in (inner|outer)HTML.
|
|
|
|
unmark: function(nodes) {
|
|
|
|
for (var i = 0, node; node = nodes[i]; i++)
|
|
|
|
node.removeAttribute('_countedByPrototype');
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
});
|
2007-11-27 18:43:56 +00:00
|
|
|
}
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
function $$() {
|
|
|
|
return Selector.findChildElements(document, $A(arguments));
|
|
|
|
}
|