test_transformParsing.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <!doctype html>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=946529
  5. -->
  6. <head>
  7. <meta charset="utf-8">
  8. <title>Test transform parsing</title>
  9. <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  10. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  11. </head>
  12. <body>
  13. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=946529">Mozilla Bug 946529</a>
  14. <p id="display"></p>
  15. <div id="content" style="display: none">
  16. <svg width="100%" height="1" id="svg">
  17. <g id="g"/>
  18. </svg>
  19. </div>
  20. <pre id="test">
  21. <script class="testbody" type="text/javascript">
  22. // Test cases
  23. checkParseOk("", [ ]);
  24. checkParseOk("matrix(-.7235 .6903 .6903 .7235 -2050 1.14e4)",
  25. [ { type: "matrix", a: -0.7235, b: 0.6903, c: 0.6903,
  26. d: 0.7235, e: -2050, f: 11400 } ]);
  27. checkParseOk("matrix(0e0 1e0 1e1 1e-1 1E+2 -.1e1)",
  28. [ { type: "matrix", a: 0, b: 1, c: 10,
  29. d: 0.1, e: 100, f: -1 } ]);
  30. checkParseOk("matrix(-0e-0 1e+0 0e-5 1e-10 12.3e+4 .12e2)",
  31. [ { type: "matrix", a: 0, b: 1, c: 0,
  32. d: 0.0000000001, e: 123000, f: 12 } ]);
  33. // Fail cases
  34. checkParseFail("matrix(1e+ 0 0 0 0 0)");
  35. checkParseFail("matrix(e2 0 0 0 0 0)");
  36. checkParseFail("matrix(1 e2 0 0 0 0 0)");
  37. checkParseFail("matrix(1e 2 0 0 0 0 0)");
  38. checkParseFail("matrix(1e+-2 0 0 0 0 0)");
  39. checkParseFail("matrix(1e 0 0 0 0 0)");
  40. checkParseFail("matrix(1e1.1 0 0 0 0 0)");
  41. checkParseFail("scale(2) matrix(1e1.1 0 0 0 0 0)");
  42. function checkParseOk(spec, expected) {
  43. var g = document.getElementById("g");
  44. // Clear previous value
  45. g.removeAttribute("transform");
  46. g.setAttribute("transform", spec);
  47. // Check length
  48. var transformList = g.transform.baseVal;
  49. is(transformList.numberOfItems, expected.length, spec + " - length");
  50. if (transformList.numberOfItems != expected.length)
  51. return;
  52. // Check each item
  53. for (var i=0; i < transformList.numberOfItems; i++) {
  54. checkTransform(transformList.getItem(i), expected[i], spec, i);
  55. }
  56. }
  57. function checkTransform(transform, expected, spec, index) {
  58. var typeMapping = {
  59. "unknown": SVGTransform.SVG_TRANSFORM_UNKNOWN,
  60. "matrix": SVGTransform.SVG_TRANSFORM_MATRIX,
  61. "translate": SVGTransform.SVG_TRANSFORM_TRANSLATE,
  62. "scale": SVGTransform.SVG_TRANSFORM_SCALE,
  63. "rotate": SVGTransform.SVG_TRANSFORM_ROTATE,
  64. "skewx": SVGTransform.SVG_TRANSFORM_SKEWX,
  65. "skewy": SVGTransform.SVG_TRANSFORM_SKEWY
  66. };
  67. var name = "Item " + index + " of '" + spec + "'";
  68. // Compare type
  69. if (typeof expected.type != "undefined") {
  70. is(transform.type, typeMapping[expected.type], name + " - transform type");
  71. }
  72. // Compare angle
  73. if (typeof expected.angle != "undefined") {
  74. is(transform.angle, expected.angle, name + " - angle");
  75. }
  76. // Compare matrix values (roughly)
  77. ['a', 'b', 'c', 'd', 'e', 'f'].forEach(function(item) {
  78. var actual = transform.matrix[item];
  79. var msg = name + " - matrix:" + item;
  80. const tolerance = 1 / 65535;
  81. ok(Math.abs(actual - expected[item]) < tolerance,
  82. msg + ' - got ' + actual + ', expected ' + expected[item]);
  83. });
  84. }
  85. function checkParseFail(spec) {
  86. checkParseOk(spec, []);
  87. }
  88. </script>
  89. </pre>
  90. </body>
  91. </html>