<!doctype html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<style id="style-container">
</style>
</head>
<body>
</body>
<script>
description('Test the parsing of :nth-child(of) for querySelector and style.');

function testValidSelector(selectorString, expectedSerializedSelector)
{
    shouldNotThrow('document.querySelector(":nth-child(' + selectorString.replace(/\\/g, '\\\\') + ')")');

    var styleContainer = document.getElementById('style-container');
    styleContainer.innerHTML = ':nth-child(' + selectorString + ') { }';
    shouldBe("document.getElementById('style-container').sheet.cssRules.length", "1");
    if (!expectedSerializedSelector)
        expectedSerializedSelector = selectorString;
    shouldBeEqualToString("document.getElementById('style-container').sheet.cssRules[0].selectorText", ':nth-child(' + expectedSerializedSelector + ')');
    styleContainer.innerHTML = '';
}

// There are multiple ways of parsing :nth-child() based on the An+B part, we should test everything.
var validNthAnPlusB = [
    "even",
    "odd",
    "n",
    "-n",
    "3",
    "-3",
    "n+0",
    "n-0",
    "0n",
    "3n+5",
    "-3n+5",
    "3n-5",
    "-3n-5",
];

var expectedNthAnPlusB = [
    "2n",
    "2n+1",
    "n",
    "-n",
    "3",
    "-3",
    "n",
    "n",
    "0",
    "3n+5",
    "-3n+5",
    "3n-5",
    "-3n-5",
];

debug("Basic valid cases without [of selectors]");
for (var i = 0; i < validNthAnPlusB.length; ++i)
    testValidSelector(validNthAnPlusB[i], expectedNthAnPlusB[i]);

var validRightSide = [
    // Basic types.
    "*",
    "foobar",
    "#id",
    ".class",
    ":first-child",
    ":last-child",

    // Compound selectors.
    "foobar#id.class",
    ".class:not(.notclass)",
    "#id:empty",

    // Complex selectors.
    "a > b",
    "a b",
    "a + b",
    "a ~ b",
    "a + b > c ~ d e + g",

    // Selector lists.
    "a, a",
    "a, b",
    "a, b, c, d",
    ".foo, .bar, .baz",
    "a > b, a b, a + b, a ~ b",
];

debug("Basic cases with [of selectors]");
for (var leftSideIndex = 0; leftSideIndex < validNthAnPlusB.length; ++leftSideIndex) {
    for (var rightSideIndex = 0; rightSideIndex < validRightSide.length; ++rightSideIndex) {
        var selectorString = validNthAnPlusB[leftSideIndex] + " of " + validRightSide[rightSideIndex];
        var expectedSelectorString = expectedNthAnPlusB[leftSideIndex] + " of " + validRightSide[rightSideIndex];
        testValidSelector(selectorString, expectedSelectorString);
        testValidSelector(selectorString + "    ", expectedSelectorString);
    }
}

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