Chromium Code Reviews| 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 // Setup a real SVG document, so SVGLength can resolve relative units. |
| 8 <div id="console"></div> | 8 var svgElement = document.createElementNS("http://www.w3.org/2000/svg", "svg"); |
| 9 <script src="script-tests/SVGLength-px-with-context.js"></script> | 9 svgElement.setAttribute("width", "150"); |
| 10 </body> | 10 svgElement.setAttribute("height", "50"); |
| 11 </html> | 11 svgElement.setAttribute("viewBox", "0 0 150 50"); |
| 12 | |
| 13 var rectElement = document.createElementNS("http://www.w3.org/2000/svg", "rect") ; | |
| 14 rectElement.setAttribute("style", "visibility: hidden; font-size: 12px; font-fam ily: Ahem;"); | |
| 15 svgElement.appendChild(rectElement); | |
| 16 document.querySelector("p").appendChild(svgElement); | |
| 17 | |
| 18 // Extract test information | |
| 19 var length = rectElement.x.baseVal; | |
| 20 var svgWidth = svgElement.width.baseVal.value; | |
| 21 var svgHeight = svgElement.height.baseVal.value; | |
| 22 var fontSize = parseInt(rectElement.style.fontSize); | |
| 23 // TODO(shanmuga.m) : Remove calculateDPI() hack and use hardcoded value 96. | |
|
fs
2016/08/26 09:03:06
Instead of this, could you just add something that
Shanmuga Pandi
2016/09/01 11:59:56
Done.
| |
| 24 var cssPixelsPerInch = calculateDPI(); | |
| 25 | |
| 26 function calculateDPI() | |
| 27 { | |
| 28 // Crude hack to determine the DPI, instead of hardcoding our 96 dpi here. | |
| 29 var divElement = document.createElement("div"); | |
| 30 divElement.setAttribute("style", "height: 1in"); | |
| 31 var pElement = document.querySelector("p"); | |
| 32 pElement.appendChild(divElement); | |
| 33 var cssPixelsPerInch = divElement.offsetHeight; | |
| 34 pElement.removeChild(divElement); | |
| 35 | |
| 36 return cssPixelsPerInch; | |
| 37 } | |
| 38 | |
| 39 function calculateXHeight() { | |
| 40 // Crude hack to calculate the x-height | |
| 41 var divElement = document.createElement("div"); | |
| 42 divElement.setAttribute("style", "height: 1ex; font-size: 12px; font-family: A hem;"); | |
| 43 var pElement = document.querySelector("p"); | |
| 44 pElement.appendChild(divElement); | |
| 45 var xHeight = divElement.offsetHeight; | |
| 46 pElement.removeChild(divElement); | |
| 47 return xHeight; | |
| 48 } | |
|
fs
2016/08/26 09:03:06
Everything above here could go in a (testharness)
| |
| 49 | |
| 50 test(function() { | |
| 51 // Set value to be 2px. | |
| 52 length.valueAsString = "2px"; | |
| 53 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PX); | |
| 54 assert_equals(length.value, 2); | |
| 55 assert_equals(length.valueInSpecifiedUnits, 2); | |
| 56 assert_equals(length.valueAsString, "2px"); | |
| 57 }, "Check initial value to be 2px"); | |
|
fs
2016/08/26 09:03:07
Same naming strategy here. Also, this particular t
| |
| 58 | |
| 59 test(function() { | |
| 60 length.valueAsString = "2px"; | |
| 61 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER); | |
| 62 assert_equals(length.valueAsString, "2"); | |
| 63 assert_equals(length.value, 2); | |
| 64 assert_equals(length.valueInSpecifiedUnits, 2); | |
| 65 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_NUMBER); | |
| 66 }, "Convert from px to unitless"); | |
| 67 | |
| 68 test(function() { | |
| 69 length.valueAsString = "2px"; | |
| 70 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE); | |
| 71 referenceValue = Number(2 / svgWidth * 100).toFixed(5); | |
| 72 assert_equals(length.valueAsString, referenceValue + "%"); | |
| 73 assert_equals(length.valueInSpecifiedUnits.toFixed(5), referenceValue); | |
| 74 assert_equals(length.value.toFixed(1), "2.0"); | |
| 75 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PERCENTAGE); | |
| 76 }, "Convert from px to percentage"); | |
| 77 | |
| 78 test(function() { | |
| 79 length.valueAsString = "2px"; | |
| 80 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EMS); | |
| 81 referenceValue = Number(2 / fontSize).toFixed(6); | |
| 82 assert_equals(length.valueAsString, referenceValue + "em"); | |
| 83 assert_equals(length.valueInSpecifiedUnits.toFixed(6), referenceValue); | |
| 84 assert_equals(length.value.toFixed(1), "2.0"); | |
| 85 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_EMS); | |
| 86 }, "Convert from px to ems"); | |
| 87 | |
| 88 test(function() { | |
| 89 length.valueAsString = "2px"; | |
| 90 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EXS); | |
| 91 referenceValue = Number(2 / calculateXHeight()).toFixed(1); | |
| 92 // Don't check valueAsString here, it's unreliable across browsers. | |
| 93 assert_equals(length.valueInSpecifiedUnits.toFixed(1), referenceValue); | |
| 94 assert_equals(length.value.toFixed(1), "2.0"); | |
| 95 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_EXS); | |
| 96 }, "Convert from px to exs"); | |
| 97 | |
| 98 test(function() { | |
| 99 length.valueAsString = "2px"; | |
| 100 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_CM); | |
| 101 referenceValue = Number(2 * 2.54 / cssPixelsPerInch).toFixed(7); | |
| 102 assert_equals(length.valueAsString, referenceValue + "cm"); | |
| 103 assert_equals(length.valueInSpecifiedUnits.toFixed(7), referenceValue); | |
| 104 assert_equals(length.value.toFixed(1), "2.0"); | |
| 105 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_CM); | |
| 106 }, "Convert from px to cm"); | |
| 107 | |
| 108 test(function() { | |
| 109 length.valueAsString = "2px"; | |
| 110 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM); | |
| 111 referenceValue = Number(2 * 25.4 / cssPixelsPerInch).toFixed(6); | |
| 112 assert_equals(length.valueAsString, referenceValue + "mm"); | |
| 113 assert_equals(length.valueInSpecifiedUnits.toFixed(6), referenceValue); | |
| 114 assert_equals(length.value.toFixed(1), "2.0"); | |
| 115 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_MM); | |
| 116 }, "Convert from px to mm"); | |
| 117 | |
| 118 test(function() { | |
| 119 length.valueAsString = "2px"; | |
| 120 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_IN); | |
| 121 referenceValue = Number(2 / cssPixelsPerInch).toFixed(7); | |
| 122 assert_equals(length.valueAsString, referenceValue + "in"); | |
| 123 assert_equals(length.valueInSpecifiedUnits.toFixed(7), referenceValue); | |
| 124 assert_equals(length.value.toFixed(1), "2.0"); | |
| 125 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_IN); | |
| 126 }, "Convert from px to in"); | |
| 127 | |
| 128 test(function() { | |
| 129 length.valueAsString = "2px"; | |
| 130 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PT); | |
| 131 referenceValue = Number(2 / cssPixelsPerInch * 72); | |
| 132 assert_equals(length.valueAsString, referenceValue + "pt"); | |
| 133 assert_equals(length.valueInSpecifiedUnits, referenceValue); | |
| 134 assert_equals(length.value.toFixed(1), "2.0"); | |
| 135 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PT); | |
| 136 }, "Convert from px to pt"); | |
| 137 | |
| 138 test(function() { | |
| 139 length.valueAsString = "2px"; | |
| 140 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PC); | |
| 141 referenceValue = Number(2 / cssPixelsPerInch * 6).toFixed(3); | |
| 142 // Don't check valueAsString here, it's unreliable across browsers. | |
| 143 assert_equals(length.valueInSpecifiedUnits.toFixed(3), referenceValue); | |
| 144 assert_equals(length.value.toFixed(1), "2.0"); | |
| 145 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PC); | |
| 146 }, "Convert from px to pc"); | |
| 147 </script> | |
| OLD | NEW |