calc-unit-analysis

CSS Variables Allowed Syntax

WeasyPrint

This browser

Flags
script

Source

 1 <!DOCTYPE html>
 2 <html><head>
 3   <title>CSS Variables Allowed Syntax</title>
 4   <link href="https://dbaron.org/" rel="author" title="L. David Baron">
 5   <link href="http://mozilla.com/" rel="author" title="Mozilla Corporation">
 6   <link href="http://www.w3.org/TR/css3-values/#lengths" rel="help">
 7   <link href="http://www.w3.org/TR/css3-values/#calc-type-checking" rel="help">
 8   <script src="/resources/testharness.js"></script>
 9   <script src="/resources/testharnessreport.js"></script>
10 <style id="style"></style>
11 <script id="metadata_cache">/*
12 {
13   "unitless_zero_in_calc_is_a_numeric_type_not_length": { "assert": "invalid calc expression: unitless zero in calc() is a numeric type, not length" },
14   "0px_in_calc": { "assert": "invalid calc expression: 0px in calc()" },
15   "addition_of_length_and_number": { "assert": "invalid calc expression: addition of length and number" },
16   "addition_of_number_and_length": { "assert": "invalid calc expression: addition of number and length" },
17   "subtraction_of_length_and_number": { "assert": "invalid calc expression: subtraction of length and number" },
18   "subtraction_of_number_and_length": { "assert": "invalid calc expression: subtraction of number and length" },
19   "multiplication_of_length_and_number": { "assert": "invalid calc expression: multiplication of length and number" },
20   "multiplication_of_number_and_length": { "assert": "invalid calc expression: multiplication of number and length" },
21   "multiplication_of_length_and_length": { "assert": "invalid calc expression: multiplication of length and length" }
22 }
23 */</script>
24 </head>
25 <body onload="run()">
26 <div id="log"></div>
27 <div id="test"></div>
28 <script>
29 
30 function run() {
31   var test_elt = document.getElementById("test");
32   var test_cs = window.getComputedStyle(test_elt, "");
33 
34   function description_to_name(description) {
35     return description.replace(/\W+/g, "_").replace(/^_/, "").replace(/_$/, "");
36   }
37 
38   function assert_invalid_value(property, value, description) {
39     test(function() {
40            test_elt.style.setProperty(property, "inherit");
41            test_elt.style.setProperty(property, value);
42            assert_equals(test_elt.style.getPropertyValue(property),
43                          "inherit");
44            test_elt.style.setProperty(property, value);
45            test_elt.style.removeProperty(property);
46          },
47          description_to_name(description),
48          { assert: "invalid calc expression: " + description });
49   }
50 
51   function assert_valid_value(property, value, computes_to, description) {
52     test(function() {
53            test_elt.style.setProperty(property, "inherit");
54            test_elt.style.setProperty(property, value);
55            assert_not_equals(test_elt.style.getPropertyValue(property),
56                              "inherit");
57            assert_equals(test_cs.getPropertyValue(property),
58                          computes_to);
59            test_elt.style.removeProperty(property);
60          },
61          description_to_name(description),
62          { assert: "valid calc expression: " + description });
63   }
64 
65   assert_invalid_value("margin-left", "calc(0)",
66                        "unitless zero in calc() is a numeric type, not length");
67   assert_valid_value("margin-left", "calc(0px)", "0px",
68                      "0px in calc()");
69   assert_invalid_value("margin-left", "calc(1px + 2)",
70                        "addition of length and number");
71   assert_invalid_value("margin-left", "calc(2 + 1px)",
72                        "addition of number and length");
73   assert_invalid_value("margin-left", "calc(1px - 2)",
74                        "subtraction of length and number");
75   assert_invalid_value("margin-left", "calc(2 - 1px)",
76                        "subtraction of number and length");
77   assert_valid_value("margin-left", "calc(2px * 2)", "4px",
78                      "multiplication of length and number");
79   assert_valid_value("margin-left", "calc(2 * 2px)", "4px",
80                      "multiplication of number and length");
81   assert_invalid_value("margin-left", "calc(2px * 1px)",
82                        "multiplication of length and length");
83 
84 }
85 
86 run();
87 
88 </script>
89 
90 
91 </body></html>