<!doctype html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<style>
target {
    background-color: white;
}
target:nth-last-child(n+5 of .webkit) {
    background-color: rgb(1, 2, 3);
}
</style>
</head>
<body>
    <div id="with-renderer">
        <target class="element_1 webkit"></target>
        <target class="element_2 webkit"></target>
        <target class="element_3 webkit"></target>
        <target class="element_4 webkit"></target>
        <target class="element_5 webkit"></target>
    </div>
    <div id="without-renderer" style="display:none;">
        <target class="element_1 webkit"></target>
        <target class="element_2 webkit"></target>
        <target class="element_3 webkit"></target>
        <target class="element_4 webkit"></target>
        <target class="element_5 webkit"></target>
    </div>
</body>
<script>
description('Test style update of :nth-last-child() when the tree structure is modified.');

function testColor(classesThatShouldMatch) {
    var allTargets = document.querySelectorAll("target");
    for (var i = 0; i < allTargets.length; ++i) {
        var expectMath = classesThatShouldMatch.indexOf(allTargets[i].classList[0]) != -1;
        shouldBeEqualToString('getComputedStyle(document.querySelectorAll("target")[' + i + ']).backgroundColor', expectMath ? 'rgb(1, 2, 3)' : 'rgb(255, 255, 255)');
    }
}

function appendElement(tagName, className)
{
    var newElement = document.createElement(tagName);
    newElement.className = className + ' webkit';

    var withRenderer = document.getElementById("with-renderer");
    withRenderer.appendChild(newElement);

    var withoutRenderer = document.getElementById("without-renderer");
    withoutRenderer.appendChild(newElement.cloneNode());
}

function removeWebKitClassFromElementsOfClass(className)
{
    var allElementsToUpdate = document.querySelectorAll('.' + className);
    for (var i = 0; i < allElementsToUpdate.length; ++i)
        allElementsToUpdate[i].classList.remove("webkit");
}

function addWebKitClassToElementsOfClass(className)
{
    var allElementsToUpdate = document.querySelectorAll('.' + className);
    for (var i = 0; i < allElementsToUpdate.length; ++i)
        allElementsToUpdate[i].classList.add("webkit");
}


function removeElementsOfClass(className)
{
    var allElementsToRemove = document.querySelectorAll('.' + className);
    for (var i = 0; i < allElementsToRemove.length; ++i)
        allElementsToRemove[i].parentElement.removeChild(allElementsToRemove[i]);
}

debug("Initial state.");
testColor(["element_1"]);

debug("Removing the .webkit class from the fifth element, nothing should match.");
removeWebKitClassFromElementsOfClass("element_5");
testColor([]);

debug("Adding it back, we should be back to the original state.");
addWebKitClassToElementsOfClass("element_5");
testColor(["element_1"]);

debug("Removing the .webkit class from the third element, nothing should match.");
removeWebKitClassFromElementsOfClass("element_3");
testColor([]);

debug("Adding it back, we should be back to the original state.");
addWebKitClassToElementsOfClass("element_3");
testColor(["element_1"]);

debug("Removing the .webkit class from the second element, nothing should match.");
removeWebKitClassFromElementsOfClass("element_2");
testColor([]);

debug("Appending we should have a match again, skipping 2.");
appendElement("target", "element_6")
testColor(["element_1"]);

debug("Adding the .webkit class back on 2, we should now match 2 elements.");
addWebKitClassToElementsOfClass("element_2");
testColor(["element_1", "element_2"]);

debug("Removing the element we added should put us back in the initial state.");
removeElementsOfClass("element_6");
testColor(["element_1"]);

debug("Appending an element, we should match the first two targets again.");
appendElement("target", "element_6")
testColor(["element_1", "element_2"]);

// Using nottarget is interesting because the ':nth-last-child()' part is not matched for those elements.
debug("Appending an element &lt;nottarget&gt;, we should now match 1, 2, 3.");
appendElement("nottarget", "element_-1")
testColor(["element_1", "element_2", "element_3"]);

debug("Appending an element &lt;nottarget&gt;, we should now match 1, 2, 3, 4.");
appendElement("nottarget", "element_-2")
testColor(["element_1", "element_2", "element_3", "element_4"]);

debug("Appending an element &lt;target&gt;, we should now match 1, 2, 3, 4 and 5.");
appendElement("target", "element_-3")
testColor(["element_1", "element_2", "element_3", "element_4", "element_5"]);

debug("Removing one of the &lt;nottarget&gt;, 5 should no longer match.");
removeElementsOfClass("element_-2");
testColor(["element_1", "element_2", "element_3", "element_4"]);

debug("Removing the class from one of the &lt;nottarget&gt;, 4 should no longer match.");
removeWebKitClassFromElementsOfClass("element_-1");
testColor(["element_1", "element_2", "element_3"]);

debug("Removing one of the &lt;notarget&gt;, 3 should no longer match.");
removeElementsOfClass("element_-3");
testColor(["element_1", "element_2"]);

debug("Removing the class from one of the &lt;target&gt;, 2 should no longer match.");
removeWebKitClassFromElementsOfClass("element_3");
testColor(["element_1"]);

debug("Removing one of the &lt;target&gt;, nothing should match.");
removeElementsOfClass("element_4");
testColor([]);

</script>
<script src="../../resources/js-test-post.js"></script>
</html>
