OLD | NEW |
---|---|
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> | 1 <!DOCTYPE HTML> |
2 <html> | 2 <title>SVGLength, converting from 'px' to other units (attached)</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> | 5 <p></p> |
6 <body> | 6 <script> |
7 <p id="description"></p> | 7 var cssPixelsPerInch = 96; |
8 <div id="console"></div> | 8 setup(function() { |
9 <script src="script-tests/SVGLength-px-with-context.js"></script> | 9 // Setup a real SVG document, so SVGLength can resolve relative units. |
10 </body> | 10 var svgElement = document.createElementNS("http://www.w3.org/2000/svg", "svg") ; |
11 </html> | 11 svgElement.setAttribute("width", "150"); |
12 svgElement.setAttribute("height", "50"); | |
13 svgElement.setAttribute("viewBox", "0 0 150 50"); | |
14 var rectElement = document.createElementNS("http://www.w3.org/2000/svg", "rect "); | |
15 rectElement.setAttribute("style", "visibility: hidden; font-size: 12px; font-f amily: Ahem;"); | |
16 svgElement.appendChild(rectElement); | |
17 document.querySelector("p").appendChild(svgElement); | |
18 | |
19 // Extract test information | |
20 window.length = rectElement.x.baseVal; | |
21 window.svgWidth = svgElement.width.baseVal.value; | |
22 window.svgHeight = svgElement.height.baseVal.value; | |
23 window.fontSize = parseInt(rectElement.style.fontSize); | |
24 // TODO(shanmuga.m) : // Since calculateXHeight() triggers force layout, relat ive length units are resolved. | |
25 // So make convertToSpecifiedUnits to update pending style sheet and do relayo ut if needed. | |
26 window.xHeight = calculateXHeight(); | |
27 | |
28 function calculateXHeight() { | |
29 // Crude hack to calculate the x-height | |
30 var divElement = document.createElement("div"); | |
31 divElement.setAttribute("style", "height: 1ex; font-size: 12px; font-family: Ahem;"); | |
32 var pElement = document.querySelector("p"); | |
33 pElement.appendChild(divElement); | |
34 var xHeight = divElement.offsetHeight; | |
35 pElement.removeChild(divElement); | |
36 return xHeight; | |
37 } | |
38 }); | |
39 | |
40 test(function() { | |
41 length.valueAsString = "2px"; | |
42 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER); | |
43 assert_equals(length.valueAsString, "2"); | |
44 assert_equals(length.value, 2); | |
45 assert_equals(length.valueInSpecifiedUnits, 2); | |
46 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_NUMBER); | |
47 }, document.title + ", unitless"); | |
48 | |
49 test(function() { | |
50 length.valueAsString = "2px"; | |
51 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE); | |
52 referenceValue = Number(2 / svgWidth * 100).toFixed(5); | |
fs
2016/09/01 12:31:12
var referenceValue
Shanmuga Pandi
2016/09/01 13:18:17
Done.
| |
53 assert_equals(length.valueAsString, referenceValue + "%"); | |
54 assert_equals(length.valueInSpecifiedUnits.toFixed(5), referenceValue); | |
55 assert_equals(length.value.toFixed(1), "2.0"); | |
56 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PERCENTAGE); | |
57 }, document.title + ", percentage"); | |
58 | |
59 test(function() { | |
60 length.valueAsString = "2px"; | |
61 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EMS); | |
62 referenceValue = Number(2 / fontSize).toFixed(6); | |
fs
2016/09/01 12:31:12
Ditto.
| |
63 assert_equals(length.valueAsString, referenceValue + "em"); | |
64 assert_equals(length.valueInSpecifiedUnits.toFixed(6), referenceValue); | |
65 assert_equals(length.value.toFixed(1), "2.0"); | |
66 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_EMS); | |
67 }, document.title + ", ems"); | |
68 | |
69 test(function() { | |
70 length.valueAsString = "2px"; | |
71 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EXS); | |
72 referenceValue = Number(2 / xHeight).toFixed(1); | |
fs
2016/09/01 12:31:12
Ditto.
| |
73 // Don't check valueAsString here, it's unreliable across browsers. | |
74 assert_equals(length.valueInSpecifiedUnits.toFixed(1), referenceValue); | |
75 assert_equals(length.value.toFixed(1), "2.0"); | |
76 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_EXS); | |
77 }, document.title + " , exs"); | |
78 | |
79 test(function() { | |
80 length.valueAsString = "2px"; | |
81 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_CM); | |
82 referenceValue = Number(2 * 2.54 / cssPixelsPerInch).toFixed(7); | |
fs
2016/09/01 12:31:12
Ditto.
| |
83 assert_equals(length.valueAsString, referenceValue + "cm"); | |
84 assert_equals(length.valueInSpecifiedUnits.toFixed(7), referenceValue); | |
85 assert_equals(length.value.toFixed(1), "2.0"); | |
86 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_CM); | |
87 }, document.title + ", cm"); | |
88 | |
89 test(function() { | |
90 length.valueAsString = "2px"; | |
91 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM); | |
92 referenceValue = Number(2 * 25.4 / cssPixelsPerInch).toFixed(6); | |
fs
2016/09/01 12:31:12
Ditto.
| |
93 assert_equals(length.valueAsString, referenceValue + "mm"); | |
94 assert_equals(length.valueInSpecifiedUnits.toFixed(6), referenceValue); | |
95 assert_equals(length.value.toFixed(1), "2.0"); | |
96 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_MM); | |
97 }, document.title + ", mm"); | |
98 | |
99 test(function() { | |
100 length.valueAsString = "2px"; | |
101 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_IN); | |
102 referenceValue = Number(2 / cssPixelsPerInch).toFixed(7); | |
fs
2016/09/01 12:31:12
Etc...
| |
103 assert_equals(length.valueAsString, referenceValue + "in"); | |
104 assert_equals(length.valueInSpecifiedUnits.toFixed(7), referenceValue); | |
105 assert_equals(length.value.toFixed(1), "2.0"); | |
106 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_IN); | |
107 }, document.title + ", in"); | |
108 | |
109 test(function() { | |
110 length.valueAsString = "2px"; | |
111 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PT); | |
112 referenceValue = Number(2 / cssPixelsPerInch * 72); | |
113 assert_equals(length.valueAsString, referenceValue + "pt"); | |
114 assert_equals(length.valueInSpecifiedUnits, referenceValue); | |
115 assert_equals(length.value.toFixed(1), "2.0"); | |
116 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PT); | |
117 }, document.title + ", pt"); | |
118 | |
119 test(function() { | |
120 length.valueAsString = "2px"; | |
121 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PC); | |
122 referenceValue = Number(2 / cssPixelsPerInch * 6).toFixed(3); | |
123 // Don't check valueAsString here, it's unreliable across browsers. | |
124 assert_equals(length.valueInSpecifiedUnits.toFixed(3), referenceValue); | |
125 assert_equals(length.value.toFixed(1), "2.0"); | |
126 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PC); | |
127 }, document.title + ", pc"); | |
128 </script> | |
OLD | NEW |