michael@0: /* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const Cu = Components.utils; michael@0: Cu.import("resource://gre/modules/devtools/Loader.jsm"); michael@0: const {parseDeclarations} = devtools.require("devtools/styleinspector/css-parsing-utils"); michael@0: michael@0: const TEST_DATA = [ michael@0: // Simple test michael@0: { michael@0: input: "p:v;", michael@0: expected: [{name: "p", value: "v", priority: ""}] michael@0: }, michael@0: // Simple test michael@0: { michael@0: input: "this:is;a:test;", michael@0: expected: [ michael@0: {name: "this", value: "is", priority: ""}, michael@0: {name: "a", value: "test", priority: ""} michael@0: ] michael@0: }, michael@0: // Test a single declaration with semi-colon michael@0: { michael@0: input: "name:value;", michael@0: expected: [{name: "name", value: "value", priority: ""}] michael@0: }, michael@0: // Test a single declaration without semi-colon michael@0: { michael@0: input: "name:value", michael@0: expected: [{name: "name", value: "value", priority: ""}] michael@0: }, michael@0: // Test multiple declarations separated by whitespaces and carriage returns and tabs michael@0: { michael@0: input: "p1 : v1 ; \t\t \n p2:v2; \n\n\n\n\t p3 : v3;", michael@0: expected: [ michael@0: {name: "p1", value: "v1", priority: ""}, michael@0: {name: "p2", value: "v2", priority: ""}, michael@0: {name: "p3", value: "v3", priority: ""}, michael@0: ] michael@0: }, michael@0: // Test simple priority michael@0: { michael@0: input: "p1: v1; p2: v2 !important;", michael@0: expected: [ michael@0: {name: "p1", value: "v1", priority: ""}, michael@0: {name: "p2", value: "v2", priority: "important"} michael@0: ] michael@0: }, michael@0: // Test simple priority michael@0: { michael@0: input: "p1: v1 !important; p2: v2", michael@0: expected: [ michael@0: {name: "p1", value: "v1", priority: "important"}, michael@0: {name: "p2", value: "v2", priority: ""} michael@0: ] michael@0: }, michael@0: // Test simple priority michael@0: { michael@0: input: "p1: v1 ! important; p2: v2 ! important;", michael@0: expected: [ michael@0: {name: "p1", value: "v1", priority: "important"}, michael@0: {name: "p2", value: "v2", priority: "important"} michael@0: ] michael@0: }, michael@0: // Test invalid priority michael@0: { michael@0: input: "p1: v1 important;", michael@0: expected: [ michael@0: {name: "p1", value: "v1 important", priority: ""} michael@0: ] michael@0: }, michael@0: // Test various types of background-image urls michael@0: { michael@0: input: "background-image: url(../../relative/image.png)", michael@0: expected: [{name: "background-image", value: "url(\"../../relative/image.png\")", priority: ""}] michael@0: }, michael@0: { michael@0: input: "background-image: url(http://site.com/test.png)", michael@0: expected: [{name: "background-image", value: "url(\"http://site.com/test.png\")", priority: ""}] michael@0: }, michael@0: { michael@0: input: "background-image: url(wow.gif)", michael@0: expected: [{name: "background-image", value: "url(\"wow.gif\")", priority: ""}] michael@0: }, michael@0: // Test that urls with :;{} characters in them are parsed correctly michael@0: { michael@0: input: "background: red url(\"http://site.com/image{}:;.png?id=4#wat\") repeat top right", michael@0: expected: [ michael@0: {name: "background", value: "red url(\"http://site.com/image{}:;.png?id=4#wat\") repeat top right", priority: ""} michael@0: ] michael@0: }, michael@0: // Test that an empty string results in an empty array michael@0: {input: "", expected: []}, michael@0: // Test that a string comprised only of whitespaces results in an empty array michael@0: {input: " \n \n \n \n \t \t\t\t ", expected: []}, michael@0: // Test that a null input throws an exception michael@0: {input: null, throws: true}, michael@0: // Test that a undefined input throws an exception michael@0: {input: undefined, throws: true}, michael@0: // Test that :;{} characters in quoted content are not parsed as multiple declarations michael@0: { michael@0: input: "content: \";color:red;}selector{color:yellow;\"", michael@0: expected: [ michael@0: {name: "content", value: "\";color:red;}selector{color:yellow;\"", priority: ""} michael@0: ] michael@0: }, michael@0: // Test that rules aren't parsed, just declarations. So { and } found after a michael@0: // property name should be part of the property name, same for values. michael@0: { michael@0: input: "body {color:red;} p {color: blue;}", michael@0: expected: [ michael@0: {name: "body {color", value: "red", priority: ""}, michael@0: {name: "} p {color", value: "blue", priority: ""}, michael@0: {name: "}", value: "", priority: ""} michael@0: ] michael@0: }, michael@0: // Test unbalanced : and ; michael@0: { michael@0: input: "color :red : font : arial;", michael@0: expected : [ michael@0: {name: "color", value: "red : font : arial", priority: ""} michael@0: ] michael@0: }, michael@0: {input: "background: red;;;;;", expected: [{name: "background", value: "red", priority: ""}]}, michael@0: {input: "background:;", expected: [{name: "background", value: "", priority: ""}]}, michael@0: {input: ";;;;;", expected: []}, michael@0: {input: ":;:;", expected: []}, michael@0: // Test name only michael@0: {input: "color", expected: [ michael@0: {name: "color", value: "", priority: ""} michael@0: ]}, michael@0: // Test trailing name without : michael@0: {input: "color:blue;font", expected: [ michael@0: {name: "color", value: "blue", priority: ""}, michael@0: {name: "font", value: "", priority: ""} michael@0: ]}, michael@0: // Test trailing name with : michael@0: {input: "color:blue;font:", expected: [ michael@0: {name: "color", value: "blue", priority: ""}, michael@0: {name: "font", value: "", priority: ""} michael@0: ]}, michael@0: // Test leading value michael@0: {input: "Arial;color:blue;", expected: [ michael@0: {name: "", value: "Arial", priority: ""}, michael@0: {name: "color", value: "blue", priority: ""} michael@0: ]}, michael@0: // Test hex colors michael@0: {input: "color: #333", expected: [{name: "color", value: "#333", priority: ""}]}, michael@0: {input: "color: #456789", expected: [{name: "color", value: "#456789", priority: ""}]}, michael@0: {input: "wat: #XYZ", expected: [{name: "wat", value: "#XYZ", priority: ""}]}, michael@0: // Test string/url quotes escaping michael@0: {input: "content: \"this is a 'string'\"", expected: [{name: "content", value: "\"this is a 'string'\"", priority: ""}]}, michael@0: {input: 'content: "this is a \\"string\\""', expected: [{name: "content", value: '\'this is a "string"\'', priority: ""}]}, michael@0: {input: "content: 'this is a \"string\"'", expected: [{name: "content", value: '\'this is a "string"\'', priority: ""}]}, michael@0: {input: "content: 'this is a \\'string\\'", expected: [{name: "content", value: '"this is a \'string\'"', priority: ""}]}, michael@0: {input: "content: 'this \\' is a \" really strange string'", expected: [{name: "content", value: '"this \' is a \" really strange string"', priority: ""}]}, michael@0: { michael@0: input: "content: \"a not s\\\ michael@0: o very long title\"", michael@0: expected: [ michael@0: {name: "content", value: '"a not s\ michael@0: o very long title"', priority: ""} michael@0: ] michael@0: } michael@0: ]; michael@0: michael@0: function run_test() { michael@0: for (let test of TEST_DATA) { michael@0: do_print("Test input string " + test.input); michael@0: let output; michael@0: try { michael@0: output = parseDeclarations(test.input); michael@0: } catch (e) { michael@0: do_print("parseDeclarations threw an exception with the given input string"); michael@0: if (test.throws) { michael@0: do_print("Exception expected"); michael@0: do_check_true(true); michael@0: } else { michael@0: do_print("Exception unexpected\n" + e); michael@0: do_check_true(false); michael@0: } michael@0: } michael@0: if (output) { michael@0: assertOutput(output, test.expected); michael@0: } michael@0: } michael@0: } michael@0: michael@0: function assertOutput(actual, expected) { michael@0: if (actual.length === expected.length) { michael@0: for (let i = 0; i < expected.length; i ++) { michael@0: do_check_true(!!actual[i]); michael@0: do_print("Check that the output item has the expected name, value and priority"); michael@0: do_check_eq(expected[i].name, actual[i].name); michael@0: do_check_eq(expected[i].value, actual[i].value); michael@0: do_check_eq(expected[i].priority, actual[i].priority); michael@0: } michael@0: } else { michael@0: for (let prop of actual) { michael@0: do_print("Actual output contained: {name: "+prop.name+", value: "+prop.value+", priority: "+prop.priority+"}"); michael@0: } michael@0: do_check_eq(actual.length, expected.length); michael@0: } michael@0: }