test_variable_serialization_specified.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <!DOCTYPE html>
  2. <title>Test serialization of specified CSS variable values</title>
  3. <script src="/MochiKit/MochiKit.js"></script>
  4. <script src="/tests/SimpleTest/SimpleTest.js"></script>
  5. <link rel="stylesheet" href="/tests/SimpleTest/test.css" type="text/css">
  6. <style id=style1>#test { }</style>
  7. <style id=style2></style>
  8. <script>
  9. // Values that should be serialized back to the same string.
  10. var values_with_unchanged_specified_value_serialization = [
  11. "var(--a)",
  12. "var(--a)",
  13. "var(--a) ",
  14. "var( --a ) ",
  15. "var(--a, )",
  16. "var(--a,/**/a)",
  17. "1px var(--a)",
  18. "var(--a) 1px",
  19. "something 3px url(whereever) calc(var(--a) + 1px)",
  20. "var(--a)",
  21. "var(--a)var(--b)",
  22. "var(--a, var(--b, var(--c, black)))",
  23. "var(--a) <!--",
  24. "--> var(--a)",
  25. "{ [ var(--a) ] }",
  26. "[;] var(--a)",
  27. "var(--a,(;))",
  28. "VAR(--a)",
  29. "var(--0)",
  30. "var(--\\30)",
  31. "var(--\\d800)",
  32. "var(--\\ffffff)",
  33. ];
  34. // Values that serialize differently, due to additional implied closing
  35. // characters at EOF.
  36. var values_with_changed_specified_value_serialization = [
  37. ["var(--a", "var(--a)"],
  38. ["var(--a , ", "var(--a , )"],
  39. ["var(--a, ", "var(--a, )"],
  40. ["var(--a, var(--b", "var(--a, var(--b))"],
  41. ["var(--a /* unclosed comment", "var(--a /* unclosed comment*/)"],
  42. ["var(--a /* unclosed comment *", "var(--a /* unclosed comment */)"],
  43. ["[{(((var(--a", "[{(((var(--a))))}]"],
  44. ["var(--a, \"unclosed string", "var(--a, \"unclosed string\")"],
  45. ["var(--a, 'unclosed string", "var(--a, 'unclosed string')"],
  46. ["var(--a) \"unclosed string\\", "var(--a) \"unclosed string\""],
  47. ["var(--a) 'unclosed string\\", "var(--a) 'unclosed string'"],
  48. ["var(--a) \\", "var(--a) \\\ufffd"],
  49. ["var(--a) url(unclosedurl", "var(--a) url(unclosedurl)"],
  50. ["var(--a) url('unclosedurl", "var(--a) url('unclosedurl')"],
  51. ["var(--a) url(\"unclosedurl", "var(--a) url(\"unclosedurl\")"],
  52. ["var(--a) url(unclosedurl\\", "var(--a) url(unclosedurl\\\ufffd)"],
  53. ["var(--a) url('unclosedurl\\", "var(--a) url('unclosedurl')"],
  54. ["var(--a) url(\"unclosedurl\\", "var(--a) url(\"unclosedurl\")"],
  55. ];
  56. var style1 = document.getElementById("style1");
  57. var style2 = document.getElementById("style2");
  58. var decl = style1.sheet.cssRules[0].style;
  59. function test_specified_value_serialization(value, expected) {
  60. // Test setting value on a custom property with setProperty.
  61. decl.setProperty("--test", value, "");
  62. is(decl.getPropertyValue("--test"), expected,
  63. "value with identical serialization set on custom property with setProperty");
  64. // Test setting value on a custom property via style sheet parsing.
  65. style2.textContent = "#test { --test:" + value;
  66. is(style2.sheet.cssRules[0].style.getPropertyValue("--test"), expected,
  67. "value with identical serialization set on custom property via parsing");
  68. // Test setting value on a non-custom longhand property with setProperty.
  69. decl.setProperty("color", value, "");
  70. is(decl.getPropertyValue("color"), expected,
  71. "value with identical serialization set on non-custom longhand property with setProperty");
  72. // Test setting value on a non-custom longhand property via style sheet parsing.
  73. style2.textContent = "#test { color:" + value;
  74. is(style2.sheet.cssRules[0].style.getPropertyValue("color"), expected,
  75. "value with identical serialization set on non-custom longhand property via parsing");
  76. // Test setting value on a non-custom shorthand property with setProperty.
  77. decl.setProperty("margin", value, "");
  78. is(decl.getPropertyValue("margin"), expected,
  79. "value with identical serialization set on non-custom shorthand property with setProperty");
  80. // Test setting value on a non-custom shorthand property via style sheet parsing.
  81. style2.textContent = "#test { margin:" + value;
  82. is(style2.sheet.cssRules[0].style.getPropertyValue("margin"), expected,
  83. "value with identical serialization set on non-custom shorthand property via parsing");
  84. // Clean up.
  85. decl.removeProperty("--test");
  86. decl.removeProperty("color");
  87. decl.removeProperty("margin");
  88. }
  89. function runTest() {
  90. values_with_unchanged_specified_value_serialization.forEach(function(value) {
  91. test_specified_value_serialization(value, value);
  92. });
  93. values_with_changed_specified_value_serialization.forEach(function(pair) {
  94. test_specified_value_serialization(pair[0], pair[1]);
  95. });
  96. SimpleTest.finish();
  97. }
  98. SimpleTest.waitForExplicitFinish();
  99. SpecialPowers.pushPrefEnv({ set: [["layout.css.variables.enabled", true]] },
  100. runTest);
  101. </script>