DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Line Code
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
<!DOCTYPE html>
<html>
  <head>
    <title>Shadow DOM and CSSOM View: Document.prototype.elementFromPoint</title>
    <meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
    <meta name="assert" content="DocumentOrShadowRoot must have elementFromPoint and must return retarget the result against the context object.">
    <link rel="help" href="https://www.w3.org/TR/cssom-view-1/#dom-document-elementfrompoint">
    <link rel="help" href="https://www.w3.org/TR/shadow-dom/#extensions-to-the-documentorshadowroot-mixin">
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
  </head>
  <body>
    <div id="container"></div>
    <style>
test-element { display: block; width: 100px; height: 100px; }
    </style>
    <script>

function pointInElement(node) {
  let x = 5;
  let y = 5;
  do {
    x += node.offsetLeft;
    y += node.offsetTop;
    node = node.offsetParent;
  } while (node);
  return [x, y];
}

const displayValues = ['inline', 'block', 'inline-block'];
var container = document.getElementById('container');
customElements.define('test-element', class extends HTMLElement {
  constructor() {
    super();
  }
});

displayValues.forEach(function (displayValue) {
  test(function () {
    container.innerHTML = '';
    let host = document.createElement('test-element');
    host.style.display = displayValue;
    let shadow = host.attachShadow({mode: 'closed'});
    shadow.innerHTML = 'hello';
    container.appendChild(host);
    assert_equals(document.elementFromPoint(...pointInElement(host)), host);
    assert_equals(shadow.elementFromPoint(...pointInElement(host)), host);
  }, 'document.elementFromPoint and shadow.ElementFromPoint must return the shadow host of the hit-tested text node when the hit-tested text node is a direct child of the root and the host has display: ' + displayValue);
});

displayValues.forEach(function (displayValue) {
  test(function () {
    container.innerHTML = '';
    let host = document.createElement('test-element');
    host.style.display = displayValue;
    let shadow = host.attachShadow({mode: 'closed'});
    shadow.innerHTML = '<slot></slot>';
    host.innerHTML = 'text';
    container.appendChild(host);
    assert_equals(document.elementFromPoint(...pointInElement(host)), host);
    assert_equals(shadow.elementFromPoint(...pointInElement(host)), host);
  }, 'document.elementFromPoint and shadowRoot.elementFromPoint must return the shadow host when the hit-tested text node is assigned to a slot and the host has display: ' + displayValue);
});

displayValues.forEach(function (displayValue) {
  test(function () {
    container.innerHTML = '';
    let host = document.createElement('test-element');
    host.style.display = displayValue;
    let shadow = host.attachShadow({mode: 'closed'});
    shadow.innerHTML = '<slot></slot>';
    host.innerHTML = '<span>text</span>';
    container.appendChild(host);
    assert_equals(document.elementFromPoint(...pointInElement(host)), host.querySelector('span'));
    assert_equals(shadow.elementFromPoint(...pointInElement(host)), host.querySelector('span'));
  }, 'document.elementFromPoint and shadowRoot.elementFromPoint must return the element assigned to a slot when hit-tested text node under an element is assigned to a slot in the shadow tree and the shadow host of the slot has display: ' + displayValue);
});

displayValues.forEach(function (displayValue) {
  test(function () {
    container.innerHTML = '';
    let host = document.createElement('test-element');
    host.style.display = displayValue;
    let shadow = host.attachShadow({mode: 'closed'});
    shadow.innerHTML = '<span>text</span>';
    container.appendChild(host);
    assert_equals(document.elementFromPoint(...pointInElement(host)), host);
    assert_equals(shadow.elementFromPoint(...pointInElement(host)), shadow.querySelector('span'));
  }, 'document.elementFromPoint must return the shadow host of the hit-tested element under a shadow root and shadowRoot.elementFromPoint must return the element parent of the hit-tested text node under the point when the shadow host has display: ' + displayValue);
});

displayValues.forEach(function (displayValue) {
  test(function () {
    container.innerHTML = '';
    let host = document.createElement('test-element');
    host.style.display = displayValue;
    let shadow = host.attachShadow({mode: 'closed'});
    shadow.innerHTML = '<slot>fallback</slot>';
    container.appendChild(host);
    assert_equals(document.elementFromPoint(...pointInElement(host)), host);
    assert_equals(shadow.elementFromPoint(...pointInElement(host)), shadow.querySelector('slot'));
  }, 'document.elementFromPoint must return the shadow host and shadowRoot.elementFromPoint must return the slot parent of the fallback text when the hit-tested text node is a fallback content and the host has display: ' + displayValue);
});



