Merging the latest from Sizzle.

This commit is contained in:
John Resig 2009-01-11 19:45:04 +00:00
parent 73ff49ac9a
commit 2e42c5b044

View file

@ -1,16 +1,16 @@
/* /*!
* Sizzle CSS Selector Engine - v0.9 * Sizzle CSS Selector Engine - v0.9.1
* Copyright 2009, John Resig (http://ejohn.org/) * Copyright 2009, The Dojo Foundation
* released under the MIT License * Released under the MIT, BSD, and GPL Licenses.
* More information: http://sizzlejs.com/
*/ */
(function(){ (function(){
var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|[^[\]]+)+\]|\\.|[^ >+~,(\[]+)+|[>+~])(\s*,\s*)?/g; var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|[^[\]]+)+\]|\\.|[^ >+~,(\[]+)+|[>+~])(\s*,\s*)?/g,
done = 0,
var done = 0; toString = Object.prototype.toString;
var Sizzle = function(selector, context, results, seed) { var Sizzle = function(selector, context, results, seed) {
var doCache = !results;
results = results || []; results = results || [];
context = context || document; context = context || document;
@ -90,7 +90,7 @@ var Sizzle = function(selector, context, results, seed) {
pop = context; pop = context;
} }
Expr.relative[ cur ]( checkSet, pop ); Expr.relative[ cur ]( checkSet, pop, isXML(context) );
} }
} }
@ -102,7 +102,7 @@ var Sizzle = function(selector, context, results, seed) {
throw "Syntax error, unrecognized expression: " + (cur || selector); throw "Syntax error, unrecognized expression: " + (cur || selector);
} }
if ( checkSet instanceof Array ) { if ( toString.call(checkSet) === "[object Array]" ) {
if ( !prune ) { if ( !prune ) {
results.push.apply( results, checkSet ); results.push.apply( results, checkSet );
} else if ( context.nodeType === 1 ) { } else if ( context.nodeType === 1 ) {
@ -254,17 +254,18 @@ Sizzle.filter = function(expr, set, inplace, not){
var Expr = Sizzle.selectors = { var Expr = Sizzle.selectors = {
order: [ "ID", "NAME", "TAG" ], order: [ "ID", "NAME", "TAG" ],
match: { match: {
ID: /#((?:[\w\u0128-\uFFFF_-]|\\.)+)/, ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
CLASS: /\.((?:[\w\u0128-\uFFFF_-]|\\.)+)/, CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
NAME: /\[name=['"]*((?:[\w\u0128-\uFFFF_-]|\\.)+)['"]*\]/, NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/,
ATTR: /\[((?:[\w\u0128-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\]/, ATTR: /\[((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\]/,
TAG: /^((?:[\w\u0128-\uFFFF\*_-]|\\.)+)/, TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/,
CHILD: /:(only|nth|last|first)-child\(?(even|odd|[\dn+-]*)\)?/, CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/,
POS: /:(nth|eq|gt|lt|first|last|even|odd)\(?(\d*)\)?(?:[^-]|$)/, POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/,
PSEUDO: /:((?:[\w\u0128-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/ PSEUDO: /:((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
}, },
attrMap: { attrMap: {
"class": "className" "class": "className",
"for": "htmlFor"
}, },
relative: { relative: {
"+": function(checkSet, part){ "+": function(checkSet, part){
@ -285,9 +286,9 @@ var Expr = Sizzle.selectors = {
Sizzle.filter( part, checkSet, true ); Sizzle.filter( part, checkSet, true );
} }
}, },
">": function(checkSet, part){ ">": function(checkSet, part, isXML){
if ( typeof part === "string" && !/\W/.test(part) ) { if ( typeof part === "string" && !/\W/.test(part) ) {
part = part.toUpperCase(); part = isXML ? part : part.toUpperCase();
for ( var i = 0, l = checkSet.length; i < l; i++ ) { for ( var i = 0, l = checkSet.length; i < l; i++ ) {
var elem = checkSet[i]; var elem = checkSet[i];
@ -311,21 +312,21 @@ var Expr = Sizzle.selectors = {
} }
} }
}, },
"": function(checkSet, part){ "": function(checkSet, part, isXML){
var doneName = "done" + (done++), checkFn = dirCheck; var doneName = "done" + (done++), checkFn = dirCheck;
if ( !part.match(/\W/) ) { if ( !part.match(/\W/) ) {
var nodeCheck = part = part.toUpperCase(); var nodeCheck = part = isXML ? part : part.toUpperCase();
checkFn = dirNodeCheck; checkFn = dirNodeCheck;
} }
checkFn("parentNode", part, doneName, checkSet, nodeCheck); checkFn("parentNode", part, doneName, checkSet, nodeCheck);
}, },
"~": function(checkSet, part){ "~": function(checkSet, part, isXML){
var doneName = "done" + (done++), checkFn = dirCheck; var doneName = "done" + (done++), checkFn = dirCheck;
if ( typeof part === "string" && !part.match(/\W/) ) { if ( typeof part === "string" && !part.match(/\W/) ) {
var nodeCheck = part = part.toUpperCase(); var nodeCheck = part = isXML ? part : part.toUpperCase();
checkFn = dirNodeCheck; checkFn = dirNodeCheck;
} }
@ -362,10 +363,11 @@ var Expr = Sizzle.selectors = {
return false; return false;
}, },
ID: function(match){ ID: function(match){
return match[1]; return match[1].replace(/\\/g, "");
}, },
TAG: function(match){ TAG: function(match, curLoop){
return match[1].toUpperCase(); for ( var i = 0; !curLoop[i]; i++ ){}
return isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
}, },
CHILD: function(match){ CHILD: function(match){
if ( match[1] == "nth" ) { if ( match[1] == "nth" ) {
@ -630,7 +632,7 @@ try {
makeArray = function(array, results) { makeArray = function(array, results) {
var ret = results || []; var ret = results || [];
if ( array instanceof Array ) { if ( toString.call(array) === "[object Array]" ) {
Array.prototype.push.apply( ret, array ); Array.prototype.push.apply( ret, array );
} else { } else {
if ( typeof array.length === "number" ) { if ( typeof array.length === "number" ) {
@ -809,6 +811,11 @@ var contains = document.compareDocumentPosition ? function(a, b){
return a !== b && (a.contains ? a.contains(b) : true); return a !== b && (a.contains ? a.contains(b) : true);
}; };
var isXML = function(elem){
return elem.documentElement && !elem.body ||
elem.tagName && elem.ownerDocument && !elem.ownerDocument.body;
};
// EXPOSE // EXPOSE
jQuery.find = Sizzle; jQuery.find = Sizzle;
jQuery.filter = Sizzle.filter; jQuery.filter = Sizzle.filter;