summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/chrome/test_nodesFromPoint.html
blob: 30675a41dd2ef49ebcf87087c46eabe88807d8f6 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!doctype html>
<meta charset="utf-8">
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<style>
  div { text-align: justify; max-height: 100vh; }
</style>
<div id="testElement"></div>
<script>

// TODO(emilio): Upstream to WPT once there's a spec for this and our
// implementation is not [ChromeOnly].

const testElement = document.getElementById("testElement");

testElement.innerHTML = "X ".repeat(5000);

{
  const rect = testElement.getBoundingClientRect();

  const node =
    document.nodeFromPoint(rect.x + rect.width / 2, rect.y + rect.height / 2);
  is(node, testElement.firstChild, "Should return the text node");

  const nodes =
    document.nodesFromPoint(rect.x + rect.width / 2, rect.y + rect.height / 2);

  const expected = [testElement.firstChild, testElement, document.body, document.documentElement];
  is(nodes.length, expected.length, "Not the amount of expected nodes");

  for (let i = 0; i < nodes.length; ++i)
    is(nodes[i], expected[i]);
}

// Make the test slotted, and add some fallback that we'll test later as well.
{
  // Work around the sanitizer by building the DOM manually....
  const slot = document.createElement("slot");
  slot.innerHTML = "Y ".repeat(5000);

  const wrapper = document.createElement("div");
  wrapper.appendChild(slot);

  testElement.attachShadow({ mode: "open" }).appendChild(wrapper);
}

{
  const rect = testElement.getBoundingClientRect();

  const node =
    document.nodeFromPoint(rect.x + rect.width / 2, rect.y + rect.height / 2);
  is(node, testElement.firstChild, "Should return the text node");

  const nodes =
    document.nodesFromPoint(rect.x + rect.width / 2, rect.y + rect.height / 2);

  const expected = [testElement.firstChild, testElement, document.body, document.documentElement];
  is(nodes.length, expected.length, "Not the amount of expected nodes (returned nodes in the shadow?)");

  for (let i = 0; i < nodes.length; ++i)
    is(nodes[i], expected[i]);
}

{
  const rect = testElement.getBoundingClientRect();

  const node =
    testElement.shadowRoot.nodeFromPoint(rect.x + rect.width / 2, rect.y + rect.height / 2);
  is(node, testElement.shadowRoot.firstChild, "Should return the div wrapping the text node");

  const nodes =
    testElement.shadowRoot.nodesFromPoint(rect.x + rect.width / 2, rect.y + rect.height / 2);

  const expected = [testElement.shadowRoot.firstChild];
  is(nodes.length, expected.length, "Not the amount of expected nodes (returned nodes outside of the shadow?)");

  for (let i = 0; i < nodes.length; ++i)
    is(nodes[i], expected[i]);
}

// Show the fallback.
testElement.firstChild.remove();

{
  const rect = testElement.getBoundingClientRect();

  const fallbackText = testElement.shadowRoot.querySelector("slot").firstChild;

  const node =
    testElement.shadowRoot.nodeFromPoint(rect.x + rect.width / 2, rect.y + rect.height / 2);
  is(node, fallbackText, "Should return the fallback text");

  const nodes =
    testElement.shadowRoot.nodesFromPoint(rect.x + rect.width / 2, rect.y + rect.height / 2);

  const expected = [fallbackText, testElement.shadowRoot.firstChild];
  is(nodes.length, expected.length, "Not the amount of expected nodes (returned nodes outside of the shadow?)");

  for (let i = 0; i < nodes.length; ++i)
    is(nodes[i], expected[i]);
}

// Test the fallback from the document.
{
  const rect = testElement.getBoundingClientRect();

  const node =
    document.nodeFromPoint(rect.x + rect.width / 2, rect.y + rect.height / 2);
  is(node, testElement, "Should return the element, since the fallback text is in the shadow");

  const nodes =
    document.nodesFromPoint(rect.x + rect.width / 2, rect.y + rect.height / 2);

  const expected = [testElement, document.body, document.documentElement];
  is(nodes.length, expected.length, "Not the amount of expected nodes (returned nodes inside of the shadow?)");

  for (let i = 0; i < nodes.length; ++i)
    is(nodes[i], expected[i]);
}
</script>