<!doctype html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<style>
span.target {
    background-color:rgb(0,0,0);
    color:rgb(0,0,0);
}

a + :nth-child(n+1 of a + a ~ b) span.target {
    background-color:rgb(1,2,3);
}
b:nth-child(n+1 of a + a ~ b) span.target {
    color:rgb(1,2,3);
}

:nth-child(n+1 of c + c + c ~ d) span.target {
    background-color:rgb(4,5,6);
}
c + c + c + d:nth-child(n+1 of c + c + c ~ d) span.target {
    color:rgb(4,5,6);
}

f:nth-child(n+1 of e + e ~ f) ~ g span.target {
    background-color:rgb(7,8,9);
}
e + f:nth-child(n+1 of e + e ~ f) ~ g span.target {
    color:rgb(7,8,9);
}

span.target:nth-child(n+1 of h + h ~ span.target) {
    background-color:rgb(10,11,12);
}
h + span.target:nth-child(n+1 of h + h ~ span.target) {
    color:rgb(10,11,12);
}

j:nth-child(n+2 of j+j+j)~j>k span.target {
    background-color:rgb(13,14,15);
}
j+j+j:nth-child(n+2 of j+j+j)~j>k span.target {
    color:rgb(13,14,15);
}

m l:nth-child(n+2 of l+l+l+l)~m>m>m span.target {
    background-color:rgb(16,17,18);
}
m l+l+l+l:nth-child(n+2 of l+l+l+l)~m>m>m span.target {
    color:rgb(16,17,18);
}

n o:nth-child(n+2 of o)~n>n>n span.target {
    background-color:rgb(19,20,21);
}

p:nth-child(n+2 of p) ~ span.target {
    background-color:rgb(22,23,24);
}

