<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="../../resources/js-test-pre.js"></script>
<style>
* {
    background-color: white;
    color: black;
    fill-opacity: 1;
}
[data-æøå] {
    background-color: rgb(1, 2, 3);
}
[data-ÆØÅ] {
    fill-opacity: 0.5;
}
[data-Æøå] {
    color: rgb(4, 5, 6);
}
</style>
</head>
<body>
    <div>
        <!-- With renderer -->
        <target></target>
    </div>
    <div style="display:none;">
        <!-- Without renderer -->
        <target></target>
    </div>
</body>
<script>
description('Test the basic cases of style update for attribute selectors for HTML.');

var noMatch = 0;
var matchFirst = 1;
var matchSecond = 1 << 1;
var matchThird = 1 << 2;

function testColor(mask) {
    shouldBeEqualToString('getComputedStyle(document.querySelectorAll("target")[0]).backgroundColor', (mask & matchFirst) ? 'rgb(1, 2, 3)' : 'rgb(255, 255, 255)');
    shouldBeEqualToString('getComputedStyle(document.querySelectorAll("target")[1]).backgroundColor', (mask & matchFirst) ? 'rgb(1, 2, 3)' : 'rgb(255, 255, 255)');

    shouldBeEqualToString('getComputedStyle(document.querySelectorAll("target")[0]).fillOpacity', (mask & matchSecond) ? '0.5' : '1');
    shouldBeEqualToString('getComputedStyle(document.querySelectorAll("target")[1]).fillOpacity', (mask & matchSecond) ? '0.5' : '1');

    shouldBeEqualToString('getComputedStyle(document.querySelectorAll("target")[0]).color', (mask & matchThird) ? 'rgb(4, 5, 6)' : 'rgb(0, 0, 0)');
    shouldBeEqualToString('getComputedStyle(document.querySelectorAll("target")[1]).color', (mask & matchThird) ? 'rgb(4, 5, 6)' : 'rgb(0, 0, 0)');
}

function setAttribute(attribute, value) {
    var allTargets = document.querySelectorAll("target");
    for (var i = 0; i < allTargets.length; ++i)
        allTargets[i].setAttribute(attribute, value);
}

function removeAttribute(attribute) {
    var allTargets = document.querySelectorAll("target");
    for (var i = 0; i < allTargets.length; ++i)
        allTargets[i].removeAttribute(attribute);
}

debug("Initial state does not match anything, there are no attributes on the targets.");
testColor(noMatch);

debug("Adding \"data-æøå\", the background-color should match.");
setAttribute("data-æøå");
testColor(matchFirst);

debug("Removing \"data-æøå\", the background-color should no longer match.");
removeAttribute("data-æøå");
testColor(noMatch);

debug("Adding \"DATA-æøå\", the background-color should match because the document is HTML and has case-insensitive attribute matching.");
setAttribute("DATA-æøå");
testColor(matchFirst);

debug("Removing \"DATA-æøå\", the background-color should no longer match.");
removeAttribute("DATA-æøå");
testColor(noMatch);

debug("Adding \"DATA-æøå\", the background-color should match because the document is HTML and has case-insensitive attribute matching.");
setAttribute("DATA-æøå");
testColor(matchFirst);


debug("Adding \"data-ÆØÅ\", the fill-opacity should match.");
setAttribute("data-ÆØÅ");
testColor(matchFirst | matchSecond);

debug("Removing \"data-ÆØÅ\", the fill-opacity should no longer match.");
removeAttribute("data-ÆØÅ");
testColor(matchFirst);

debug("Adding \"dAta-ÆØÅ\", the fill-opacity should match because the document is HTML and has case-insensitive attribute matching.");
setAttribute("dAta-ÆØÅ");
testColor(matchFirst | matchSecond);

debug("Removing \"dAta-ÆØÅ\", the fill-opacity should no longer match.");
removeAttribute("dAta-ÆØÅ");
testColor(matchFirst);

debug("Adding \"Data-ÆØÅ\", the fill-opacity should match because the document is HTML and has case-insensitive attribute matching.");
setAttribute("Data-ÆØÅ");
testColor(matchFirst | matchSecond);


debug("Adding \"data-Æøå\", the color should match.");
setAttribute("data-Æøå");
testColor(matchFirst | matchSecond | matchThird);

debug("Removing \"data-Æøå\", the color should no longer match.");
removeAttribute("data-Æøå");
testColor(matchFirst | matchSecond);

debug("Adding \"Data-Æøå\", the color should match because the document is HTML and has case-insensitive attribute matching.");
setAttribute("Data-Æøå");
testColor(matchFirst | matchSecond | matchThird);

debug("Removing \"Data-Æøå\", the color should no longer match.");
removeAttribute("Data-Æøå");
testColor(matchFirst | matchSecond);

debug("Adding \"DATA-Æøå\", the color should match because the document is HTML and has case-insensitive attribute matching.");
setAttribute("DATA-Æøå");
testColor(matchFirst | matchSecond | matchThird);
</script>
<script src="../../resources/js-test-post.js"></script>
</html>
