<!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 attribute selector with the case-insensitive value flag (e.g. [foo=bar i]). This test cover the more advanced cases that do not use a canonical form but are still valid.');

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

function testValidSelector(selectorString, expectedSerializedSelector) {
    shouldNotThrow('document.querySelector("' + selectorString.replace(/"/g, '\\"') + '")');
    shouldNotThrow('document.querySelector("' + selectorString.replace(/"/g, '\\"').replace('[', '[*|') + '")');
    shouldNotThrow('document.querySelector("' + selectorString.replace(/"/g, '\\"').replace('[', '[|') + '")');

    testValidSelectorCSSOMSerialization(selectorString, expectedSerializedSelector);
    testValidSelectorCSSOMSerialization(selectorString.replace('[', '[|'), expectedSerializedSelector);
    testValidSelectorCSSOMSerialization(selectorString.replace('[', '[*|'), expectedSerializedSelector.replace('[', '[*|'));
}

var alternativeValidSelectors = [
    // Uppercase 'i'.
    ['[foo=bar I]', '[foo="bar" i]'],
    ['[foo="bar" I]', '[foo="bar" i]'],
    ['[foo~=bar I]', '[foo~="bar" i]'],
    ['[foo~="bar" I]', '[foo~="bar" i]'],
    ['[foo|=bar I]', '[foo|="bar" i]'],
    ['[foo|="bar" I]', '[foo|="bar" i]'],
    ['[foo^=bar I]', '[foo^="bar" i]'],
    ['[foo^="bar" I]', '[foo^="bar" i]'],
    ['[foo$=bar I]', '[foo$="bar" i]'],
    ['[foo$="bar" I]', '[foo$="bar" i]'],
    ['[foo*=bar I]', '[foo*="bar" i]'],
    ['[foo*="bar" I]', '[foo*="bar" i]'],

    // Make sure the case of the value is preserved on serialization.
    ['[foo=BaR i]', '[foo="BaR" i]'],
    ['[foo="BaR" i]', '[foo="BaR" i]'],
    ['[foo~=BaR i]', '[foo~="BaR" i]'],
    ['[foo~="BaR" i]', '[foo~="BaR" i]'],
    ['[foo|=BaR i]', '[foo|="BaR" i]'],
    ['[foo|="BaR" i]', '[foo|="BaR" i]'],
    ['[foo^=BaR i]', '[foo^="BaR" i]'],
    ['[foo^="BaR" i]', '[foo^="BaR" i]'],
    ['[foo$=BaR i]', '[foo$="BaR" i]'],
    ['[foo$="BaR" i]', '[foo$="BaR" i]'],
    ['[foo*=BaR i]', '[foo*="BaR" i]'],
    ['[foo*="BaR" i]', '[foo*="BaR" i]'],

    // Tab instead of space before the 'i'.
    ['[foo=bar\ti]', '[foo="bar" i]'],
    ['[foo="bar"\ti]', '[foo="bar" i]'],
    ['[foo~=bar\ti]', '[foo~="bar" i]'],
    ['[foo~="bar"\ti]', '[foo~="bar" i]'],
    ['[foo|=bar\ti]', '[foo|="bar" i]'],
    ['[foo|="bar"\ti]', '[foo|="bar" i]'],
    ['[foo^=bar\ti]', '[foo^="bar" i]'],
    ['[foo^="bar"\ti]', '[foo^="bar" i]'],
    ['[foo$=bar\ti]', '[foo$="bar" i]'],
    ['[foo$="bar"\ti]', '[foo$="bar" i]'],
    ['[foo*=bar\ti]', '[foo*="bar" i]'],
    ['[foo*="bar"\ti]', '[foo*="bar" i]'],

    // Spacing after the 'i'.
    ['[foo=bar i ]', '[foo="bar" i]'],
    ['[foo="bar" i ]', '[foo="bar" i]'],
    ['[foo~=bar i ]', '[foo~="bar" i]'],
    ['[foo~="bar" i ]', '[foo~="bar" i]'],
    ['[foo|=bar i ]', '[foo|="bar" i]'],
    ['[foo|="bar" i ]', '[foo|="bar" i]'],
    ['[foo^=bar i ]', '[foo^="bar" i]'],
    ['[foo^="bar" i ]', '[foo^="bar" i]'],
    ['[foo$=bar i ]', '[foo$="bar" i]'],
    ['[foo$="bar" i ]', '[foo$="bar" i]'],
    ['[foo*=bar i ]', '[foo*="bar" i]'],
    ['[foo*="bar" i ]', '[foo*="bar" i]'],

    ['[foo=bar i\t]', '[foo="bar" i]'],
    ['[foo="bar" i\t]', '[foo="bar" i]'],
    ['[foo~=bar i\t]', '[foo~="bar" i]'],
    ['[foo~="bar" i\t]', '[foo~="bar" i]'],
    ['[foo|=bar i\t]', '[foo|="bar" i]'],
    ['[foo|="bar" i\t]', '[foo|="bar" i]'],
    ['[foo^=bar i\t]', '[foo^="bar" i]'],
    ['[foo^="bar" i\t]', '[foo^="bar" i]'],
    ['[foo$=bar i\t]', '[foo$="bar" i]'],
    ['[foo$="bar" i\t]', '[foo$="bar" i]'],
    ['[foo*=bar i\t]', '[foo*="bar" i]'],
    ['[foo*="bar" i\t]', '[foo*="bar" i]'],

    // Spaces everywhere.
    ['[foo=bar \t i \t ]', '[foo="bar" i]'],
    ['[foo="bar" \t i \t ]', '[foo="bar" i]'],
    ['[foo~=bar \t i \t ]', '[foo~="bar" i]'],
    ['[foo~="bar" \t i \t ]', '[foo~="bar" i]'],
    ['[foo|=bar \t i \t ]', '[foo|="bar" i]'],
    ['[foo|="bar" \t i \t ]', '[foo|="bar" i]'],
    ['[foo^=bar \t i \t ]', '[foo^="bar" i]'],
    ['[foo^="bar" \t i \t ]', '[foo^="bar" i]'],
    ['[foo$=bar \t i \t ]', '[foo$="bar" i]'],
    ['[foo$="bar" \t i \t ]', '[foo$="bar" i]'],
    ['[foo*=bar \t i \t ]', '[foo*="bar" i]'],
    ['[foo*="bar" \t i \t ]', '[foo*="bar" i]']
];

debug("Various valid cases of the selector that are not using the canonical form.");
for (var testCase of alternativeValidSelectors) {
    testValidSelector(testCase[0], testCase[1]);
}
</script>
<script src="../../resources/js-test-post.js"></script>
</html>
