Update :nth-child(An+B of selector-list) to the latest specification
https://bugs.webkit.org/show_bug.cgi?id=137593

Reviewed by Andreas Kling.

Source/WebCore:

Following my implementation feedback on :nth-child(An+B of selector-list),
the CSS WG decided to change the definition such that the current element on which
the selector is matched must also match the selector list in :nth-child().

This patch updates the implementation accordingly.

Test: fast/selectors/nth-child-of-chained-3.html

* css/SelectorChecker.cpp:
(WebCore::SelectorChecker::checkOne):
(WebCore::SelectorChecker::matchSelectorList):
* css/SelectorChecker.h:
* cssjit/SelectorCompiler.cpp:
(WebCore::SelectorCompiler::SelectorCodeGenerator::generateElementMatchesSelectorList):
(WebCore::SelectorCompiler::SelectorCodeGenerator::generateElementIsNthChildOf):

LayoutTests:

* fast/selectors/nth-child-of-chained-3-expected.txt: Added.
* fast/selectors/nth-child-of-chained-3.html: Added.
This is an interesting new case with the new definition. Since the initial
element must also match the selector, it is now possible to fail even
if the combination An+B can match anything.

* fast/css/nth-child-of-classname-expected.html:
* fast/css/nth-child-of-classname.html:
* fast/css/nth-child-of-compound-selector-expected.html:
* fast/css/nth-child-of-compound-selector.html:
* fast/css/nth-child-of-tagname-expected.html:
* fast/css/nth-child-of-tagname.html:
* fast/selectors/nth-child-of-backtracking-adjacent-2.html:
* fast/selectors/nth-child-of-backtracking-adjacent.html:
* fast/selectors/nth-child-of-basics-2-expected.txt:
* fast/selectors/nth-child-of-basics-2.html:
* fast/selectors/nth-child-of-basics-expected.txt:
* fast/selectors/nth-child-of-basics.html:
* fast/selectors/nth-child-of-chained-2-expected.txt:
* fast/selectors/nth-child-of-chained-2.html:
* fast/selectors/nth-child-of-complex-selectors-expected.txt:
* fast/selectors/nth-child-of-complex-selectors.html:
* fast/selectors/nth-child-of-never-matching-selector-expected.txt:
* fast/selectors/nth-child-of-never-matching-selector.html:
* fast/selectors/nth-child-of-selector-list-expected.txt:
* fast/selectors/nth-child-of-selector-list.html:
Updating the test and tests result for the new definition.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@174613 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index 3341247..f31be8f 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,38 @@
+2014-10-10  Benjamin Poulain  <benjamin@webkit.org>
+
+        Update :nth-child(An+B of selector-list) to the latest specification
+        https://bugs.webkit.org/show_bug.cgi?id=137593
+
+        Reviewed by Andreas Kling.
+
+        * fast/selectors/nth-child-of-chained-3-expected.txt: Added.
+        * fast/selectors/nth-child-of-chained-3.html: Added.
+        This is an interesting new case with the new definition. Since the initial
+        element must also match the selector, it is now possible to fail even
+        if the combination An+B can match anything.
+
+        * fast/css/nth-child-of-classname-expected.html:
+        * fast/css/nth-child-of-classname.html:
+        * fast/css/nth-child-of-compound-selector-expected.html:
+        * fast/css/nth-child-of-compound-selector.html:
+        * fast/css/nth-child-of-tagname-expected.html:
+        * fast/css/nth-child-of-tagname.html:
+        * fast/selectors/nth-child-of-backtracking-adjacent-2.html:
+        * fast/selectors/nth-child-of-backtracking-adjacent.html:
+        * fast/selectors/nth-child-of-basics-2-expected.txt:
+        * fast/selectors/nth-child-of-basics-2.html:
+        * fast/selectors/nth-child-of-basics-expected.txt:
+        * fast/selectors/nth-child-of-basics.html:
+        * fast/selectors/nth-child-of-chained-2-expected.txt:
+        * fast/selectors/nth-child-of-chained-2.html:
+        * fast/selectors/nth-child-of-complex-selectors-expected.txt:
+        * fast/selectors/nth-child-of-complex-selectors.html:
+        * fast/selectors/nth-child-of-never-matching-selector-expected.txt:
+        * fast/selectors/nth-child-of-never-matching-selector.html:
+        * fast/selectors/nth-child-of-selector-list-expected.txt:
+        * fast/selectors/nth-child-of-selector-list.html:
+        Updating the test and tests result for the new definition.
+
 2014-10-10  Andy Estes  <aestes@apple.com>
 
         [Mac] Update a Mavericks-specific baseline.
diff --git a/LayoutTests/fast/css/nth-child-of-classname-expected.html b/LayoutTests/fast/css/nth-child-of-classname-expected.html
index 1633d88..b424e73b 100644
--- a/LayoutTests/fast/css/nth-child-of-classname-expected.html
+++ b/LayoutTests/fast/css/nth-child-of-classname-expected.html
@@ -3,13 +3,13 @@
 <body>
     <p>This test the styling of paragraphs with :nth-child(An+B of selector). If the test succeed, paragraphs that say "green" should have a green background.</p>
     <p class="webkit">White</p>
-    <p style="background-color: lime">Green!</p>
-    <p style="background-color: lime">Green!</p>
+    <p>White</p>
+    <p>White</p>
     <p style="background-color: lime">Green!</p>
     <p>White</p>
     <p>White</p>
     <p>White</p>
-    <p style="background-color: lime">Green!</p>
+    <p>White</p>
     <p style="background-color: lime">Green!</p>
     <p>White</p>
 </body>
diff --git a/LayoutTests/fast/css/nth-child-of-classname.html b/LayoutTests/fast/css/nth-child-of-classname.html
index df7d8d0..0af99d5 100644
--- a/LayoutTests/fast/css/nth-child-of-classname.html
+++ b/LayoutTests/fast/css/nth-child-of-classname.html
@@ -10,13 +10,13 @@
 <body>
     <p>This test the styling of paragraphs with :nth-child(An+B of selector). If the test succeed, paragraphs that say "green" should have a green background.</p>
     <p class="webkit">White</p>
-    <p>Green!</p>
-    <p>Green!</p>
+    <p>White</p>
+    <p>White</p>
     <p class="fast">Green!</p>
     <p>White</p>
     <p>White</p>
     <p class="webkit">White</p>
-    <p>Green!</p>
+    <p>White</p>
     <p class="webkit">Green!</p>
     <p>White</p>
 </body>
diff --git a/LayoutTests/fast/css/nth-child-of-compound-selector-expected.html b/LayoutTests/fast/css/nth-child-of-compound-selector-expected.html
index f97a9df..3acec23 100644
--- a/LayoutTests/fast/css/nth-child-of-compound-selector-expected.html
+++ b/LayoutTests/fast/css/nth-child-of-compound-selector-expected.html
@@ -7,12 +7,12 @@
     <p>White!</p>
     <p>White!</p>
     <p>White!</p>
-    <p style="background-color: lime">Green</p>
-    <p style="background-color: lime">Green</p>
-    <p>White!</p>
-    <p>White!</p>
-    <p>White!</p>
     <p>White!</p>
     <p style="background-color: lime">Green</p>
+    <p>White!</p>
+    <p>White!</p>
+    <p>White!</p>
+    <p>White!</p>
+    <p>White!</p>
 </body>
 </html>
diff --git a/LayoutTests/fast/css/nth-child-of-compound-selector.html b/LayoutTests/fast/css/nth-child-of-compound-selector.html
index 07695c7..de7e109 100644
--- a/LayoutTests/fast/css/nth-child-of-compound-selector.html
+++ b/LayoutTests/fast/css/nth-child-of-compound-selector.html
@@ -15,12 +15,12 @@
     <p class="foobar foo">White!</p>
     <p>White!</p>
     <p class="foo foobar">White!</p>
-    <p>Green</p>
+    <p>White!</p>
     <p class="foobar foo">Green</p>
     <p class="foobar">White!</p>
     <p class="foo foobar">White!</p>
     <p class="foobar">White!</p>
     <p class="foo foobar baz">White!</p>
-    <p>Green</p>
+    <p>White!</p>
 </body>
 </html>
diff --git a/LayoutTests/fast/css/nth-child-of-tagname-expected.html b/LayoutTests/fast/css/nth-child-of-tagname-expected.html
index 6201738..99c8262 100644
--- a/LayoutTests/fast/css/nth-child-of-tagname-expected.html
+++ b/LayoutTests/fast/css/nth-child-of-tagname-expected.html
@@ -6,11 +6,11 @@
     <p>White!</p>
     <p>White!</p>
     <fast>White!</fast>
-    <p style="background-color: lime">Green</p>
-    <p style="background-color: lime">Green</p>
-    <webkit>White!</webkit>
+    <p>White!</p>
+    <p>White!</p>
+    <webkit style="background-color: lime">Green</webkit>
     <p>White!</p>
     <webkit>White!</webkit>
-    <p style="background-color: lime">Green</p>
+    <p>White!</p>
 </body>
 </html>
diff --git a/LayoutTests/fast/css/nth-child-of-tagname.html b/LayoutTests/fast/css/nth-child-of-tagname.html
index 5c9feb8..c94cfdb 100644
--- a/LayoutTests/fast/css/nth-child-of-tagname.html
+++ b/LayoutTests/fast/css/nth-child-of-tagname.html
@@ -2,7 +2,7 @@
 <html>
 <head>
 <style>
