Chromium Code Reviews| 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> |
|
fs
2016/08/25 10:16:51
Like for the other -px test, but change 'detached'
| |
| 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 function calculateDPI() { |
|
fs
2016/08/25 10:16:51
Shouldn't need this.
Shanmuga Pandi
2016/08/25 14:02:54
Observed strange issue if we remove calculateDPI()
fs
2016/08/25 14:12:27
Yes, that may very well be the case. SVGLengthTear
Shanmuga Pandi
2016/08/26 07:35:18
Right. I added TODO.
I will try to make a separat
| |
| 8 <div id="console"></div> | 8 // Crude hack to determine the DPI, instead of hardcoding our 96 dpi here. |
| 9 <script src="script-tests/SVGLength-px-with-context.js"></script> | 9 var divElement = document.createElement("div"); |
| 10 </body> | 10 divElement.setAttribute("style", "height: 1in"); |
| 11 </html> | 11 var pElement = document.querySelector("p"); |
| 12 pElement.appendChild(divElement); | |
| 13 var cssPixelsPerInch = divElement.offsetHeight; | |
| 14 pElement.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 // Crude hack to calculate the x-height | |
| 28 var divElement = document.createElement("div"); | |
| 29 divElement.setAttribute("style", "height: 1ex; font-size: 12px; font-family: A hem;"); | |
| 30 var pElement = document.querySelector("p"); | |
| 31 pElement.appendChild(divElement); | |
| 32 var xHeight = divElement.offsetHeight; | |
| 33 pElement.removeChild(divElement); | |
| 34 return xHeight; | |
| 35 } | |
| 36 | |
| 37 test(function() { | |
|
fs
2016/08/25 10:16:51
Should be possible to split this into several (sub
Shanmuga Pandi
2016/08/26 07:35:18
Done.
| |
| 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.querySelector("p").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 |