| <!doctype html> |
| <html> |
| <head> |
| <script src="../../resources/js-test-pre.js"></script> |
| <style> |
| #test-root * { |
| background-color: red; |
| } |
| </style> |
| <style id="style"> |
| </style> |
| </head> |
| <body> |
| <div style="display:none" id="test-root"> |
| <testcaseA id="testcase1" attribute1="value1" class="bar baz bazoo" attribute3="value3">Not empty</testcaseA> |
| <testcaseB id="testcase2" class="bar baz bazoo" attribute2="value2" attribute3="value3"></testcaseB> |
| <testcaseA id="testcase3" attribute1="value1" class="foo baz bazoo">Not empty</testcaseA> |
| <testcaseB id="testcase4" attribute1="value1" class="foo bar bazoo">Not empty</testcaseB> |
| <testcaseA id="testcase5" class="foo baz bazoo" attribute2="value2" attribute3="value3">Not empty</testcaseA> |
| <testcaseB id="testcase6" attribute1="value1" class="foo bar baz" attribute3="value3">Not empty</testcaseB> |
| <testcaseA id="testcase7" attribute1="value1" class="bar baz bazoo">Not empty</testcaseA> |
| <testcaseB id="testcase8" attribute1="value1" class="foo baz bazoo">Not empty</testcaseB> |
| <testcaseA id="testcase9" class="foo bar bazoo" attribute2="value2" attribute3="value3"></testcaseA> |
| <testcaseB id="testcase10" attribute1="value1" class="foo bar bazoo" attribute3="value3">Not empty</testcaseB> |
| <testcaseA id="testcase11" attribute1="value1" class="foo bar baz">Not empty</testcaseA> |
| <testcaseB id="testcase12" attribute1="value1" class="bar baz bazoo">Not empty</testcaseB> |
| <testcaseA id="testcase13" attribute1="value1" class="foo baz bazoo" attribute3="value3">Not empty</testcaseA> |
| <testcaseB id="testcase14" class="foo bar baz" attribute2="value2">Not empty</testcaseB> |
| <testcaseA id="testcase15" attribute1="value1" class="foo bar bazoo">Not empty</testcaseA> |
| <testcaseB id="testcase16" attribute1="value1" class="foo bar baz" attribute3="value3">Not empty</testcaseB> |
| <testcaseA id="testcase17" attribute1="value1" class="bar baz bazoo" attribute3="value3">Not empty</testcaseA> |
| <testcaseB id="testcase18" attribute1="value1" class="foo baz bazoo">Not empty</testcaseB> |
| <testcaseA id="testcase19" attribute1="value1" class="foo bar bazoo">Not empty</testcaseA> |
| <testcaseB id="testcase20" class="bar baz bazoo" attribute2="value2" attribute3="value3">Not empty</testcaseB> |
| <testcaseA id="testcase21" attribute1="value1" class="foo bar baz" attribute3="value3"></testcaseA> |
| <testcaseB id="testcase22" attribute1="value1" class="bar baz bazoo">Not empty</testcaseB> |
| <testcaseA id="testcase23" attribute1="value1" class="foo baz bazoo">Not empty</testcaseA> |
| <testcaseB id="testcase24" attribute1="value1" class="foo bar bazoo" attribute3="value3">Not empty</testcaseB> |
| <testcaseA id="testcase25" attribute1="value1" class="foo bar baz" attribute3="value3">Not empty</testcaseA> |
| <testcaseB id="testcase26" attribute1="value1" class="bar baz bazoo">Not empty</testcaseB> |
| <testcaseA id="testcase27" class="foo baz bazoo" attribute2="value2"></testcaseA> |
| <testcaseB id="testcase28" attribute1="value1" class="foo baz bazoo" attribute3="value3">Not empty</testcaseB> |
| <testcaseA id="testcase29" attribute1="value1" class="foo bar bazoo">Not empty</testcaseA> |
| <testcaseB id="testcase30" attribute1="value1" class="foo bar baz">Not empty</testcaseB> |
| </div> |
| </body> |
| <script> |
| description('Check the basic features of the ":nth-child(An+B of selectorList)" pseudo class.'); |
| |
| function testQuerySelector(selector, expectedIds) { |
| shouldBe("document.querySelectorAll('" + selector + "').length", '' + expectedIds.length); |
| for (var i = 0; i < expectedIds.length; ++i) |
| shouldBeEqualToString("document.querySelectorAll('" + selector + "')[" + i + "].id", expectedIds[i]); |
| } |
| |
| function testStyling(selector, expectedIds) { |
| var stylingElement = document.getElementById("style"); |
| stylingElement.innerHTML = '' + selector + ' { background-color: rgb(10, 100, 200); }'; |
| |
| var allTestCases = document.querySelectorAll("#test-root *"); |
| for (var i = 0; i < allTestCases.length; ++i) { |
| var expectMatch = expectedIds.indexOf(allTestCases[i].id) >= 0; |
| shouldBeEqualToString('getComputedStyle(document.querySelectorAll("#test-root *")[' + i + ']).backgroundColor', expectMatch ? 'rgb(10, 100, 200)' : 'rgb(255, 0, 0)'); |
| } |
| |
| stylingElement.innerHTML = ''; |
| } |
| |
| function testSelector(selector, expectedIds) { |
| debug("Testing \"" + selector + "\""); |
| testQuerySelector("#test-root " + selector, expectedIds); |
| testStyling("#test-root " + selector, expectedIds); |
| debug(""); |
| } |
| |
| // Should be the same as :nth-child(even). |
| testSelector(":nth-child(2n of *)", ["testcase2", "testcase4", "testcase6", "testcase8", "testcase10", "testcase12", "testcase14", "testcase16", "testcase18", "testcase20", "testcase22", "testcase24", "testcase26", "testcase28", "testcase30"]); |
| |
| /* -- Some generic tests for primitive selectors. -- */ |
| |
| // Even of a tag name. |
| testSelector(":nth-child(2n + 1 of testcaseA)", ["testcase1", "testcase5", "testcase9", "testcase13", "testcase17", "testcase21", "testcase25", "testcase29"]); |
| |
| // Attribute. |
| testSelector(":nth-child(3n - 2 of [attribute1=value1])", ["testcase1", "testcase6", "testcase10", "testcase13", "testcase17", "testcase21", "testcase24", "testcase28"]); |
| |
| // Class. |
| testSelector(":nth-child(4n - 1 of .bar)", ["testcase4", "testcase10", "testcase15", "testcase20", "testcase25"]); |
| |
| // Value. |
| testSelector(":nth-child(2n + 2 of :empty)", ["testcase9", "testcase27"]); |
| |
| |
| /* -- In practice, we expect a lot of filtering will be done on the same type. -- */ |
| |
| // Even of testcaseA. |
| testSelector("testcaseA:nth-child(even of testcaseA)", ["testcase3", "testcase7", "testcase11", "testcase15", "testcase19", "testcase23", "testcase27"]); |
| |
| // All the testcaseA with an odd number of testcaseA.bazoo before it. |
| testSelector("testcaseA:nth-child(even of testcaseA.bazoo)", ["testcase3", "testcase7", "testcase13", "testcase17", "testcase23", "testcase29"]); |
| |
| // Odd of [attribute2]. |
| testSelector("[attribute2]:nth-child(odd of [attribute2])", ["testcase2", "testcase9", "testcase20"]); |
| |
| // Even of .foo. |
| testSelector(".foo:nth-child(even of .foo)", ["testcase4", "testcase6", "testcase9", "testcase11", "testcase14", "testcase16", "testcase19", "testcase23", "testcase25", "testcase28", "testcase30"]); |
| |
| |
| /* -- Compound selectors -- */ |
| testSelector("testcaseA:nth-child(even of .bar.foo)", ["testcase21", "testcase25"]); |
| testSelector('*:nth-child(3n+1 of [id][attribute2^="value"].bazoo)', ["testcase2", "testcase20"]); |
| testSelector(':-webkit-any(testcaseA, dontExist):nth-child(n+3 of .foo:empty)', ["testcase27"]); |
| testSelector('*:nth-child(even of :nth-child(odd of [attribute2]))', ["testcase9"]); |
| </script> |
| <script src="../../resources/js-test-post.js"></script> |
| </html> |