-p:nth-child(odd of webkit, fast) {
+:nth-child(odd of webkit, fast) {
     background-color: lime;
 }
 </style>
@@ -13,11 +13,11 @@
     <p>White!</p>
     <p>White!</p>
     <fast>White!</fast>
-    <p>Green</p>
-    <p>Green</p>
-    <webkit>White!</webkit>
+    <p>White!</p>
+    <p>White!</p>
+    <webkit>Green</webkit>
     <p>White!</p>
     <webkit>White!</webkit>
-    <p>Green</p>
+    <p>White!</p>
 </body>
 </html>
diff --git a/LayoutTests/fast/selectors/nth-child-of-backtracking-adjacent-2.html b/LayoutTests/fast/selectors/nth-child-of-backtracking-adjacent-2.html
index cf4a169..1729c23 100644
--- a/LayoutTests/fast/selectors/nth-child-of-backtracking-adjacent-2.html
+++ b/LayoutTests/fast/selectors/nth-child-of-backtracking-adjacent-2.html
@@ -3,65 +3,65 @@
 <head>
 <script src="../../resources/js-test-pre.js"></script>
 <style>
-span.target {
+sibling.target {
     background-color:rgb(0,0,0);
 }
-span.target:nth-child(n+2 of a + b sibling) {
+sibling.target:nth-child(n+2 of a + b sibling) {
     background-color:rgb(1,2,3);
 }
-span.target:nth-child(n+2 of c + c + d sibling) {
+sibling.target:nth-child(n+2 of c + c + d sibling) {
     background-color:rgb(4,5,6);
 }
-span.target:nth-child(n+2 of e + f ~ g sibling) {
+sibling.target:nth-child(n+2 of e + f ~ g sibling) {
     background-color:rgb(7,8,9);
 }
-span.target:nth-child(n+2 of h + sibling) {
+sibling.target:nth-child(n+2 of h + sibling) {
     background-color:rgb(10,11,12);
 }
-span.target:nth-child(n+2 of j+j+j~j>k sibling) {
+sibling.target:nth-child(n+2 of j+j+j~j>k sibling) {
     background-color:rgb(13,14,15);
 }
-span.target:nth-child(n+2 of m l+l+l+l~m>m>m sibling) {
+sibling.target:nth-child(n+2 of m l+l+l+l~m>m>m sibling) {
     background-color:rgb(16,17,18);
 }
-span.target:nth-child(n+2 of n o~n>n>n sibling) {
+sibling.target:nth-child(n+2 of n o~n>n>n sibling) {
     background-color:rgb(19,20,21);
 }
-span.target:nth-child(n+2 of p ~ sibling) {
+sibling.target:nth-child(n+2 of p ~ sibling) {
     background-color:rgb(22,23,24);
 }
-span.target:nth-child(n+2 of q ~ r sibling) {
+sibling.target:nth-child(n+2 of q ~ r sibling) {
     background-color:rgb(25,26,27);
 }
-span.target:nth-child(n+2 of s t+t+t+s>s>s sibling) {
+sibling.target:nth-child(n+2 of s t+t+t+s>s>s sibling) {
     background-color:rgb(28,29,30);
 }
 </style>
 </head>
 <body>
 <div style="display:none">
-    <!-- span.target:nth-child(n+2 of a + b sibling) -->
+    <!-- sibling.target:nth-child(n+2 of a + b sibling) -->
     <target1>
         <a></a>
         <b>
             <sibling></sibling>
-            <span class="target" id="target1"></span>
+            <sibling class="target" id="target1"></sibling>
         </b>
     </target1>
 
-    <!-- span.target:nth-child(n+2 of a + b sibling) -->
+    <!-- sibling.target:nth-child(n+2 of a + b sibling) -->
     <target2>
         <a></a>
         <b>
             <b></b>  <!-- Fail here and restart backtracking. -->
             <b>
                 <sibling></sibling>
-                <span class="target" id="target2"></span>
+                <sibling class="target" id="target2"></sibling>
             </b>
         </b>
     </target2>
 
-    <!-- span.target:nth-child(n+2 of c + c + d sibling) -->
+    <!-- sibling.target:nth-child(n+2 of c + c + d sibling) -->
     <target3>
         <c></c>
         <c></c>
@@ -73,13 +73,13 @@
                 <c></c>
                 <d>
                     <sibling></sibling>
-                    <span class="target" id="target3"></span>
+                    <sibling class="target" id="target3"></sibling>
                 </d>
             </d>
         </d>
     </target3>
 
-    <!-- span.target:nth-child(n+2 of e + f ~ g sibling) -->
+    <!-- sibling.target:nth-child(n+2 of e + f ~ g sibling) -->
     <target4>
         <d></d>
         <e></e>
@@ -91,32 +91,33 @@
             <f></f>
             <g>
                 <sibling></sibling>
-                <span class="target" id="target4"></span>
+                <sibling class="target" id="target4"></sibling>
             </g>
         </g>
     </target4>
 
-    <!-- span.target:nth-child(n+2 of j+j+j~j>k sibling) -->
+    <!-- sibling.target:nth-child(n+2 of j+j+j~j>k sibling) -->
     <target6>
         <h></h>
         <sibling></sibling>
-        <span class="target" id="target6.1"></span>
+        <h></h>        
+        <sibling class="target" id="target6.1"></sibling>
     </target6>
 
-    <!-- span.target:nth-child(n+2 of j+j+j~j>k sibling) -->
+    <!-- sibling.target:nth-child(n+2 of j+j+j~j>k sibling) -->
     <target6>
         <a></a>
         <sibling></sibling>
-        <span class="target" id="target6.2"></span>
+        <sibling class="target" id="target6.2"></sibling>
     </target6>
 
-    <!-- span.target:nth-child(n+2 of j+j+j~j>k sibling) -->
+    <!-- sibling.target:nth-child(n+2 of j+j+j~j>k sibling) -->
     <target6>
         <sibling></sibling>
-        <span class="target" id="target6.3"></span>
+        <sibling class="target" id="target6.3"></sibling>
     </target6>
 
-    <!-- span.target:nth-child(n+2 of j+j+j~j>k sibling) -->
+    <!-- sibling.target:nth-child(n+2 of j+j+j~j>k sibling) -->
     <target7>
         <d></d>  <!-- Fail here. -->
         <j></j>
@@ -124,11 +125,11 @@
         <j></j>
         <k>
             <sibling></sibling>
-            <span class="target" id="target7"></span>
+            <sibling class="target" id="target7"></sibling>
         </k>
     </target7>
 
-    <!-- span.target:nth-child(n+2 of j+j+j~j>k sibling) -->
+    <!-- sibling.target:nth-child(n+2 of j+j+j~j>k sibling) -->
     <target8>
         <j></j>  <!-- Match here. -->
         <j></j>
@@ -140,12 +141,12 @@
         <j>
             <k>
                 <sibling></sibling>
-                <span class="target" id="target8"></span>
+                <sibling class="target" id="target8"></sibling>
             </k>
         </j>
     </target8>
 
-    <!-- span.target:nth-child(n+2 of m l+l+l+l~m>m>m sibling) -->
+    <!-- sibling.target:nth-child(n+2 of m l+l+l+l~m>m>m sibling) -->
     <target9>
         <m>
             <l></l>  <!-- Fail here -->
@@ -161,7 +162,7 @@
                     <m>
                         <m>
                             <sibling></sibling>
-                            <span class="target" id="target9"></span>
+                            <sibling class="target" id="target9"></sibling>
                         </m>
                     </m>
                 </m>
@@ -169,7 +170,7 @@
         </m>
     </target9>
 
-    <!-- span.target:nth-child(n+2 of m l+l+l+l~m>m>m sibling) -->
+    <!-- sibling.target:nth-child(n+2 of m l+l+l+l~m>m>m sibling) -->
     <target10>
         <m>
             <l></l>  <!-- Match here -->
@@ -186,7 +187,7 @@
                     <m>
                         <m>
                             <sibling></sibling>
-                            <span class="target" id="target10"></span>
+                            <sibling class="target" id="target10"></sibling>
                         </m>
                     </m>
                 </m>
@@ -194,7 +195,7 @@
         </m>
     </target10>
 
-    <!-- span.target:nth-child(n+2 of n o~n>n>n sibling) -->
+    <!-- sibling.target:nth-child(n+2 of n o~n>n>n sibling) -->
     <target11>
         <n>
             <a></a>  <!-- Fail here -->
@@ -205,7 +206,7 @@
                     <n>
                         <n>
                             <sibling></sibling>
-                            <span class="target" id="target11"></span>
+                            <sibling class="target" id="target11"></sibling>
                         </n>
                     </n>
                 </n>
@@ -213,7 +214,7 @@
         </n>
     </target11>
 
-    <!-- span.target:nth-child(n+2 of n o~n>n>n sibling) -->
+    <!-- sibling.target:nth-child(n+2 of n o~n>n>n sibling) -->
     <target12>
         <n>
             <o></o>  <!-- Match here -->
@@ -225,7 +226,7 @@
                     <n>
                         <n>
                             <sibling></sibling>
-                            <span class="target" id="target12"></span>
+                            <sibling class="target" id="target12"></sibling>
                         </n>
                     </n>
                 </n>
@@ -233,28 +234,28 @@
         </n>
     </target12>
 
-    <!-- span.target:nth-child(n+2 of p ~ sibling) -->
+    <!-- sibling.target:nth-child(n+2 of p ~ sibling) -->
     <target13>
         <p></p>  <!-- Match here -->
         <sibling></sibling>
-        <span class="target" id="target13.1"></span>
+        <sibling class="target" id="target13.1"></sibling>
     </target13>
 
-    <!-- span.target:nth-child(n+2 of p ~ sibling) -->
+    <!-- sibling.target:nth-child(n+2 of p ~ sibling) -->
     <target13>
         <a></a>  <!-- Fail here -->
         <sibling></sibling>
-        <span class="target" id="target13.2"></span>
+        <sibling class="target" id="target13.2"></sibling>
     </target13>
 
-    <!-- span.target:nth-child(n+2 of p ~ sibling) -->
+    <!-- sibling.target:nth-child(n+2 of p ~ sibling) -->
     <target13>
         <!-- Fail here -->
         <sibling></sibling>
-        <span class="target" id="target13.3"></span>
+        <sibling class="target" id="target13.3"></sibling>
     </target13>
 
-    <!-- span.target:nth-child(n+2 of q ~ r sibling) -->
+    <!-- sibling.target:nth-child(n+2 of q ~ r sibling) -->
     <target14>
         <q></q>  <!-- Match here -->
         <a></a>
@@ -262,36 +263,36 @@
             <a></a>  <!-- Fail here -->
             <r>
                 <sibling></sibling>
-                <span class="target" id="target14.1"></span>
+                <sibling class="target" id="target14.1"></sibling>
             </r>
         </r>
     </target14>
 
-    <!-- span.target:nth-child(n+2 of q ~ r sibling) -->
+    <!-- sibling.target:nth-child(n+2 of q ~ r sibling) -->
     <target14>
         <!-- Fail here -->
         <r>
             <a></a>  <!-- Fail here -->
             <r>
                 <sibling></sibling>
-                <span class="target" id="target14.2"></span>
+                <sibling class="target" id="target14.2"></sibling>
             </r>
         </r>
     </target14>
 
-    <!-- span.target:nth-child(n+2 of q ~ r sibling) -->
+    <!-- sibling.target:nth-child(n+2 of q ~ r sibling) -->
     <target14>
         <!-- Fail here -->
         <r>
             <!-- Fail here -->
             <r>
                 <sibling></sibling>
-                <span class="target" id="target14.3"></span>
+                <sibling class="target" id="target14.3"></sibling>
             </r>
         </r>
     </target14>
 
-    <!-- span.target:nth-child(n+2 of s t+t+t+s>s>s sibling) -->
+    <!-- sibling.target:nth-child(n+2 of s t+t+t+s>s>s sibling) -->
     <target15>
         <s>
             <!-- Fail here and backtrack with the tail -->
@@ -305,7 +306,7 @@
                     <s>
                         <s>
                             <sibling></sibling>
-                            <span class="target" id="target15"></span>
+                            <sibling class="target" id="target15"></sibling>
                         </s>
                     </s>
                 </s>
@@ -313,7 +314,7 @@
         </s>
     </target15>
 
-    <!-- span.target:nth-child(n+2 of s t+t+t+s>s>s sibling) -->
+    <!-- sibling.target:nth-child(n+2 of s t+t+t+s>s>s sibling) -->
     <target16>
         <s>
             <t></t>  <!-- Match here -->
@@ -331,7 +332,7 @@
                         <s>
                             <s>
                                 <sibling></sibling>
-                                <span class="target" id="target16"></span>
+                                <sibling class="target" id="target16"></sibling>
                             </s>
                         </s>
                     </s>
diff --git a/LayoutTests/fast/selectors/nth-child-of-backtracking-adjacent.html b/LayoutTests/fast/selectors/nth-child-of-backtracking-adjacent.html
index ad06cf1..c9eaf41 100644
--- a/LayoutTests/fast/selectors/nth-child-of-backtracking-adjacent.html
+++ b/LayoutTests/fast/selectors/nth-child-of-backtracking-adjacent.html
@@ -8,31 +8,31 @@
     color:rgb(0,0,0);
 }
 
-a + b:nth-child(n+2 of a + a) span.target {
+a + :nth-child(n+1 of a + a ~ b) span.target {
     background-color:rgb(1,2,3);
 }
-b:nth-child(n+2 of a + a) span.target {
+b:nth-child(n+1 of a + a ~ b) span.target {
     color:rgb(1,2,3);
 }
 
-d:nth-child(n+2 of c + c + c) span.target {
+:nth-child(n+1 of c + c + c ~ d) span.target {
     background-color:rgb(4,5,6);
 }
-c + c + c + d:nth-child(n+2 of c + c + c) span.target {
+c + c + c + d:nth-child(n+1 of c + c + c ~ d) span.target {
     color:rgb(4,5,6);
 }
 
-f:nth-child(n+2 of e + e) ~ g span.target {
+f:nth-child(n+1 of e + e ~ f) ~ g span.target {
     background-color:rgb(7,8,9);
 }
-e + f:nth-child(n+2 of e + e) ~ g span.target {
+e + f:nth-child(n+1 of e + e ~ f) ~ g span.target {
     color:rgb(7,8,9);
 }
 
-span.target:nth-child(n+2 of h + h) {
+span.target:nth-child(n+1 of h + h ~ span.target) {
     background-color:rgb(10,11,12);
 }
-h + span.target:nth-child(n+2 of h + h) {
+h + span.target:nth-child(n+1 of h + h ~ span.target) {
     color:rgb(10,11,12);
 }
 
@@ -62,10 +62,10 @@
     background-color:rgb(25,26,27);
 }
 
-s s:nth-child(n+2 of t+t+t)>s>s span.target {
+s s:nth-child(n+1 of t+t+t+s)>s>s span.target {
     background-color:rgb(28,29,30);
 }
-s t+t+t+s:nth-child(n+2 of t+t+t)>s>s span.target {
+s t+t+t+s:nth-child(n+1 of t+t+t+s)>s>s span.target {
     color:rgb(28,29,30);
 }
 </style>
diff --git a/LayoutTests/fast/selectors/nth-child-of-basics-2-expected.txt b/LayoutTests/fast/selectors/nth-child-of-basics-2-expected.txt
index e2cb338..c04dc99 100644
--- a/LayoutTests/fast/selectors/nth-child-of-basics-2-expected.txt
+++ b/LayoutTests/fast/selectors/nth-child-of-basics-2-expected.txt
@@ -361,13 +361,12 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(3 of testcaseA)"
-PASS document.querySelectorAll('#test-root :nth-child(3 of testcaseA)').length is 2
-PASS document.querySelectorAll('#test-root :nth-child(3 of testcaseA)')[0].id is "testcase4"
-PASS document.querySelectorAll('#test-root :nth-child(3 of testcaseA)')[1].id is "testcase5"
+PASS document.querySelectorAll('#test-root :nth-child(3 of testcaseA)').length is 1
+PASS document.querySelectorAll('#test-root :nth-child(3 of testcaseA)')[0].id is "testcase5"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
@@ -430,16 +429,15 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(4 of testcaseB)"
-PASS document.querySelectorAll('#test-root :nth-child(4 of testcaseB)').length is 2
-PASS document.querySelectorAll('#test-root :nth-child(4 of testcaseB)')[0].id is "testcase7"
-PASS document.querySelectorAll('#test-root :nth-child(4 of testcaseB)')[1].id is "testcase8"
+PASS document.querySelectorAll('#test-root :nth-child(4 of testcaseB)').length is 1
+PASS document.querySelectorAll('#test-root :nth-child(4 of testcaseB)')[0].id is "testcase8"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
@@ -511,64 +509,50 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(n+3 of testcaseA)"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)').length is 27
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[0].id is "testcase4"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[1].id is "testcase5"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[2].id is "testcase6"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[3].id is "testcase7"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[4].id is "testcase8"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[5].id is "testcase9"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[6].id is "testcase10"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[7].id is "testcase11"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[8].id is "testcase12"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[9].id is "testcase13"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[10].id is "testcase14"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[11].id is "testcase15"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[12].id is "testcase16"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[13].id is "testcase17"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[14].id is "testcase18"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[15].id is "testcase19"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[16].id is "testcase20"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[17].id is "testcase21"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[18].id is "testcase22"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[19].id is "testcase23"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[20].id is "testcase24"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[21].id is "testcase25"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[22].id is "testcase26"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[23].id is "testcase27"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[24].id is "testcase28"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[25].id is "testcase29"
-PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[26].id is "testcase30"
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)').length is 13
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[0].id is "testcase5"
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[1].id is "testcase7"
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[2].id is "testcase9"
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[3].id is "testcase11"
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[4].id is "testcase13"
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[5].id is "testcase15"
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[6].id is "testcase17"
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[7].id is "testcase19"
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[8].id is "testcase21"
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[9].id is "testcase23"
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[10].id is "testcase25"
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[11].id is "testcase27"
+PASS document.querySelectorAll('#test-root :nth-child(n+3 of testcaseA)')[12].id is "testcase29"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing "testcaseA:nth-child(2n+2 of testcaseA)"
 PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n+2 of testcaseA)').length is 7
diff --git a/LayoutTests/fast/selectors/nth-child-of-basics-2.html b/LayoutTests/fast/selectors/nth-child-of-basics-2.html
index 7ce7afd..2f8882e 100644
--- a/LayoutTests/fast/selectors/nth-child-of-basics-2.html
+++ b/LayoutTests/fast/selectors/nth-child-of-basics-2.html
@@ -85,12 +85,12 @@
 
 // Test cases inspired by the :nth-of-type() examples from http://nthmaster.com/.
 testSelector("testcaseA:nth-child(3 of testcaseA)", ["testcase5"]);
-testSelector(":nth-child(3 of testcaseA)", ["testcase4", "testcase5"]);
+testSelector(":nth-child(3 of testcaseA)", ["testcase5"]);
 testSelector("testcaseB:nth-child(4 of testcaseB)", ["testcase8"]);
-testSelector(":nth-child(4 of testcaseB)", ["testcase7", "testcase8"]);
+testSelector(":nth-child(4 of testcaseB)", ["testcase8"]);
 
 testSelector("testcaseA:nth-child(n+3 of testcaseA)", ["testcase5", "testcase7", "testcase9", "testcase11", "testcase13", "testcase15", "testcase17", "testcase19", "testcase21", "testcase23", "testcase25", "testcase27", "testcase29"]);
-testSelector(":nth-child(n+3 of testcaseA)", ["testcase4", "testcase5", "testcase6", "testcase7", "testcase8", "testcase9", "testcase10", "testcase11", "testcase12", "testcase13", "testcase14", "testcase15", "testcase16", "testcase17", "testcase18", "testcase19", "testcase20", "testcase21", "testcase22", "testcase23", "testcase24", "testcase25", "testcase26", "testcase27", "testcase28", "testcase29", "testcase30"]);
+testSelector(":nth-child(n+3 of testcaseA)", ["testcase5", "testcase7", "testcase9", "testcase11", "testcase13", "testcase15", "testcase17", "testcase19", "testcase21", "testcase23", "testcase25", "testcase27", "testcase29"]);
 
 testSelector("testcaseA:nth-child(2n+2 of testcaseA)", ["testcase3", "testcase7", "testcase11", "testcase15", "testcase19", "testcase23", "testcase27"]);
 
diff --git a/LayoutTests/fast/selectors/nth-child-of-basics-expected.txt b/LayoutTests/fast/selectors/nth-child-of-basics-expected.txt
index 029393a..de0cb19 100644
--- a/LayoutTests/fast/selectors/nth-child-of-basics-expected.txt
+++ b/LayoutTests/fast/selectors/nth-child-of-basics-expected.txt
@@ -52,76 +52,65 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(10, 100, 200)"
 
 Testing ":nth-child(2n + 1 of testcaseA)"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)').length is 15
+PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)').length is 8
 PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[0].id is "testcase1"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[1].id is "testcase4"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[2].id is "testcase5"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[3].id is "testcase8"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[4].id is "testcase9"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[5].id is "testcase12"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[6].id is "testcase13"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[7].id is "testcase16"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[8].id is "testcase17"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[9].id is "testcase20"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[10].id is "testcase21"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[11].id is "testcase24"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[12].id is "testcase25"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[13].id is "testcase28"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[14].id is "testcase29"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
-
-Testing ":nth-child(3n - 2 of [attribute1=value1])"
-PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])').length is 12
-PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[0].id is "testcase1"
-PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[1].id is "testcase5"
-PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[2].id is "testcase6"
-PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[3].id is "testcase9"
-PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[4].id is "testcase10"
-PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[5].id is "testcase13"
-PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[6].id is "testcase17"
-PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[7].id is "testcase20"
-PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[8].id is "testcase21"
-PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[9].id is "testcase24"
-PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[10].id is "testcase27"
-PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[11].id is "testcase28"
+PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[1].id is "testcase5"
+PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[2].id is "testcase9"
+PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[3].id is "testcase13"
+PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[4].id is "testcase17"
+PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[5].id is "testcase21"
+PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[6].id is "testcase25"
+PASS document.querySelectorAll('#test-root :nth-child(2n + 1 of testcaseA)')[7].id is "testcase29"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
+
+Testing ":nth-child(3n - 2 of [attribute1=value1])"
+PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])').length is 8
+PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[0].id is "testcase1"
+PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[1].id is "testcase6"
+PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[2].id is "testcase10"
+PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[3].id is "testcase13"
+PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[4].id is "testcase17"
+PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[5].id is "testcase21"
+PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[6].id is "testcase24"
+PASS document.querySelectorAll('#test-root :nth-child(3n - 2 of [attribute1=value1])')[7].id is "testcase28"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
@@ -132,29 +121,28 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(4n - 1 of .bar)"
-PASS document.querySelectorAll('#test-root :nth-child(4n - 1 of .bar)').length is 6
-PASS document.querySelectorAll('#test-root :nth-child(4n - 1 of .bar)')[0].id is "testcase3"
-PASS document.querySelectorAll('#test-root :nth-child(4n - 1 of .bar)')[1].id is "testcase4"
-PASS document.querySelectorAll('#test-root :nth-child(4n - 1 of .bar)')[2].id is "testcase10"
-PASS document.querySelectorAll('#test-root :nth-child(4n - 1 of .bar)')[3].id is "testcase15"
-PASS document.querySelectorAll('#test-root :nth-child(4n - 1 of .bar)')[4].id is "testcase20"
-PASS document.querySelectorAll('#test-root :nth-child(4n - 1 of .bar)')[5].id is "testcase25"
+PASS document.querySelectorAll('#test-root :nth-child(4n - 1 of .bar)').length is 5
+PASS document.querySelectorAll('#test-root :nth-child(4n - 1 of .bar)')[0].id is "testcase4"
+PASS document.querySelectorAll('#test-root :nth-child(4n - 1 of .bar)')[1].id is "testcase10"
+PASS document.querySelectorAll('#test-root :nth-child(4n - 1 of .bar)')[2].id is "testcase15"
+PASS document.querySelectorAll('#test-root :nth-child(4n - 1 of .bar)')[3].id is "testcase20"
+PASS document.querySelectorAll('#test-root :nth-child(4n - 1 of .bar)')[4].id is "testcase25"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
@@ -184,28 +172,17 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(2n + 2 of :empty)"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)').length is 13
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[0].id is "testcase3"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[1].id is "testcase4"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[2].id is "testcase5"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[3].id is "testcase6"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[4].id is "testcase7"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[5].id is "testcase8"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[6].id is "testcase9"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[7].id is "testcase22"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[8].id is "testcase23"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[9].id is "testcase24"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[10].id is "testcase25"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[11].id is "testcase26"
-PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[12].id is "testcase27"
+PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)').length is 2
+PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[0].id is "testcase9"
+PASS document.querySelectorAll('#test-root :nth-child(2n + 2 of :empty)')[1].id is "testcase27"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
@@ -219,11 +196,11 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
@@ -270,15 +247,13 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing "testcaseA:nth-child(even of testcaseA.bazoo)"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)').length is 8
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)').length is 6
 PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)')[0].id is "testcase3"
 PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)')[1].id is "testcase7"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)')[2].id is "testcase11"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)')[3].id is "testcase13"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)')[4].id is "testcase17"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)')[5].id is "testcase21"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)')[6].id is "testcase23"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)')[7].id is "testcase29"
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)')[2].id is "testcase13"
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)')[3].id is "testcase17"
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)')[4].id is "testcase23"
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of testcaseA.bazoo)')[5].id is "testcase29"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
@@ -289,7 +264,7 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
@@ -299,7 +274,7 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
@@ -391,16 +366,14 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(10, 100, 200)"
 
 Testing "testcaseA:nth-child(even of .bar.foo)"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of .bar.foo)').length is 4
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of .bar.foo)')[0].id is "testcase5"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of .bar.foo)')[1].id is "testcase13"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of .bar.foo)')[2].id is "testcase21"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of .bar.foo)')[3].id is "testcase25"
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of .bar.foo)').length is 2
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of .bar.foo)')[0].id is "testcase21"
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(even of .bar.foo)')[1].id is "testcase25"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
@@ -408,7 +381,7 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
@@ -428,21 +401,10 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing "*:nth-child(3n+1 of [id][attribute2^="value"].bazoo)"
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)').length is 13
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[0].id is "testcase1"
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[1].id is "testcase2"
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[2].id is "testcase10"
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[3].id is "testcase11"
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[4].id is "testcase12"
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[5].id is "testcase13"
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[6].id is "testcase14"
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[7].id is "testcase15"
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[8].id is "testcase16"
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[9].id is "testcase17"
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[10].id is "testcase18"
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[11].id is "testcase19"
-PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[12].id is "testcase20"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(10, 100, 200)"
+PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)').length is 2
+PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[0].id is "testcase2"
+PASS document.querySelectorAll('#test-root *:nth-child(3n+1 of [id][attribute2^="value"].bazoo)')[1].id is "testcase20"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
@@ -451,16 +413,16 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
@@ -474,11 +436,8 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":-webkit-any(testcaseA, dontExist):nth-child(n+3 of .foo:empty)"
-PASS document.querySelectorAll('#test-root :-webkit-any(testcaseA, dontExist):nth-child(n+3 of .foo:empty)').length is 4
-PASS document.querySelectorAll('#test-root :-webkit-any(testcaseA, dontExist):nth-child(n+3 of .foo:empty)')[0].id is "testcase23"
-PASS document.querySelectorAll('#test-root :-webkit-any(testcaseA, dontExist):nth-child(n+3 of .foo:empty)')[1].id is "testcase25"
-PASS document.querySelectorAll('#test-root :-webkit-any(testcaseA, dontExist):nth-child(n+3 of .foo:empty)')[2].id is "testcase27"
-PASS document.querySelectorAll('#test-root :-webkit-any(testcaseA, dontExist):nth-child(n+3 of .foo:empty)')[3].id is "testcase29"
+PASS document.querySelectorAll('#test-root :-webkit-any(testcaseA, dontExist):nth-child(n+3 of .foo:empty)').length is 1
+PASS document.querySelectorAll('#test-root :-webkit-any(testcaseA, dontExist):nth-child(n+3 of .foo:empty)')[0].id is "testcase27"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
@@ -501,31 +460,25 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing "*:nth-child(even of :nth-child(odd of [attribute2]))"
-PASS document.querySelectorAll('#test-root *:nth-child(even of :nth-child(odd of [attribute2]))').length is 7
-PASS document.querySelectorAll('#test-root *:nth-child(even of :nth-child(odd of [attribute2]))')[0].id is "testcase2"
-PASS document.querySelectorAll('#test-root *:nth-child(even of :nth-child(odd of [attribute2]))')[1].id is "testcase7"
-PASS document.querySelectorAll('#test-root *:nth-child(even of :nth-child(odd of [attribute2]))')[2].id is "testcase9"
-PASS document.querySelectorAll('#test-root *:nth-child(even of :nth-child(odd of [attribute2]))')[3].id is "testcase16"
-PASS document.querySelectorAll('#test-root *:nth-child(even of :nth-child(odd of [attribute2]))')[4].id is "testcase18"
-PASS document.querySelectorAll('#test-root *:nth-child(even of :nth-child(odd of [attribute2]))')[5].id is "testcase20"
-PASS document.querySelectorAll('#test-root *:nth-child(even of :nth-child(odd of [attribute2]))')[6].id is "testcase29"
+PASS document.querySelectorAll('#test-root *:nth-child(even of :nth-child(odd of [attribute2]))').length is 1
+PASS document.querySelectorAll('#test-root *:nth-child(even of :nth-child(odd of [attribute2]))')[0].id is "testcase9"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
@@ -534,11 +487,11 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(255, 0, 0)"
@@ -547,7 +500,7 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 PASS successfullyParsed is true
diff --git a/LayoutTests/fast/selectors/nth-child-of-basics.html b/LayoutTests/fast/selectors/nth-child-of-basics.html
index 768a008..c5943a7 100644
--- a/LayoutTests/fast/selectors/nth-child-of-basics.html
+++ b/LayoutTests/fast/selectors/nth-child-of-basics.html
@@ -79,16 +79,16 @@
 /* -- Some generic tests for primitive selectors. -- */
 
 // Even of a tag name.