displayValues.forEach(function (displayValue) {
  test(function () {
    container.innerHTML = '';
    let host = document.createElement('test-element');
    host.style.display = displayValue;
    let shadow = host.attachShadow({mode: 'closed'});
    shadow.innerHTML = '<slot></slot>';
    host.innerHTML = '<inner-host>hello</inner-host>';
    container.appendChild(host);

    let innerHost = host.querySelector('inner-host');
    let innerShadow = innerHost.attachShadow({mode: 'closed'});
    innerShadow.innerHTML = '<slot></slot>';
    assert_equals(document.elementFromPoint(...pointInElement(host)), innerHost);
    assert_equals(shadow.elementFromPoint(...pointInElement(host)), innerHost);
    assert_equals(innerShadow.elementFromPoint(...pointInElement(host)), innerHost);
  }, 'document.elementFromPoint, shadowRoot.elementFromPoint, innerShadow.elementFromPoint must return a child element assigned to a slot'
  + ' when the hit-tested text node is assigned to a slot in the shadow tree of the child element and the outer shadow host has display: ' + displayValue);
});

displayValues.forEach(function (displayValue) {
  test(function () {
    container.innerHTML = '';
    let host = document.createElement('test-element');
    host.style.display = displayValue;
    let shadow = host.attachShadow({mode: 'closed'});
    shadow.innerHTML = '<slot></slot>';
    host.innerHTML = '<inner-host></inner-host>';
    container.appendChild(host);

    let innerHost = host.querySelector('inner-host');
    let innerShadow = innerHost.attachShadow({mode: 'closed'});
    innerShadow.innerHTML = 'hello';
    assert_equals(document.elementFromPoint(...pointInElement(host)), innerHost);
    assert_equals(shadow.elementFromPoint(...pointInElement(host)), innerHost);
    assert_equals(innerShadow.elementFromPoint(...pointInElement(host)), innerHost);
  }, 'document.elementFromPoint, shadowRoot.elementFromPoint, innerShadow.elementFromPoint must return a child element with its own shadow tree assigned to a slot'
  + ' when the hit-tested text node is its direct child and the outer shadow host has display: ' + displayValue);
});

displayValues.forEach(function (displayValue) {
  test(function () {
    container.innerHTML = '';
    let host = document.createElement('test-element');
    host.style.display = displayValue;
    let shadow = host.attachShadow({mode: 'closed'});
    shadow.innerHTML = '<slot></slot>';
    host.innerHTML = '<inner-host></inner-host>';
    container.appendChild(host);

    let innerHost = host.querySelector('inner-host');
    let innerShadow = innerHost.attachShadow({mode: 'closed'});
    innerShadow.innerHTML = '<span>hello</span>';

    assert_equals(document.elementFromPoint(...pointInElement(host)), innerHost);
    assert_equals(shadow.elementFromPoint(...pointInElement(host)), innerHost);
    assert_equals(innerShadow.elementFromPoint(...pointInElement(host)), innerShadow.querySelector('span'));
  }, 'document.elementFromPoint, shadowRoot.elementFromPoint must return a child element with its own shadow tree assigned to a slot'
  + ' when the hit-tested text node is a child of another element and innerShadow.elementFromPoint must return the parent element of the hit-tested text node under it when the outer shadow host has display: ' + displayValue);
});

displayValues.forEach(function (displayValue) {
  test(function () {
    container.innerHTML = '';
    let host = document.createElement('test-element');
    host.style.display = displayValue;
    let shadow = host.attachShadow({mode: 'closed'});
    shadow.innerHTML = 'hello';
    container.appendChild(host);
    assert_array_equals(document.elementsFromPoint(...pointInElement(host)), [host, container, document.body, document.documentElement]);
    assert_array_equals(shadow.elementsFromPoint(...pointInElement(host)), [host, container, document.body, document.documentElement]);
  }, 'document.elementsFromPoint and shadow.elementsFromPoint must return the shadow host and its ancestors of the hit-tested text node when the hit-tested text node is a direct child of the root and the host has display: ' + displayValue);
});

displayValues.forEach(function (displayValue) {
  test(function () {
    container.innerHTML = '';
    let host = document.createElement('test-element');
    host.style.display = displayValue;
    let shadow = host.attachShadow({mode: 'closed'});
    shadow.innerHTML = '<slot></slot>';
    host.innerHTML = 'text';
    container.appendChild(host);
    assert_array_equals(document.elementsFromPoint(...pointInElement(host)), [host, container, document.body, document.documentElement]);
    assert_array_equals(shadow.elementsFromPoint(...pointInElement(host)), [host, container, document.body, document.documentElement]);
  },'document.elementsFromPoint and shadowRoot.elementsFromPoint must return the shadow host and its ancestors when the hit-tested text node is assigned to a slot and the host has display: ' + displayValue);
});

