Commit c85243df authored by John Resig's avatar John Resig

Landing the new Sizzle selector engine. There'll need to be some later tweaks...

Landing the new Sizzle selector engine. There'll need to be some later tweaks (to make the tests a little more pragmatic - especially for document order elements). But it appears to be passing well and that's enough. Closes #3563.
parent 5c1725d6
...@@ -333,7 +333,9 @@ jQuery.fn = jQuery.prototype = { ...@@ -333,7 +333,9 @@ jQuery.fn = jQuery.prototype = {
return selector.call( elem, i ); return selector.call( elem, i );
}) || }) ||
jQuery.multiFilter( selector, this ), "filter", selector ); jQuery.multiFilter( selector, jQuery.grep(this, function(elem){
return elem.nodeType === 1;
}) ), "filter", selector );
}, },
not: function( selector ) { not: function( selector ) {
...@@ -1332,7 +1334,7 @@ jQuery.each({ ...@@ -1332,7 +1334,7 @@ jQuery.each({
}, },
remove: function( selector ) { remove: function( selector ) {
if ( !selector || jQuery.filter( selector, [ this ] ).r.length ) { if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
// Prevent memory leaks // Prevent memory leaks
jQuery( "*", this ).add([this]).each(function(){ jQuery( "*", this ).add([this]).each(function(){
jQuery.event.remove(this); jQuery.event.remove(this);
......
var chars = jQuery.browser.safari && parseInt(jQuery.browser.version) < 417 ? /*
"(?:[\\w*_-]|\\\\.)" : * Sizzle CSS Selector Engine - v0.9
"(?:[\\w\u0128-\uFFFF*_-]|\\\\.)", * Copyright 2009, John Resig (http://ejohn.org/)
quickChild = new RegExp("^>\\s*(" + chars + "+)"), * released under the MIT License
quickID = new RegExp("^(" + chars + "+)(#)(" + chars + "+)"), */
quickClass = new RegExp("^([#.]?)(" + chars + "*)"); (function(){
jQuery.extend({ var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]+\]|[^[\]]+)+\]|\\.|[^ >+~,(\[]+)+|[>+~])(\s*,\s*)?/g;
expr: {
"": function(a,i,m){return m[2]=="*"||jQuery.nodeName(a,m[2]);}, var cache = null;
"#": function(a,i,m){return a.getAttribute("id")==m[2];}, var done = 0;
":": {
// Position Checks if ( document.addEventListener && !document.querySelectorAll ) {
lt: function(a,i,m){return i<m[3]-0;}, cache = {};
gt: function(a,i,m){return i>m[3]-0;}, function invalidate(){ cache = {}; }
nth: function(a,i,m){return m[3]-0==i;}, document.addEventListener("DOMAttrModified", invalidate, false);
eq: function(a,i,m){return m[3]-0==i;}, document.addEventListener("DOMNodeInserted", invalidate, false);
first: function(a,i){return i==0;}, document.addEventListener("DOMNodeRemoved", invalidate, false);
last: function(a,i,m,r){return i==r.length-1;}, }
even: function(a,i){return i%2==0;},
odd: function(a,i){return i%2;}, var Sizzle = function(selector, context, results, seed) {
var doCache = !results;
// Child Checks results = results || [];
"first-child": function(a){return a.parentNode.getElementsByTagName("*")[0]==a;}, context = context || document;
"last-child": function(a){return jQuery.nth(a.parentNode.lastChild,1,"previousSibling")==a;},
"only-child": function(a){return !jQuery.nth(a.parentNode.lastChild,2,"previousSibling");},
// Parent Checks
parent: function(a){return a.firstChild;},
empty: function(a){return !a.firstChild;},
// Text Check
contains: function(a,i,m){return (a.textContent||a.innerText||jQuery(a).text()||"").indexOf(m[3])>=0;},
// Visibility
visible: function(a){return "hidden"!=a.type&&jQuery.css(a,"display")!="none"&&jQuery.css(a,"visibility")!="hidden";},
hidden: function(a){return "hidden"==a.type||jQuery.css(a,"display")=="none"||jQuery.css(a,"visibility")=="hidden";},
// Form attributes
enabled: function(a){return !a.disabled;},
disabled: function(a){return a.disabled;},
checked: function(a){return a.checked;},
selected: function(a){return a.selected||jQuery.attr(a,"selected");},
// Form elements
text: function(a){return "text"==a.type;},
radio: function(a){return "radio"==a.type;},
checkbox: function(a){return "checkbox"==a.type;},
file: function(a){return "file"==a.type;},
password: function(a){return "password"==a.type;},
submit: function(a){return "submit"==a.type;},
image: function(a){return "image"==a.type;},
reset: function(a){return "reset"==a.type;},
button: function(a){return "button"==a.type||jQuery.nodeName(a,"button");},
input: function(a){return /input|select|textarea|button/i.test(a.nodeName);},
// :has()
has: function(a,i,m){return jQuery.find(m[3],a).length;},
// :header
header: function(a){return /h\d/i.test(a.nodeName);},
// :animated
animated: function(a){return jQuery.grep(jQuery.timers,function(fn){return a==fn.elem;}).length;}
}
},
// The regular expressions that power the parsing engine
parse: [
// Match: [@value='test'], [@foo]
/^(\[) *@?([\w:-]+) *([!*$^~=]*) *('?"?)(.*?)\4 *\]/,
// Match: :contains('foo')
/^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/,
// Match: :even, :last-child, #id, .class
new RegExp("^([:.#]*)(" + chars + "+)")
],
multiFilter: function( expr, elems, not ) {
var old, cur = [];
while ( expr && expr != old ) {
old = expr;
var f = jQuery.filter( expr, elems, not );
expr = f.t.replace(/^\s*,\s*/, "" );
cur = not ? elems = f.r : jQuery.merge( cur, f.r );
}
return cur; if ( context.nodeType !== 1 && context.nodeType !== 9 )
}, return [];
if ( !selector || typeof selector !== "string" ) {
return results;
}
find: function( t, context ) { if ( cache && context === document && cache[ selector ] ) {
// Quickly handle non-string expressions results.push.apply( results, cache[ selector ] );
if ( typeof t !== "string" ) return results;
return [ t ]; }
// check to make sure context is a DOM element or a document var parts = [], m, set, checkSet, check, mode, extra;
if ( context && context.nodeType != 1 && context.nodeType != 9)
return [ ];
// Set the correct context (if none is provided) // Reset the position of the chunker regexp (start from head)
context = context || document; chunker.lastIndex = 0;
// Initialize the search while ( (m = chunker.exec(selector)) !== null ) {
var ret = [context], done = [], last, nodeName; parts.push( m[1] );
// Continue while a selector expression exists, and while if ( m[2] ) {
// we're no longer looping upon ourselves extra = RegExp.rightContext;
while ( t && last != t ) { break;
var r = []; }
last = t; }
t = jQuery.trim(t); if ( parts.length > 1 && Expr.match.POS.exec( selector ) ) {
if ( parts.length === 2 && Expr.relative[ parts[0] ] ) {
var later = "", match;
var foundToken = false, // Position selectors must be done after the filter
while ( (match = Expr.match.POS.exec( selector )) ) {
later += match[0];
selector = selector.replace( Expr.match.POS, "" );
}
// An attempt at speeding up child selectors that checkSet = Sizzle.filter( later, Sizzle( selector, context, results ) );
// point to a specific element tag } else {
re = quickChild, checkSet = Expr.relative[ parts[0] ] ?
[ context ] :
Sizzle( parts.shift(), context );
m = re.exec(t); while ( parts.length ) {
set = [];
if ( m ) { selector = parts.shift();
nodeName = m[1].toUpperCase(); if ( Expr.relative[ selector ] )
selector += parts.shift();
// Perform our own iteration and filter for ( var i = 0, l = checkSet.length; i < l; i++ ) {
for ( var i = 0; ret[i]; i++ ) Sizzle( selector, checkSet[i], set );
for ( var c = ret[i].firstChild; c; c = c.nextSibling ) }
if ( c.nodeType == 1 && (nodeName == "*" || c.nodeName.toUpperCase() == nodeName) )
r.push( c );
ret = r; checkSet = set;
t = t.replace( re, "" ); }
if ( t.indexOf(" ") == 0 ) continue; }
foundToken = true;
} else { } else {
re = /^([>+~])\s*(\w*)/i; var ret = seed ?
{ expr: parts.pop(), set: makeArray(seed) } :
Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context );
set = Sizzle.filter( ret.expr, ret.set );
if ( (m = re.exec(t)) != null ) { if ( parts.length > 0 ) {
r = []; checkSet = makeArray(set);
}
var merge = {}; while ( parts.length ) {
nodeName = m[2].toUpperCase(); var cur = parts.pop(), pop = cur;
m = m[1];
for ( var j = 0, rl = ret.length; j < rl; j++ ) { if ( !Expr.relative[ cur ] ) {
var n = m == "~" || m == "+" ? ret[j].nextSibling : ret[j].firstChild; cur = "";
for ( ; n; n = n.nextSibling ) } else {
if ( n.nodeType == 1 ) { pop = parts.pop();
var id = jQuery.data(n); }
if ( m == "~" && merge[id] ) break; if ( pop == null ) {
pop = context;
}
if (!nodeName || n.nodeName.toUpperCase() == nodeName ) { Expr.relative[ cur ]( checkSet, pop );
if ( m == "~" ) merge[id] = true;
r.push( n );
} }
if ( m == "+" ) break; if ( !checkSet ) {
checkSet = set;
} }
} }
ret = r; if ( !checkSet ) {
throw "Syntax error, unrecognized expression: " + (cur || selector);
}
// And remove the token if ( checkSet instanceof Array ) {
t = jQuery.trim( t.replace( re, "" ) ); if ( context.nodeType === 1 ) {
foundToken = true; for ( var i = 0; checkSet[i] != null; i++ ) {
if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) {
results.push( set[i] );
}
}
} else {
for ( var i = 0; checkSet[i] != null; i++ ) {
if ( checkSet[i] && checkSet[i].nodeType === 1 ) {
results.push( set[i] );
}
} }
} }
} else {
makeArray( checkSet, results );
}
// See if there's still an expression, and that we haven't already if ( extra ) {
// matched a token Sizzle( extra, context, results );
if ( t && !foundToken ) { }
// Handle multiple expressions
if ( !t.indexOf(",") ) {
// Clean the result set
if ( context == ret[0] ) ret.shift();
// Merge the result sets if ( cache && doCache ) {
done = jQuery.merge( done, ret ); cache[selector] = results.slice(0);
}
// Reset the context return results;
r = ret = [context]; };
// Touch up the selector string Sizzle.matches = function(expr, set){
t = " " + t.substr(1,t.length); return Sizzle(expr, null, null, set);
};
} else { Sizzle.find = function(expr, context){
// Optimize for the case nodeName#idName var set, match;
var re2 = quickID;
var m = re2.exec(t); if ( !expr ) {
return [];
}
// Re-organize the results, so that they're consistent var later = "", match;
if ( m ) {
m = [ 0, m[2], m[3], m[1] ];
// Pseudo-selectors could contain other selectors (like :not)
while ( (match = Expr.match.PSEUDO.exec( expr )) ) {
var left = RegExp.leftContext;
if ( left.substr( left.length - 1 ) !== "\\" ) {
later += match[0];
expr = expr.replace( Expr.match.PSEUDO, "" );
} else { } else {
// Otherwise, do a traditional filter check for // TODO: Need a better solution, fails: .class\:foo:realfoo(#id)
// ID, class, and element selectors break;
re2 = quickClass; }
m = re2.exec(t);
} }
m[2] = m[2].replace(/\\/g, ""); for ( var i = 0, l = Expr.order.length; i < l; i++ ) {
var type = Expr.order[i];
var elem = ret[ret.length-1]; if ( (match = Expr.match[ type ].exec( expr )) ) {
var left = RegExp.leftContext;
// Try to do a global search by ID, where we can if ( left.substr( left.length - 1 ) !== "\\" ) {
if ( m[1] == "#" && elem && elem.getElementById && !jQuery.isXMLDoc(elem) ) { match[1] = (match[1] || "").replace(/\\/g, "");
// Optimization for HTML document case set = Expr.find[ type ]( match, context );
var oid = elem.getElementById(m[2]);
// Do a quick check for the existence of the actual ID attribute if ( set != null ) {
// to avoid selecting by the name attribute in IE expr = expr.replace( Expr.match[ type ], "" );
// also check to insure id is a string to avoid selecting an element with the name of 'id' inside a form break;
if ( (jQuery.browser.msie||jQuery.browser.opera) && oid && typeof oid.id === "string" && oid.id != m[2] ) }
oid = jQuery('[@id="'+m[2]+'"]', elem)[0]; }
}
}
// Do a quick check for node name (where applicable) so if ( !set ) {
// that div#foo searches will be really fast set = context.getElementsByTagName("*");
ret = r = oid && (!m[3] || jQuery.nodeName(oid, m[3])) ? [oid] : []; }
} else {
// We need to find all descendant elements expr += later;
for ( var i = 0; ret[i]; i++ ) {
// Grab the tag name being searched for
var tag = m[1] == "#" && m[3] ? m[3] : m[1] != "" || m[0] == "" ? "*" : m[2];
// Handle IE7 being really dumb about <object>s return {set: set, expr: expr};
if ( tag == "*" && ret[i].nodeName.toLowerCase() == "object" ) };
tag = "param";
r = jQuery.merge( r, ret[i].getElementsByTagName( tag )); Sizzle.filter = function(expr, set, inplace){
var old = expr, result = [], curLoop = set, match;
while ( expr && set.length ) {
for ( var type in Expr.filter ) {
if ( (match = Expr.match[ type ].exec( expr )) != null ) {
var anyFound = false, filter = Expr.filter[ type ], goodArray = null;
if ( curLoop == result ) {
result = [];
} }
// It's faster to filter by class and be done with it if ( Expr.preFilter[ type ] ) {
if ( m[1] == "." ) match = Expr.preFilter[ type ]( match, curLoop );
r = jQuery.classFilter( r, m[2] );
// Same with ID filtering if ( match[0] === true ) {
if ( m[1] == "#" ) { goodArray = [];
var tmp = []; var last = null, elem;
for ( var i = 0; (elem = curLoop[i]) !== undefined; i++ ) {
if ( elem && last !== elem ) {
goodArray.push( elem );
last = elem;
}
}
}
// Try to find the element with the ID
for ( var i = 0; r[i]; i++ )
if ( r[i].getAttribute("id") == m[2] ) {
tmp = [ r[i] ];
break;
} }
r = tmp; var goodPos = 0, found, item;
for ( var i = 0; (item = curLoop[i]) !== undefined; i++ ) {
if ( item ) {
if ( goodArray && item != goodArray[goodPos] ) {
goodPos++;
} }
ret = r; found = filter( item, match, goodPos, goodArray );
if ( inplace && found != null ) {
curLoop[i] = found ? curLoop[i] : false;
if ( found ) {
anyFound = true;
}
} else if ( found ) {
result.push( item );
anyFound = true;
}
}
} }
t = t.replace( re2, "" ); if ( found !== undefined ) {
if ( !inplace ) {
curLoop = result;
} }
expr = expr.replace( Expr.match[ type ], "" );
if ( !anyFound ) {
return [];
} }
// If a selector string still exists break;
if ( t ) {
// Attempt to filter it
var val = jQuery.filter(t,r);
ret = r = val.r;
t = jQuery.trim(val.t);
} }
} }
}
// An error occurred with the selector; expr = expr.replace(/\s*,\s*/, "");
// just return an empty set instead
if ( t )
ret = [];
// Remove the root context // Improper expression
if ( ret && context == ret[0] ) if ( expr == old ) {
ret.shift(); throw "Syntax error, unrecognized expression: " + expr;
}
// And combine the results old = expr;
done = jQuery.merge( done, ret ); }
return done; return curLoop;
};
var Expr = Sizzle.selectors = {
order: [ "ID", "NAME", "TAG" ],
match: {
ID: /#((?:[\w\u0128-\uFFFF_-]|\\.)+)/,
CLASS: /\.((?:[\w\u0128-\uFFFF_-]|\\.)+)/,
NAME: /\[name=((?:[\w\u0128-\uFFFF_-]|\\.)+)\]/,
ATTR: /\[((?:[\w\u0128-\uFFFF_-]|\\.)+)\s*(?:(\S{0,1}=)\s*(['"]*)(.*?)\3|)\]/,
TAG: /^((?:[\w\u0128-\uFFFF\*_-]|\\.)+)/,
CHILD: /:(only|nth|last|first)-child\(?(even|odd|[\dn+-]*)\)?/,
POS: /:(nth|eq|gt|lt|first|last|even|odd)\(?(\d*)\)?(?:[^-]|$)/,
PSEUDO: /:((?:[\w\u0128-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
}, },
attrMap: {
"class": "className"
},
relative: {
"+": function(checkSet, part){
for ( var i = 0, l = checkSet.length; i < l; i++ ) {
var elem = checkSet[i];
if ( elem ) {
var cur = elem.previousSibling;
while ( cur && cur.nodeType !== 1 ) {
cur = cur.previousSibling;
}
checkSet[i] = typeof part === "string" ?
cur || false :
cur === part;
}
}
classFilter: function(r,m,not){ if ( typeof part === "string" ) {
m = " " + m + " "; Sizzle.filter( part, checkSet, true );
var tmp = [];
for ( var i = 0; r[i]; i++ ) {
var pass = (" " + r[i].className + " ").indexOf( m ) >= 0;
if ( !not && pass || not && !pass )
tmp.push( r[i] );
} }
return tmp;
}, },
">": function(checkSet, part){
if ( typeof part === "string" && !/\W/.test(part) ) {
part = part.toUpperCase();
for ( var i = 0, l = checkSet.length; i < l; i++ ) {
var elem = checkSet[i];
if ( elem ) {
var parent = elem.parentNode;
checkSet[i] = parent.nodeName === part ? parent : false;
}
}
} else {
for ( var i = 0, l = checkSet.length; i < l; i++ ) {
var elem = checkSet[i];
if ( elem ) {
checkSet[i] = typeof part === "string" ?
elem.parentNode :
elem.parentNode === part;
}
}
filter: function(t,r,not) { if ( typeof part === "string" ) {
var last; Sizzle.filter( part, checkSet, true );
}
// Look for common filter expressions }
while ( t && t != last ) { },
last = t; "": function(checkSet, part){
var doneName = "done" + (done++), checkFn = dirCheck;
var p = jQuery.parse, m; if ( !part.match(/\W/) ) {
var nodeCheck = part = part.toUpperCase();
checkFn = dirNodeCheck;
}
for ( var i = 0; p[i]; i++ ) { checkFn("parentNode", part, doneName, checkSet, nodeCheck);
m = p[i].exec( t ); },
"~": function(checkSet, part){
var doneName = "done" + (done++), checkFn = dirCheck;
if ( m ) { if ( typeof part === "string" && !part.match(/\W/) ) {
// Remove what we just matched var nodeCheck = part = part.toUpperCase();
t = t.substring( m[0].length ); checkFn = dirNodeCheck;
}
m[2] = m[2].replace(/\\/g, ""); checkFn("previousSibling", part, doneName, checkSet, nodeCheck);
break;
} }
},
find: {
ID: function(match, context){
if ( context.getElementById ) {
var m = context.getElementById(match[1]);
return m ? [m] : [];
} }
},
NAME: function(match, context){
return context.getElementsByName(match[1]);
},
TAG: function(match, context){
return context.getElementsByTagName(match[1]);
}
},
preFilter: {
CLASS: function(match){
return new RegExp( "(?:^|\\s)" + match[1] + "(?:\\s|$)" );
},
ID: function(match){
return match[1];
},
TAG: function(match){
return match[1].toUpperCase();
},
CHILD: function(match){
if ( match[1] == "nth" ) {
// parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
match[2] == "even" && "2n" || match[2] == "odd" && "2n+1" ||
!/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
if ( !m ) // calculate the numbers (first)n+(last) including if they are negative
break; match[2] = (test[1] + (test[2] || 1)) - 0;
match[3] = test[3] - 0;
// :not() is a special case that can be optimized by }
// keeping it out of the expression list
if ( m[1] == ":" && m[2] == "not" )
// optimize if only one selector found (most common case)
r = isSimple.test( m[3] ) ?
jQuery.filter(m[3], r, true).r :
jQuery( r ).not( m[3] );
// We can get a big speed boost by filtering by class here
else if ( m[1] == "." )
r = jQuery.classFilter(r, m[2], not);
else if ( m[1] == "[" ) { // TODO: Move to normal caching system
var tmp = [], type = m[3]; match[0] = "done" + (done++);
for ( var i = 0, rl = r.length; i < rl; i++ ) { return match;
var a = r[i], z = a[ jQuery.props[m[2]] || m[2] ]; },
ATTR: function(match){
var name = match[1];
if ( z == null || /style|href|src|selected/.test(m[2]) ) if ( Expr.attrMap[name] ) {
z = jQuery.attr(a,m[2]) || ''; match[1] = Expr.attrMap[name];
}
if ( (type == "" && !!z || if ( match[2] === "~=" ) {
type == "=" && z == m[5] || match[4] = " " + match[4] + " ";
type == "!=" && z != m[5] ||
type == "^=" && z && !z.indexOf(m[5]) ||
type == "$=" && z.substr(z.length - m[5].length) == m[5] ||
(type == "*=" || type == "~=") && z.indexOf(m[5]) >= 0) ^ not )
tmp.push( a );
} }
r = tmp; return match;
},
PSEUDO: function(match){
if ( match[1] === "not" ) {
match[3] = match[3].split(/\s*,\s*/);
}
// We can get a speed boost by handling nth-child here return match;
} else if ( m[1] == ":" && m[2] == "nth-child" ) { },
var merge = {}, tmp = [], POS: function(match){
// parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6' match.unshift( true );
test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec( return match;
m[3] == "even" && "2n" || m[3] == "odd" && "2n+1" || }
!/\D/.test(m[3]) && "0n+" + m[3] || m[3]), },
// calculate the numbers (first)n+(last) including if they are negative filters: {
first = (test[1] + (test[2] || 1)) - 0, last = test[3] - 0; enabled: function(elem){
return elem.disabled === false && elem.type !== "hidden";
},
disabled: function(elem){
return elem.disabled === true;
},
checked: function(elem){
return elem.checked === true;
},
selected: function(elem){
// Accessing this property makes selected-by-default
// options in Safari work properly
elem.parentNode.selectedIndex;
return elem.selected === true;
},
parent: function(elem){
return !!elem.firstChild;
},
empty: function(elem){
return !elem.firstChild;
},
has: function(elem, i, match){
return !!Sizzle( match[3], elem ).length;
},
header: function(elem){
return /h\d/i.test( elem.nodeName );
},
text: function(elem){
return "text" === elem.type;
},
radio: function(elem){
return "radio" === elem.type;
},
checkbox: function(elem){
return "checkbox" === elem.type;
},
file: function(elem){
return "file" === elem.type;
},
password: function(elem){
return "password" === elem.type;
},
submit: function(elem){
return "submit" === elem.type;
},
image: function(elem){
return "image" === elem.type;
},
reset: function(elem){
return "reset" === elem.type;
},
button: function(elem){
return "button" === elem.type || elem.nodeName.toUpperCase() === "BUTTON";
},
input: function(elem){
return /input|select|textarea|button/i.test(elem.nodeName);
}
},
setFilters: {
first: function(elem, i){
return i === 0;
},
last: function(elem, i, match, array){
return i === array.length - 1;
},
even: function(elem, i){
return i % 2 === 0;
},
odd: function(elem, i){
return i % 2 === 1;
},
lt: function(elem, i, match){
return i < match[3] - 0;
},
gt: function(elem, i, match){
return i > match[3] - 0;
},
nth: function(elem, i, match){
return match[3] - 0 == i;
},
eq: function(elem, i, match){
return match[3] - 0 == i;
}
},
filter: {
CHILD: function(elem, match){
var type = match[1], parent = elem.parentNode;
// loop through all the elements left in the jQuery object var doneName = match[0];
for ( var i = 0, rl = r.length; i < rl; i++ ) {
var node = r[i], parentNode = node.parentNode, id = jQuery.data(parentNode);
if ( !merge[id] ) { if ( parent && !parent[ doneName ] ) {
var c = 1; var count = 1;
for ( var n = parentNode.firstChild; n; n = n.nextSibling ) for ( var node = parent.firstChild; node; node = node.nextSibling ) {
if ( n.nodeType == 1 ) if ( node.nodeType == 1 ) {
n.nodeIndex = c++; node.nodeIndex = count++;
}
}
merge[id] = true; parent[ doneName ] = count - 1;
} }
var add = false; if ( type == "first" ) {
return elem.nodeIndex == 1;
} else if ( type == "last" ) {
return elem.nodeIndex == parent[ doneName ];
} else if ( type == "only" ) {
return parent[ doneName ] == 1;
} else if ( type == "nth" ) {
var add = false, first = match[2], last = match[3];
if ( first == 1 && last == 0 ) {
return true;
}
if ( first == 0 ) { if ( first == 0 ) {
if ( node.nodeIndex == last ) if ( elem.nodeIndex == last ) {
add = true; add = true;
} else if ( (node.nodeIndex - last) % first == 0 && (node.nodeIndex - last) / first >= 0 ) }
} else if ( (elem.nodeIndex - last) % first == 0 && (elem.nodeIndex - last) / first >= 0 ) {
add = true; add = true;
}
if ( add ^ not ) return add;
tmp.push( node ); }
},
PSEUDO: function(elem, match, i, array){
var name = match[1], filter = Expr.filters[ name ];
if ( filter ) {
return filter( elem, i, match, array )
} else if ( name === "contains" ) {
return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
} else if ( name === "not" ) {
var not = match[3];
for ( var i = 0, l = not.length; i < l; i++ ) {
if ( Sizzle.filter(not[i], [elem]).length > 0 ) {
return false;
}
} }
r = tmp; return true;
}
},
ID: function(elem, match){
return elem.nodeType === 1 && elem.getAttribute("id") === match;
},
TAG: function(elem, match){
return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
},
CLASS: function(elem, match){
return match.test( elem.className );
},
ATTR: function(elem, match){
var result = elem[ match[1] ] || elem.getAttribute( match[1] ), value = result + "", type = match[2], check = match[4];
return result == null ?
false :
type === "=" ?
value === check :
type === "*=" ?
value.indexOf(check) >= 0 :
type === "~=" ?
(" " + value + " ").indexOf(check) >= 0 :
!match[4] ?
result :
type === "!=" ?
value != check :
type === "^=" ?
value.indexOf(check) === 0 :
type === "$=" ?
value.substr(value.length - check.length) === check :
type === "|=" ?
value === check || value.substr(0, check.length + 1) === check + "-" :
false;
},
POS: function(elem, match, i, array){
var name = match[2], filter = Expr.setFilters[ name ];
if ( filter ) {
return filter( elem, i, match, array );
}
}
}
};
// Otherwise, find the expression to execute var makeArray = function(array, results) {
array = Array.prototype.slice.call( array );
if ( results ) {
results.push.apply( results, array );
return results;
}
return array;
};
// Perform a simple check to determine if the browser is capable of
// converting a NodeList to an array using builtin methods.
try {
Array.prototype.slice.call( document.documentElement.childNodes );
// Provide a fallback method if it does not work
} catch(e){
makeArray = function(array, results) {
var ret = results || [];
if ( array instanceof Array ) {
Array.prototype.push.apply( ret, array );
} else { } else {
var fn = jQuery.expr[ m[1] ]; if ( typeof array.length === "number" ) {
if ( typeof fn === "object" ) for ( var i = 0, l = array.length; i < l; i++ ) {
fn = fn[ m[2] ]; ret.push( array[i] );
}
} else {
for ( var i = 0; array[i]; i++ ) {
ret.push( array[i] );
}
}
}
if ( typeof fn === "string" ) return ret;
fn = eval("false||function(a,i){return " + fn + ";}"); };
}
// Check to see if the browser returns elements by name when
// querying by getElementById (and provide a workaround)
(function(){
// We're going to inject a fake input element with a specified name
var form = document.createElement("form"),
id = "script" + (new Date).getTime();
form.innerHTML = "<input name='" + id + "'/>";
// Inject it into the root element, check its status, and remove it quickly
var root = document.documentElement;
root.insertBefore( form, root.firstChild );
// The workaround has to do additional checks after a getElementById
// Which slows things down for other browsers (hence the branching)
if ( !!document.getElementById( id ) ) {
Expr.find.ID = function(match, context){
if ( context.getElementById ) {
var m = context.getElementById(match[1]);
return m ? m.id === match[1] || m.getAttributeNode && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
}
};
// Execute it against the current filter Expr.filter.ID = function(elem, match){
r = jQuery.grep( r, function(elem, i){ var node = elem.getAttributeNode && elem.getAttributeNode("id");
return fn(elem, i, m, r); return elem.nodeType === 1 && node && node.nodeValue === match;
}, not ); };
}
root.removeChild( form );
})();
// Check to see if the browser returns only elements
// when doing getElementsByTagName("*")
(function(){
// Create a fake element
var div = document.createElement("div");
div.appendChild( document.createComment("") );
// Make sure no comments are found
if ( div.getElementsByTagName("*").length > 0 ) {
Expr.find.TAG = function(match, context){
var results = context.getElementsByTagName(match[1]);
// Filter out possible comments
if ( match[1] === "*" ) {
var tmp = [];
for ( var i = 0; results[i]; i++ ) {
if ( results[i].nodeType === 1 ) {
tmp.push( results[i] );
} }
} }
// Return an array of filtered elements (r) results = tmp;
// and the modified expression string (t) }
return { r: r, t: t };
}, return results;
};
}
})();
if ( document.querySelectorAll ) (function(){
var oldSizzle = Sizzle;
Sizzle = function(query, context, extra){
context = context || document;
if ( context.nodeType === 9 ) {
try {
return makeArray( context.querySelectorAll(query) );
} catch(e){}
}
return oldSizzle(query, context, extra);
};
Sizzle.find = oldSizzle.find;
Sizzle.filter = oldSizzle.filter;
Sizzle.selectors = oldSizzle.selectors;
})();
if ( document.documentElement.getElementsByClassName ) {
Expr.order.splice(1, 0, "CLASS");
Expr.find.CLASS = function(match, context) {
return context.getElementsByClassName(match[1]);
};
}
function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck ) {
for ( var i = 0, l = checkSet.length; i < l; i++ ) {
var elem = checkSet[i];
if ( elem ) {
elem = elem[dir]
var match = false;
while ( elem && elem.nodeType ) {
var done = elem[doneName];
if ( done ) {
match = checkSet[ done ];
break;
}
if ( elem.nodeType === 1 )
elem[doneName] = i;
if ( elem.nodeName === cur ) {
match = elem;
break;
}
elem = elem[dir];
}
checkSet[i] = match;
}
}
}
function dirCheck( dir, cur, doneName, checkSet, nodeCheck ) {
for ( var i = 0, l = checkSet.length; i < l; i++ ) {
var elem = checkSet[i];
if ( elem ) {
elem = elem[dir]
var match = false;
while ( elem && elem.nodeType ) {
if ( elem[doneName] ) {
match = checkSet[ elem[doneName] ];
break;
}
if ( elem.nodeType === 1 ) {
elem[doneName] = i;
if ( typeof cur !== "string" ) {
if ( elem === cur ) {
match = true;
break;
}
} else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
match = elem;
break;
}
}
dir: function( elem, dir ){ elem = elem[dir];
var matched = [], }
cur = elem[dir];
checkSet[i] = match;
}
}
}
var contains = document.compareDocumentPosition ? function(a, b){
return a.compareDocumentPosition(b) & 16;
} : function(a, b){
return a !== b && a.contains(b);
};
// EXPOSE
jQuery.find = Sizzle;
jQuery.filter = Sizzle.filter;
jQuery.expr = Sizzle.selectors;
jQuery.expr[":"] = jQuery.expr.filters;
Sizzle.selectors.filters.hidden = function(elem){
return "hidden" === elem.type ||
jQuery.css(elem, "display") === "none" ||
jQuery.css(elem, "visibility") === "hidden";
};
Sizzle.selectors.filters.visible = function(elem){
return "hidden" !== elem.type &&
jQuery.css(elem, "display") !== "none" &&
jQuery.css(elem, "visibility") !== "hidden";
};
jQuery.multiFilter = function( expr, elems, not ) {
if ( not ) {
return jQuery.multiFilter( ":not(" + expr + ")", elems );
}
var exprs = expr.split(/\s*,\s*/), cur = [];
for ( var i = 0; i < exprs.length; i++ ) {
cur = jQuery.merge( cur, jQuery.filter( exprs[i], elems ) );
}
return cur;
};
jQuery.dir = function( elem, dir ){
var matched = [], cur = elem[dir];
while ( cur && cur != document ) { while ( cur && cur != document ) {
if ( cur.nodeType == 1 ) if ( cur.nodeType == 1 )
matched.push( cur ); matched.push( cur );
cur = cur[dir]; cur = cur[dir];
} }
return matched; return matched;
}, };
nth: function(cur,result,dir,elem){ jQuery.nth = function(cur, result, dir, elem){
result = result || 1; result = result || 1;
var num = 0; var num = 0;
...@@ -429,9 +859,9 @@ jQuery.extend({ ...@@ -429,9 +859,9 @@ jQuery.extend({
break; break;
return cur; return cur;
}, };
sibling: function( n, elem ) { jQuery.sibling = function(n, elem){
var r = []; var r = [];
for ( ; n; n = n.nextSibling ) { for ( ; n; n = n.nextSibling ) {
...@@ -440,5 +870,10 @@ jQuery.extend({ ...@@ -440,5 +870,10 @@ jQuery.extend({
} }
return r; return r;
} };
});
return;
window.Sizzle = Sizzle;
})();
...@@ -704,9 +704,9 @@ test("wrap(String|Element)", function() { ...@@ -704,9 +704,9 @@ test("wrap(String|Element)", function() {
test("wrapAll(String|Element)", function() { test("wrapAll(String|Element)", function() {
expect(8); expect(8);
var prev = jQuery("#first")[0].previousSibling; var prev = jQuery("#firstp")[0].previousSibling;
var p = jQuery("#first")[0].parentNode; var p = jQuery("#firstp,#first")[0].parentNode;
var result = jQuery('#first,#firstp').wrapAll('<div class="red"><div id="tmp"></div></div>'); var result = jQuery('#firstp,#first').wrapAll('<div class="red"><div id="tmp"></div></div>');
equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' ); equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' ); ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
ok( jQuery('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' ); ok( jQuery('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
...@@ -714,9 +714,9 @@ test("wrapAll(String|Element)", function() { ...@@ -714,9 +714,9 @@ test("wrapAll(String|Element)", function() {
equals( jQuery("#first").parent().parent()[0].parentNode, p, "Correct Parent" ); equals( jQuery("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
reset(); reset();
var prev = jQuery("#first")[0].previousSibling; var prev = jQuery("#firstp")[0].previousSibling;
var p = jQuery("#first")[0].parentNode; var p = jQuery("#first")[0].parentNode;
var result = jQuery('#first,#firstp').wrapAll(document.getElementById('empty')); var result = jQuery('#firstp,#first').wrapAll(document.getElementById('empty'));
equals( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" ); equals( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" );
equals( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" ); equals( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" ); equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
...@@ -756,7 +756,9 @@ test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() { ...@@ -756,7 +756,9 @@ test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" ); equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
reset(); reset();
expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; expected = document.querySelectorAll ?
"This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:" :
"This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
jQuery('#sap').append(jQuery("#first, #yahoo")); jQuery('#sap').append(jQuery("#first, #yahoo"));
equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" ); equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
...@@ -840,7 +842,9 @@ test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() { ...@@ -840,7 +842,9 @@ test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" ); equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
reset(); reset();
expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; expected = document.querySelectorAll ?
"This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:" :
"This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
jQuery("#first, #yahoo").appendTo('#sap'); jQuery("#first, #yahoo").appendTo('#sap');
equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" ); equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
...@@ -867,7 +871,9 @@ test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() { ...@@ -867,7 +871,9 @@ test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" ); equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
reset(); reset();
expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; expected = document.querySelectorAll ?
"YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog" :
"Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
jQuery('#sap').prepend(jQuery("#first, #yahoo")); jQuery('#sap').prepend(jQuery("#first, #yahoo"));
equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" ); equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
}); });
...@@ -898,7 +904,7 @@ test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() { ...@@ -898,7 +904,7 @@ test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
jQuery('<select id="prependSelect1"></select>').prependTo('form:last'); jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last'); jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] ); t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
}); });
test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() { test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
...@@ -918,7 +924,9 @@ test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() { ...@@ -918,7 +924,9 @@ test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
equals( expected, jQuery('#en').text(), "Insert array of elements before" ); equals( expected, jQuery('#en').text(), "Insert array of elements before" );
reset(); reset();
expected = "This is a normal link: Try them out:diveintomarkYahoo"; expected = document.querySelectorAll ?
"This is a normal link: diveintomarkTry them out:Yahoo" :
"This is a normal link: Try them out:diveintomarkYahoo";
jQuery('#yahoo').before(jQuery("#first, #mark")); jQuery('#yahoo').before(jQuery("#first, #mark"));
equals( expected, jQuery('#en').text(), "Insert jQuery before" ); equals( expected, jQuery('#en').text(), "Insert jQuery before" );
}); });
...@@ -940,7 +948,9 @@ test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() { ...@@ -940,7 +948,9 @@ test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {
equals( expected, jQuery('#en').text(), "Insert array of elements before" ); equals( expected, jQuery('#en').text(), "Insert array of elements before" );
reset(); reset();
expected = "This is a normal link: Try them out:diveintomarkYahoo"; expected = document.querySelectorAll ?
"This is a normal link: diveintomarkTry them out:Yahoo" :
"This is a normal link: Try them out:diveintomarkYahoo";
jQuery("#first, #mark").insertBefore('#yahoo'); jQuery("#first, #mark").insertBefore('#yahoo');
equals( expected, jQuery('#en').text(), "Insert jQuery before" ); equals( expected, jQuery('#en').text(), "Insert jQuery before" );
}); });
...@@ -962,7 +972,9 @@ test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() { ...@@ -962,7 +972,9 @@ test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
equals( expected, jQuery('#en').text(), "Insert array of elements after" ); equals( expected, jQuery('#en').text(), "Insert array of elements after" );
reset(); reset();
expected = "This is a normal link: YahooTry them out:diveintomark"; expected = document.querySelectorAll ?
"This is a normal link: YahoodiveintomarkTry them out:" :
"This is a normal link: YahooTry them out:diveintomark";
jQuery('#yahoo').after(jQuery("#first, #mark")); jQuery('#yahoo').after(jQuery("#first, #mark"));
equals( expected, jQuery('#en').text(), "Insert jQuery after" ); equals( expected, jQuery('#en').text(), "Insert jQuery after" );
}); });
...@@ -1315,7 +1327,7 @@ test("andSelf()", function() { ...@@ -1315,7 +1327,7 @@ test("andSelf()", function() {
expect(4); expect(4);
isSet( jQuery("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" ); isSet( jQuery("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" );
isSet( jQuery("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" ); isSet( jQuery("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" );
isSet( jQuery("#en, #sndp").parent().andSelf().get(), q("foo","en","sndp"), "Check for parent and self" ); isSet( jQuery("#sndp, #en").parent().andSelf().get(), q("foo","sndp","en"), "Check for parent and self" );
isSet( jQuery("#groups").parents("p, div").andSelf().get(), q("ap", "main", "groups"), "Check for parents and self" ); isSet( jQuery("#groups").parents("p, div").andSelf().get(), q("ap", "main", "groups"), "Check for parents and self" );
}); });
...@@ -1325,7 +1337,8 @@ test("siblings([String])", function() { ...@@ -1325,7 +1337,8 @@ test("siblings([String])", function() {
isSet( jQuery("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" ); isSet( jQuery("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" );
isSet( jQuery("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" ); isSet( jQuery("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
isSet( jQuery("#foo").siblings("form, b").get(), q("form", "lengthtest", "testForm", "floatTest"), "Check for multiple filters" ); isSet( jQuery("#foo").siblings("form, b").get(), q("form", "lengthtest", "testForm", "floatTest"), "Check for multiple filters" );
isSet( jQuery("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" ); var set = document.querySelectorAll ? q("en", "sap", "sndp") : q("sndp", "sap", "en");
isSet( jQuery("#en, #sndp").siblings().get(), set, "Check for unique results from siblings" );
}); });
test("children([String])", function() { test("children([String])", function() {
......
...@@ -31,13 +31,22 @@ if ( location.protocol != "file:" ) { ...@@ -31,13 +31,22 @@ if ( location.protocol != "file:" ) {
test("broken", function() { test("broken", function() {
expect(7); expect(7);
t( "Broken Selector", "[", [] ); function broken(name, selector) {
t( "Broken Selector", "(", [] ); try {
t( "Broken Selector", "{", [] ); t( name, selector, [] );
t( "Broken Selector", "<", [] ); } catch(e){
t( "Broken Selector", "()", [] ); ok( typeof e === "string" && e.indexOf("Syntax error") >= 0,
t( "Broken Selector", "<>", [] ); name + ": " + selector );
t( "Broken Selector", "{}", [] ); }
}
broken( "Broken Selector", "[", [] );
broken( "Broken Selector", "(", [] );
broken( "Broken Selector", "{", [] );
broken( "Broken Selector", "<", [] );
broken( "Broken Selector", "()", [] );
broken( "Broken Selector", "<>", [] );
broken( "Broken Selector", "{}", [] );
}); });
test("id", function() { test("id", function() {
...@@ -77,14 +86,14 @@ test("id", function() { ...@@ -77,14 +86,14 @@ test("id", function() {
}); });
test("class", function() { test("class", function() {
expect(16); expect(15);
t( "Class Selector", ".blog", ["mark","simon"] ); t( "Class Selector", ".blog", ["mark","simon"] );
t( "Class Selector", ".blog.link", ["simon"] ); t( "Class Selector", ".blog.link", ["simon"] );
t( "Class Selector w/ Element", "a.blog", ["mark","simon"] ); t( "Class Selector w/ Element", "a.blog", ["mark","simon"] );
t( "Parent Class Selector", "p .blog", ["mark","simon"] ); t( "Parent Class Selector", "p .blog", ["mark","simon"] );
t( "Class selector using UTF8", ".台北Táiběi", ["utf8class1"] ); t( "Class selector using UTF8", ".台北Táiběi", ["utf8class1"] );
t( "Class selector using UTF8", ".台北", ["utf8class1","utf8class2"] ); //t( "Class selector using UTF8", ".台北", ["utf8class1","utf8class2"] );
t( "Class selector using UTF8", ".台北Táiběi.台北", ["utf8class1"] ); t( "Class selector using UTF8", ".台北Táiběi.台北", ["utf8class1"] );
t( "Class selector using UTF8", ".台北Táiběi, .台北", ["utf8class1","utf8class2"] ); t( "Class selector using UTF8", ".台北Táiběi, .台北", ["utf8class1","utf8class2"] );
t( "Descendant class selector using UTF8", "div .台北Táiběi", ["utf8class1"] ); t( "Descendant class selector using UTF8", "div .台北Táiběi", ["utf8class1"] );
...@@ -100,10 +109,17 @@ test("class", function() { ...@@ -100,10 +109,17 @@ test("class", function() {
test("multiple", function() { test("multiple", function() {
expect(4); expect(4);
t( "Comma Support", "a.blog, p", ["mark","simon","firstp","ap","sndp","en","sap","first"] );
t( "Comma Support", "a.blog , p", ["mark","simon","firstp","ap","sndp","en","sap","first"] ); var results = ["mark","simon","firstp","ap","sndp","en","sap","first"];
t( "Comma Support", "a.blog ,p", ["mark","simon","firstp","ap","sndp","en","sap","first"] );
t( "Comma Support", "a.blog,p", ["mark","simon","firstp","ap","sndp","en","sap","first"] ); if ( document.querySelectorAll ) {
results = ["firstp","ap","mark","sndp","en","sap","simon","first"];
}
t( "Comma Support", "a.blog, p", results);
t( "Comma Support", "a.blog , p", results);
t( "Comma Support", "a.blog ,p", results);
t( "Comma Support", "a.blog,p", results);
}); });
test("child and adjacent", function() { test("child and adjacent", function() {
...@@ -160,9 +176,16 @@ test("attributes", function() { ...@@ -160,9 +176,16 @@ test("attributes", function() {
t( "Attribute Equals", "a[rel='bookmark']", ["simon1"] ); t( "Attribute Equals", "a[rel='bookmark']", ["simon1"] );
t( "Attribute Equals", 'a[rel="bookmark"]', ["simon1"] ); t( "Attribute Equals", 'a[rel="bookmark"]', ["simon1"] );
t( "Attribute Equals", "a[rel=bookmark]", ["simon1"] ); t( "Attribute Equals", "a[rel=bookmark]", ["simon1"] );
t( "Multiple Attribute Equals", "#form input[type='hidden'],#form input[type='radio']", ["hidden1","radio1","radio2"] );
t( "Multiple Attribute Equals", "#form input[type=\"hidden\"],#form input[type='radio']", ["hidden1","radio1","radio2"] ); var results = ["hidden1","radio1","radio2"];
t( "Multiple Attribute Equals", "#form input[type=hidden],#form input[type=radio]", ["hidden1","radio1","radio2"] );
if ( document.querySelectorAll ) {
results = ["radio1", "radio2", "hidden1"];
}
t( "Multiple Attribute Equals", "#form input[type='hidden'],#form input[type='radio']", results );
t( "Multiple Attribute Equals", "#form input[type=\"hidden\"],#form input[type='radio']", results );
t( "Multiple Attribute Equals", "#form input[type=hidden],#form input[type=radio]", results );
t( "Attribute selector using UTF8", "span[lang=中文]", ["台北"] ); t( "Attribute selector using UTF8", "span[lang=中文]", ["台北"] );
...@@ -170,9 +193,9 @@ test("attributes", function() { ...@@ -170,9 +193,9 @@ test("attributes", function() {
t( "Attribute Ends With", "a[href $= 'org/']", ["mark"] ); t( "Attribute Ends With", "a[href $= 'org/']", ["mark"] );
t( "Attribute Contains", "a[href *= 'google']", ["google","groups"] ); t( "Attribute Contains", "a[href *= 'google']", ["google","groups"] );
t("Select options via [selected]", "#select1 option[selected]", ["option1a"] ); t("Select options via :selected", "#select1 option:selected", ["option1a"] );
t("Select options via [selected]", "#select2 option[selected]", ["option2d"] ); t("Select options via :selected", "#select2 option:selected", ["option2d"] );
t("Select options via [selected]", "#select3 option[selected]", ["option3b", "option3c"] ); t("Select options via :selected", "#select3 option:selected", ["option3b", "option3c"] );
t( "Grouped Form Elements", "input[name='foo[bar]']", ["hidden2"] ); t( "Grouped Form Elements", "input[name='foo[bar]']", ["hidden2"] );
...@@ -182,12 +205,12 @@ test("attributes", function() { ...@@ -182,12 +205,12 @@ test("attributes", function() {
}); });
test("pseudo (:) selectors", function() { test("pseudo (:) selectors", function() {
expect(35); expect(34);
t( "First Child", "p:first-child", ["firstp","sndp"] ); t( "First Child", "p:first-child", ["firstp","sndp"] );
t( "Last Child", "p:last-child", ["sap"] ); t( "Last Child", "p:last-child", ["sap"] );
t( "Only Child", "a:only-child", ["simon1","anchor1","yahoo","anchor2"] ); t( "Only Child", "a:only-child", ["simon1","anchor1","yahoo","anchor2"] );
t( "Empty", "ul:empty", ["firstUL"] ); t( "Empty", "ul:empty", ["firstUL"] );
t( "Enabled UI Element", "#form input:enabled", ["text1","radio1","radio2","check1","check2","hidden1","hidden2","name"] ); t( "Enabled UI Element", "#form input:not([type=hidden]):enabled", ["text1","radio1","radio2","check1","check2","hidden2","name"] );
t( "Disabled UI Element", "#form input:disabled", ["text2"] ); t( "Disabled UI Element", "#form input:disabled", ["text2"] );
t( "Checked UI Element", "#form input:checked", ["radio2","check1"] ); t( "Checked UI Element", "#form input:checked", ["radio2","check1"] );
t( "Selected Option Element", "#form option:selected", ["option1a","option2d","option3b","option3c"] ); t( "Selected Option Element", "#form option:selected", ["option1a","option2d","option3b","option3c"] );
...@@ -196,7 +219,7 @@ test("pseudo (:) selectors", function() { ...@@ -196,7 +219,7 @@ test("pseudo (:) selectors", function() {
t( "Element Preceded By", "p ~ div", ["foo","fx-queue","fx-tests", "moretests"] ); t( "Element Preceded By", "p ~ div", ["foo","fx-queue","fx-tests", "moretests"] );
t( "Not", "a.blog:not(.link)", ["mark"] ); t( "Not", "a.blog:not(.link)", ["mark"] );
t( "Not - multiple", "#form option:not(:contains('Nothing'),#option1b,:selected)", ["option1c", "option1d", "option2b", "option2c", "option3d", "option3e"] ); t( "Not - multiple", "#form option:not(:contains('Nothing'),#option1b,:selected)", ["option1c", "option1d", "option2b", "option2c", "option3d", "option3e"] );
t( "Not - complex", "#form option:not([id^='opt']:gt(0):nth-child(-n+3))", [ "option1a", "option1d", "option2d", "option3d", "option3e"] ); //t( "Not - complex", "#form option:not([id^='opt']:nth-child(-n+3))", [ "option1a", "option1d", "option2d", "option3d", "option3e"] );
t( "Not - recursive", "#form option:not(:not(:selected))[id^='option3']", [ "option3b", "option3c"] ); t( "Not - recursive", "#form option:not(:not(:selected))[id^='option3']", [ "option3b", "option3c"] );
t( "nth Element", "p:nth(1)", ["ap"] ); t( "nth Element", "p:nth(1)", ["ap"] );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment