diff --git a/src/selector.js b/src/selector.js index 42e7e415..08dee275 100644 --- a/src/selector.js +++ b/src/selector.js @@ -261,23 +261,32 @@ var Expr = Sizzle.selectors = { }, relative: { "+": function(checkSet, part, isXML){ - var isPartStr = typeof part === "string", + var isPartStr = typeof part === "string", isTag = isPartStr && !/\W/.test(part), isPartStrNotTag = isPartStr && !isTag; - if ( isTag && !isXML ) part = part.toUpperCase(); + + if ( isTag && !isXML ) { + part = part.toUpperCase(); + } + for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) { - if ( elem = checkSet[i] ) { - while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {}; + if ( (elem = checkSet[i]) ) { + while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {} + checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ? - elem : elem === part; + elem || false : + elem === part; } } - if (isPartStrNotTag) { + + if ( isPartStrNotTag ) { Sizzle.filter( part, checkSet, true ); } }, ">": function(checkSet, part, isXML){ - if ( typeof part === "string" && !/\W/.test(part) ) { + var isPartStr = typeof part === "string"; + + if ( isPartStr && !/\W/.test(part) ) { part = isXML ? part : part.toUpperCase(); for ( var i = 0, l = checkSet.length; i < l; i++ ) { @@ -291,13 +300,13 @@ var Expr = Sizzle.selectors = { for ( var i = 0, l = checkSet.length; i < l; i++ ) { var elem = checkSet[i]; if ( elem ) { - checkSet[i] = typeof part === "string" ? + checkSet[i] = isPartStr ? elem.parentNode : elem.parentNode === part; } } - if ( typeof part === "string" ) { + if ( isPartStr ) { Sizzle.filter( part, checkSet, true ); } } diff --git a/test/unit/selector.js b/test/unit/selector.js index 341d59b9..f4b5b68f 100644 --- a/test/unit/selector.js +++ b/test/unit/selector.js @@ -180,7 +180,7 @@ test("multiple", function() { }); test("child and adjacent", function() { - expect(45); + expect(48); t( "Child", "p > a", ["simon1","google","groups","mark","yahoo","simon"] ); t( "Child", "p> a", ["simon1","google","groups","mark","yahoo","simon"] ); t( "Child", "p >a", ["simon1","google","groups","mark","yahoo","simon"] ); @@ -193,6 +193,9 @@ test("child and adjacent", function() { t( "Adjacent", "a+ a", ["groups"] ); t( "Adjacent", "a+a", ["groups"] ); t( "Adjacent", "p + p", ["ap","en","sap"] ); + t( "Adjacent", "p#firstp + p", ["ap"] ); + t( "Adjacent", "p[lang=en] + p", ["sap"] ); + t( "Adjacent", "a.GROUPS + code + a", ["mark"] ); t( "Comma, Child, and Adjacent", "a + a, code > a", ["groups","anchor1","anchor2"] ); t( "Verify deep class selector", "div.blah > p > a", [] );