-testSelector(":nth-child(2n + 1 of testcaseA)", ["testcase1", "testcase4", "testcase5", "testcase8", "testcase9", "testcase12", "testcase13", "testcase16", "testcase17", "testcase20", "testcase21", "testcase24", "testcase25", "testcase28", "testcase29"]);
+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", "testcase5", "testcase6", "testcase9", "testcase10", "testcase13", "testcase17", "testcase20", "testcase21", "testcase24", "testcase27", "testcase28"]);
+testSelector(":nth-child(3n - 2 of [attribute1=value1])", ["testcase1", "testcase6", "testcase10", "testcase13", "testcase17", "testcase21", "testcase24", "testcase28"]);
 
 // Class.
-testSelector(":nth-child(4n - 1 of .bar)", ["testcase3", "testcase4", "testcase10", "testcase15", "testcase20", "testcase25"]);
+testSelector(":nth-child(4n - 1 of .bar)", ["testcase4", "testcase10", "testcase15", "testcase20", "testcase25"]);
 
 // Value.
-testSelector(":nth-child(2n + 2 of :empty)", ["testcase3", "testcase4", "testcase5", "testcase6", "testcase7", "testcase8", "testcase9", "testcase22", "testcase23", "testcase24", "testcase25", "testcase26", "testcase27"]);
+testSelector(":nth-child(2n + 2 of :empty)", ["testcase9", "testcase27"]);
 
 
 /* -- In practice, we expect a lot of filtering will be done on the same type. -- */
