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 return viewportWidth / 100; |
| 19 } |
| 20 |
| 21 function assert_calc_expression_on_width_attr(expression, expected) { |
| 22 var rect = document.querySelector('rect'); |
| 23 var rectWidth; |
| 24 // Test calc with setAttribute |
| 25 rect.setAttribute('width', expression); |
| 26 rectWidth = rect.getBoundingClientRect().width; |
| 27 assert_approx_equals(rectWidth, expected, EPSILON); |
| 28 assert_approx_equals(rect.width.baseVal.value, expected, EPSILON); |
| 29 assert_equals(rect.width.baseVal.unitType, SVGLength.SVG_LENGTHTYPE_UNKNOWN); |
| 30 assert_equals(rect.width.baseVal.valueInSpecifiedUnits, 0); |
| 31 |
| 32 // Test valueInSpecifiedUnits setter after 'calc' value attribute |
| 33 rect.width.baseVal.valueInSpecifiedUnits = 100; |
| 34 assert_equals(rect.width.baseVal.unitType, SVGLength.SVG_LENGTHTYPE_NUMBER); |
| 35 assert_equals(rect.width.baseVal.valueInSpecifiedUnits, 100); |
| 36 |
| 37 // Test value setter after 'calc' value attribute |
| 38 rect.setAttribute('width', expression); // reset to calc value |
| 39 rect.width.baseVal.value = 10; |
| 40 assert_equals(rect.width.baseVal.unitType, SVGLength.SVG_LENGTHTYPE_NUMBER); |
| 41 assert_equals(rect.width.baseVal.valueInSpecifiedUnits, 10); |
| 42 |
| 43 // Test calc with "valueAsString" |
| 44 rect.setAttribute('width', '20px'); // reset to normal value |
| 45 assert_throws(null, function() {rect.width.baseVal.valueAsString = expression}
); |
| 46 assert_equals(rect.width.baseVal.value, 20); |
| 47 } |
| 48 |
| 49 function assert_calc_expression_on_markerWidth_attr(expression, expected) { |
| 50 var marker = document.querySelector('marker'); |
| 51 // Test calc with setAttribute |
| 52 marker.setAttribute('markerWidth', expression); |
| 53 assert_approx_equals(marker.markerWidth.baseVal.value, expected, EPSILON); |
| 54 assert_equals(marker.markerWidth.baseVal.unitType, SVGLength.SVG_LENGTHTYPE_UN
KNOWN); |
| 55 assert_equals(marker.markerWidth.baseVal.valueInSpecifiedUnits, 0); |
| 56 |
| 57 // Test valueInSpecifiedUnits setter after 'calc' value attribute |
| 58 marker.markerWidth.baseVal.valueInSpecifiedUnits = 100; |
| 59 assert_equals(marker.markerWidth.baseVal.unitType, SVGLength.SVG_LENGTHTYPE_NU
MBER); |
| 60 assert_equals(marker.markerWidth.baseVal.valueInSpecifiedUnits, 100); |
| 61 |
| 62 // Test value setter after 'calc' value attribute |
| 63 marker.setAttribute('markerWidth', expression); // reset to calc value |
| 64 marker.markerWidth.baseVal.value = 10; |
| 65 assert_equals(marker.markerWidth.baseVal.unitType, SVGLength.SVG_LENGTHTYPE_NU
MBER); |
| 66 assert_equals(marker.markerWidth.baseVal.valueInSpecifiedUnits, 10); |
| 67 |
| 68 // Test calc with "valueAsString" |
| 69 marker.setAttribute('markerWidth', '20px'); // reset to normal value |
| 70 assert_throws(null, function() {marker.markerWidth.baseVal.valueAsString = exp
ression}); |
| 71 assert_equals(marker.markerWidth.baseVal.value, 20); |
| 72 } |
| 73 |
| 74 function assert_calc_expression(expression, expected) { |
| 75 assert_calc_expression_on_width_attr(expression, expected); // presentation
attr |
| 76 assert_calc_expression_on_markerWidth_attr(expression, expected); // non pre
sentation attr |
| 77 } |
| 78 |
| 79 test(function() { |
| 80 assert_calc_expression("calc(20%)", (20 * viewportWidthPercent())); |
| 81 assert_calc_expression("calc(10mm + 10in)", (10 * cssPixelsPerMillimeter) +
(10 * cssPixelsPerInch)); |
| 82 assert_calc_expression("calc(10mm + 10mm)", (20 * cssPixelsPerMillimeter)); |
| 83 assert_calc_expression("calc(20mm)", (20 * cssPixelsPerMillimeter)); |
| 84 assert_calc_expression("calc(10 + 10)", 20); |
| 85 assert_calc_expression("calc(10mm + 10)", (10 * cssPixelsPerMillimeter) + 10
); |
| 86 assert_calc_expression("calc(10% + 10)", (10 * viewportWidthPercent()) + 10)
; |
| 87 assert_calc_expression("calc(1cm + 2in + 1cm + 2)", (2 * cssPixelsPerInch) +
(2 * cssPixelsPerCentimeter) + 2); |
| 88 assert_calc_expression("calc(1cm + 2 + 1cm + 2in)", (2 * cssPixelsPerInch) +
(2 * cssPixelsPerCentimeter) + 2); |
| 89 assert_calc_expression("calc(10% + 10 + 2% + 10pc)", (12 * viewportWidthPerc
ent()) + 10 + (10 * cssPixelsPerPica)); |
| 90 }, "Tests calc() on presentation and non-presentation attr in svgLength"); |
| 91 </script> |
OLD | NEW |