diff --git a/src/selector.js b/src/selector.js index 085f0b32..56c8a2a6 100644 --- a/src/selector.js +++ b/src/selector.js @@ -45,7 +45,7 @@ var Sizzle = function(selector, context, results, seed) { selector = selector.replace( Expr.match.POS, "" ); } - set = Sizzle.filter( later, Sizzle( selector, context ) ); + set = Sizzle.filter( later, Sizzle( /\s$/.test(selector) ? selector + "*" : selector, context ) ); } else { set = Expr.relative[ parts[0] ] ? [ context ] : @@ -259,7 +259,7 @@ var Expr = Sizzle.selectors = { ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/, CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/, NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/, - ATTR: /\[((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\]/, + ATTR: /\[\s*((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/, TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/, CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/, POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/, @@ -269,6 +269,11 @@ var Expr = Sizzle.selectors = { "class": "className", "for": "htmlFor" }, + attrHandle: { + href: function(elem){ + return elem.getAttribute("href"); + } + }, relative: { "+": function(checkSet, part){ for ( var i = 0, l = checkSet.length; i < l; i++ ) { @@ -322,7 +327,7 @@ var Expr = Sizzle.selectors = { checkFn = dirNodeCheck; } - checkFn("parentNode", part, doneName, checkSet, nodeCheck); + checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML); }, "~": function(checkSet, part, isXML){ var doneName = "done" + (done++), checkFn = dirCheck; @@ -332,7 +337,7 @@ var Expr = Sizzle.selectors = { checkFn = dirNodeCheck; } - checkFn("previousSibling", part, doneName, checkSet, nodeCheck); + checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML); } }, find: { @@ -580,7 +585,7 @@ var Expr = Sizzle.selectors = { 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]; + var result = Expr.attrHandle[ match[1] ] ? Expr.attrHandle[ match[1] ]( elem ) : elem[ match[1] ] || elem.getAttribute( match[1] ), value = result + "", type = match[2], check = match[4]; return result == null ? false : type === "=" ? @@ -685,9 +690,10 @@ try { root.removeChild( form ); })(); -// Check to see if the browser returns only elements -// when doing getElementsByTagName("*") (function(){ + // Check to see if the browser returns only elements + // when doing getElementsByTagName("*") + // Create a fake element var div = document.createElement("div"); div.appendChild( document.createComment("") ); @@ -713,6 +719,14 @@ try { return results; }; } + + // Check to see if an attribute returns normalized href attributes + div.innerHTML = ""; + if ( div.firstChild.getAttribute("href") !== "#" ) { + Expr.attrHandle.href = function(elem){ + return elem.getAttribute("href", 2); + }; + } })(); if ( document.querySelectorAll ) (function(){ @@ -743,7 +757,7 @@ if ( document.documentElement.getElementsByClassName ) { }; } -function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck ) { +function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) { for ( var i = 0, l = checkSet.length; i < l; i++ ) { var elem = checkSet[i]; if ( elem ) { @@ -757,7 +771,7 @@ function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck ) { break; } - if ( elem.nodeType === 1 ) + if ( elem.nodeType === 1 && !isXML ) elem[doneName] = i; if ( elem.nodeName === cur ) { @@ -773,7 +787,7 @@ function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck ) { } } -function dirCheck( dir, cur, doneName, checkSet, nodeCheck ) { +function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) { for ( var i = 0, l = checkSet.length; i < l; i++ ) { var elem = checkSet[i]; if ( elem ) { @@ -787,7 +801,8 @@ function dirCheck( dir, cur, doneName, checkSet, nodeCheck ) { } if ( elem.nodeType === 1 ) { - elem[doneName] = i; + if ( !isXML ) + elem[doneName] = i; if ( typeof cur !== "string" ) { if ( elem === cur ) { diff --git a/test/index.html b/test/index.html index 30e955c5..cd5a247a 100644 --- a/test/index.html +++ b/test/index.html @@ -54,6 +54,7 @@