q:nth-child(n+2 of q) ~ r span.target {
    background-color:rgb(25,26,27);
}

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+1 of t+t+t+s)>s>s span.target {
    color:rgb(28,29,30);
}
</style>
</head>
<body>
<div style="display:none">
    <!-- a + b span.target -->
    <target1>
        <a></a>
        <a></a>
        <b>
            <span class="target" id="target1"></span>
        </b>
    </target1>

    <!-- a + b span.target -->
    <target2>
        <a></a>
        <a></a>
        <b>
            <b></b>  <!-- Fail here and restart backtracking. -->
            <b>
                <span class="target" id="target2.1"></span>
            </b>
        </b>
    </target2>

    <!-- a + b span.target -->
    <target2>
        <a></a>
        <a></a>
        <b>
            <b></b>  <!-- Fail here and restart backtracking. -->
            <b>
                <b></b>  <!-- Fail here and restart backtracking. -->
                <a></a>
                <b>
                    <span class="target" id="target2.2"></span>
                </b>
            </b>
        </b>
    </target2>

    <!-- c + c + c + d span.target -->
    <target3>
        <c></c>
        <c></c>
        <c></c>
        <d>
            <b></b>  <!-- Fail here and restart backtracking with the parent of the current element. -->
            <c></c>
            <c></c>
            <d>
                <c></c>
                <b></b>  <!-- Fail here and restart backtracking with the parent of the current element. -->
                <d>
                    <b></b>  <!-- Fail here and restart backtracking with the parent of the current element. -->
                    <c></c>
                    <d>
                        <span class="target" id="target3"></span>
                    </d>
                </d>
            </d>
        </d>
    </target3>

    <!-- e + f ~ g span.target -->
    <target4>
        <e></e>
        <e></e>
        <f></f>
        <d></d>
        <e></e> <!-- Fail here and restart backtracking indirect adjacent matching. -->
        <f></f>
        <d></d>
        <d></d>
        <g>
            <d></d>  <!-- Fail here and restart backtracking indirect adjacent matching. -->
            <f></f>
            <g>
                <span class="target" id="target4"></span>
            </g>
        </g>
    </target4>

    <!-- h + span.target -->
    <target6>
        <h></h>
        <h></h>
        <span class="target" id="target6.1"></span>
    </target6>

    <!-- h + span.target -->
    <target6>
        <h></h>
        <a></a>
        <span class="target" id="target6.2"></span>
    </target6>


    <!-- h + span.target -->
    <target6>
        <a></a>
        <h></h>
        <span class="target" id="target6.3"></span>
    </target6>

    <!-- h + span.target -->
    <target6>
        <span class="target" id="target6.4"></span>
    </target6>

    <!-- j+j+j~j>k span.target -->
    <target7>
        <d></d>  <!-- Fail here. -->
        <j></j>
        <j></j>
        <j></j>
        <k>
            <span class="target" id="target7"></span>
        </k>
    </target7>

    <!-- j+j+j~j>k span.target -->
    <target8>
        <j></j>  <!-- Match j+j+j:nth-child(n+2 of j+j+j)~j>k. -->
        <j></j>
        <j></j>
        <j></j>
        <d></d>  <!-- Fail here. -->
        <j></j>  <!-- Match j:nth-child(n+2 of j+j+j)~j>k here, fail j+j+j:nth-child(n+2 of j+j+j)~j>k. -->
        <j></j>
        <j></j>
        <d></d>  <!-- Fail here. -->
        <j></j>
        <j></j>
        <d></d>  <!-- Fail here. -->
        <j></j>
        <j>
            <k>
                <span class="target" id="target8"></span>
            </k>
        </j>
    </target8>

    <!-- m l+l+l+l~m>m>m span.target -->
    <target9>
        <m>
            <l></l>  <!-- Fail here -->
            <l></l>
            <l></l>
            <l></l>
            <m>
                <l></l>  <!-- Fail here -->
                <l></l>
                <l></l>
                <m>
                    <a></a>  <!-- Fail here and backtrack with the tail -->
                    <l></l>
                    <l></l>
                    <a></a>  <!-- Fail here -->
                    <l></l>
                    <m>
                        <m>
                            <m>
                                <span class="target" id="target9"></span>
                            </m>
                        </m>
                    </m>
                </m>
            </m>
        </m>
    </target9>

    <!-- m l+l+l+l~m>m>m span.target -->
    <target10>
        <m>
            <l></l>  <!-- Match here -->
            <l></l>
            <l></l>
            <l></l>
            <l></l>
            <m>
                <l></l>  <!-- Fail here -->
                <l></l>
                <l></l>
                <l></l>
                <m>
                    <a></a>  <!-- Fail here and backtrack with the tail -->
                    <l></l>
                    <l></l>
                    <a></a>  <!-- Fail here -->
                    <l></l>
                    <m>
                        <m>
                            <m>
                                <span class="target" id="target10"></span>
                            </m>
                        </m>
                    </m>
                </m>
            </m>
        </m>
    </target10>

    <!-- n o~n>n>n span.target -->
    <target11>
        <n>
            <n>
                <a></a>  <!-- Fail here -->
                <o></o>
                <a></a>  <!-- Fail here -->
                <n>
                    <a></a>  <!-- Fail here and backtrack with the tail -->
                    <a></a>  <!-- Fail here -->
                    <n>
                        <n>
                            <n>
                                <span class="target" id="target11"></span>
                            </n>
                        </n>
                    </n>
                </n>
            </n>
        </n>
    </target11>

    <!-- n o~n>n>n span.target -->
    <target12>
        <n>
            <o></o>  <!-- Match here -->
            <o></o>
            <a></a>  <!-- Fail here -->
            <o></o>
            <a></a>  <!-- Fail here -->
            <n>
                <a></a>  <!-- Fail here and backtrack with the tail -->
                <a></a>  <!-- Fail here -->
                <n>
                    <n>
                        <n>
                            <span class="target" id="target12"></span>
                        </n>
                    </n>
                </n>
            </n>
        </n>
    </target12>

    <!-- p:nth-child(n+2 of p) ~ span.target -->
    <target13>
        <p></p>  <!-- Match here -->
        <p></p>
        <span class="target" id="target13.1"></span>
    </target13>

    <!-- p ~ span.target -->
    <target13>
        <a></a>  <!-- Fail here -->
        <p></p>
        <span class="target" id="target13.2"></span>
    </target13>

    <!-- p ~ span.target -->
    <target13>
        <!-- Fail here -->
        <span class="target" id="target13.3"></span>
    </target13>

    <!-- p ~ span.target -->
    <target13>
        <a></a>  <!-- Fail here -->
        <span class="target" id="target13.4"></span>
    </target13>

    <!-- q ~ r span.target -->
    <target14>
        <q></q>  <!-- Match here -->
        <q></q>
        <a></a>
        <r>
            <a></a>  <!-- Fail here -->
            <r>
                <span class="target" id="target14.1"></span>
            </r>
        </r>
    </target14>

    <!-- q ~ r span.target -->
    <target14>
        <!-- Fail here -->
        <r>
            <a></a>  <!-- Fail here -->
            <r>
                <span class="target" id="target14.2"></span>
            </r>
        </r>
    </target14>

    <!-- q ~ r span.target -->
    <target14>
        <!-- Fail here -->
        <r>
            <!-- Fail here -->
            <r>
                <span class="target" id="target14.3"></span>
            </r>
        </r>
    </target14>

    <!-- q ~ r span.target -->
    <target14>
        <q></q>  <!-- Match here -->
        <a></a>
        <r>
            <a></a>  <!-- Fail here -->
            <r>
                <span class="target" id="target14.4"></span>
            </r>
        </r>
    </target14>

    <!-- s t+t+t+s>s>s span.target -->
    <target15>
        <s>
            <!-- Fail here and backtrack with the tail -->
            <t></t>
            <t></t>
            <s>
                <a></a>  <!-- Fail here and backtrack with the tail -->
                <t></t>
                <t></t>
                <s>
                    <s>
                        <s>
                            <span class="target" id="target15"></span>
                        </s>
                    </s>
                </s>
            </s>
        </s>
    </target15>

    <!-- s t+t+t+s>s>s span.target -->
    <target16>
        <s>
            <t></t>  <!-- Match here -->
            <t></t>
            <t></t>
            <s>
                <!-- Fail here and backtrack with the tail -->
                <t></t>
                <t></t>
                <s>
                    <a></a>  <!-- Fail here and backtrack with the tail -->
                    <t></t>
                    <t></t>
                    <s>
                        <s>
                            <s>
                                <span class="target" id="target16"></span>
                            </s>
                        </s>
                    </s>
                </s>
            </s>
        </s>
    </target16>
