chromium/third_party/blink/web_tests/svg/dom/rgb-color-parser.html

<html>
<head>
<script src="../../resources/js-test.js"></script>
<script src="resources/scripted-random.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
    description("This test fuzzes the color parser with semi-random attribute values and dumps the results of any values that parse successfully.");

    var characters = [
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        " ",
        "\t",
        "(",
        ")",
        "%",
        "+",
        "-",
        ".",
        "e",
        ","
    ];
    
    var stopElement = document.createElementNS("http://www.w3.org/2000/svg", "stop");
    function parseRGBColor(string)
    {
        try {
            stopElement.style.removeProperty("stop-color");
            stopElement.style.stopColor = string;
            var stopColor = stopElement.style.getPropertyValue("stop-color");
            if (!stopColor) {
                debug("Failed to parse: " + string);
                return;
            }
            debug("Parsed as " + stopColor + ": " + string);
        } catch(e) {
            debug("Threw exception " + e + ": " + string);
        }
    }

    function fuzz()
    {
        // Some valid values.
        parseRGBColor("blue");
        parseRGBColor("rgb(0, 255, 0)");
        
        // Some invalid ones.
        parseRGBColor("rgb(100%,100%,0%");
        parseRGBColor("rgba(100%,100%,0%");
        parseRGBColor("rgb(100%,100%,r)");
        parseRGBColor("rgb (100%,100%,r)");
        parseRGBColor("rgb(100%, 1, 1)");
        parseRGBColor("hsl(100%,100%,100%)");
        parseRGBColor("yello");
        parseRGBColor("!#fff");
        parseRGBColor("#sdf");
        parseRGBColor("#");
        parseRGBColor("#f");
        parseRGBColor("#ff");
        parseRGBColor("#fffff");
        parseRGBColor("#fffffff");
        parseRGBColor("green,");

        // Some more valid ones.
        parseRGBColor("rgb(0, 10, 20)");
        parseRGBColor("#fff");
        parseRGBColor("#ffff");
        parseRGBColor("#ffffff");
        parseRGBColor("#ffffffff");
        parseRGBColor("#FFF");
        parseRGBColor("#FFFFFF");
        parseRGBColor(" green");
        parseRGBColor("green ");
        parseRGBColor("green");
        
        // Random assortments of valid characters
        for (var i = 0; i < 250; i++) { //>
            var colorString = "rgb(";
            var count = Math.scriptedRandomInt(20);
            for (var j = 0; j < count; j++) { //>
                colorString += characters[Math.scriptedRandomInt(characters.length)];
            }
            parseRGBColor(colorString);
        }
        
        // Empty-ish colors
        parseRGBColor("");
        parseRGBColor(String.fromCharCode(0));
        parseRGBColor("rgb(" + String.fromCharCode(0) + ")");
        
        // One more valid value.
        parseRGBColor("#0f0");
    }

    fuzz();

</script>
</html>