<!doctype html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
</head>
<body>
    <div lang="en-GB"></div>
    <div lang="fr-be"></div>
    <div lang="fr"></div>
    <script>
        description('Verify chained multiple :lang() selectors will not blow up the stack');
        function createChainedSelector(lang, n) {
            var chained = '';
            for (var i = 0; i < n; ++i) {
                chained += lang;
            }
            return chained;
        }

        function createMultipleArguments(arg, n) {
            var args = '';
            for (var i = 0; i < n; ++i) {
                args += arg + (i == n - 1 ? '' : ', ');
            }
            return args;
        }

        debug('');

        shouldBe('document.querySelectorAll(":lang(en):lang(gb)").length', '0');
        shouldBe('document.querySelectorAll(":lang(en, fr):lang(en, es):lang(en, de)").length', '1');
        shouldBe('document.querySelectorAll(":lang(en, fr):lang(en, de):lang(en, es)").length', '1');
        shouldBe('document.querySelectorAll(":lang(en, de):lang(en, es):lang(en, fr)").length', '1');

        debug('');
        shouldBe('document.querySelectorAll(":lang(fr):lang(fr):lang(fr)").length', '2');
        shouldBe('document.querySelectorAll(":lang(fr):lang(fr):lang(fr-be)").length', '1');
        shouldBe('document.querySelectorAll(":lang(fr):lang(en):lang(fr-be)").length', '0');

        debug('');
        
        shouldBe('document.querySelectorAll("' + createChainedSelector(':lang(fr)', 200) + '").length', '2');
        shouldBe('document.querySelectorAll("' + createChainedSelector(':lang(fr)' + ':lang(fr-be)' , 200) + '").length', '1');
        shouldBe('document.querySelectorAll("' + createChainedSelector(':lang(fr-be)', 200) + '").length', '1');
        shouldBe('document.querySelectorAll("' + createChainedSelector(':lang(fr, es)', 200) + '").length', '2');

        debug('');

        shouldBe('document.querySelectorAll(":lang(' + createMultipleArguments('en-gb', 100) + ')").length', '1'); 
        shouldBe('document.querySelectorAll(":lang(' + createMultipleArguments('en-gb', 200) + ')").length', '1'); 
        shouldBe('document.querySelectorAll("' + createChainedSelector(':lang(' + createMultipleArguments('en-gb', 10) + ')', 200) + '").length', '1');
        shouldBe('document.querySelectorAll("' + createChainedSelector(':lang(' + createMultipleArguments('en-gb', 20) + ')', 200) + '").length', '1');
        shouldBe('document.querySelectorAll("' + createChainedSelector(':lang(' + createMultipleArguments('en-gb', 30) + ')', 200) + '").length', '1');
    </script>
    <script src="../../resources/js-test-post.js"></script>
</body>
</html>

