browser/devtools/styleinspector/test/unit/test_parseDeclarations.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/styleinspector/test/unit/test_parseDeclarations.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,206 @@
     1.4 +/* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* Any copyright is dedicated to the Public Domain.
     1.7 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.8 +
     1.9 +const Cu = Components.utils;
    1.10 +Cu.import("resource://gre/modules/devtools/Loader.jsm");
    1.11 +const {parseDeclarations} = devtools.require("devtools/styleinspector/css-parsing-utils");
    1.12 +
    1.13 +const TEST_DATA = [
    1.14 +  // Simple test
    1.15 +  {
    1.16 +    input: "p:v;",
    1.17 +    expected: [{name: "p", value: "v", priority: ""}]
    1.18 +  },
    1.19 +  // Simple test
    1.20 +  {
    1.21 +    input: "this:is;a:test;",
    1.22 +    expected: [
    1.23 +      {name: "this", value: "is", priority: ""},
    1.24 +      {name: "a", value: "test", priority: ""}
    1.25 +    ]
    1.26 +  },
    1.27 +  // Test a single declaration with semi-colon
    1.28 +  {
    1.29 +    input: "name:value;",
    1.30 +    expected: [{name: "name", value: "value", priority: ""}]
    1.31 +  },
    1.32 +  // Test a single declaration without semi-colon
    1.33 +  {
    1.34 +    input: "name:value",
    1.35 +    expected: [{name: "name", value: "value", priority: ""}]
    1.36 +  },
    1.37 +  // Test multiple declarations separated by whitespaces and carriage returns and tabs
    1.38 +  {
    1.39 +    input: "p1 : v1 ; \t\t  \n p2:v2;   \n\n\n\n\t  p3    :   v3;",
    1.40 +    expected: [
    1.41 +      {name: "p1", value: "v1", priority: ""},
    1.42 +      {name: "p2", value: "v2", priority: ""},
    1.43 +      {name: "p3", value: "v3", priority: ""},
    1.44 +    ]
    1.45 +  },
    1.46 +  // Test simple priority
    1.47 +  {
    1.48 +    input: "p1: v1; p2: v2 !important;",
    1.49 +    expected: [
    1.50 +      {name: "p1", value: "v1", priority: ""},
    1.51 +      {name: "p2", value: "v2", priority: "important"}
    1.52 +    ]
    1.53 +  },
    1.54 +  // Test simple priority
    1.55 +  {
    1.56 +    input: "p1: v1 !important; p2: v2",
    1.57 +    expected: [
    1.58 +      {name: "p1", value: "v1", priority: "important"},
    1.59 +      {name: "p2", value: "v2", priority: ""}
    1.60 +    ]
    1.61 +  },
    1.62 +  // Test simple priority
    1.63 +  {
    1.64 +    input: "p1: v1 !  important; p2: v2 ! important;",
    1.65 +    expected: [
    1.66 +      {name: "p1", value: "v1", priority: "important"},
    1.67 +      {name: "p2", value: "v2", priority: "important"}
    1.68 +    ]
    1.69 +  },
    1.70 +  // Test invalid priority
    1.71 +  {
    1.72 +    input: "p1: v1 important;",
    1.73 +    expected: [
    1.74 +      {name: "p1", value: "v1 important", priority: ""}
    1.75 +    ]
    1.76 +  },
    1.77 +  // Test various types of background-image urls
    1.78 +  {
    1.79 +    input: "background-image: url(../../relative/image.png)",
    1.80 +    expected: [{name: "background-image", value: "url(\"../../relative/image.png\")", priority: ""}]
    1.81 +  },
    1.82 +  {
    1.83 +    input: "background-image: url(http://site.com/test.png)",
    1.84 +    expected: [{name: "background-image", value: "url(\"http://site.com/test.png\")", priority: ""}]
    1.85 +  },
    1.86 +  {
    1.87 +    input: "background-image: url(wow.gif)",
    1.88 +    expected: [{name: "background-image", value: "url(\"wow.gif\")", priority: ""}]
    1.89 +  },
    1.90 +  // Test that urls with :;{} characters in them are parsed correctly
    1.91 +  {
    1.92 +    input: "background: red url(\"http://site.com/image{}:;.png?id=4#wat\") repeat top right",
    1.93 +    expected: [
    1.94 +      {name: "background", value: "red url(\"http://site.com/image{}:;.png?id=4#wat\") repeat top right", priority: ""}
    1.95 +    ]
    1.96 +  },
    1.97 +  // Test that an empty string results in an empty array
    1.98 +  {input: "", expected: []},
    1.99 +  // Test that a string comprised only of whitespaces results in an empty array
   1.100 +  {input: "       \n \n   \n   \n \t  \t\t\t  ", expected: []},
   1.101 +  // Test that a null input throws an exception
   1.102 +  {input: null, throws: true},
   1.103 +  // Test that a undefined input throws an exception
   1.104 +  {input: undefined, throws: true},
   1.105 +  // Test that :;{} characters in quoted content are not parsed as multiple declarations
   1.106 +  {
   1.107 +    input: "content: \";color:red;}selector{color:yellow;\"",
   1.108 +    expected: [
   1.109 +      {name: "content", value: "\";color:red;}selector{color:yellow;\"", priority: ""}
   1.110 +    ]
   1.111 +  },
   1.112 +  // Test that rules aren't parsed, just declarations. So { and } found after a
   1.113 +  // property name should be part of the property name, same for values.
   1.114 +  {
   1.115 +    input: "body {color:red;} p {color: blue;}",
   1.116 +    expected: [
   1.117 +      {name: "body {color", value: "red", priority: ""},
   1.118 +      {name: "} p {color", value: "blue", priority: ""},
   1.119 +      {name: "}", value: "", priority: ""}
   1.120 +    ]
   1.121 +  },
   1.122 +  // Test unbalanced : and ;
   1.123 +  {
   1.124 +    input: "color :red : font : arial;",
   1.125 +    expected : [
   1.126 +      {name: "color", value: "red : font : arial", priority: ""}
   1.127 +    ]
   1.128 +  },
   1.129 +  {input: "background: red;;;;;", expected: [{name: "background", value: "red", priority: ""}]},
   1.130 +  {input: "background:;", expected: [{name: "background", value: "", priority: ""}]},
   1.131 +  {input: ";;;;;", expected: []},
   1.132 +  {input: ":;:;", expected: []},
   1.133 +  // Test name only
   1.134 +  {input: "color", expected: [
   1.135 +    {name: "color", value: "", priority: ""}
   1.136 +  ]},
   1.137 +  // Test trailing name without :
   1.138 +  {input: "color:blue;font", expected: [
   1.139 +    {name: "color", value: "blue", priority: ""},
   1.140 +    {name: "font", value: "", priority: ""}
   1.141 +  ]},
   1.142 +  // Test trailing name with :
   1.143 +  {input: "color:blue;font:", expected: [
   1.144 +    {name: "color", value: "blue", priority: ""},
   1.145 +    {name: "font", value: "", priority: ""}
   1.146 +  ]},
   1.147 +  // Test leading value
   1.148 +  {input: "Arial;color:blue;", expected: [
   1.149 +    {name: "", value: "Arial", priority: ""},
   1.150 +    {name: "color", value: "blue", priority: ""}
   1.151 +  ]},
   1.152 +  // Test hex colors
   1.153 +  {input: "color: #333", expected: [{name: "color", value: "#333", priority: ""}]},
   1.154 +  {input: "color: #456789", expected: [{name: "color", value: "#456789", priority: ""}]},
   1.155 +  {input: "wat: #XYZ", expected: [{name: "wat", value: "#XYZ", priority: ""}]},
   1.156 +  // Test string/url quotes escaping
   1.157 +  {input: "content: \"this is a 'string'\"", expected: [{name: "content", value: "\"this is a 'string'\"", priority: ""}]},
   1.158 +  {input: 'content: "this is a \\"string\\""', expected: [{name: "content", value: '\'this is a "string"\'', priority: ""}]},
   1.159 +  {input: "content: 'this is a \"string\"'", expected: [{name: "content", value: '\'this is a "string"\'', priority: ""}]},
   1.160 +  {input: "content: 'this is a \\'string\\'", expected: [{name: "content", value: '"this is a \'string\'"', priority: ""}]},
   1.161 +  {input: "content: 'this \\' is a \" really strange string'", expected: [{name: "content", value: '"this \' is a \" really strange string"', priority: ""}]},
   1.162 +  {
   1.163 +    input: "content: \"a not s\\\
   1.164 +          o very long title\"",
   1.165 +    expected: [
   1.166 +      {name: "content", value: '"a not s\
   1.167 +          o very long title"', priority: ""}
   1.168 +    ]
   1.169 +  }
   1.170 +];
   1.171 +
   1.172 +function run_test() {
   1.173 +  for (let test of TEST_DATA) {
   1.174 +    do_print("Test input string " + test.input);
   1.175 +    let output;
   1.176 +    try {
   1.177 +      output = parseDeclarations(test.input);
   1.178 +    } catch (e) {
   1.179 +      do_print("parseDeclarations threw an exception with the given input string");
   1.180 +      if (test.throws) {
   1.181 +        do_print("Exception expected");
   1.182 +        do_check_true(true);
   1.183 +      } else {
   1.184 +        do_print("Exception unexpected\n" + e);
   1.185 +        do_check_true(false);
   1.186 +      }
   1.187 +    }
   1.188 +    if (output) {
   1.189 +      assertOutput(output, test.expected);
   1.190 +    }
   1.191 +  }
   1.192 +}
   1.193 +
   1.194 +function assertOutput(actual, expected) {
   1.195 +  if (actual.length === expected.length) {
   1.196 +    for (let i = 0; i < expected.length; i ++) {
   1.197 +      do_check_true(!!actual[i]);
   1.198 +      do_print("Check that the output item has the expected name, value and priority");
   1.199 +      do_check_eq(expected[i].name, actual[i].name);
   1.200 +      do_check_eq(expected[i].value, actual[i].value);
   1.201 +      do_check_eq(expected[i].priority, actual[i].priority);
   1.202 +    }
   1.203 +  } else {
   1.204 +    for (let prop of actual) {
   1.205 +      do_print("Actual output contained: {name: "+prop.name+", value: "+prop.value+", priority: "+prop.priority+"}");
   1.206 +    }
   1.207 +    do_check_eq(actual.length, expected.length);
   1.208 +  }
   1.209 +}

mercurial