OLD | NEW |
---|---|
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> | 1 <!DOCTYPE HTML> |
2 <html> | 2 <title>Checks SVGLength - converting from px to all other unit types</title> |
3 <head> | 3 <script src="../../resources/testharness.js"></script> |
4 <script src="../../resources/js-test.js"></script> | 4 <script src="../../resources/testharnessreport.js"></script> |
5 </head> | |
6 <body> | |
7 <p id="description"></p> | 5 <p id="description"></p> |
8 <div id="console"></div> | 6 <script> |
9 <script src="script-tests/SVGLength-px-with-context.js"></script> | 7 function calculateDPI() |
10 </body> | 8 { |
Srirama
2016/08/25 05:18:51
nit: move it to previous line.
Shanmuga Pandi
2016/08/25 07:14:52
Done.
| |
11 </html> | 9 // Crude hack to determine the DPI, instead of hardcoding our 96 dpi here. |
10 var divElement = document.createElement("div"); | |
Srirama
2016/08/25 05:18:51
same comments as in previous test to use querysele
Shanmuga Pandi
2016/08/25 07:14:52
Done.
| |
11 divElement.setAttribute("style", "height: 1in"); | |
12 document.getElementById("description").appendChild(divElement); | |
13 var cssPixelsPerInch = divElement.offsetHeight; | |
14 document.getElementById("description").removeChild(divElement); | |
15 | |
16 // Crude hack to make this test pass with Opera/Mac | |
17 if (navigator.userAgent.indexOf("Opera") != -1) { | |
18 if (navigator.userAgent.indexOf("Macintosh") != -1) { | |
19 cssPixelsPerInch = 72; | |
20 } | |
21 } | |
22 | |
23 return cssPixelsPerInch; | |
24 } | |
25 | |
26 function calculateXHeight() | |
27 { | |
Srirama
2016/08/25 05:18:51
nit:move it to previous line.
Shanmuga Pandi
2016/08/25 07:14:52
Done.
| |
28 // Crude hack to calculate the x-height | |
29 var divElement = document.createElement("div"); | |
30 divElement.setAttribute("style", "height: 1ex; font-size: 12px; font-family: A hem;"); | |
31 document.getElementById("description").appendChild(divElement); | |
Srirama
2016/08/25 05:18:51
same here as well.
Shanmuga Pandi
2016/08/25 07:14:52
Done.
| |
32 var xHeight = divElement.offsetHeight; | |
33 document.getElementById("description").removeChild(divElement); | |
34 return xHeight; | |
35 } | |
36 | |
37 test(function() { | |
38 // Setup a real SVG document, so SVGLength can resolve relative units. | |
39 var svgElement = document.createElementNS("http://www.w3.org/2000/svg", "svg") ; | |
40 svgElement.setAttribute("width", "150"); | |
41 svgElement.setAttribute("height", "50"); | |
42 | |
43 var rectElement = document.createElementNS("http://www.w3.org/2000/svg", "rect "); | |
44 rectElement.setAttribute("style", "visibility: hidden; font-size: 12px; font-f amily: Ahem;"); | |
45 svgElement.appendChild(rectElement); | |
46 document.getElementById("description").appendChild(svgElement); | |
47 | |
48 // Extract test information | |
49 var length = rectElement.x.baseVal; | |
50 var svgWidth = svgElement.width.baseVal.value; | |
51 var svgHeight = svgElement.height.baseVal.value; | |
52 var fontSize = parseInt(rectElement.style.fontSize); | |
53 var cssPixelsPerInch = calculateDPI(); | |
54 | |
55 // Set value to be 2px | |
56 length.valueAsString = "2px"; | |
57 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PX); | |
58 assert_equals(length.value, 2); | |
59 assert_equals(length.valueInSpecifiedUnits, 2); | |
60 assert_equals(length.valueAsString, "2px"); | |
61 | |
62 // Convert from px to unitless | |
63 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER); | |
64 assert_equals(length.valueAsString, "2"); | |
65 assert_equals(length.value, 2); | |
66 assert_equals(length.valueInSpecifiedUnits, 2); | |
67 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_NUMBER); | |
68 | |
69 // Reset to 2px | |
70 length.valueAsString = "2px"; | |
71 | |
72 // Convert from px to percentage | |
73 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE); | |
74 referenceValue = Number(2 / svgWidth * 100).toFixed(5); | |
75 assert_equals(length.valueAsString, referenceValue + "%"); | |
76 assert_equals(length.valueInSpecifiedUnits.toFixed(5), referenceValue); | |
77 assert_equals(length.value.toFixed(1), "2.0"); | |
78 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PERCENTAGE); | |
79 | |
80 // Reset to 2px | |
81 length.valueAsString = "2px"; | |
82 | |
83 // Convert from px to ems | |
84 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EMS); | |
85 referenceValue = Number(2 / fontSize).toFixed(6); | |
86 assert_equals(length.valueAsString, referenceValue + "em"); | |
87 assert_equals(length.valueInSpecifiedUnits.toFixed(6), referenceValue); | |
88 assert_equals(length.value.toFixed(1), "2.0"); | |
89 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_EMS); | |
90 | |
91 // Reset to 2px | |
92 length.valueAsString = "2px"; | |
93 | |
94 // Convert from px to exs | |
95 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EXS); | |
96 referenceValue = Number(2 / calculateXHeight()).toFixed(1); | |
97 // Don't check valueAsString here, it's unreliable across browsers. | |
98 assert_equals(length.valueInSpecifiedUnits.toFixed(1), referenceValue); | |
99 assert_equals(length.value.toFixed(1), "2.0"); | |
100 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_EXS); | |
101 | |
102 // Reset to 2px"); | |
103 length.valueAsString = "2px"; | |
104 | |
105 // Convert from px to cm | |
106 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_CM); | |
107 referenceValue = Number(2 * 2.54 / cssPixelsPerInch).toFixed(7); | |
108 assert_equals(length.valueAsString, referenceValue + "cm"); | |
109 assert_equals(length.valueInSpecifiedUnits.toFixed(7), referenceValue); | |
110 assert_equals(length.value.toFixed(1), "2.0"); | |
111 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_CM); | |
112 | |
113 // Reset to 2px | |
114 length.valueAsString = "2px"; | |
115 | |
116 // Convert from px to mm | |
117 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM); | |
118 referenceValue = Number(2 * 25.4 / cssPixelsPerInch).toFixed(6); | |
119 assert_equals(length.valueAsString, referenceValue + "mm"); | |
120 assert_equals(length.valueInSpecifiedUnits.toFixed(6), referenceValue); | |
121 assert_equals(length.value.toFixed(1), "2.0"); | |
122 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_MM); | |
123 | |
124 // Reset to 2px | |
125 length.valueAsString = "2px"; | |
126 | |
127 // Convert from px to in | |
128 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_IN); | |
129 referenceValue = Number(2 / cssPixelsPerInch).toFixed(7); | |
130 assert_equals(length.valueAsString, referenceValue + "in"); | |
131 assert_equals(length.valueInSpecifiedUnits.toFixed(7), referenceValue); | |
132 assert_equals(length.value.toFixed(1), "2.0"); | |
133 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_IN); | |
134 | |
135 // Reset to 2px | |
136 length.valueAsString = "2px"; | |
137 | |
138 // Convert from px to pt | |
139 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PT); | |
140 referenceValue = Number(2 / cssPixelsPerInch * 72); | |
141 assert_equals(length.valueAsString, referenceValue + "pt"); | |
142 assert_equals(length.valueInSpecifiedUnits, referenceValue); | |
143 assert_equals(length.value.toFixed(1), "2.0"); | |
144 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PT); | |
145 | |
146 // Reset to 2px | |
147 length.valueAsString = "2px"; | |
148 | |
149 // Convert from px to pc | |
150 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PC); | |
151 referenceValue = Number(2 / cssPixelsPerInch * 6).toFixed(3); | |
152 // Don't check valueAsString here, it's unreliable across browsers. | |
153 assert_equals(length.valueInSpecifiedUnits.toFixed(3), referenceValue); | |
154 assert_equals(length.value.toFixed(1), "2.0"); | |
155 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PC); | |
156 }); | |
157 </script> | |
OLD | NEW |