summaryrefslogtreecommitdiffstats
path: root/dom/svg/test/test_lengthParsing.html
blob: 8471b198553df455a8469b2dc6a03ac0242d439c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!doctype html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=946529
-->
<head>
  <meta charset="utf-8">
  <title>Test transform parsing</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=946529">Mozilla Bug 946529</a>
<p id="display"></p>
<div id="content" style="display: none">
  <svg width="100%" height="1" id="svg">
    <rect id="rect"/>
  </svg>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
// Test cases
checkParseOk("", 0);
checkParseOk("-.1", -0.1);
checkParseOk("1e1", 10);
checkParseOk("1em", 1, "em");
checkParseOk("1ex", 1, "ex");
checkParseOk("1e1em", 10, "em");
checkParseOk("1E+2", 100);
checkParseOk(".1e-2", 0.001);
checkParseOk("   10", 10);
checkParseOk("10  ", 10);
checkParseOk("  10  ", 10);
checkParseOk("  10em  ", 10, "em");

// Fail cases
checkParseFail("1e");
checkParseFail("1 e");
checkParseFail("1 em");
checkParseFail("1ee");
checkParseFail(" 10 20");

function checkParseOk(spec, valueInUnits, units) {
  var rect = document.getElementById("rect");

  // Clear previous value
  rect.removeAttribute("x");
  rect.setAttribute("x", spec);

  // Check number part
  const tolerance = 1 / 65535;
  var actual = rect.x.baseVal.valueInSpecifiedUnits;
  ok(Math.abs(actual - valueInUnits) < tolerance,
     spec + " (value) - got " + actual + ", expected " + valueInUnits);

  // Check unit part
  var unitMapping = {
    "unknown": SVGLength.SVG_LENGTHTYPE_UNKNOWN,
    "": SVGLength.SVG_LENGTHTYPE_NUMBER,
    "%": SVGLength.SVG_LENGTHTYPE_PERCENTAGE,
    "em": SVGLength.SVG_LENGTHTYPE_EMS,
    "ex": SVGLength.SVG_LENGTHTYPE_EXS,
    "px": SVGLength.SVG_LENGTHTYPE_PX,
    "cm": SVGLength.SVG_LENGTHTYPE_CM,
    "mm": SVGLength.SVG_LENGTHTYPE_MM,
    "in": SVGLength.SVG_LENGTHTYPE_IN,
    "pt": SVGLength.SVG_LENGTHTYPE_PT,
    "pc": SVGLength.SVG_LENGTHTYPE_PC,
  };
  if (typeof units == "undefined") {
    units = "";
  }
  is(rect.x.baseVal.unitType, unitMapping[units], spec + " (unit)");
}

function checkParseFail(spec) {
  checkParseOk(spec, 0);
}
</script>
</pre>
</body>
</html>