<!doctype html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<style>
* {
    background-color: white;
    color: black;
}
:nth-child(4)+padding+padding+padding+bar {
    background-color: rgb(1, 2, 3);
}
:nth-child(4)+padding+padding+padding+bar baz {
    color: rgb(4, 5, 6);
}
</style>
</head>
<body>
    <div id="with-renderer">
        <padding></padding>
        <padding></padding>
        <foo></foo>
        <padding></padding>
        <padding></padding>
        <padding></padding>
        <bar id="bar-with-renderer">
            <baz id="baz-with-renderer"></baz>
        </bar>
    </div>
    <div id="without-renderer" style="display:none;">
        <padding></padding>
        <padding></padding>
        <foo></foo>
        <padding></padding>
        <padding></padding>
        <padding></padding>
        <bar id="bar-without-renderer">
            <baz id="baz-without-renderer"></baz>
        </bar>
    </div>
</body>
<script>
description('Test style update caused by :nth-child() changes on a direct adjacent. This test does not use any sibling "~" combinator to avoid its more generic marking.');

function testColor(expectMatch) {
    shouldBeEqualToString('getComputedStyle(document.getElementById("bar-with-renderer")).backgroundColor', expectMatch ? 'rgb(1, 2, 3)' : 'rgb(255, 255, 255)');
    shouldBeEqualToString('getComputedStyle(document.getElementById("baz-with-renderer")).backgroundColor', 'rgb(255, 255, 255)');
    shouldBeEqualToString('getComputedStyle(document.getElementById("bar-without-renderer")).backgroundColor', expectMatch ? 'rgb(1, 2, 3)' : 'rgb(255, 255, 255)');
    shouldBeEqualToString('getComputedStyle(document.getElementById("baz-without-renderer")).backgroundColor', 'rgb(255, 255, 255)');

    shouldBeEqualToString('getComputedStyle(document.getElementById("bar-with-renderer")).color', 'rgb(0, 0, 0)');
    shouldBeEqualToString('getComputedStyle(document.getElementById("baz-with-renderer")).color', expectMatch ? 'rgb(4, 5, 6)' : 'rgb(0, 0, 0)');
    shouldBeEqualToString('getComputedStyle(document.getElementById("bar-without-renderer")).color', 'rgb(0, 0, 0)');
    shouldBeEqualToString('getComputedStyle(document.getElementById("baz-without-renderer")).color', expectMatch ? 'rgb(4, 5, 6)' : 'rgb(0, 0, 0)');
}

function addElementAsFirstChild(tagName, className)
{
    var newElement = document.createElement(tagName);
    newElement.className = className;

    var withRenderer = document.getElementById("with-renderer");
    withRenderer.insertBefore(newElement, withRenderer.firstChild);

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

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

debug("Initially, &lt;foo&gt; is the third child and does not match.");
testColor(false);

debug("Adding one element before moves &lt;foo&gt; to the fourth position, we should have a match.");
addElementAsFirstChild("padding", "padding1")
testColor(true);

debug("Adding one more element before moves &lt;foo&gt; to the fifth position, we should no longer match.");
addElementAsFirstChild("padding", "padding2")
testColor(false);

debug("Removing one element, we should be back to 4 and have a match.");
removeElementsOfClass("padding1");
testColor(true);

debug("Removing one more element, we should be back to the initial state.");
removeElementsOfClass("padding2");
testColor(false);
</script>
<script src="../../resources/js-test-post.js"></script>
</html>