@@ -97,7 +97,7 @@
 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", "testcase11", "testcase13", "testcase17", "testcase21", "testcase23", "testcase29"]);
+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"]);
@@ -107,10 +107,10 @@
 
 
 /* -- Compound selectors -- */
-testSelector("testcaseA:nth-child(even of .bar.foo)", ["testcase5", "testcase13", "testcase21", "testcase25"]);
-testSelector('*:nth-child(3n+1 of [id][attribute2^="value"].bazoo)', ["testcase1", "testcase2", "testcase10", "testcase11", "testcase12", "testcase13", "testcase14", "testcase15", "testcase16", "testcase17", "testcase18", "testcase19", "testcase20"]);
-testSelector(':-webkit-any(testcaseA, dontExist):nth-child(n+3 of .foo:empty)', ["testcase23", "testcase25", "testcase27", "testcase29"]);
-testSelector('*:nth-child(even of :nth-child(odd of [attribute2]))', ["testcase2", "testcase7", "testcase9", "testcase16", "testcase18", "testcase20", "testcase29"]);
+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>
diff --git a/LayoutTests/fast/selectors/nth-child-of-chained-2-expected.txt b/LayoutTests/fast/selectors/nth-child-of-chained-2-expected.txt
index e96de08..a0b6cf3 100644
--- a/LayoutTests/fast/selectors/nth-child-of-chained-2-expected.txt
+++ b/LayoutTests/fast/selectors/nth-child-of-chained-2-expected.txt
@@ -44,20 +44,18 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing "testcaseA:nth-child(2n of [attribute1])"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])').length is 8
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])').length is 6
 PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])')[0].id is "testcase3"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])')[1].id is "testcase5"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])')[2].id is "testcase11"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])')[3].id is "testcase13"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])')[4].id is "testcase21"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])')[5].id is "testcase23"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])')[6].id is "testcase25"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])')[7].id is "testcase27"
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])')[1].id is "testcase11"
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])')[2].id is "testcase13"
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])')[3].id is "testcase21"
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])')[4].id is "testcase23"
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of [attribute1])')[5].id is "testcase25"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
@@ -79,17 +77,16 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing "testcaseA:nth-child(2n of testcaseA):nth-child(2n of [attribute1])"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of testcaseA):nth-child(2n of [attribute1])').length is 4
+PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of testcaseA):nth-child(2n of [attribute1])').length is 3
 PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of testcaseA):nth-child(2n of [attribute1])')[0].id is "testcase3"
 PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of testcaseA):nth-child(2n of [attribute1])')[1].id is "testcase11"
 PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of testcaseA):nth-child(2n of [attribute1])')[2].id is "testcase23"