</div>
</body>
<script>
description('The backtracking from adjacent combinators combined with :nth-child(An+B of selector)');

debug("Backtracking without tail, succeeded without backtracking");
shouldBeEqualToString('getComputedStyle(document.getElementById("target1")).backgroundColor', 'rgb(1, 2, 3)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target1")).color', 'rgb(1, 2, 3)');

debug("Backtracking without tail without indirect adjacent, failed and restart.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target2.1")).backgroundColor', 'rgb(1, 2, 3)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target2.1")).color', 'rgb(1, 2, 3)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target2.2")).backgroundColor', 'rgb(1, 2, 3)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target2.2")).color', 'rgb(1, 2, 3)');

debug("Backtracking without tail, 3 direct adjacents without indirect adjacent, failed and restart backtracking.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target3")).backgroundColor', 'rgb(4, 5, 6)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target3")).color', 'rgb(4, 5, 6)');

debug("Backtracking without tail, indirect adjacent.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target4")).backgroundColor', 'rgb(7, 8, 9)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target4")).color', 'rgb(7, 8, 9)');

debug("Backtracking from direct adjacent without tail. Matches.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target6.1")).backgroundColor', 'rgb(10, 11, 12)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target6.1")).color', 'rgb(10, 11, 12)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target6.2")).backgroundColor', 'rgb(0, 0, 0)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target6.2")).color', 'rgb(0, 0, 0)');


debug("Backtracking from direct adjacent tag matching without tail. Fails.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target6.3")).backgroundColor', 'rgb(0, 0, 0)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target6.3")).color', 'rgb(0, 0, 0)');

debug("Backtracking from direct adjacent traversal without tail. Fails.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target6.4")).backgroundColor', 'rgb(0, 0, 0)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target6.4")).color', 'rgb(0, 0, 0)');

debug("Backtracking without tail. And fails.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target7")).backgroundColor', 'rgb(0, 0, 0)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target7")).color', 'rgb(0, 0, 0)');

debug("Backtracking without tail. And Matches.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target8")).backgroundColor', 'rgb(13, 14, 15)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target8")).color', 'rgb(13, 14, 15)');

debug("Backtracking from direct adjacent with tail. And fails.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target9")).backgroundColor', 'rgb(0, 0, 0)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target9")).color', 'rgb(0, 0, 0)');

debug("Backtracking from direct adjacent with tail. And Matches.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target10")).backgroundColor', 'rgb(16, 17, 18)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target10")).color', 'rgb(16, 17, 18)');

debug("Backtracking from indirect adjacent with tail. And fails.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target11")).backgroundColor', 'rgb(0, 0, 0)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target11")).color', 'rgb(0, 0, 0)');

debug("Backtracking from indirect adjacent with tail. And Matches.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target12")).backgroundColor', 'rgb(19, 20, 21)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target12")).color', 'rgb(0, 0, 0)');

debug("Backtracking from indirect adjacent without tail. Matches.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target13.1")).backgroundColor', 'rgb(22, 23, 24)');

debug("Backtracking from indirect adjacent tag matching without tail. Fails.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target13.2")).backgroundColor', 'rgb(0, 0, 0)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target13.3")).backgroundColor', 'rgb(0, 0, 0)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target13.4")).backgroundColor', 'rgb(0, 0, 0)');

debug("Backtracking from indirect adjacent without tail. Matches.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target14.1")).backgroundColor', 'rgb(25, 26, 27)');

debug("Backtracking from indirect adjacent tag matching without tail. Fails.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target14.2")).backgroundColor', 'rgb(0, 0, 0)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target14.3")).backgroundColor', 'rgb(0, 0, 0)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target14.4")).backgroundColor', 'rgb(0, 0, 0)');

debug("Backtracking from direct adjacent with tail. And fails.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target15")).backgroundColor', 'rgb(0, 0, 0)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target15")).color', 'rgb(0, 0, 0)');

debug("Backtracking from direct adjacent with tail. And Matches.");
shouldBeEqualToString('getComputedStyle(document.getElementById("target16")).backgroundColor', 'rgb(28, 29, 30)');
shouldBeEqualToString('getComputedStyle(document.getElementById("target16")).color', 'rgb(28, 29, 30)');
</script>
<script src="../../resources/js-test-post.js"></script>
</html>
