OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <script src='../resources/testharness.js'></script> | 2 <script src='../resources/testharness.js'></script> |
3 <script src='../resources/testharnessreport.js'></script> | 3 <script src='../resources/testharnessreport.js'></script> |
4 | 4 |
5 <script> | 5 <script> |
6 var EPSILON = 1e-6; // float epsilon | 6 var EPSILON = 1e-6; // float epsilon |
7 | 7 |
8 let angleUnits = [ | 8 let angleUnits = [ |
9 'deg', | 9 'deg', |
10 'rad', | 10 'rad', |
11 'grad', | 11 'grad', |
12 'turn' | 12 'turn' |
13 ]; | 13 ]; |
14 | 14 |
| 15 let fixedLengthUnits = [ |
| 16 'px', |
| 17 'in', |
| 18 'cm', |
| 19 'mm', |
| 20 'pt', |
| 21 'pc' |
| 22 ]; |
| 23 |
15 let conversionFactors = { | 24 let conversionFactors = { |
16 'deg': { | 25 'deg': { |
17 'deg': 1, | 26 'deg': 1, |
18 'rad': Math.PI / 180, | 27 'rad': Math.PI / 180, |
19 'grad': 400 / 360, | 28 'grad': 400 / 360, |
20 'turn': 1 / 360, | 29 'turn': 1 / 360, |
21 }, | 30 }, |
22 'rad': { | 31 'rad': { |
23 'deg': 180 / Math.PI, | 32 'deg': 180 / Math.PI, |
24 'rad': 1, | 33 'rad': 1, |
25 'grad': 200 / Math.PI, | 34 'grad': 200 / Math.PI, |
26 'turn': 0.5 / Math.PI, | 35 'turn': 0.5 / Math.PI, |
27 }, | 36 }, |
28 'grad': { | 37 'grad': { |
29 'deg': 360 / 400, | 38 'deg': 360 / 400, |
30 'rad': Math.PI / 200, | 39 'rad': Math.PI / 200, |
31 'grad': 1, | 40 'grad': 1, |
32 'turn': 1 / 400, | 41 'turn': 1 / 400, |
33 }, | 42 }, |
34 'turn': { | 43 'turn': { |
35 'deg': 360, | 44 'deg': 360, |
36 'rad': 2 * Math.PI, | 45 'rad': 2 * Math.PI, |
37 'grad': 400, | 46 'grad': 400, |
38 'turn': 1, | 47 'turn': 1, |
39 }, | 48 }, |
| 49 // 96 px per in |
| 50 // 2.54 cm per in |
| 51 // 10 mm per cm |
| 52 // 72 pt per in |
| 53 // 6 pc per in |
| 54 'px': { |
| 55 'px': 1, |
| 56 'in': 1 / 96, |
| 57 'cm': 2.54 / 96, |
| 58 'mm': 25.4 / 96, |
| 59 'pt': 72 / 96, |
| 60 'pc': 6 / 96 |
| 61 }, |
| 62 'in': { |
| 63 'px': 96, |
| 64 'in': 1, |
| 65 'cm': 2.54, |
| 66 'mm': 25.4, |
| 67 'pt': 72, |
| 68 'pc': 6 |
| 69 }, |
| 70 'cm': { |
| 71 'px': 96 / 2.54, |
| 72 'in': 1 / 2.54, |
| 73 'cm': 1, |
| 74 'mm': 10, |
| 75 'pt': 72 / 2.54, |
| 76 'pc': 6 / 2.54 |
| 77 }, |
| 78 'mm': { |
| 79 'px': 96 / 25.4, |
| 80 'in': 1 / 25.4, |
| 81 'cm': 1 / 10, |
| 82 'mm': 1, |
| 83 'pt': 72 / 25.4, |
| 84 'pc': 6 / 25.4 |
| 85 }, |
| 86 'pt': { |
| 87 'px': 96 / 72, |
| 88 'in': 1 / 72, |
| 89 'cm': 2.54 / 72, |
| 90 'mm': 25.4 / 72, |
| 91 'pt': 1, |
| 92 'pc': 6 / 72, |
| 93 }, |
| 94 'pc': { |
| 95 'px': 96 / 6, |
| 96 'in': 1 / 6, |
| 97 'cm': 2.54 / 6, |
| 98 'mm': 25.4 / 6, |
| 99 'pt': 72 / 6, |
| 100 'pc': 1 |
| 101 } |
40 } | 102 } |
41 | 103 |
42 test(() => { | 104 test(() => { |
43 let unitValue = new CSSUnitValue(1, 'deg'); | 105 let unitValue = new CSSUnitValue(1, 'deg'); |
44 assert_throws(new SyntaxError(), () => { | 106 assert_throws(new SyntaxError(), () => { |
45 unitValue.to('bananas'); | 107 unitValue.to('bananas'); |
46 }); | 108 }); |
47 }, 'Converting to invalid unit throws'); | 109 }, 'Converting to invalid unit throws'); |
48 | 110 |
49 test(() => { | 111 test(() => { |
50 let unitValue = new CSSUnitValue(1, 'deg'); | 112 let unitValue = new CSSUnitValue(1, 'deg'); |
51 assert_throws(new TypeError(), () => { | 113 assert_throws(new TypeError(), () => { |
52 unitValue.to('px'); | 114 unitValue.to('px'); |
53 }); | 115 }); |
54 }, 'Converting to a non-compatible unit throws'); | 116 }, 'Converting to a non-compatible unit throws'); |
55 | 117 |
56 for (let unit of angleUnits) { | 118 for (let unit of angleUnits) { |
57 for (let toUnit of angleUnits) { | 119 for (let toUnit of angleUnits) { |
58 test(() => { | 120 test(() => { |
59 let unitValue = new CSSUnitValue(1, unit); | 121 let unitValue = new CSSUnitValue(1, unit); |
60 let result = unitValue.to(toUnit); | 122 let result = unitValue.to(toUnit); |
61 assert_approx_equals( | 123 assert_approx_equals( |
62 result.value, conversionFactors[unit][toUnit], EPSILON); | 124 result.value, conversionFactors[unit][toUnit], EPSILON); |
63 assert_equals(result.unit, toUnit); | 125 assert_equals(result.unit, toUnit); |
64 }, 'Converting angle unit ' + unit + ' to ' + toUnit); | 126 }, 'Converting angle unit ' + unit + ' to ' + toUnit); |
65 } | 127 } |
66 } | 128 } |
67 | 129 |
| 130 for (let unit of fixedLengthUnits) { |
| 131 for (let toUnit of fixedLengthUnits) { |
| 132 test(() => { |
| 133 let unitValue = new CSSUnitValue(1, unit); |
| 134 let result = unitValue.to(toUnit); |
| 135 assert_approx_equals( |
| 136 result.value, conversionFactors[unit][toUnit], EPSILON); |
| 137 assert_equals(result.unit, toUnit); |
| 138 }, 'Converting fixed length unit ' + unit + ' to ' + toUnit); |
| 139 } |
| 140 } |
| 141 |
68 </script> | 142 </script> |
OLD | NEW |