-PASS document.querySelectorAll('#test-root testcaseA:nth-child(2n of testcaseA):nth-child(2n of [attribute1])')[3].id is "testcase27"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
@@ -116,7 +113,7 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
diff --git a/LayoutTests/fast/selectors/nth-child-of-chained-2.html b/LayoutTests/fast/selectors/nth-child-of-chained-2.html
index c45c1e9..8cf9623 100644
--- a/LayoutTests/fast/selectors/nth-child-of-chained-2.html
+++ b/LayoutTests/fast/selectors/nth-child-of-chained-2.html
@@ -74,10 +74,10 @@
 }
 
 testSelector("testcaseA:nth-child(2n of testcaseA)", ["testcase3", "testcase7", "testcase11", "testcase15", "testcase19", "testcase23", "testcase27"]);
-testSelector("testcaseA:nth-child(2n of [attribute1])", ["testcase3", "testcase5", "testcase11", "testcase13", "testcase21", "testcase23", "testcase25", "testcase27"]);
+testSelector("testcaseA:nth-child(2n of [attribute1])", ["testcase3", "testcase11", "testcase13", "testcase21", "testcase23", "testcase25"]);
 
 // This should be the conjunction of the two selectors, which should return the intersection of the results.
-testSelector("testcaseA:nth-child(2n of testcaseA):nth-child(2n of [attribute1])", ["testcase3", "testcase11", "testcase23", "testcase27"]);
+testSelector("testcaseA:nth-child(2n of testcaseA):nth-child(2n of [attribute1])", ["testcase3", "testcase11", "testcase23"]);
 
 // This is the disjunction of the selectors. Due to the counting, it is not the union of the previous results.
 testSelector("testcaseA:nth-child(2n of testcaseA, [attribute1])", ["testcase3", "testcase5", "testcase7", "testcase9", "testcase11", "testcase13", "testcase21", "testcase23", "testcase25", "testcase27", "testcase29"]);
diff --git a/LayoutTests/fast/selectors/nth-child-of-chained-3-expected.txt b/LayoutTests/fast/selectors/nth-child-of-chained-3-expected.txt
new file mode 100644
index 0000000..d11ddfc
--- /dev/null
+++ b/LayoutTests/fast/selectors/nth-child-of-chained-3-expected.txt
@@ -0,0 +1,264 @@
+Test the chaining of multiple :nth-child(An+B of selector) does match every one of the selector.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+Testing "testcaseA.foo[id]"
+PASS document.querySelectorAll('#test-root testcaseA.foo[id]').length is 12
+PASS document.querySelectorAll('#test-root testcaseA.foo[id]')[0].id is "testcase3"
+PASS document.querySelectorAll('#test-root testcaseA.foo[id]')[1].id is "testcase5"
+PASS document.querySelectorAll('#test-root testcaseA.foo[id]')[2].id is "testcase9"
+PASS document.querySelectorAll('#test-root testcaseA.foo[id]')[3].id is "testcase11"
+PASS document.querySelectorAll('#test-root testcaseA.foo[id]')[4].id is "testcase13"
+PASS document.querySelectorAll('#test-root testcaseA.foo[id]')[5].id is "testcase15"
+PASS document.querySelectorAll('#test-root testcaseA.foo[id]')[6].id is "testcase19"
+PASS document.querySelectorAll('#test-root testcaseA.foo[id]')[7].id is "testcase21"
+PASS document.querySelectorAll('#test-root testcaseA.foo[id]')[8].id is "testcase23"
+PASS document.querySelectorAll('#test-root testcaseA.foo[id]')[9].id is "testcase25"
+PASS document.querySelectorAll('#test-root testcaseA.foo[id]')[10].id is "testcase27"
+PASS document.querySelectorAll('#test-root testcaseA.foo[id]')[11].id is "testcase29"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
+
+Testing ":nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])"
+PASS document.querySelectorAll('#test-root :nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])').length is 12
+PASS document.querySelectorAll('#test-root :nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])')[0].id is "testcase3"
+PASS document.querySelectorAll('#test-root :nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])')[1].id is "testcase5"
+PASS document.querySelectorAll('#test-root :nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])')[2].id is "testcase9"
+PASS document.querySelectorAll('#test-root :nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])')[3].id is "testcase11"
+PASS document.querySelectorAll('#test-root :nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])')[4].id is "testcase13"
+PASS document.querySelectorAll('#test-root :nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])')[5].id is "testcase15"
+PASS document.querySelectorAll('#test-root :nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])')[6].id is "testcase19"
+PASS document.querySelectorAll('#test-root :nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])')[7].id is "testcase21"
+PASS document.querySelectorAll('#test-root :nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])')[8].id is "testcase23"
+PASS document.querySelectorAll('#test-root :nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])')[9].id is "testcase25"
+PASS document.querySelectorAll('#test-root :nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])')[10].id is "testcase27"
+PASS document.querySelectorAll('#test-root :nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])')[11].id is "testcase29"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
+
+Testing ":nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])').length is 12
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])')[0].id is "testcase3"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])')[1].id is "testcase5"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])')[2].id is "testcase9"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])')[3].id is "testcase11"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])')[4].id is "testcase13"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])')[5].id is "testcase15"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])')[6].id is "testcase19"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])')[7].id is "testcase21"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])')[8].id is "testcase23"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])')[9].id is "testcase25"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])')[10].id is "testcase27"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])')[11].id is "testcase29"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
+
+Testing ":nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])').length is 12
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])')[0].id is "testcase3"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])')[1].id is "testcase5"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])')[2].id is "testcase9"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])')[3].id is "testcase11"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])')[4].id is "testcase13"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])')[5].id is "testcase15"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])')[6].id is "testcase19"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])')[7].id is "testcase21"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])')[8].id is "testcase23"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])')[9].id is "testcase25"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])')[10].id is "testcase27"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])')[11].id is "testcase29"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
+
+Testing ":nth-child(n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])').length is 6
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])')[0].id is "testcase3"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])')[1].id is "testcase11"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])')[2].id is "testcase15"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])')[3].id is "testcase19"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])')[4].id is "testcase23"
+PASS document.querySelectorAll('#test-root :nth-child(n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])')[5].id is "testcase27"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
+
+Testing ":nth-child(2n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])"
+PASS document.querySelectorAll('#test-root :nth-child(2n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])').length is 3
+PASS document.querySelectorAll('#test-root :nth-child(2n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])')[0].id is "testcase3"
+PASS document.querySelectorAll('#test-root :nth-child(2n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])')[1].id is "testcase15"
+PASS document.querySelectorAll('#test-root :nth-child(2n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])')[2].id is "testcase27"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
+
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/fast/selectors/nth-child-of-chained-3.html b/LayoutTests/fast/selectors/nth-child-of-chained-3.html
new file mode 100644
index 0000000..cc463d1
--- /dev/null
+++ b/LayoutTests/fast/selectors/nth-child-of-chained-3.html
@@ -0,0 +1,86 @@
+<!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('Test the chaining of multiple :nth-child(An+B of selector) does match every one of the selector.');
+
+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("");
+}
+
+// Using n and n+1 are interesting because they always match regardless of the siblings. But since the definition of :nth-child(An+B) include the current element, at least that one needs to match.
+testSelector("testcaseA.foo[id]", ["testcase3", "testcase5", "testcase9", "testcase11", "testcase13", "testcase15", "testcase19", "testcase21", "testcase23", "testcase25", "testcase27", "testcase29"]);
+testSelector(":nth-child(n of .foo):nth-child(n of testcaseA):nth-child(n of [id])", ["testcase3", "testcase5", "testcase9", "testcase11", "testcase13", "testcase15", "testcase19", "testcase21", "testcase23", "testcase25", "testcase27", "testcase29"]);
+testSelector(":nth-child(n+1 of .foo):nth-child(n of testcaseA):nth-child(n+1 of [id])", ["testcase3", "testcase5", "testcase9", "testcase11", "testcase13", "testcase15", "testcase19", "testcase21", "testcase23", "testcase25", "testcase27", "testcase29"]);
+testSelector(":nth-child(n+1 of .foo):nth-child(n+1 of testcaseA):nth-child(n of [id])", ["testcase3", "testcase5", "testcase9", "testcase11", "testcase13", "testcase15", "testcase19", "testcase21", "testcase23", "testcase25", "testcase27", "testcase29"]);
+
+testSelector(":nth-child(n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])", ["testcase3", "testcase11", "testcase15", "testcase19", "testcase23", "testcase27"]);
+testSelector(":nth-child(2n+1 of .foo):nth-child(2n of testcaseA):nth-child(n of [id])", ["testcase3", "testcase15", "testcase27"]);
+</script>
+<script src="../../resources/js-test-post.js"></script>
+</html>
diff --git a/LayoutTests/fast/selectors/nth-child-of-complex-selectors-expected.txt b/LayoutTests/fast/selectors/nth-child-of-complex-selectors-expected.txt
index a0557fa..da5c135 100644
--- a/LayoutTests/fast/selectors/nth-child-of-complex-selectors-expected.txt
+++ b/LayoutTests/fast/selectors/nth-child-of-complex-selectors-expected.txt
@@ -4,23 +4,20 @@
 
 
 Testing ":nth-child(2n of :root parent1>testcaseA)"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent1>testcaseA)').length is 5
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent1>testcaseA)')[0].id is "target2"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent1>testcaseA)')[1].id is "target3"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent1>testcaseA)')[2].id is "target6"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent1>testcaseA)')[3].id is "target7"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent1>testcaseA)')[4].id is "target10"
+PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent1>testcaseA)').length is 2
+PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent1>testcaseA)')[0].id is "target3"
+PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent1>testcaseA)')[1].id is "target7"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
@@ -34,12 +31,9 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(2n of :root parent2>.foo)"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo)').length is 5
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo)')[0].id is "target12"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo)')[1].id is "target13"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo)')[2].id is "target16"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo)')[3].id is "target17"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo)')[4].id is "target20"
+PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo)').length is 2
+PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo)')[0].id is "target13"
+PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo)')[1].id is "target17"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
@@ -53,79 +47,67 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)').length is 10
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[0].id is "target2"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[1].id is "target3"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[2].id is "target6"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[3].id is "target7"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[4].id is "target10"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[5].id is "target12"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[6].id is "target13"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[7].id is "target16"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[8].id is "target17"
-PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[9].id is "target20"
+PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)').length is 4
+PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[0].id is "target3"
+PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[1].id is "target7"
+PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[2].id is "target13"
+PASS document.querySelectorAll('#test-root :nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)')[3].id is "target17"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing "> * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)').length is 12
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[0].id is "target1"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[1].id is "target2"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[2].id is "target3"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[3].id is "target4"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[4].id is "target7"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[5].id is "target8"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[6].id is "target11"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[7].id is "target12"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[8].id is "target13"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[9].id is "target15"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[10].id is "target17"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[11].id is "target19"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)').length is 6
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[0].id is "target4"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[1].id is "target8"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[2].id is "target13"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[3].id is "target15"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[4].id is "target17"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)')[5].id is "target19"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(10, 100, 200)"
@@ -136,66 +118,50 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing "> * > *:nth-child(2n+1 of html .foo)"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)').length is 15
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[0].id is "target1"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[1].id is "target2"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[2].id is "target3"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[3].id is "target4"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[4].id is "target5"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[5].id is "target6"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[6].id is "target7"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[7].id is "target8"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[8].id is "target9"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[9].id is "target10"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[10].id is "target11"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[11].id is "target14"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[12].id is "target15"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[13].id is "target18"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[14].id is "target19"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)').length is 3
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[0].id is "target11"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[1].id is "target15"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html .foo)')[2].id is "target19"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing "> * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)').length is 11
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[0].id is "target1"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[1].id is "target2"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[2].id is "target3"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[3].id is "target4"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[4].id is "target7"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[5].id is "target8"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[6].id is "target11"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[7].id is "target14"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[8].id is "target16"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[9].id is "target18"
-PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[10].id is "target20"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)').length is 7
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[0].id is "target4"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[1].id is "target8"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[2].id is "target11"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[3].id is "target14"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[4].id is "target16"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[5].id is "target18"
+PASS document.querySelectorAll('#test-root > * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)')[6].id is "target20"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
diff --git a/LayoutTests/fast/selectors/nth-child-of-complex-selectors.html b/LayoutTests/fast/selectors/nth-child-of-complex-selectors.html
index ad1516c..6b7ee1c 100644
--- a/LayoutTests/fast/selectors/nth-child-of-complex-selectors.html
+++ b/LayoutTests/fast/selectors/nth-child-of-complex-selectors.html
@@ -67,19 +67,19 @@
     debug("");
 }
 
