benjamin@webkit.org | eac4f07 | 2014-09-22 22:09:13 +0000 | [diff] [blame] | 1 | <!doctype html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <script src="../../resources/js-test-pre.js"></script> |
| 5 | <style> |
| 6 | #test-root * { |
| 7 | background-color: red; |
| 8 | } |
| 9 | </style> |
| 10 | <style id="style"> |
| 11 | </style> |
| 12 | </head> |
| 13 | <body> |
| 14 | <div style="display:none" id="test-root"> |
| 15 | <testcaseA id="testcase1" attribute1="value1" class="bar baz bazoo" attribute3="value3">Not empty</testcaseA> |
| 16 | <testcaseB id="testcase2" class="bar baz bazoo" attribute2="value2" attribute3="value3"></testcaseB> |
| 17 | <testcaseA id="testcase3" attribute1="value1" class="foo baz bazoo">Not empty</testcaseA> |
| 18 | <testcaseB id="testcase4" attribute1="value1" class="foo bar bazoo">Not empty</testcaseB> |
| 19 | <testcaseA id="testcase5" class="foo baz bazoo" attribute2="value2" attribute3="value3">Not empty</testcaseA> |
| 20 | <testcaseB id="testcase6" attribute1="value1" class="foo bar baz" attribute3="value3">Not empty</testcaseB> |
| 21 | <testcaseA id="testcase7" attribute1="value1" class="bar baz bazoo">Not empty</testcaseA> |
| 22 | <testcaseB id="testcase8" attribute1="value1" class="foo baz bazoo">Not empty</testcaseB> |
| 23 | <testcaseA id="testcase9" class="foo bar bazoo" attribute2="value2" attribute3="value3"></testcaseA> |
| 24 | <testcaseB id="testcase10" attribute1="value1" class="foo bar bazoo" attribute3="value3">Not empty</testcaseB> |
| 25 | <testcaseA id="testcase11" attribute1="value1" class="foo bar baz">Not empty</testcaseA> |
| 26 | <testcaseB id="testcase12" attribute1="value1" class="bar baz bazoo">Not empty</testcaseB> |
| 27 | <testcaseA id="testcase13" attribute1="value1" class="foo baz bazoo" attribute3="value3">Not empty</testcaseA> |
| 28 | <testcaseB id="testcase14" class="foo bar baz" attribute2="value2">Not empty</testcaseB> |
| 29 | <testcaseA id="testcase15" attribute1="value1" class="foo bar bazoo">Not empty</testcaseA> |
| 30 | <testcaseB id="testcase16" attribute1="value1" class="foo bar baz" attribute3="value3">Not empty</testcaseB> |
| 31 | <testcaseA id="testcase17" attribute1="value1" class="bar baz bazoo" attribute3="value3">Not empty</testcaseA> |
| 32 | <testcaseB id="testcase18" attribute1="value1" class="foo baz bazoo">Not empty</testcaseB> |
| 33 | <testcaseA id="testcase19" attribute1="value1" class="foo bar bazoo">Not empty</testcaseA> |
| 34 | <testcaseB id="testcase20" class="bar baz bazoo" attribute2="value2" attribute3="value3">Not empty</testcaseB> |
| 35 | <testcaseA id="testcase21" attribute1="value1" class="foo bar baz" attribute3="value3"></testcaseA> |
| 36 | <testcaseB id="testcase22" attribute1="value1" class="bar baz bazoo">Not empty</testcaseB> |
| 37 | <testcaseA id="testcase23" attribute1="value1" class="foo baz bazoo">Not empty</testcaseA> |
| 38 | <testcaseB id="testcase24" attribute1="value1" class="foo bar bazoo" attribute3="value3">Not empty</testcaseB> |
| 39 | <testcaseA id="testcase25" attribute1="value1" class="foo bar baz" attribute3="value3">Not empty</testcaseA> |
| 40 | <testcaseB id="testcase26" attribute1="value1" class="bar baz bazoo">Not empty</testcaseB> |
| 41 | <testcaseA id="testcase27" class="foo baz bazoo" attribute2="value2"></testcaseA> |
| 42 | <testcaseB id="testcase28" attribute1="value1" class="foo baz bazoo" attribute3="value3">Not empty</testcaseB> |
| 43 | <testcaseA id="testcase29" attribute1="value1" class="foo bar bazoo">Not empty</testcaseA> |
| 44 | <testcaseB id="testcase30" attribute1="value1" class="foo bar baz">Not empty</testcaseB> |
| 45 | </div> |
| 46 | </body> |
| 47 | <script> |
| 48 | description('Check the basic features of the ":nth-child(An+B of selectorList)" pseudo class.'); |
| 49 | |
| 50 | function testQuerySelector(selector, expectedIds) { |
| 51 | shouldBe("document.querySelectorAll('" + selector + "').length", '' + expectedIds.length); |
| 52 | for (var i = 0; i < expectedIds.length; ++i) |
| 53 | shouldBeEqualToString("document.querySelectorAll('" + selector + "')[" + i + "].id", expectedIds[i]); |
| 54 | } |
| 55 | |
| 56 | function testStyling(selector, expectedIds) { |
| 57 | var stylingElement = document.getElementById("style"); |
| 58 | stylingElement.innerHTML = '' + selector + ' { background-color: rgb(10, 100, 200); }'; |
| 59 | |
| 60 | var allTestCases = document.querySelectorAll("#test-root *"); |
| 61 | for (var i = 0; i < allTestCases.length; ++i) { |
| 62 | var expectMatch = expectedIds.indexOf(allTestCases[i].id) >= 0; |
| 63 | shouldBeEqualToString('getComputedStyle(document.querySelectorAll("#test-root *")[' + i + ']).backgroundColor', expectMatch ? 'rgb(10, 100, 200)' : 'rgb(255, 0, 0)'); |
| 64 | } |
| 65 | |
| 66 | stylingElement.innerHTML = ''; |
| 67 | } |
| 68 | |
| 69 | function testSelector(selector, expectedIds) { |
| 70 | debug("Testing \"" + selector + "\""); |
| 71 | testQuerySelector("#test-root " + selector, expectedIds); |
| 72 | testStyling("#test-root " + selector, expectedIds); |
| 73 | debug(""); |
| 74 | } |
| 75 | |
| 76 | // Should be the same as :nth-child(even). |
| 77 | testSelector(":nth-child(2n of *)", ["testcase2", "testcase4", "testcase6", "testcase8", "testcase10", "testcase12", "testcase14", "testcase16", "testcase18", "testcase20", "testcase22", "testcase24", "testcase26", "testcase28", "testcase30"]); |
| 78 | |
| 79 | /* -- Some generic tests for primitive selectors. -- */ |
| 80 | |
| 81 | // Even of a tag name. |
benjamin@webkit.org | e43e034 | 2014-10-10 21:23:37 +0000 | [diff] [blame] | 82 | testSelector(":nth-child(2n + 1 of testcaseA)", ["testcase1", "testcase5", "testcase9", "testcase13", "testcase17", "testcase21", "testcase25", "testcase29"]); |
benjamin@webkit.org | eac4f07 | 2014-09-22 22:09:13 +0000 | [diff] [blame] | 83 | |
| 84 | // Attribute. |
benjamin@webkit.org | e43e034 | 2014-10-10 21:23:37 +0000 | [diff] [blame] | 85 | testSelector(":nth-child(3n - 2 of [attribute1=value1])", ["testcase1", "testcase6", "testcase10", "testcase13", "testcase17", "testcase21", "testcase24", "testcase28"]); |
benjamin@webkit.org | eac4f07 | 2014-09-22 22:09:13 +0000 | [diff] [blame] | 86 | |
| 87 | // Class. |
benjamin@webkit.org | e43e034 | 2014-10-10 21:23:37 +0000 | [diff] [blame] | 88 | testSelector(":nth-child(4n - 1 of .bar)", ["testcase4", "testcase10", "testcase15", "testcase20", "testcase25"]); |
benjamin@webkit.org | eac4f07 | 2014-09-22 22:09:13 +0000 | [diff] [blame] | 89 | |
| 90 | // Value. |
benjamin@webkit.org | e43e034 | 2014-10-10 21:23:37 +0000 | [diff] [blame] | 91 | testSelector(":nth-child(2n + 2 of :empty)", ["testcase9", "testcase27"]); |
benjamin@webkit.org | eac4f07 | 2014-09-22 22:09:13 +0000 | [diff] [blame] | 92 | |
| 93 | |
| 94 | /* -- In practice, we expect a lot of filtering will be done on the same type. -- */ |
| 95 | |
| 96 | // Even of testcaseA. |
| 97 | testSelector("testcaseA:nth-child(even of testcaseA)", ["testcase3", "testcase7", "testcase11", "testcase15", "testcase19", "testcase23", "testcase27"]); |
| 98 | |
| 99 | // All the testcaseA with an odd number of testcaseA.bazoo before it. |
benjamin@webkit.org | e43e034 | 2014-10-10 21:23:37 +0000 | [diff] [blame] | 100 | testSelector("testcaseA:nth-child(even of testcaseA.bazoo)", ["testcase3", "testcase7", "testcase13", "testcase17", "testcase23", "testcase29"]); |
benjamin@webkit.org | eac4f07 | 2014-09-22 22:09:13 +0000 | [diff] [blame] | 101 | |
| 102 | // Odd of [attribute2]. |
| 103 | testSelector("[attribute2]:nth-child(odd of [attribute2])", ["testcase2", "testcase9", "testcase20"]); |
| 104 | |
| 105 | // Even of .foo. |
| 106 | testSelector(".foo:nth-child(even of .foo)", ["testcase4", "testcase6", "testcase9", "testcase11", "testcase14", "testcase16", "testcase19", "testcase23", "testcase25", "testcase28", "testcase30"]); |
| 107 | |
| 108 | |
| 109 | /* -- Compound selectors -- */ |
benjamin@webkit.org | e43e034 | 2014-10-10 21:23:37 +0000 | [diff] [blame] | 110 | testSelector("testcaseA:nth-child(even of .bar.foo)", ["testcase21", "testcase25"]); |
| 111 | testSelector('*:nth-child(3n+1 of [id][attribute2^="value"].bazoo)', ["testcase2", "testcase20"]); |
| 112 | testSelector(':-webkit-any(testcaseA, dontExist):nth-child(n+3 of .foo:empty)', ["testcase27"]); |
| 113 | testSelector('*:nth-child(even of :nth-child(odd of [attribute2]))', ["testcase9"]); |
benjamin@webkit.org | eac4f07 | 2014-09-22 22:09:13 +0000 | [diff] [blame] | 114 | </script> |
| 115 | <script src="../../resources/js-test-post.js"></script> |
| 116 | </html> |