displayValues.forEach(function (displayValue) {
  test(function () {
    container.innerHTML = '';
    let host = document.createElement('test-element');
    host.style.display = displayValue;
    let shadow = host.attachShadow({mode: 'closed'});
    shadow.innerHTML = '<div><slot></slot></div>';
    host.innerHTML = '<span>text</span>';
    container.appendChild(host);
    assert_array_equals(document.elementsFromPoint(...pointInElement(host)), [host.querySelector('span'), host, container, document.body, document.documentElement]);
    assert_array_equals(shadow.elementsFromPoint(...pointInElement(host)), [host.querySelector('span'), shadow.querySelector('div'),  host, container, document.body, document.documentElement]);
  }, 'document.elementsFromPoint and shadowRoot.elementsFromPoint must return the element assigned to a slot and its non-shadow ancestors when hit-tested text node under an element is assigned to a slot in the shadow tree and the shadow host of the slot has display: ' + displayValue);
});

displayValues.forEach(function (displayValue) {
  test(function () {
    container.innerHTML = '';
    let host = document.createElement('test-element');
    host.style.display = displayValue;
    let shadow = host.attachShadow({mode: 'closed'});
    shadow.innerHTML = '<span>text</span>';
    container.appendChild(host);
    assert_array_equals(document.elementsFromPoint(...pointInElement(host)), [host, container, document.body, document.documentElement]);
    assert_array_equals(shadow.elementsFromPoint(...pointInElement(host)), [shadow.querySelector('span'), host, container, document.body, document.documentElement]);
  }, 'document.elementsFromPoint must return the shadow host and its ancestors of the hit-tested element under a shadow root and'
  + 'shadowRoot.elementsFromPoint must return the element parent and its non-shadow ancestors of the hit-tested text node under the point when the shadow host has display: ' + displayValue);
});

displayValues.forEach(function (displayValue) {
  test(function () {
    container.innerHTML = '';
    let host = document.createElement('test-element');
    host.style.display = displayValue;
    let shadow = host.attachShadow({mode: 'closed'});
    shadow.innerHTML = '<div><slot>fallback</slot></div>';
    container.appendChild(host);
    assert_array_equals(document.elementsFromPoint(...pointInElement(host)), [host, container, document.body, document.documentElement]);
    assert_array_equals(shadow.elementsFromPoint(...pointInElement(host)), [shadow.querySelector('slot'), shadow.querySelector('div'), host, container, document.body, document.documentElement]);
  }, 'document.elementsFromPoint must return the shadow host and its ancestors and shadowRoot.elementsFromPoint must return the slot parent of the fallback text and its non-shadow ancestors when the hit-tested text node is a fallback content and the host has display: ' + displayValue);
});

test(function () {
  container.innerHTML = '';
  let host = document.createElement('test-element');
  host.style.display = 'block';
  let shadow = host.attachShadow({mode: 'closed'});
  shadow.innerHTML = '<div style="margin: 2px;">not hit</div>';
  let aboveHost = document.createElement("div");
  aboveHost.appendChild(host);
  container.appendChild(aboveHost);
  document.documentElement.style = 'background-attachment: scroll; height: 2px;';
  let boundingRect = host.getBoundingClientRect();
  assert_array_equals(document.elementsFromPoint(boundingRect.x, boundingRect.y), [host, aboveHost, container, document.body, document.documentElement]);
  assert_array_equals(shadow.elementsFromPoint(boundingRect.x, boundingRect.y), [host, aboveHost, container, document.body, document.documentElement]);
}, 'shadowRoot.elementsFromPoint must behave the same with document.elementsFromPoint regarding HTML element');

container.innerHTML = '';

test(function(t) {
  const container = document.createElement("div");
  const span = document.createElement("span");
  span.innerText = "foo";
  document.body.appendChild(container);
  container.appendChild(span);

  let elements = document.elementsFromPoint(...pointInElement(span));
  assert_equals(elements.length, 4);
  assert_equals(elements[0], span);
  assert_equals(elements[1], container);
  assert_equals(elements[2].nodeName, 'BODY');
  assert_equals(elements[3].nodeName, 'HTML');

  const host = document.createElement("div");
  const shadowRoot = host.attachShadow({ mode: "open" });
  elements = shadowRoot.elementsFromPoint(...pointInElement(span));
  assert_equals(elements.length, 4);
  assert_equals(elements[0], span);
  assert_equals(elements[1], container);
  assert_equals(elements[2].nodeName, 'BODY');
  assert_equals(elements[3].nodeName, 'HTML');
}, "elementsFromPoint should return all elements under a point, even when context object is not connected");
</script>
</body>
</html>