From 2e42c5b0445b7c871c67b0a3bb7a0dc42d1d2112 Mon Sep 17 00:00:00 2001 From: John Resig Date: Sun, 11 Jan 2009 19:45:04 +0000 Subject: [PATCH] Merging the latest from Sizzle. --- src/selector.js | 65 +++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/src/selector.js b/src/selector.js index eb545174e..e1e272bcb 100644 --- a/src/selector.js +++ b/src/selector.js @@ -1,16 +1,16 @@ -/* - * Sizzle CSS Selector Engine - v0.9 - * Copyright 2009, John Resig (http://ejohn.org/) - * released under the MIT License +/*! + * Sizzle CSS Selector Engine - v0.9.1 + * Copyright 2009, The Dojo Foundation + * Released under the MIT, BSD, and GPL Licenses. + * More information: http://sizzlejs.com/ */ (function(){ -var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|[^[\]]+)+\]|\\.|[^ >+~,(\[]+)+|[>+~])(\s*,\s*)?/g; - -var done = 0; +var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|[^[\]]+)+\]|\\.|[^ >+~,(\[]+)+|[>+~])(\s*,\s*)?/g, + done = 0, + toString = Object.prototype.toString; var Sizzle = function(selector, context, results, seed) { - var doCache = !results; results = results || []; context = context || document; @@ -90,7 +90,7 @@ var Sizzle = function(selector, context, results, seed) { 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); } - if ( checkSet instanceof Array ) { + if ( toString.call(checkSet) === "[object Array]" ) { if ( !prune ) { results.push.apply( results, checkSet ); } else if ( context.nodeType === 1 ) { @@ -254,17 +254,18 @@ Sizzle.filter = function(expr, set, inplace, not){ 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?=)\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\))?/ + ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/, + CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/, + NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/, + ATTR: /\[((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\]/, + TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/, + CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/, + POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/, + PSEUDO: /:((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/ }, attrMap: { - "class": "className" + "class": "className", + "for": "htmlFor" }, relative: { "+": function(checkSet, part){ @@ -285,9 +286,9 @@ var Expr = Sizzle.selectors = { Sizzle.filter( part, checkSet, true ); } }, - ">": function(checkSet, part){ + ">": function(checkSet, part, isXML){ 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++ ) { 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; if ( !part.match(/\W/) ) { - var nodeCheck = part = part.toUpperCase(); + var nodeCheck = part = isXML ? part : part.toUpperCase(); checkFn = dirNodeCheck; } checkFn("parentNode", part, doneName, checkSet, nodeCheck); }, - "~": function(checkSet, part){ + "~": function(checkSet, part, isXML){ var doneName = "done" + (done++), checkFn = dirCheck; if ( typeof part === "string" && !part.match(/\W/) ) { - var nodeCheck = part = part.toUpperCase(); + var nodeCheck = part = isXML ? part : part.toUpperCase(); checkFn = dirNodeCheck; } @@ -362,10 +363,11 @@ var Expr = Sizzle.selectors = { return false; }, ID: function(match){ - return match[1]; + return match[1].replace(/\\/g, ""); }, - TAG: function(match){ - return match[1].toUpperCase(); + TAG: function(match, curLoop){ + for ( var i = 0; !curLoop[i]; i++ ){} + return isXML(curLoop[i]) ? match[1] : match[1].toUpperCase(); }, CHILD: function(match){ if ( match[1] == "nth" ) { @@ -630,7 +632,7 @@ try { makeArray = function(array, results) { var ret = results || []; - if ( array instanceof Array ) { + if ( toString.call(array) === "[object Array]" ) { Array.prototype.push.apply( ret, array ); } else { 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); }; +var isXML = function(elem){ + return elem.documentElement && !elem.body || + elem.tagName && elem.ownerDocument && !elem.ownerDocument.body; +}; + // EXPOSE jQuery.find = Sizzle; jQuery.filter = Sizzle.filter; -- 2.39.5