<!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">
        <testcase id="testcase1"></testcase>
        <testcase id="testcase2"></testcase>
        <testcase id="testcase3"></testcase>
        <testcase id="testcase4"></testcase>
        <testcase id="testcase5"></testcase>
        <testcase id="testcase6"></testcase>
        <testcase id="testcase7"></testcase>
        <testcase id="testcase8"></testcase>
        <testcase id="testcase9"></testcase>
        <testcase id="testcase10"></testcase>
        <testcase id="testcase11"></testcase>
    </div>
</body>
<script>
description('Test basic uses cases of :nth-child().');

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("");
}

// Test cases presented on http://nthmaster.com/ to have a simple baseline.
testSelector(":nth-child(8)", ["testcase8"]);
testSelector(":nth-child(n+6)", ["testcase6", "testcase7", "testcase8", "testcase9", "testcase10", "testcase11"]);
testSelector(":nth-child(-n+9)", ["testcase1", "testcase2", "testcase3", "testcase4", "testcase5", "testcase6", "testcase7", "testcase8", "testcase9"]);
testSelector(":nth-child(n+4):nth-child(-n+8)", ["testcase4", "testcase5", "testcase6", "testcase7", "testcase8"]);
testSelector(":nth-child(-n+8):nth-child(n+4)", ["testcase4", "testcase5", "testcase6", "testcase7", "testcase8"]);
testSelector(":nth-child(n+2):nth-child(odd):nth-child(-n+9)", ["testcase3", "testcase5", "testcase7", "testcase9"]);
testSelector(":nth-child(3n+1):nth-child(even)", ["testcase4", "testcase10"]);
testSelector(":nth-child(even):nth-child(3n+1)", ["testcase4", "testcase10"]);

// The following was using :nth-child() on http://nthmaster.com/. It is adapted here for completness.
testSelector(":nth-child(3)", ["testcase3"]);
testSelector(":nth-child(4)", ["testcase4"]);
testSelector(":nth-child(3):nth-child(4)", []);
testSelector(":nth-child(4):nth-child(3)", []);

testSelector(":nth-child(n+3)", ["testcase3", "testcase4", "testcase5", "testcase6", "testcase7", "testcase8", "testcase9", "testcase10", "testcase11"]);
testSelector(":nth-child(2n+2)", ["testcase2", "testcase4", "testcase6", "testcase8", "testcase10"]);
testSelector(":nth-child(n+3):nth-child(2n+2)", ["testcase4", "testcase6", "testcase8", "testcase10"]);
testSelector(":nth-child(2n+2):nth-child(n+3)", ["testcase4", "testcase6", "testcase8", "testcase10"]);

testSelector(":nth-child(-n+4)", ["testcase1", "testcase2", "testcase3", "testcase4"]);
testSelector(":nth-child(-n+5)", ["testcase1", "testcase2", "testcase3", "testcase4", "testcase5"]);
testSelector(":nth-child(-n+4):nth-child(-n+5)", ["testcase1", "testcase2", "testcase3", "testcase4"]);
testSelector(":nth-child(-n+5):nth-child(-n+4)", ["testcase1", "testcase2", "testcase3", "testcase4"]);

testSelector(":nth-child(n+3):nth-child(-n+6)", ["testcase3", "testcase4", "testcase5", "testcase6"]);
testSelector(":nth-child(-n+6):nth-child(n+3)", ["testcase3", "testcase4", "testcase5", "testcase6"]);
testSelector(":nth-child(n+1):nth-child(-n+3)", ["testcase1", "testcase2", "testcase3"]);
testSelector(":nth-child(-n+3):nth-child(n+1)", ["testcase1", "testcase2", "testcase3"]);
testSelector(":nth-child(n+3):nth-child(-n+6):nth-child(n+1):nth-child(-n+3)", ["testcase3"]);
testSelector(":nth-child(-n+6):nth-child(n+3):nth-child(-n+3):nth-child(n+1)", ["testcase3"]);

testSelector(":nth-child(n+3):nth-child(odd):nth-child(-n+6)", ["testcase3", "testcase5"]);
testSelector(":nth-child(-n+6):nth-child(odd):nth-child(n+3)", ["testcase3", "testcase5"]);
testSelector(":nth-child(odd):nth-child(n+3):nth-child(-n+6)", ["testcase3", "testcase5"]);
testSelector(":nth-child(odd):nth-child(-n+6):nth-child(n+3)", ["testcase3", "testcase5"]);
testSelector(":nth-child(n+3):nth-child(-n+6):nth-child(odd)", ["testcase3", "testcase5"]);
testSelector(":nth-child(-n+6):nth-child(n+3):nth-child(odd)", ["testcase3", "testcase5"]);

testSelector(":nth-child(n+1):nth-child(even):nth-child(-n+3)", ["testcase2"]);
testSelector(":nth-child(-n+3):nth-child(even):nth-child(n+1)", ["testcase2"]);
testSelector(":nth-child(even):nth-child(n+1):nth-child(-n+3)", ["testcase2"]);
testSelector(":nth-child(even):nth-child(-n+3):nth-child(n+1)", ["testcase2"]);
testSelector(":nth-child(-n+3):nth-child(n+1):nth-child(even)", ["testcase2"]);
testSelector(":nth-child(n+1):nth-child(-n+3):nth-child(even)", ["testcase2"]);

testSelector(":nth-child(n+3):nth-child(odd):nth-child(-n+6):nth-child(n+1):nth-child(-n+3):nth-child(even)", []);
</script>
<script src="../../resources/js-test-post.js"></script>
</html>