-testSelector(':nth-child(2n of :root parent1>testcaseA)', ["target2", "target3", "target6", "target7", "target10"]);
+testSelector(':nth-child(2n of :root parent1>testcaseA)', ["target3", "target7"]);
 
-testSelector(':nth-child(2n of :root parent2>.foo)', ["target12", "target13", "target16", "target17", "target20"]);
+testSelector(':nth-child(2n of :root parent2>.foo)', ["target13", "target17"]);
 
 // Since the two above are disjoint sets, the combined selector should match the combined results.
-testSelector(':nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)', ["target2", "target3", "target6", "target7", "target10", "target12", "target13", "target16", "target17", "target20"]);
+testSelector(':nth-child(2n of :root parent2>.foo, :root parent1>testcaseA)', ["target3", "target7", "target13", "target17"]);
 
 
-testSelector('> * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)', ["target1", "target2", "target3", "target4", "target7", "target8", "target11", "target12", "target13", "target15", "target17", "target19"]);
+testSelector('> * > *:nth-child(2n + 1 of html div testcaseA~testcaseA + *)', ["target4", "target8", "target13", "target15", "target17", "target19"]);
 
-testSelector('> * > *:nth-child(2n+1 of html .foo)', ["target1", "target2", "target3", "target4", "target5", "target6", "target7", "target8", "target9", "target10", "target11", "target14", "target15", "target18", "target19"]);
+testSelector('> * > *:nth-child(2n+1 of html .foo)', ["target11", "target15", "target19"]);
 
-testSelector('> * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)', ["target1", "target2", "target3", "target4", "target7", "target8", "target11", "target14", "target16", "target18", "target20"]);
+testSelector('> * > *:nth-child(2n+1 of html div testcaseA~testcaseA + *, html .foo)', ["target4", "target8", "target11", "target14", "target16", "target18", "target20"]);
 
 </script>
 <script src="../../resources/js-test-post.js"></script>
diff --git a/LayoutTests/fast/selectors/nth-child-of-never-matching-selector-expected.txt b/LayoutTests/fast/selectors/nth-child-of-never-matching-selector-expected.txt
index 0de68b7..c6b8abf 100644
--- a/LayoutTests/fast/selectors/nth-child-of-never-matching-selector-expected.txt
+++ b/LayoutTests/fast/selectors/nth-child-of-never-matching-selector-expected.txt
@@ -70,193 +70,103 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(1 of :not(*))"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))').length is 30
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[0].id is "testcase1"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[1].id is "testcase2"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[2].id is "testcase3"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[3].id is "testcase4"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[4].id is "testcase5"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[5].id is "testcase6"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[6].id is "testcase7"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[7].id is "testcase8"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[8].id is "testcase9"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[9].id is "testcase10"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[10].id is "testcase11"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[11].id is "testcase12"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[12].id is "testcase13"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[13].id is "testcase14"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[14].id is "testcase15"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[15].id is "testcase16"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[16].id is "testcase17"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[17].id is "testcase18"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[18].id is "testcase19"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[19].id is "testcase20"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[20].id is "testcase21"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[21].id is "testcase22"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[22].id is "testcase23"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[23].id is "testcase24"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[24].id is "testcase25"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[25].id is "testcase26"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[26].id is "testcase27"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[27].id is "testcase28"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[28].id is "testcase29"
-PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))')[29].id is "testcase30"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(10, 100, 200)"
+PASS document.querySelectorAll('#test-root :nth-child(1 of :not(*))').length is 0
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(n of :not(*))"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))').length is 30
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[0].id is "testcase1"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[1].id is "testcase2"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[2].id is "testcase3"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[3].id is "testcase4"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[4].id is "testcase5"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[5].id is "testcase6"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[6].id is "testcase7"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[7].id is "testcase8"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[8].id is "testcase9"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[9].id is "testcase10"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[10].id is "testcase11"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[11].id is "testcase12"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[12].id is "testcase13"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[13].id is "testcase14"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[14].id is "testcase15"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[15].id is "testcase16"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[16].id is "testcase17"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[17].id is "testcase18"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[18].id is "testcase19"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[19].id is "testcase20"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[20].id is "testcase21"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[21].id is "testcase22"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[22].id is "testcase23"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[23].id is "testcase24"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[24].id is "testcase25"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[25].id is "testcase26"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[26].id is "testcase27"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[27].id is "testcase28"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[28].id is "testcase29"
-PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))')[29].id is "testcase30"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(10, 100, 200)"
+PASS document.querySelectorAll('#test-root :nth-child(n of :not(*))').length is 0
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(n-99999 of :not(*))"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))').length is 30
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[0].id is "testcase1"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[1].id is "testcase2"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[2].id is "testcase3"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[3].id is "testcase4"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[4].id is "testcase5"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[5].id is "testcase6"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[6].id is "testcase7"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[7].id is "testcase8"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[8].id is "testcase9"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[9].id is "testcase10"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[10].id is "testcase11"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[11].id is "testcase12"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[12].id is "testcase13"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[13].id is "testcase14"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[14].id is "testcase15"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[15].id is "testcase16"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[16].id is "testcase17"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[17].id is "testcase18"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[18].id is "testcase19"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[19].id is "testcase20"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[20].id is "testcase21"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[21].id is "testcase22"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[22].id is "testcase23"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[23].id is "testcase24"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[24].id is "testcase25"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[25].id is "testcase26"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[26].id is "testcase27"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[27].id is "testcase28"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[28].id is "testcase29"
-PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))')[29].id is "testcase30"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(10, 100, 200)"
+PASS document.querySelectorAll('#test-root :nth-child(n-99999 of :not(*))').length is 0
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[6]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[7]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(n+2 of [attribute4])"
 PASS document.querySelectorAll('#test-root :nth-child(n+2 of [attribute4])').length is 0
