<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[206795] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/206795">206795</a></dd>
<dt>Author</dt> <dd>rniwa@webkit.org</dd>
<dt>Date</dt> <dd>2016-10-04 16:42:18 -0700 (Tue, 04 Oct 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>ShadowRoot interface should have elementFromPoint
https://bugs.webkit.org/show_bug.cgi?id=162882

Reviewed by Chris Dumez.

Source/WebCore:

Add elementFromPoint to ShadowRoot's prototype as specified at:
https://www.w3.org/TR/shadow-dom/#extensions-to-the-documentorshadowroot-mixin
with changes proposed at https://github.com/w3c/csswg-drafts/issues/556

Added TreeScope::retargetToScope which implements

This patch also factors DocumentOrShadowRoot.idl out of Document and ShadowRoot interfaces to better match
the latest DOM specification: https://dom.spec.whatwg.org/#mixin-documentorshadowroot

Test: fast/shadow-dom/Document-prototype-elementFromPoint.html

* CMakeLists.txt:
* DerivedSources.make:
* WebCore.xcodeproj/project.pbxproj:
* dom/Document.cpp:
(WebCore::Document::nodeFromPoint): Moved to TreeScope.
(WebCore::Document::elementFromPoint): Moved to TreeScope.
* dom/Document.h:
* dom/Document.idl: Moved elementFromPoint and activeElement to DocumentOrShadowRoot.idl.
* dom/DocumentOrShadowRoot.idl: Added.
* dom/EventPath.cpp:
(WebCore::RelatedNodeRetargeter::checkConsistency): Use newly added TreeScope::retargetToScope.
* dom/ShadowRoot.idl: Moved activeElement to DocumentOrShadowRoot.idl.
* dom/TreeScope.cpp:
(WebCore::TreeScope::retargetToScope): Added. Implements https://dom.spec.whatwg.org/#retarget efficiently.
Instead of checking whether A (node) is a shadow-including inclusive ancestor of B (this scope) at each
parent, find the lowest ancestor which contains both A and B, and return the self-inclusive ancestor of B
in that tree. To find the lowest common ancestor in O(n), traverse all ancestors of A and B separately and
do a top-down traversal. The last tree scope in which A's ancestor and B's ancestor match is the lowest
common ancestor.
(WebCore::TreeScope::nodeFromPoint): Moved from Document.
(WebCore::TreeScope::elementFromPoint): Moved from Document. Use retargetToScope and parentInComposedTree
instead of parentNode and ancestorInThisScope to match the semantics proposed in
https://github.com/w3c/csswg-drafts/issues/556
* dom/TreeScope.h:

LayoutTests:

Add a W3C style testharness.js test for elementFromPoint on ShadowRoot.

* fast/shadow-dom/DocumentOrShadowRoot-prototype-elementFromPoint-expected.txt: Added.
* fast/shadow-dom/DocumentOrShadowRoot-prototype-elementFromPoint.html: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreCMakeListstxt">trunk/Source/WebCore/CMakeLists.txt</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreDerivedSourcesmake">trunk/Source/WebCore/DerivedSources.make</a></li>
<li><a href="#trunkSourceWebCoreWebCorexcodeprojprojectpbxproj">trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj</a></li>
<li><a href="#trunkSourceWebCoredomDocumentcpp">trunk/Source/WebCore/dom/Document.cpp</a></li>
<li><a href="#trunkSourceWebCoredomDocumenth">trunk/Source/WebCore/dom/Document.h</a></li>
<li><a href="#trunkSourceWebCoredomDocumentidl">trunk/Source/WebCore/dom/Document.idl</a></li>
<li><a href="#trunkSourceWebCoredomEventPathcpp">trunk/Source/WebCore/dom/EventPath.cpp</a></li>
<li><a href="#trunkSourceWebCoredomShadowRootidl">trunk/Source/WebCore/dom/ShadowRoot.idl</a></li>
<li><a href="#trunkSourceWebCoredomTreeScopecpp">trunk/Source/WebCore/dom/TreeScope.cpp</a></li>
<li><a href="#trunkSourceWebCoredomTreeScopeh">trunk/Source/WebCore/dom/TreeScope.h</a></li>
<li><a href="#trunkSourceWebKit2WebProcessWebPageWebPagecpp">trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsfastshadowdomDocumentOrShadowRootprototypeelementFromPointexpectedtxt">trunk/LayoutTests/fast/shadow-dom/DocumentOrShadowRoot-prototype-elementFromPoint-expected.txt</a></li>
<li><a href="#trunkLayoutTestsfastshadowdomDocumentOrShadowRootprototypeelementFromPointhtml">trunk/LayoutTests/fast/shadow-dom/DocumentOrShadowRoot-prototype-elementFromPoint.html</a></li>
<li><a href="#trunkSourceWebCoredomDocumentOrShadowRootidl">trunk/Source/WebCore/dom/DocumentOrShadowRoot.idl</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (206794 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2016-10-04 23:25:33 UTC (rev 206794)
+++ trunk/LayoutTests/ChangeLog        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -1,3 +1,15 @@
</span><ins>+2016-10-03  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
+
+        ShadowRoot interface should have elementFromPoint
+        https://bugs.webkit.org/show_bug.cgi?id=162882
+
+        Reviewed by Chris Dumez.
+
+        Add a W3C style testharness.js test for elementFromPoint on ShadowRoot.
+
+        * fast/shadow-dom/DocumentOrShadowRoot-prototype-elementFromPoint-expected.txt: Added.
+        * fast/shadow-dom/DocumentOrShadowRoot-prototype-elementFromPoint.html: Added.
+
</ins><span class="cx"> 2016-10-04  Myles C. Maxfield  &lt;mmaxfield@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         font-family: cursive should map to KaiTi in Chinese
</span></span></pre></div>
<a id="trunkLayoutTestsfastshadowdomDocumentOrShadowRootprototypeelementFromPointexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/shadow-dom/DocumentOrShadowRoot-prototype-elementFromPoint-expected.txt (0 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/shadow-dom/DocumentOrShadowRoot-prototype-elementFromPoint-expected.txt                                (rev 0)
+++ trunk/LayoutTests/fast/shadow-dom/DocumentOrShadowRoot-prototype-elementFromPoint-expected.txt        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -0,0 +1,35 @@
</span><ins>+
+PASS document.elementFromPoint must return the shadow host of the hit-tested text node when the host has display: inline 
+PASS document.elementFromPoint must return the shadow host of the hit-tested text node when the host has display: block 
+PASS document.elementFromPoint must return the shadow host of the hit-tested text node when the host has display: inline-block 
+PASS document.elementFromPoint must return the shadow host of the hit-tested text node assigned to a slot when the host has display: inline 
+PASS document.elementFromPoint must return the shadow host of the hit-tested text node assigned to a slot when the host has display: block 
+PASS document.elementFromPoint must return the shadow host of the hit-tested text node assigned to a slot when the host has display: inline-block 
+PASS document.elementFromPoint must return the element assigned to a slot when the shadow host of the slot has display: inline 
+PASS document.elementFromPoint must return the element assigned to a slot when the shadow host of the slot has display: block 
+PASS document.elementFromPoint must return the element assigned to a slot when the shadow host of the slot has display: inline-block 
+PASS shadowRoot.elementFromPoint must return the element parent of the hit-tested text node under the point when the shadow host has display: inline 
+PASS shadowRoot.elementFromPoint must return the element parent of the hit-tested text node under the point when the shadow host has display: block 
+PASS shadowRoot.elementFromPoint must return the element parent of the hit-tested text node under the point when the shadow host has display: inline-block 
+PASS shadowRoot.elementFromPoint must return the shadow host when the hit-tested text node is a direct child of the root and the host has display: inline 
+PASS shadowRoot.elementFromPoint must return the shadow host when the hit-tested text node is a direct child of the root and the host has display: block 
+PASS shadowRoot.elementFromPoint must return the shadow host when the hit-tested text node is a direct child of the root and the host has display: inline-block 
+PASS shadowRoot.elementFromPoint must return the slot to which the hit-tested text node is assigned when its host has display: inline 
+PASS shadowRoot.elementFromPoint must return the slot to which the hit-tested text node is assigned when its host has display: block 
+PASS shadowRoot.elementFromPoint must return the slot to which the hit-tested text node is assigned when its host has display: inline-block 
+PASS shadowRoot.elementFromPoint must return the element parent of the hit-tested text node assigned to a slot in the shadow tree when its host has display: inline 
+PASS shadowRoot.elementFromPoint must return the element parent of the hit-tested text node assigned to a slot in the shadow tree when its host has display: block 
+PASS shadowRoot.elementFromPoint must return the element parent of the hit-tested text node assigned to a slot in the shadow tree when its host has display: inline-block 
+PASS shadowRoot.elementFromPoint must return a child element assigned to a slot when the hit-tested text node is assigned to the shadow tree of the child element and the outer shadow host has display: inline 
+PASS shadowRoot.elementFromPoint must return a child element assigned to a slot when the hit-tested text node is assigned to the shadow tree of the child element and the outer shadow host has display: block 
+PASS shadowRoot.elementFromPoint must return a child element assigned to a slot when the hit-tested text node is assigned to the shadow tree of the child element and the outer shadow host has display: inline-block 
+PASS shadowRoot.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: inline 
+PASS shadowRoot.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: block 
+PASS shadowRoot.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: inline-block 
+PASS shadowRoot.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: inline 
+PASS shadowRoot.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: block 
+PASS shadowRoot.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: inline-block 
+PASS 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 the outer shadow host has display: inline 
+PASS 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 the outer shadow host has display: block 
+PASS 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 the outer shadow host has display: inline-block 
+
</ins></span></pre></div>
<a id="trunkLayoutTestsfastshadowdomDocumentOrShadowRootprototypeelementFromPointhtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/fast/shadow-dom/DocumentOrShadowRoot-prototype-elementFromPoint.html (0 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/shadow-dom/DocumentOrShadowRoot-prototype-elementFromPoint.html                                (rev 0)
+++ trunk/LayoutTests/fast/shadow-dom/DocumentOrShadowRoot-prototype-elementFromPoint.html        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -0,0 +1,204 @@
</span><ins>+&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;title&gt;Shadow DOM and CSSOM View: Document.prototype.elementFromPoint&lt;/title&gt;
+&lt;meta name=&quot;author&quot; title=&quot;Ryosuke Niwa&quot; href=&quot;mailto:rniwa@webkit.org&quot;&gt;
+&lt;meta name=&quot;assert&quot; content=&quot;DocumentOrShadowRoot must have elementFromPoint and must return retarget the result against the context object.&quot;&gt;
+&lt;link rel=&quot;help&quot; href=&quot;https://www.w3.org/TR/cssom-view-1/#dom-document-elementfrompoint&quot;&gt;
+&lt;link rel=&quot;help&quot; href=&quot;https://www.w3.org/TR/shadow-dom/#extensions-to-the-documentorshadowroot-mixin&quot;&gt;
+&lt;script src=&quot;../../resources/testharness.js&quot;&gt;&lt;/script&gt;
+&lt;script src=&quot;../../resources/testharnessreport.js&quot;&gt;&lt;/script&gt;
+&lt;/head&gt;
+&lt;body&gt;
+&lt;div id=&quot;container&quot;&gt;&lt;/div&gt;
+&lt;style&gt;
+
+container { position: relative; }
+test-element { display: block; width: 100px; height: 100px; }
+
+&lt;/style&gt;
+&lt;script&gt;
+
+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');
+
+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);
+    }, 'document.elementFromPoint must return the shadow host of the hit-tested text node when 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 = '&lt;slot&gt;&lt;/slot&gt;';
+        host.innerHTML = 'text';
+        container.appendChild(host);
+        assert_equals(document.elementFromPoint(...pointInElement(host)), host);
+    }, 'document.elementFromPoint must return the shadow host of the hit-tested text node assigned to a slot when 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 = '&lt;slot&gt;&lt;/slot&gt;';
+        host.innerHTML = '&lt;span&gt;text&lt;/span&gt;';
+        container.appendChild(host);
+        assert_equals(document.elementFromPoint(...pointInElement(host)), host.querySelector('span'));
+    }, 'document.elementFromPoint must return the element assigned to a slot when 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 = '&lt;span&gt;text&lt;/span&gt;';
+        container.appendChild(host);
+        assert_equals(shadow.elementFromPoint(...pointInElement(shadow.querySelector('span'))), shadow.querySelector('span'));
+    }, '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 = 'text';
+        container.appendChild(host);
+        assert_equals(shadow.elementFromPoint(...pointInElement(host)), host);
+    }, 'shadowRoot.elementFromPoint must return the shadow host 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 = '&lt;slot&gt;&lt;/slot&gt;';
+        host.innerHTML = 'hello';
+        container.appendChild(host);
+        assert_equals(shadow.elementFromPoint(...pointInElement(host)), shadow.querySelector('slot'));
+    }, 'shadowRoot.elementFromPoint must return the slot to which the hit-tested text node is assigned when its 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 = '&lt;slot&gt;&lt;/slot&gt;';
+        host.innerHTML = '&lt;span&gt;hello&lt;/span&gt;';
+        container.appendChild(host);
+        assert_equals(shadow.elementFromPoint(...pointInElement(host)), host.querySelector('span'));
+    }, 'shadowRoot.elementFromPoint must return the element parent of the hit-tested text node assigned to a slot in the shadow tree when its 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 = '&lt;slot&gt;&lt;/slot&gt;';
+        host.innerHTML = '&lt;inner-host&gt;hello&lt;/inner-host&gt;';
+        container.appendChild(host);
+
+        let innerHost = host.querySelector('inner-host');
+        let innerShadow = innerHost.attachShadow({mode: 'closed'});
+        innerShadow.innerHTML = '&lt;slot&gt;&lt;/slot&gt;';
+
+        assert_equals(shadow.elementFromPoint(...pointInElement(host)), innerHost);
+    }, 'shadowRoot.elementFromPoint must return a child element assigned to a slot'
+        + ' when the hit-tested text node is assigned to 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 = '&lt;slot&gt;&lt;/slot&gt;';
+        host.innerHTML = '&lt;inner-host&gt;hello&lt;/inner-host&gt;';
+        container.appendChild(host);
+
+        let innerHost = host.querySelector('inner-host');
+        let innerShadow = innerHost.attachShadow({mode: 'closed'});
+        innerShadow.innerHTML = '&lt;slot&gt;&lt;/slot&gt;';
+
+        assert_equals(shadow.elementFromPoint(...pointInElement(host)), innerHost);
+    }, 'shadowRoot.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 = '&lt;slot&gt;&lt;/slot&gt;';
+        host.innerHTML = '&lt;inner-host&gt;&lt;/inner-host&gt;';
+        container.appendChild(host);
+
+        let innerHost = host.querySelector('inner-host');
+        let innerShadow = innerHost.attachShadow({mode: 'closed'});
+        innerShadow.innerHTML = 'hello';
+
+        assert_equals(shadow.elementFromPoint(...pointInElement(host)), innerHost);
+    }, 'shadowRoot.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 = '&lt;slot&gt;&lt;/slot&gt;';
+        host.innerHTML = '&lt;inner-host&gt;&lt;/inner-host&gt;';
+        container.appendChild(host);
+
+        let innerHost = host.querySelector('inner-host');
+        let innerShadow = innerHost.attachShadow({mode: 'closed'});
+        innerShadow.innerHTML = '&lt;span&gt;hello&lt;/span&gt;';
+
+        assert_equals(shadow.elementFromPoint(...pointInElement(host)), innerHost);
+    }, '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 the outer shadow host has display: ' + displayValue);
+});
+
+container.innerHTML = '';
+
+&lt;/script&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkSourceWebCoreCMakeListstxt"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/CMakeLists.txt (206794 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/CMakeLists.txt        2016-10-04 23:25:33 UTC (rev 206794)
+++ trunk/Source/WebCore/CMakeLists.txt        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -391,6 +391,7 @@
</span><span class="cx">     dom/DeviceOrientationEvent.idl
</span><span class="cx">     dom/Document.idl
</span><span class="cx">     dom/DocumentFragment.idl
</span><ins>+    dom/DocumentOrShadowRoot.idl
</ins><span class="cx">     dom/DocumentType.idl
</span><span class="cx">     dom/Element.idl
</span><span class="cx">     dom/ErrorEvent.idl
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (206794 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2016-10-04 23:25:33 UTC (rev 206794)
+++ trunk/Source/WebCore/ChangeLog        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -1,3 +1,46 @@
</span><ins>+2016-10-03  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
+
+        ShadowRoot interface should have elementFromPoint
+        https://bugs.webkit.org/show_bug.cgi?id=162882
+
+        Reviewed by Chris Dumez.
+
+        Add elementFromPoint to ShadowRoot's prototype as specified at:
+        https://www.w3.org/TR/shadow-dom/#extensions-to-the-documentorshadowroot-mixin
+        with changes proposed at https://github.com/w3c/csswg-drafts/issues/556
+
+        Added TreeScope::retargetToScope which implements 
+
+        This patch also factors DocumentOrShadowRoot.idl out of Document and ShadowRoot interfaces to better match
+        the latest DOM specification: https://dom.spec.whatwg.org/#mixin-documentorshadowroot
+
+        Test: fast/shadow-dom/Document-prototype-elementFromPoint.html
+
+        * CMakeLists.txt:
+        * DerivedSources.make:
+        * WebCore.xcodeproj/project.pbxproj:
+        * dom/Document.cpp:
+        (WebCore::Document::nodeFromPoint): Moved to TreeScope.
+        (WebCore::Document::elementFromPoint): Moved to TreeScope.
+        * dom/Document.h:
+        * dom/Document.idl: Moved elementFromPoint and activeElement to DocumentOrShadowRoot.idl.
+        * dom/DocumentOrShadowRoot.idl: Added.
+        * dom/EventPath.cpp:
+        (WebCore::RelatedNodeRetargeter::checkConsistency): Use newly added TreeScope::retargetToScope.
+        * dom/ShadowRoot.idl: Moved activeElement to DocumentOrShadowRoot.idl.
+        * dom/TreeScope.cpp:
+        (WebCore::TreeScope::retargetToScope): Added. Implements https://dom.spec.whatwg.org/#retarget efficiently.
+        Instead of checking whether A (node) is a shadow-including inclusive ancestor of B (this scope) at each
+        parent, find the lowest ancestor which contains both A and B, and return the self-inclusive ancestor of B
+        in that tree. To find the lowest common ancestor in O(n), traverse all ancestors of A and B separately and
+        do a top-down traversal. The last tree scope in which A's ancestor and B's ancestor match is the lowest
+        common ancestor.
+        (WebCore::TreeScope::nodeFromPoint): Moved from Document.
+        (WebCore::TreeScope::elementFromPoint): Moved from Document. Use retargetToScope and parentInComposedTree
+        instead of parentNode and ancestorInThisScope to match the semantics proposed in
+        https://github.com/w3c/csswg-drafts/issues/556
+        * dom/TreeScope.h:
+
</ins><span class="cx"> 2016-10-04  Myles C. Maxfield  &lt;mmaxfield@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         font-family: cursive should map to KaiTi in Chinese
</span></span></pre></div>
<a id="trunkSourceWebCoreDerivedSourcesmake"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/DerivedSources.make (206794 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/DerivedSources.make        2016-10-04 23:25:33 UTC (rev 206794)
+++ trunk/Source/WebCore/DerivedSources.make        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -301,6 +301,7 @@
</span><span class="cx">     $(WebCore)/dom/DeviceOrientationEvent.idl \
</span><span class="cx">     $(WebCore)/dom/Document.idl \
</span><span class="cx">     $(WebCore)/dom/DocumentFragment.idl \
</span><ins>+    $(WebCore)/dom/DocumentOrShadowRoot.idl \
</ins><span class="cx">     $(WebCore)/dom/DocumentType.idl \
</span><span class="cx">     $(WebCore)/dom/Element.idl \
</span><span class="cx">     $(WebCore)/dom/ErrorEvent.idl \
</span></span></pre></div>
<a id="trunkSourceWebCoreWebCorexcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (206794 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj        2016-10-04 23:25:33 UTC (rev 206794)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -10877,6 +10877,7 @@
</span><span class="cx">                 9B03D8061BB3110D00B764D9 /* ReadableStreamInternalsBuiltins.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReadableStreamInternalsBuiltins.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 9B03D8061BB3110D00B764E8 /* WritableStreamBuiltins.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WritableStreamBuiltins.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 9B03D8061BB3110D00B764E9 /* WritableStreamInternalsBuiltins.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WritableStreamInternalsBuiltins.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><ins>+                9B0FE8731D9E02DF004A8ACB /* DocumentOrShadowRoot.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = DocumentOrShadowRoot.idl; sourceTree = &quot;&lt;group&gt;&quot;; };
</ins><span class="cx">                 9B19B67E1B964E5200348745 /* ShadowRoot.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = ShadowRoot.idl; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 9B1AB0791648C69D0051F3F2 /* HTMLFormControlsCollection.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = HTMLFormControlsCollection.idl; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 9B1AB07B1648C7C40051F3F2 /* JSHTMLFormControlsCollectionCustom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSHTMLFormControlsCollectionCustom.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="lines">@@ -23014,6 +23015,7 @@
</span><span class="cx">                                 ED2BA83B09A24B91006C0AC4 /* DocumentMarker.h */,
</span><span class="cx">                                 CE057FA31220731100A476D5 /* DocumentMarkerController.cpp */,
</span><span class="cx">                                 CE057FA41220731100A476D5 /* DocumentMarkerController.h */,
</span><ins>+                                9B0FE8731D9E02DF004A8ACB /* DocumentOrShadowRoot.idl */,
</ins><span class="cx">                                 14947FFB12F80CD200A0F631 /* DocumentOrderedMap.cpp */,
</span><span class="cx">                                 14947FFC12F80CD200A0F631 /* DocumentOrderedMap.h */,
</span><span class="cx">                                 A8C2280D11D4A59700D5A7D3 /* DocumentParser.cpp */,
</span></span></pre></div>
<a id="trunkSourceWebCoredomDocumentcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Document.cpp (206794 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Document.cpp        2016-10-04 23:25:33 UTC (rev 206794)
+++ trunk/Source/WebCore/dom/Document.cpp        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -1425,52 +1425,6 @@
</span><span class="cx">     return ASCIILiteral(&quot;application/xml&quot;);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-Node* Document::nodeFromPoint(const LayoutPoint&amp; clientPoint, LayoutPoint* localPoint)
-{
-    if (!frame() || !view())
-        return nullptr;
-    
-    Frame&amp; frame = *this-&gt;frame();
-    
-    float scaleFactor = frame.pageZoomFactor() * frame.frameScaleFactor();
-
-    LayoutPoint contentsPoint = clientPoint;
-    contentsPoint.scale(scaleFactor, scaleFactor);
-    contentsPoint.moveBy(view()-&gt;contentsScrollPosition());
-
-    LayoutRect visibleRect;
-#if PLATFORM(IOS)
-    visibleRect = view()-&gt;unobscuredContentRect();
-#else
-    visibleRect = view()-&gt;visibleContentRect();
-#endif
-    if (!visibleRect.contains(contentsPoint))
-        return nullptr;
-
-    HitTestResult result(contentsPoint);
-    renderView()-&gt;hitTest(HitTestRequest(), result);
-
-    if (localPoint)
-        *localPoint = result.localPoint();
-
-    return result.innerNode();
-}
-
-Element* Document::elementFromPoint(const LayoutPoint&amp; clientPoint)
-{
-    if (!hasLivingRenderTree())
-        return nullptr;
-
-    Node* node = nodeFromPoint(clientPoint);
-    while (node &amp;&amp; !is&lt;Element&gt;(*node))
-        node = node-&gt;parentNode();
-
-    if (node)
-        node = ancestorInThisScope(node);
-
-    return downcast&lt;Element&gt;(node);
-}
-
</del><span class="cx"> RefPtr&lt;Range&gt; Document::caretRangeFromPoint(int x, int y)
</span><span class="cx"> {
</span><span class="cx">     return caretRangeFromPoint(LayoutPoint(x, y));
</span></span></pre></div>
<a id="trunkSourceWebCoredomDocumenth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Document.h (206794 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Document.h        2016-10-04 23:25:33 UTC (rev 206794)
+++ trunk/Source/WebCore/dom/Document.h        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -397,9 +397,6 @@
</span><span class="cx"> 
</span><span class="cx">     NamedFlowCollection&amp; namedFlows();
</span><span class="cx"> 
</span><del>-    Element* elementFromPoint(int x, int y) { return elementFromPoint(LayoutPoint(x, y)); }
-    WEBCORE_EXPORT Element* elementFromPoint(const LayoutPoint&amp; clientPoint);
-
</del><span class="cx">     WEBCORE_EXPORT RefPtr&lt;Range&gt; caretRangeFromPoint(int x, int y);
</span><span class="cx">     RefPtr&lt;Range&gt; caretRangeFromPoint(const LayoutPoint&amp; clientPoint);
</span><span class="cx"> 
</span><span class="lines">@@ -1363,8 +1360,6 @@
</span><span class="cx"> 
</span><span class="cx">     PageVisibilityState pageVisibilityState() const;
</span><span class="cx"> 
</span><del>-    Node* nodeFromPoint(const LayoutPoint&amp; clientPoint, LayoutPoint* localPoint = nullptr);
-
</del><span class="cx">     template &lt;CollectionType collectionType&gt;
</span><span class="cx">     Ref&lt;HTMLCollection&gt; ensureCachedCollection();
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoredomDocumentidl"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Document.idl (206794 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Document.idl        2016-10-04 23:25:33 UTC (rev 206794)
+++ trunk/Source/WebCore/dom/Document.idl        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -126,7 +126,6 @@
</span><span class="cx">     [ImplementedAs=characterSetWithUTF8Fallback] readonly attribute DOMString charset;
</span><span class="cx">     readonly attribute DOMString readyState;
</span><span class="cx"> 
</span><del>-    Element elementFromPoint(optional long x = 0, optional long y = 0);
</del><span class="cx">     Range caretRangeFromPoint(optional long x = 0, optional long y = 0);
</span><span class="cx"> 
</span><span class="cx">     // Mozilla extensions
</span><span class="lines">@@ -141,7 +140,6 @@
</span><span class="cx"> 
</span><span class="cx">     HTMLCollection getElementsByClassName(DOMString classNames);
</span><span class="cx"> 
</span><del>-    readonly attribute Element? activeElement;
</del><span class="cx">     boolean hasFocus();
</span><span class="cx"> 
</span><span class="cx">     readonly attribute DOMString compatMode;
</span><span class="lines">@@ -214,4 +212,5 @@
</span><span class="cx"> 
</span><span class="cx"> Document implements ParentNode;
</span><span class="cx"> Document implements NonElementParentNode;
</span><ins>+Document implements DocumentOrShadowRoot;
</ins><span class="cx"> Document implements GlobalEventHandlers;
</span></span></pre></div>
<a id="trunkSourceWebCoredomDocumentOrShadowRootidl"></a>
<div class="addfile"><h4>Added: trunk/Source/WebCore/dom/DocumentOrShadowRoot.idl (0 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/DocumentOrShadowRoot.idl                                (rev 0)
+++ trunk/Source/WebCore/dom/DocumentOrShadowRoot.idl        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -0,0 +1,34 @@
</span><ins>+/*
+ * Copyright (C) 2016 Apple Inc. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// https://dom.spec.whatwg.org/#documentorshadowroot
+// https://www.w3.org/TR/shadow-dom/#extensions-to-the-documentorshadowroot-mixin
+[
+    NoInterfaceObject,
+] interface DocumentOrShadowRoot {
+    Element?                    elementFromPoint(optional long x = 0, optional long y = 0); // FIXME: x and y should be double and not be optional.
+    readonly attribute Element? activeElement;
+};
</ins></span></pre></div>
<a id="trunkSourceWebCoredomEventPathcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/EventPath.cpp (206794 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/EventPath.cpp        2016-10-04 23:25:33 UTC (rev 206794)
+++ trunk/Source/WebCore/dom/EventPath.cpp        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -360,17 +360,10 @@
</span><span class="cx"> #if !ASSERT_DISABLED
</span><span class="cx"> void RelatedNodeRetargeter::checkConsistency(Node&amp; currentTarget)
</span><span class="cx"> {
</span><del>-    ASSERT(!m_retargetedRelatedNode || currentTarget.isUnclosedNode(*m_retargetedRelatedNode));
-
-    // http://w3c.github.io/webcomponents/spec/shadow/#dfn-retargeting-algorithm
-    Node&amp; base = currentTarget;
-    for (Node* targetAncestor = &amp;m_relatedNode; targetAncestor; targetAncestor = targetAncestor-&gt;parentOrShadowHostNode()) {
-        if (targetAncestor-&gt;rootNode().containsIncludingShadowDOM(&amp;base)) {
-            ASSERT(m_retargetedRelatedNode == targetAncestor);
-            return;
-        }
-    }
-    ASSERT(!m_retargetedRelatedNode || m_hasDifferentTreeRoot);
</del><ins>+    if (!m_retargetedRelatedNode)
+        return;
+    ASSERT(currentTarget.isUnclosedNode(*m_retargetedRelatedNode));
+    ASSERT(m_retargetedRelatedNode == &amp;currentTarget.treeScope().retargetToScope(m_relatedNode));
</ins><span class="cx"> }
</span><span class="cx"> #endif
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoredomShadowRootidl"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/ShadowRoot.idl (206794 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/ShadowRoot.idl        2016-10-04 23:25:33 UTC (rev 206794)
+++ trunk/Source/WebCore/dom/ShadowRoot.idl        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -28,7 +28,6 @@
</span><span class="cx">     JSGenerateToJSObject
</span><span class="cx"> ] interface ShadowRoot : DocumentFragment {
</span><span class="cx">     readonly attribute ShadowRootMode   mode;
</span><del>-    readonly attribute Element          activeElement;
</del><span class="cx">     readonly attribute Element          host;
</span><span class="cx"> 
</span><span class="cx">     [TreatNullAs=EmptyString, SetterMayThrowLegacyException] attribute DOMString innerHTML;
</span><span class="lines">@@ -36,3 +35,5 @@
</span><span class="cx"> 
</span><span class="cx"> // &quot;user-agent&quot; is a WebKit extension that is not exposed to the Web.
</span><span class="cx"> enum ShadowRootMode { &quot;user-agent&quot;, &quot;closed&quot;, &quot;open&quot; };
</span><ins>+
+ShadowRoot implements DocumentOrShadowRoot;
</ins></span></pre></div>
<a id="trunkSourceWebCoredomTreeScopecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/TreeScope.cpp (206794 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/TreeScope.cpp        2016-10-04 23:25:33 UTC (rev 206794)
+++ trunk/Source/WebCore/dom/TreeScope.cpp        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -32,6 +32,7 @@
</span><span class="cx"> #include &quot;ElementIterator.h&quot;
</span><span class="cx"> #include &quot;FocusController.h&quot;
</span><span class="cx"> #include &quot;Frame.h&quot;
</span><ins>+#include &quot;FrameView.h&quot;
</ins><span class="cx"> #include &quot;HTMLAnchorElement.h&quot;
</span><span class="cx"> #include &quot;HTMLFrameOwnerElement.h&quot;
</span><span class="cx"> #include &quot;HTMLLabelElement.h&quot;
</span><span class="lines">@@ -39,6 +40,7 @@
</span><span class="cx"> #include &quot;HitTestResult.h&quot;
</span><span class="cx"> #include &quot;IdTargetObserverRegistry.h&quot;
</span><span class="cx"> #include &quot;Page.h&quot;
</span><ins>+#include &quot;RenderView.h&quot;
</ins><span class="cx"> #include &quot;RuntimeEnabledFeatures.h&quot;
</span><span class="cx"> #include &quot;ShadowRoot.h&quot;
</span><span class="cx"> #include &quot;TreeScopeAdopter.h&quot;
</span><span class="lines">@@ -168,6 +170,38 @@
</span><span class="cx">     m_elementsByName-&gt;remove(name, element);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+
+Node&amp; TreeScope::retargetToScope(Node&amp; node) const
+{
+    auto&amp; scope = node.treeScope();
+    if (LIKELY(this == &amp;scope || !node.isInShadowTree()))
+        return node;
+    ASSERT(is&lt;ShadowRoot&gt;(scope.rootNode()));
+
+    Vector&lt;TreeScope*, 8&gt; nodeTreeScopes;
+    for (auto* currentScope = &amp;scope; currentScope; currentScope = currentScope-&gt;parentTreeScope())
+        nodeTreeScopes.append(currentScope);
+    ASSERT(nodeTreeScopes.size() &gt;= 2);
+
+    Vector&lt;const TreeScope*, 8&gt; ancestorScopes;
+    for (auto* currentScope = this; currentScope; currentScope = currentScope-&gt;parentTreeScope())
+        ancestorScopes.append(currentScope);
+
+    size_t i = nodeTreeScopes.size();
+    size_t j = ancestorScopes.size();
+    while (i &gt; 0 &amp;&amp; j &gt; 0 &amp;&amp; nodeTreeScopes[i - 1] == ancestorScopes[j - 1]) {
+        --i;
+        --j;
+    }
+
+    bool nodeIsInOuterTreeScope = !i;
+    if (nodeIsInOuterTreeScope)
+        return node;
+
+    ShadowRoot&amp; shadowRootInLowestCommonTreeScope = downcast&lt;ShadowRoot&gt;(nodeTreeScopes[i - 1]-&gt;rootNode());
+    return *shadowRootInLowestCommonTreeScope.host();
+}
+
</ins><span class="cx"> Node* TreeScope::ancestorInThisScope(Node* node) const
</span><span class="cx"> {
</span><span class="cx">     for (; node; node = node-&gt;shadowHost()) {
</span><span class="lines">@@ -245,6 +279,58 @@
</span><span class="cx">     return m_labelsByForAttribute-&gt;getElementByLabelForAttribute(*forAttributeValue.impl(), *this);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+Node* TreeScope::nodeFromPoint(const LayoutPoint&amp; clientPoint, LayoutPoint* localPoint)
+{
+    auto* frame = documentScope().frame();
+    auto* view = documentScope().view();
+    if (!frame || !view)
+        return nullptr;
+
+    float scaleFactor = frame-&gt;pageZoomFactor() * frame-&gt;frameScaleFactor();
+
+    LayoutPoint contentsPoint = clientPoint;
+    contentsPoint.scale(scaleFactor, scaleFactor);
+    contentsPoint.moveBy(view-&gt;contentsScrollPosition());
+
+    LayoutRect visibleRect;
+#if PLATFORM(IOS)
+    visibleRect = view-&gt;unobscuredContentRect();
+#else
+    visibleRect = view-&gt;visibleContentRect();
+#endif
+    if (!visibleRect.contains(contentsPoint))
+        return nullptr;
+
+    HitTestResult result(contentsPoint);
+    documentScope().renderView()-&gt;hitTest(HitTestRequest(), result);
+
+    if (localPoint)
+        *localPoint = result.localPoint();
+
+    return result.innerNode();
+}
+
+Element* TreeScope::elementFromPoint(int x, int y)
+{
+    Document&amp; document = documentScope();
+    if (!document.hasLivingRenderTree())
+        return nullptr;
+
+    Node* node = nodeFromPoint(LayoutPoint(x, y), nullptr);
+    if (!node)
+        return nullptr;
+
+    node = &amp;retargetToScope(*node);
+    while (!is&lt;Element&gt;(*node)) {
+        node = node-&gt;parentInComposedTree();
+        if (!node)
+            break;
+        node = &amp;retargetToScope(*node);
+    }
+
+    return downcast&lt;Element&gt;(node);
+}
+
</ins><span class="cx"> DOMSelection* TreeScope::getSelection() const
</span><span class="cx"> {
</span><span class="cx">     if (!m_rootNode.document().frame())
</span></span></pre></div>
<a id="trunkSourceWebCoredomTreeScopeh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/TreeScope.h (206794 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/TreeScope.h        2016-10-04 23:25:33 UTC (rev 206794)
+++ trunk/Source/WebCore/dom/TreeScope.h        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -71,6 +71,9 @@
</span><span class="cx">     Document&amp; documentScope() const { return *m_documentScope; }
</span><span class="cx">     static ptrdiff_t documentScopeMemoryOffset() { return OBJECT_OFFSETOF(TreeScope, m_documentScope); }
</span><span class="cx"> 
</span><ins>+    // https://dom.spec.whatwg.org/#retarget
+    Node&amp; retargetToScope(Node&amp;) const;
+
</ins><span class="cx">     Node* ancestorInThisScope(Node*) const;
</span><span class="cx"> 
</span><span class="cx">     void addImageMap(HTMLMapElement&amp;);
</span><span class="lines">@@ -83,6 +86,8 @@
</span><span class="cx">     void removeLabel(const AtomicStringImpl&amp; forAttributeValue, HTMLLabelElement&amp;);
</span><span class="cx">     HTMLLabelElement* labelElementForId(const AtomicString&amp; forAttributeValue);
</span><span class="cx"> 
</span><ins>+    WEBCORE_EXPORT Element* elementFromPoint(int x, int y);
+
</ins><span class="cx">     DOMSelection* getSelection() const;
</span><span class="cx"> 
</span><span class="cx">     // Find first anchor with the given name.
</span><span class="lines">@@ -111,6 +116,8 @@
</span><span class="cx">         m_documentScope = document;
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    Node* nodeFromPoint(const LayoutPoint&amp; clientPoint, LayoutPoint* localPoint);
+
</ins><span class="cx"> private:
</span><span class="cx">     ContainerNode&amp; m_rootNode;
</span><span class="cx">     Document* m_documentScope;
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessWebPageWebPagecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp (206794 => 206795)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp        2016-10-04 23:25:33 UTC (rev 206794)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp        2016-10-04 23:42:18 UTC (rev 206795)
</span><span class="lines">@@ -3213,7 +3213,7 @@
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(CUSTOM_ELEMENTS)
</span><del>-    RuntimeEnabledFeatures::sharedFeatures().setCustomElementsEnabled(store.getBoolValueForKey(WebPreferencesKey::customElementsEnabledKey()));
</del><ins>+    RuntimeEnabledFeatures::sharedFeatures().setCustomElementsEnabled(true || store.getBoolValueForKey(WebPreferencesKey::customElementsEnabledKey()));
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(WEBGL2)
</span></span></pre>
</div>
</div>

</body>
</html>