summaryrefslogtreecommitdiffstats
path: root/dom/svg/test/test_hit-testing-and-viewbox.xhtml
blob: 6ab83e54be618d24041368855bd460532563286f (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
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1486952
-->
<head>
  <title>Test that hit-testing works after a viewBox update</title>

  <style>
    :hover { fill: lime; }
  </style>

  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>

<body onload="run()">
<script class="testbody" type="text/javascript">
<![CDATA[

SimpleTest.waitForExplicitFinish();

function run() {
  const div = $("div");
  const offsetX = div.offsetLeft;
  const offsetY = div.offsetTop;
  const outerRect = $("outerRect");
  const innerRect = $("innerRect");
  const outerSVG = $("outerSVG");
  const innerSVG = $("innerSVG");
  let got;

  // Update the inner SVG viewBox to "move" the inner rectangle off its current
  // position on screen:
  innerSVG.setAttribute("viewBox", "-25 0 50 50");
  got = document.elementFromPoint(offsetX + 150, offsetY + 25);
  is(got, innerRect, "Should hit inner rectangle (1)");

  // Update the inner SVG viewBox again. (At the time of writing, a reflow is
  // triggered the first time you change viewBox on an inner svg, so the
  // previous test is expected to always pass. This next test will fail if we're
  // updating overflows on the inner svg frame instead of its children).
  innerSVG.setAttribute("viewBox", "0 -25 50 50");
  got = document.elementFromPoint(offsetX + 100, offsetY + 75);
  is(got, innerRect, "Should hit inner rectangle (2)");

  // Now update the outer SVG viewBox and confirm that both rectangles are
  // registered.  (Note that in this case the overflow rectangle of the inner
  // svg is the inner svg's viewport, so be sure to "move" the outer svg so that
  // the "new" outer rectangle and inner svg are off the current outerRect
  // union inner svg viewport - hit testing still works in that union.)
  outerSVG.setAttribute("viewBox", "-200 0 400 100");
  // Outer:
  got = document.elementFromPoint(offsetX + 250, offsetY + 50);
  is(got, outerRect, "Should hit outer rectangle");
  // Inner:
  got = document.elementFromPoint(offsetX + 300, offsetY + 75);
  is(got, innerRect, "Should hit inner rectangle (3)");

  SimpleTest.finish();
}

]]>
</script>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1486952">Mozilla Bug 1486952</a>
<p id="display"></p>
<div id="content">

  <div width="100%" height="1" id="div"></div>
  <svg xmlns="http://www.w3.org/2000/svg" id="outerSVG" width="400" height="100"
       viewBox="0 0 400 100">
    <rect x="25" y="25" width="50" height="50" fill="red" id="outerRect" />
    <svg x="75" width="100" height="100" viewBox="0 0 50 50" id="innerSVG">
      <rect width="25" height="25" fill="red" id="innerRect" />
    </svg>
  </svg>

</div>
<pre id="test">
</pre>
</body>
</html>