diff --git a/LayoutTests/fast/selectors/nth-child-of-never-matching-selector.html b/LayoutTests/fast/selectors/nth-child-of-never-matching-selector.html
index e0e4df8..58b2bb4 100644
--- a/LayoutTests/fast/selectors/nth-child-of-never-matching-selector.html
+++ b/LayoutTests/fast/selectors/nth-child-of-never-matching-selector.html
@@ -77,9 +77,9 @@
 testSelector(":nth-child(n+2 of :not(*))", []);
 
 // The count of previous sibling must be An+B-1. A never matching selector always satisfies this.
-testSelector(":nth-child(1 of :not(*))", ['testcase1', 'testcase2', 'testcase3', 'testcase4', 'testcase5', 'testcase6', 'testcase7', 'testcase8', 'testcase9', 'testcase10', 'testcase11', 'testcase12', 'testcase13', 'testcase14', 'testcase15', 'testcase16', 'testcase17', 'testcase18', 'testcase19', 'testcase20', 'testcase21', 'testcase22', 'testcase23', 'testcase24', 'testcase25', 'testcase26', 'testcase27', 'testcase28', 'testcase29', 'testcase30']);
-testSelector(":nth-child(n of :not(*))", ['testcase1', 'testcase2', 'testcase3', 'testcase4', 'testcase5', 'testcase6', 'testcase7', 'testcase8', 'testcase9', 'testcase10', 'testcase11', 'testcase12', 'testcase13', 'testcase14', 'testcase15', 'testcase16', 'testcase17', 'testcase18', 'testcase19', 'testcase20', 'testcase21', 'testcase22', 'testcase23', 'testcase24', 'testcase25', 'testcase26', 'testcase27', 'testcase28', 'testcase29', 'testcase30']);
-testSelector(":nth-child(n-99999 of :not(*))", ['testcase1', 'testcase2', 'testcase3', 'testcase4', 'testcase5', 'testcase6', 'testcase7', 'testcase8', 'testcase9', 'testcase10', 'testcase11', 'testcase12', 'testcase13', 'testcase14', 'testcase15', 'testcase16', 'testcase17', 'testcase18', 'testcase19', 'testcase20', 'testcase21', 'testcase22', 'testcase23', 'testcase24', 'testcase25', 'testcase26', 'testcase27', 'testcase28', 'testcase29', 'testcase30']);
+testSelector(":nth-child(1 of :not(*))", []);
+testSelector(":nth-child(n of :not(*))", []);
+testSelector(":nth-child(n-99999 of :not(*))", []);
 
 // Selectors that could match but do not match in this particular document.
 testSelector(":nth-child(n+2 of [attribute4])", []);
diff --git a/LayoutTests/fast/selectors/nth-child-of-selector-list-expected.txt b/LayoutTests/fast/selectors/nth-child-of-selector-list-expected.txt
index e29e567..bdcb8c8 100644
--- a/LayoutTests/fast/selectors/nth-child-of-selector-list-expected.txt
+++ b/LayoutTests/fast/selectors/nth-child-of-selector-list-expected.txt
@@ -229,23 +229,14 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(even of [attribute3$="3"], :empty)"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)').length is 16
+PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)').length is 7
 PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[0].id is "testcase2"
 PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[1].id is "testcase6"
 PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[2].id is "testcase10"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[3].id is "testcase14"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[4].id is "testcase15"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[5].id is "testcase16"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[6].id is "testcase18"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[7].id is "testcase19"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[8].id is "testcase20"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[9].id is "testcase22"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[10].id is "testcase23"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[11].id is "testcase24"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[12].id is "testcase26"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[13].id is "testcase27"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[14].id is "testcase29"
-PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[15].id is "testcase30"
+PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[3].id is "testcase16"
+PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[4].id is "testcase20"
+PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[5].id is "testcase24"
+PASS document.querySelectorAll('#test-root :nth-child(even of [attribute3$="3"], :empty)')[6].id is "testcase27"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
@@ -259,42 +250,33 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(even of [id][attribute3$="3"], [id]:empty)"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)').length is 16
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)').length is 7
 PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[0].id is "testcase2"
 PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[1].id is "testcase6"
 PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[2].id is "testcase10"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[3].id is "testcase14"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[4].id is "testcase15"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[5].id is "testcase16"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[6].id is "testcase18"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[7].id is "testcase19"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[8].id is "testcase20"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[9].id is "testcase22"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[10].id is "testcase23"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[11].id is "testcase24"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[12].id is "testcase26"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[13].id is "testcase27"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[14].id is "testcase29"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[15].id is "testcase30"
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[3].id is "testcase16"
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[4].id is "testcase20"
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[5].id is "testcase24"
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute3$="3"], [id]:empty)')[6].id is "testcase27"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
@@ -308,44 +290,38 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[16]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[17]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[29]).backgroundColor is "rgb(255, 0, 0)"
 
 Testing ":nth-child(even of [id][attribute2*="alue"], .foo.bar)"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)').length is 15
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[0].id is "testcase3"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[1].id is "testcase4"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[2].id is "testcase6"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[3].id is "testcase10"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[4].id is "testcase12"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[5].id is "testcase13"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[6].id is "testcase14"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[7].id is "testcase16"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[8].id is "testcase20"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[9].id is "testcase22"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[10].id is "testcase23"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[11].id is "testcase24"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[12].id is "testcase26"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[13].id is "testcase27"
-PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[14].id is "testcase30"
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)').length is 9
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[0].id is "testcase4"
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[1].id is "testcase6"
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[2].id is "testcase10"
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[3].id is "testcase14"
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[4].id is "testcase16"
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[5].id is "testcase20"
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[6].id is "testcase24"
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[7].id is "testcase27"
+PASS document.querySelectorAll('#test-root :nth-child(even of [id][attribute2*="alue"], .foo.bar)')[8].id is "testcase30"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[0]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[1]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[2]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[3]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[4]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[5]).backgroundColor is "rgb(10, 100, 200)"
@@ -354,8 +330,8 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[8]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[9]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[10]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[11]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[12]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[13]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[14]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[15]).backgroundColor is "rgb(10, 100, 200)"
@@ -364,11 +340,11 @@
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[18]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[19]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[20]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(10, 100, 200)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[21]).backgroundColor is "rgb(255, 0, 0)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[22]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[23]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[24]).backgroundColor is "rgb(255, 0, 0)"
-PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(10, 100, 200)"
+PASS getComputedStyle(document.querySelectorAll("#test-root *")[25]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[26]).backgroundColor is "rgb(10, 100, 200)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[27]).backgroundColor is "rgb(255, 0, 0)"
 PASS getComputedStyle(document.querySelectorAll("#test-root *")[28]).backgroundColor is "rgb(255, 0, 0)"
diff --git a/LayoutTests/fast/selectors/nth-child-of-selector-list.html b/LayoutTests/fast/selectors/nth-child-of-selector-list.html
index e7c7f48..2b15e74 100644
--- a/LayoutTests/fast/selectors/nth-child-of-selector-list.html
+++ b/LayoutTests/fast/selectors/nth-child-of-selector-list.html
@@ -89,13 +89,13 @@
 testSelector(":nth-child(odd of [attribute1], [attribute2=value2])", ["testcase1", "testcase3", "testcase5", "testcase7", "testcase9", "testcase11", "testcase13", "testcase15", "testcase17", "testcase19", "testcase21", "testcase23", "testcase25", "testcase27", "testcase29"]);
 
 // Any element with an odd number of (attribute3 or empty) above.
-testSelector(':nth-child(even of [attribute3$="3"], :empty)', ["testcase2", "testcase6", "testcase10", "testcase14", "testcase15", "testcase16", "testcase18", "testcase19", "testcase20", "testcase22", "testcase23", "testcase24", "testcase26", "testcase27", "testcase29", "testcase30"]);
+testSelector(':nth-child(even of [attribute3$="3"], :empty)', ["testcase2", "testcase6", "testcase10", "testcase16", "testcase20", "testcase24", "testcase27"]);
 
 // Any element with an odd number of (attribute3 or empty) above. Overqualified with id to test compound selectors.
-testSelector(':nth-child(even of [id][attribute3$="3"], [id]:empty)', ["testcase2", "testcase6", "testcase10", "testcase14", "testcase15", "testcase16", "testcase18", "testcase19", "testcase20", "testcase22", "testcase23", "testcase24", "testcase26", "testcase27", "testcase29", "testcase30"]);
+testSelector(':nth-child(even of [id][attribute3$="3"], [id]:empty)', ["testcase2", "testcase6", "testcase10", "testcase16", "testcase20", "testcase24", "testcase27"]);
 
 // id is always there but attribute2 is uncommon. The classes .foo.bar matches many elements.
-testSelector(':nth-child(even of [id][attribute2*="alue"], .foo.bar)', ["testcase3", "testcase4", "testcase6", "testcase10", "testcase12", "testcase13", "testcase14", "testcase16", "testcase20", "testcase22", "testcase23", "testcase24", "testcase26", "testcase27", "testcase30"]);
+testSelector(':nth-child(even of [id][attribute2*="alue"], .foo.bar)', ["testcase4", "testcase6", "testcase10", "testcase14", "testcase16", "testcase20", "testcase24", "testcase27", "testcase30"]);
 
 </script>
 <script src="../../resources/js-test-post.js"></script>
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 8a20c89..df9888e 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,26 @@
+2014-10-10  Benjamin Poulain  <benjamin@webkit.org>
+
+        Update :nth-child(An+B of selector-list) to the latest specification
+        https://bugs.webkit.org/show_bug.cgi?id=137593
+
+        Reviewed by Andreas Kling.
+
+        Following my implementation feedback on :nth-child(An+B of selector-list),
+        the CSS WG decided to change the definition such that the current element on which
+        the selector is matched must also match the selector list in :nth-child().
+
+        This patch updates the implementation accordingly.
+
+        Test: fast/selectors/nth-child-of-chained-3.html
+
+        * css/SelectorChecker.cpp:
+        (WebCore::SelectorChecker::checkOne):
+        (WebCore::SelectorChecker::matchSelectorList):
+        * css/SelectorChecker.h:
+        * cssjit/SelectorCompiler.cpp:
+        (WebCore::SelectorCompiler::SelectorCodeGenerator::generateElementMatchesSelectorList):
+        (WebCore::SelectorCompiler::SelectorCodeGenerator::generateElementIsNthChildOf):
+
 2014-10-10  Chris Dumez  <cdumez@apple.com>
 
         Drop RENDER_OBJECT_TYPE_CASTS() for a lot of RenderObject subclasses
