OLD | NEW |
---|---|
(Empty) | |
1 <!DOCTYPE html> | |
2 <script src="../../resources/testharness.js"></script> | |
3 <script src="../../resources/testharnessreport.js"></script> | |
4 <svg width="500px" height="500px" viewBox="0 0 500 500"> | |
5 <rect width="1" height="1"/> | |
6 <marker markerWidth="1" markerHeight="1"/> | |
7 </svg> | |
8 <script> | |
9 var viewportWidth = 500; | |
10 var EPSILON = Math.pow(2, -8); | |
11 var cssPixelsPerInch = 96; | |
12 var cssPixelsPerCentimeter = cssPixelsPerInch / 2.54; //2.54 cm/in | |
13 var cssPixelsPerMillimeter = cssPixelsPerCentimeter / 10; | |
14 var cssPixelsPerPoint = cssPixelsPerInch / 72; | |
15 var cssPixelsPerPica = cssPixelsPerInch / 6; | |
16 | |
17 function viewportWidthPercent() | |
18 { | |
fs
2016/07/16 21:13:02
Move to previous line
Shanmuga Pandi
2016/07/18 13:35:32
Done.
| |
19 return viewportWidth / 100; | |
20 } | |
21 | |
22 function assert_calc_expression_on_width_attr(expression, expected) { | |
23 var rect = document.querySelector('rect'); | |
24 var rectWidth; | |
25 // Test calc with setAttribute | |
26 rect.setAttribute('width', expression); | |
27 rectWidth = rect.getBoundingClientRect().width; | |
28 assert_approx_equals(rectWidth, expected, EPSILON); | |
29 assert_approx_equals(rect.width.baseVal.value, expected, EPSILON); | |
30 assert_equals(rect.width.baseVal.unitType, SVGLength.SVG_LENGTHTYPE_UNKNOWN); | |
31 assert_equals(rect.width.baseVal.valueInSpecifiedUnits, 0); | |
32 | |
33 // Test valueInSpecifiedUnits setter after 'calc' value attribute | |
34 rect.width.baseVal.valueInSpecifiedUnits = 100; | |
35 assert_equals(rect.width.baseVal.unitType, SVGLength.SVG_LENGTHTYPE_NUMBER); | |
36 assert_equals(rect.width.baseVal.valueInSpecifiedUnits, 100); | |
37 | |
38 // Test value setter after 'calc' value attribute | |
39 rect.setAttribute('width', expression); // reset to calc value | |
40 rect.width.baseVal.value = 10; | |
41 assert_equals(rect.width.baseVal.unitType, SVGLength.SVG_LENGTHTYPE_NUMBER); | |
42 assert_equals(rect.width.baseVal.valueInSpecifiedUnits, 10); | |
43 | |
44 // Test calc with "valueAsString" | |
45 rect.setAttribute('width', '20px'); // reset to normal value | |
46 assert_throws(null, function() {rect.width.baseVal.valueAsString = expression} ); | |
47 assert_equals(rect.width.baseVal.value, 20); | |
48 } | |
49 | |
50 function assert_calc_expression_on_markerWidth_attr(expression, expected) { | |
51 var marker = document.querySelector('marker'); | |
52 // Test calc with setAttribute | |
53 marker.setAttribute('markerWidth', expression); | |
54 assert_approx_equals(marker.markerWidth.baseVal.value, expected, EPSILON); | |
55 assert_equals(marker.markerWidth.baseVal.unitType, SVGLength.SVG_LENGTHTYPE_UN KNOWN); | |
56 assert_equals(marker.markerWidth.baseVal.valueInSpecifiedUnits, 0); | |
57 | |
58 // Test valueInSpecifiedUnits setter after 'calc' value attribute | |
59 marker.markerWidth.baseVal.valueInSpecifiedUnits = 100; | |
60 assert_equals(marker.markerWidth.baseVal.unitType, SVGLength.SVG_LENGTHTYPE_NU MBER); | |
61 assert_equals(marker.markerWidth.baseVal.valueInSpecifiedUnits, 100); | |
62 | |
63 // Test value setter after 'calc' value attribute | |
64 marker.setAttribute('width', expression); // reset to calc value | |
fs
2016/07/16 21:13:02
s/'width'/'markerWidth'/
Shanmuga Pandi
2016/07/18 13:35:32
Done.
| |
65 marker.markerWidth.baseVal.value = 10; | |
66 assert_equals(marker.markerWidth.baseVal.unitType, SVGLength.SVG_LENGTHTYPE_NU MBER); | |
67 assert_equals(marker.markerWidth.baseVal.valueInSpecifiedUnits, 10); | |
68 | |
69 // Test calc with "valueAsString" | |
70 marker.setAttribute('markerWidth', '20px'); // reset to normal value | |
71 assert_throws(null, function() {marker.markerWidth.baseVal.valueAsString = exp ression}); | |
72 assert_equals(marker.markerWidth.baseVal.value, 20); | |
73 } | |
74 | |
75 function assert_calc_expression(expression, expected) { | |
76 assert_calc_expression_on_width_attr(expression, expected); // presentation attr | |
77 assert_calc_expression_on_markerWidth_attr(expression, expected); // non pre sentation attr | |
78 } | |
79 | |
80 test(function() { | |
81 assert_calc_expression("calc(20%)", (20 * viewportWidthPercent())); | |
82 assert_calc_expression("calc(10mm + 10in)", (10 * cssPixelsPerMillimeter) + (10 * cssPixelsPerInch)); | |
83 assert_calc_expression("calc(10mm + 10mm)", (20 * cssPixelsPerMillimeter)); | |
84 assert_calc_expression("calc(20mm)", (20 * cssPixelsPerMillimeter)); | |
85 assert_calc_expression("calc(10 + 10)", 20); | |
86 assert_calc_expression("calc(10mm + 10)", (10 * cssPixelsPerMillimeter) + 10 ); | |
87 assert_calc_expression("calc(10% + 10)", (10 * viewportWidthPercent()) + 10) ; | |
88 assert_calc_expression("calc(1cm + 2in + 1cm + 2)", (2 * cssPixelsPerInch) + (2 * cssPixelsPerCentimeter) + 2); | |
89 assert_calc_expression("calc(1cm + 2 + 1cm + 2in)", (2 * cssPixelsPerInch) + (2 * cssPixelsPerCentimeter) + 2); | |
90 assert_calc_expression("calc(10% + 10 + 2% + 10pc)", (12 * viewportWidthPerc ent()) + 10 + (10 * cssPixelsPerPica)); | |
91 }, "Tests calc() on presentation attr in svgLength"); | |
fs
2016/07/16 21:13:01
This is a bit misleading since it also tests the n
Shanmuga Pandi
2016/07/18 13:35:32
Done.
| |
92 </script> | |
OLD | NEW |