diff --git a/Source/WebCore/css/SelectorChecker.cpp b/Source/WebCore/css/SelectorChecker.cpp
index 430e6cf..2cd0176 100644
--- a/Source/WebCore/css/SelectorChecker.cpp
+++ b/Source/WebCore/css/SelectorChecker.cpp
@@ -682,6 +682,13 @@
             if (!selector->parseNth())
                 break;
             if (element->parentElement()) {
+#if ENABLE(CSS_SELECTORS_LEVEL4)
+                if (const CSSSelectorList* selectorList = selector->selectorList()) {
+                    if (!matchSelectorList(context, *element, *selectorList))
+                        return false;
+                }
+#endif
+
                 if (context.resolvingMode == Mode::ResolvingStyle)
                     element->setStyleIsAffectedByPreviousSibling();
 
@@ -692,19 +699,8 @@
                         if (context.resolvingMode == Mode::ResolvingStyle)
                             sibling->setAffectsNextSiblingElementStyle();
 
-                        for (const CSSSelector* subselector = selectorList->first(); subselector; subselector = CSSSelectorList::next(subselector)) {
-                            CheckingContextWithStatus subcontext(context);
-                            subcontext.element = sibling;
-                            subcontext.selector = subselector;
-                            subcontext.inFunctionalPseudoClass = true;
-                            subcontext.firstSelectorOfTheFragment = subselector;
-                            PseudoId ignoreDynamicPseudo = NOPSEUDO;
-                            if (matchRecursively(subcontext, ignoreDynamicPseudo) == SelectorMatches) {
-                                ASSERT(ignoreDynamicPseudo == NOPSEUDO);
-                                ++count;
-                                break;
-                            }
-                        }
+                        if (matchSelectorList(context, *sibling, *selectorList))
+                            ++count;
                     }
                 } else
 #endif
@@ -926,6 +922,23 @@
     return true;
 }
 
+bool SelectorChecker::matchSelectorList(const CheckingContextWithStatus& baseContext, Element& element, const CSSSelectorList& selectorList) const
+{
+    for (const CSSSelector* subselector = selectorList.first(); subselector; subselector = CSSSelectorList::next(subselector)) {
+        CheckingContextWithStatus subcontext(baseContext);
+        subcontext.element = &element;
+        subcontext.selector = subselector;
+        subcontext.inFunctionalPseudoClass = true;
+        subcontext.firstSelectorOfTheFragment = subselector;
+        PseudoId ignoreDynamicPseudo = NOPSEUDO;
+        if (matchRecursively(subcontext, ignoreDynamicPseudo) == SelectorMatches) {
+            ASSERT(ignoreDynamicPseudo == NOPSEUDO);
+            return true;
+        }
+    }
+    return false;
+}
+
 bool SelectorChecker::checkScrollbarPseudoClass(const CheckingContextWithStatus& context, const CSSSelector* selector) const
 {
     ASSERT(selector->match() == CSSSelector::PseudoClass);
diff --git a/Source/WebCore/css/SelectorChecker.h b/Source/WebCore/css/SelectorChecker.h
index 47c1c90..655b195 100644
--- a/Source/WebCore/css/SelectorChecker.h
+++ b/Source/WebCore/css/SelectorChecker.h
@@ -85,6 +85,7 @@
 private:
     Match matchRecursively(const CheckingContextWithStatus&, PseudoId&) const;
     bool checkOne(const CheckingContextWithStatus&) const;
+    bool matchSelectorList(const CheckingContextWithStatus&, Element&, const CSSSelectorList&) const;
 
     bool checkScrollbarPseudoClass(const CheckingContextWithStatus&, const CSSSelector*) const;
 
diff --git a/Source/WebCore/cssjit/SelectorCompiler.cpp b/Source/WebCore/cssjit/SelectorCompiler.cpp
index 3fddabf..2b5f2ef 100644
--- a/Source/WebCore/cssjit/SelectorCompiler.cpp
+++ b/Source/WebCore/cssjit/SelectorCompiler.cpp
@@ -239,6 +239,7 @@
 
     void generateSelectorChecker();
     void generateSelectorCheckerExcludingPseudoElements(Assembler::JumpList& failureCases, const SelectorFragmentList&);
+    Assembler::JumpList generateElementMatchesSelectorList(Assembler::RegisterID elementRegister, const Vector<SelectorFragmentList>&);
 
     // Element relations tree walker.
     void generateWalkToParentNode(Assembler::RegisterID targetRegister);
@@ -1597,6 +1598,45 @@
     m_backtrackingLevels.takeLast();
 }
 
+Assembler::JumpList SelectorCodeGenerator::generateElementMatchesSelectorList(Assembler::RegisterID elementRegister, const Vector<SelectorFragmentList>& selectorList)
+{
+    Assembler::JumpList matchFragmentList;
+    for (const SelectorFragmentList& selectorFragmentList : selectorList) {
+        Assembler::JumpList failureCases;
+
+        RegisterVector allocatedRegisters = m_registerAllocator.allocatedRegisters();
+        if (m_descendantBacktrackingStartInUse)
+            allocatedRegisters.remove(allocatedRegisters.find(m_descendantBacktrackingStart));
+        StackAllocator::StackReferenceVector allocatedRegistersOnStack = m_stackAllocator.push(allocatedRegisters);
+
+        StackAllocator successStack = m_stackAllocator;
+        StackAllocator failureStack = m_stackAllocator;
+
+        for (Assembler::RegisterID registerID : allocatedRegisters) {
+            if (registerID != elementAddressRegister)
+                m_registerAllocator.deallocateRegister(registerID);
+        }
+
+        if (elementRegister != elementAddressRegister)
+            m_assembler.move(elementRegister, elementAddressRegister);
+        generateSelectorCheckerExcludingPseudoElements(failureCases, selectorFragmentList);
+
+        for (Assembler::RegisterID registerID : allocatedRegisters) {
+            if (registerID != elementAddressRegister)
+                m_registerAllocator.allocateRegister(registerID);
+        }
+
+        successStack.pop(allocatedRegistersOnStack, allocatedRegisters);
+        matchFragmentList.append(m_assembler.jump());
+
+        failureCases.link(&m_assembler);
+        failureStack.pop(allocatedRegistersOnStack, allocatedRegisters);
+
+        m_stackAllocator.merge(WTF::move(successStack), WTF::move(failureStack));
+    }
+    return matchFragmentList;
+}
+
 static inline Assembler::Jump testIsElementFlagOnNode(Assembler::ResultCondition condition, Assembler& assembler, Assembler::RegisterID nodeAddress)
 {
     return assembler.branchTest32(condition, Assembler::Address(nodeAddress, Node::nodeFlagsMemoryOffset()), Assembler::TrustedImm32(Node::flagIsElement()));
@@ -3183,6 +3223,13 @@
         generateWalkToParentElement(failureCases, parentElement);
     }
 
+    // The initial element must match the selector list.
+    for (const NthChildOfSelectorInfo& nthChildOfSelectorInfo : fragment.nthChildOfFilters) {
+        Assembler::JumpList matchFragmentList = generateElementMatchesSelectorList(elementAddressRegister, nthChildOfSelectorInfo.selectorList);
+        failureCases.append(m_assembler.jump());
+        matchFragmentList.link(&m_assembler);
+    }
+
     Vector<const NthChildOfSelectorInfo*> validSubsetFilters;
     for (const NthChildOfSelectorInfo& nthChildOfSelectorInfo : fragment.nthChildOfFilters) {
         if (nthFilterIsAlwaysSatisified(nthChildOfSelectorInfo.a, nthChildOfSelectorInfo.b))
@@ -3205,53 +3252,19 @@
             LocalRegister previousSibling(m_registerAllocator);
             m_assembler.move(elementAddressRegister, previousSibling);
 
-            // Getting the child index is very efficient when it works. When there is no child index,
-            // querying at every iteration is very inefficient. We solve this by only testing the child
-            // index on the first direct adjacent.
             Assembler::JumpList noMoreSiblingsCases;
 
             Assembler::Label loopStart = m_assembler.label();
+
             generateWalkToPreviousAdjacentElement(noMoreSiblingsCases, previousSibling);
             markElementIfResolvingStyle(previousSibling, Node::flagAffectsNextSiblingElementStyle());
 
-            Assembler::JumpList matchFragmentList;
-            for (const SelectorFragmentList& selectorFragmentList : nthChildOfSelectorInfo->selectorList) {
-                Assembler::JumpList failureCases;
-
-                RegisterVector allocatedRegisters = m_registerAllocator.allocatedRegisters();
-                if (m_descendantBacktrackingStartInUse)
-                    allocatedRegisters.remove(allocatedRegisters.find(m_descendantBacktrackingStart));
-                StackAllocator::StackReferenceVector allocatedRegistersOnStack = m_stackAllocator.push(allocatedRegisters);
-
-                StackAllocator successStack = m_stackAllocator;
-                StackAllocator failureStack = m_stackAllocator;
-
-                for (Assembler::RegisterID registerID : allocatedRegisters) {
-                    if (registerID != elementAddressRegister)
-                        m_registerAllocator.deallocateRegister(registerID);
-                }
-
-                m_assembler.move(previousSibling, elementAddressRegister);
-                generateSelectorCheckerExcludingPseudoElements(failureCases, selectorFragmentList);
-
-                for (Assembler::RegisterID registerID : allocatedRegisters) {
-                    if (registerID != elementAddressRegister)
-                        m_registerAllocator.allocateRegister(registerID);
-                }
-
-                successStack.pop(allocatedRegistersOnStack, allocatedRegisters);
-                matchFragmentList.append(m_assembler.jump());
-
-                failureCases.link(&m_assembler);
-                failureStack.pop(allocatedRegistersOnStack, allocatedRegisters);
-
-                m_stackAllocator.merge(WTF::move(successStack), WTF::move(failureStack));
-            }
+            Assembler::JumpList matchFragmentList = generateElementMatchesSelectorList(previousSibling, nthChildOfSelectorInfo->selectorList);
             m_assembler.jump().linkTo(loopStart, &m_assembler);
-
             matchFragmentList.link(&m_assembler);
             m_assembler.add32(Assembler::TrustedImm32(1), elementCounter);
             m_assembler.jump().linkTo(loopStart, &m_assembler);
+
             noMoreSiblingsCases.link(&m_assembler);
         }