Clicks inside button elements are sometimes discarded when the mouse moves
https://bugs.webkit.org/show_bug.cgi?id=39620

Reviewed by Darin Adler.

Source/WebCore:

Test: fast/events/click-over-descendant-elements.html

* dom/Node.cpp:
(WebCore::ancestor):
(WebCore::commonAncestor): Method inspired from
http://src.chromium.org/viewvc/blink?view=revision&revision=162081.
(WebCore::commonAncestorCrossingShadowBoundary): Helper routine
that handles the case of nodes into a shadow node.
* dom/Node.h:
* page/EventHandler.cpp:
(WebCore::EventHandler::handleMouseReleaseEvent): Selecting click event
target node according commonAncestorOverShadowBoundary method.
(WebCore::EventHandler::targetNodeForClickEvent): Deleted.

LayoutTests:

Test coming from http://src.chromium.org/viewvc/blink?view=revision&revision=162081.
Modified to ensure click events do not end up being considered as double click events.

* fast/events/click-over-descendant-elements-expected.txt: Added.
* fast/events/click-over-descendant-elements.html: Added.
* platform/ios-simulator/TestExpectations: Marked new test as failing.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@200414 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index 741e8ee..6df8f7b 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,17 @@
+2016-05-04  Youenn Fablet  <youenn.fablet@crf.canon.fr>
+
+        Clicks inside button elements are sometimes discarded when the mouse moves
+        https://bugs.webkit.org/show_bug.cgi?id=39620
+
+        Reviewed by Darin Adler.
+
+        Test coming from http://src.chromium.org/viewvc/blink?view=revision&revision=162081.
+        Modified to ensure click events do not end up being considered as double click events.
+
+        * fast/events/click-over-descendant-elements-expected.txt: Added.
+        * fast/events/click-over-descendant-elements.html: Added.
+        * platform/ios-simulator/TestExpectations: Marked new test as failing.
+
 2016-05-03  Filip Pizlo  <fpizlo@apple.com>
 
         REGRESSION(r200383): Setting lazily initialized properties across frame boundaries crashes
diff --git a/LayoutTests/fast/events/click-over-descendant-elements-expected.txt b/LayoutTests/fast/events/click-over-descendant-elements-expected.txt
new file mode 100644
index 0000000..5c2cfa9
--- /dev/null
+++ b/LayoutTests/fast/events/click-over-descendant-elements-expected.txt
@@ -0,0 +1,27 @@
+Check if any mousedown-mouseup pairs in one element dispatch click events.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+Very normal click on container:
+PASS lastClickTarget is container
+Move inside container:
+PASS lastClickTarget is container
+Move from container to a child:
+PASS lastClickTarget is container
+Move from a child to container:
+PASS lastClickTarget is container
+Move from a child to another child:
+PASS lastClickTarget is container
+Move out from a child:
+PASS lastClickTarget is document.body
+Removing a child element in the light DOM during a click:
+FAIL lastClickTarget should be null. Was [object HTMLDivElement].
+Click on escaping button content:
+PASS lastClickTarget is button1
+Click on disappearing INPUT value:
+PASS lastClickTarget is input1
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/fast/events/click-over-descendant-elements.html b/LayoutTests/fast/events/click-over-descendant-elements.html
new file mode 100644
index 0000000..67a23a7
--- /dev/null
+++ b/LayoutTests/fast/events/click-over-descendant-elements.html
@@ -0,0 +1,144 @@
+<!DOCTYPE html>
+<body>
+<script src="../../resources/js-test-pre.js"></script>
+<style>
+body {
+    margin: 0;
+    padding: 0;
+    -webkit-user-select: none;
+}
+
+#container {
+    padding: 40px;
+}
+
+#inner1, #inner2 {
+    height: 40px;
+}
+
+#button1 {
+    padding-top: 4px;
+}
+#button1:active {
+    padding-top: 40px;
+}
+</style>
+<div id="container">
+<div id="inner1"></div>
+<div id="inner2"></div>
+</div>
+<button id="button1"><span id="button-span">Click me</span></button>
+<input id="input1" value="A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog.">
+<!-- Avoid to create a console element before the above elements -->
+<div id="console"></div>
+<script>
+description('Check if any mousedown-mouseup pairs in one element dispatch click events.');
+if (!window.eventSender)
+    debug('This test needs to run in a test environment.');
+
+var container = document.getElementById('container');
+var containerRect = container.getBoundingClientRect();
+var inner1 = document.getElementById('inner1');
+var inner1Rect = inner1.getBoundingClientRect();
+var inner2 = document.getElementById('inner2');
+var inner2Rect = inner2.getBoundingClientRect();
+var x = (containerRect.left + containerRect.right) / 2;
+var inContainer = (containerRect.top + inner1Rect.top) / 2;
+var inInner1 = (inner1Rect.top + inner1Rect.bottom) / 2;
+var inInner2 = (inner2Rect.top + inner2Rect.bottom) / 2;
+
+var lastClickTarget = null;
+document.addEventListener('click', function(event) {
+    lastClickTarget = event.target;
+}, false);
+
+function clearClick()
+{
+    eventSender.mouseMoveTo(0, 0);
+    eventSender.mouseDown(1);
+    eventSender.mouseUp(1);
+    lastClickTarget;
+}
+
+
+debug('Very normal click on container:');
+eventSender.mouseMoveTo(x, inContainer);
+eventSender.mouseDown();
+eventSender.mouseUp();
+shouldBe('lastClickTarget', 'container');
+clearClick();
+
+debug('Move inside container:');
+eventSender.mouseMoveTo(x, inContainer);
+eventSender.mouseDown();
+eventSender.mouseMoveTo(x + 10, inContainer);
+eventSender.mouseUp();
+shouldBe('lastClickTarget', 'container');
+clearClick();
+
+debug('Move from container to a child:');
+eventSender.mouseMoveTo(x, inContainer);
+eventSender.mouseDown();
+eventSender.mouseMoveTo(x, inInner1);
+eventSender.mouseUp();
+shouldBe('lastClickTarget', 'container');
+clearClick();
+
+debug('Move from a child to container:');
+eventSender.mouseMoveTo(x, inInner1);
+eventSender.mouseDown();
+eventSender.mouseMoveTo(x, inContainer);
+eventSender.mouseUp();
+shouldBe('lastClickTarget', 'container');
+clearClick();
+
+debug('Move from a child to another child:');
+eventSender.mouseMoveTo(x, inInner1);
+eventSender.mouseDown();
+eventSender.mouseMoveTo(x, inInner2);
+eventSender.mouseUp();
+shouldBe('lastClickTarget', 'container');
+clearClick();
+
+debug('Move out from a child:');
+eventSender.mouseMoveTo(x, inInner1);
+eventSender.mouseDown();
+eventSender.mouseMoveTo(x, containerRect.bottom + 10);
+eventSender.mouseUp();
+shouldBe('lastClickTarget', 'document.body');
+clearClick();
+
+debug('Removing a child element in the light DOM during a click:');
+eventSender.mouseMoveTo(x, inInner2);
+eventSender.mouseDown();
+inner2.remove();
+eventSender.mouseUp();
+shouldBeNull('lastClickTarget');
+clearClick();
+
+debug('Click on escaping button content:');
+var button1 = document.getElementById("button1");
+var spanRect = document.getElementById('button-span').getBoundingClientRect();
+eventSender.mouseMoveTo((spanRect.left + spanRect.right) / 2, spanRect.top + 1);
+eventSender.mouseDown();
+eventSender.mouseUp();
+shouldBe('lastClickTarget', 'button1');
+clearClick();
+
+debug('Click on disappearing INPUT value:');
+var input1 = document.getElementById('input1');
+input1.addEventListener('focus', function() { input1.value = ""; }, false);
+var inputRect = input1.getBoundingClientRect();
+eventSender.mouseMoveTo(inputRect.left + 8, (inputRect.top + inputRect.bottom) / 2);
+eventSender.mouseDown();
+eventSender.mouseUp();
+shouldBe('lastClickTarget', 'input1');
+clearClick();
+
+container.remove();
+button1.remove();
+input1.remove();
+
+</script>
+<script src="../../resources/js-test-post.js"></script>
+</body>
diff --git a/LayoutTests/platform/ios-simulator/TestExpectations b/LayoutTests/platform/ios-simulator/TestExpectations
index 3f050cb..5d3e29a 100644
--- a/LayoutTests/platform/ios-simulator/TestExpectations
+++ b/LayoutTests/platform/ios-simulator/TestExpectations
@@ -1665,6 +1665,7 @@
 fast/events/show-modal-dialog-onblur-onfocus.html [ Failure ]
 fast/events/tab-focus-link-in-canvas.html [ Failure ]
 fast/events/tabindex-focus-blur-all.html [ Failure ]
+fast/events/click-over-descendant-elements.html [ Failure ]
 
 # Ref-test imported from W3C that is failing because type=image input elements have rounded corners on iOS.
 imported/w3c/web-platform-tests/html/semantics/forms/the-input-element/image01.html [ ImageOnlyFailure ]
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index a00961a..ba65a6a 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,24 @@
+2016-05-04  Youenn Fablet  <youenn.fablet@crf.canon.fr>
+
+        Clicks inside button elements are sometimes discarded when the mouse moves
+        https://bugs.webkit.org/show_bug.cgi?id=39620
+
+        Reviewed by Darin Adler.
+
+        Test: fast/events/click-over-descendant-elements.html
+
+        * dom/Node.cpp:
+        (WebCore::ancestor):
+        (WebCore::commonAncestor): Method inspired from
+        http://src.chromium.org/viewvc/blink?view=revision&revision=162081.
+        (WebCore::commonAncestorCrossingShadowBoundary): Helper routine
+        that handles the case of nodes into a shadow node.
+        * dom/Node.h:
+        * page/EventHandler.cpp:
+        (WebCore::EventHandler::handleMouseReleaseEvent): Selecting click event
+        target node according commonAncestorOverShadowBoundary method.
+        (WebCore::EventHandler::targetNodeForClickEvent): Deleted.
+
 2016-05-04  Zan Dobersek  <zdobersek@igalia.com>
 
         Unreviewed, fixing crashing GTK+ tests after r200407.
diff --git a/Source/WebCore/dom/Node.cpp b/Source/WebCore/dom/Node.cpp
index d4728c7..415b406 100644
--- a/Source/WebCore/dom/Node.cpp
+++ b/Source/WebCore/dom/Node.cpp
@@ -952,6 +952,65 @@
     return false;
 }
 
+static inline Node* ancestor(Node* node, unsigned depth)
+{
+    for (unsigned i = 0; i < depth; ++i)
+        node = node->parentNode();
+    return node;
+}
+
+Node* commonAncestor(Node& thisNode, Node& otherNode)
+{
+    unsigned thisDepth = 0;
+    for (auto node = &thisNode; node; node = node->parentNode()) {
+        if (node == &otherNode)
+            return node;
+        thisDepth++;
+    }
+    unsigned otherDepth = 0;
+    for (auto node = &otherNode; node; node = node->parentNode()) {
+        if (node == &thisNode)
+            return &thisNode;
+        otherDepth++;
+    }
+
+    Node* thisAncestor = &thisNode;
+    Node* otherAncestor = &otherNode;
+    if (thisDepth > otherDepth)
+        thisAncestor = ancestor(thisAncestor, thisDepth - otherDepth);
+    else if (otherDepth > thisDepth)
+        otherAncestor = ancestor(otherAncestor, otherDepth - thisDepth);
+
+    for (; thisAncestor; thisAncestor = thisAncestor->parentNode()) {
+        if (thisAncestor == otherAncestor)
+            return thisAncestor;
+        otherAncestor = otherAncestor->parentNode();
+    }
+    ASSERT(!otherAncestor);
+    return nullptr;
+}
+
+Node* commonAncestorCrossingShadowBoundary(Node& node, Node& other)
+{
+    if (&node == &other)
+        return &node;
+
+    Element* shadowHost = node.shadowHost();
+    // FIXME: This test might be wrong for user-authored shadow trees.
+    if (shadowHost && shadowHost == other.shadowHost())
+        return shadowHost;
+
+    TreeScope* scope = commonTreeScope(&node, &other);
+    if (!scope)
+        return nullptr;
+
+    Node* parentNode = scope->ancestorInThisScope(&node);
+    ASSERT(parentNode);
+    Node* parentOther = scope->ancestorInThisScope(&other);
+    ASSERT(parentOther);
+    return commonAncestor(*parentNode, *parentOther);
+}
+
 Node* Node::pseudoAwarePreviousSibling() const
 {
     Element* parentOrHost = is<PseudoElement>(*this) ? downcast<PseudoElement>(*this).hostElement() : parentElement();
diff --git a/Source/WebCore/dom/Node.h b/Source/WebCore/dom/Node.h
index 60638f0..3fbb012 100644
--- a/Source/WebCore/dom/Node.h
+++ b/Source/WebCore/dom/Node.h
@@ -783,6 +783,9 @@
 
 #endif
 
+Node* commonAncestor(Node&, Node&);
+Node* commonAncestorCrossingShadowBoundary(Node&, Node&);
+
 } // namespace WebCore
 
 #if ENABLE(TREE_DEBUGGING)
diff --git a/Source/WebCore/page/EventHandler.cpp b/Source/WebCore/page/EventHandler.cpp
index 669caff..c773e00 100644
--- a/Source/WebCore/page/EventHandler.cpp
+++ b/Source/WebCore/page/EventHandler.cpp
@@ -1949,24 +1949,6 @@
     m_clickNode = nullptr;
 }
 
-static Node* targetNodeForClickEvent(Node* mousePressNode, Node* mouseReleaseNode)
-{
-    if (!mousePressNode || !mouseReleaseNode)
-        return nullptr;
-
-    if (mousePressNode == mouseReleaseNode)
-        return mouseReleaseNode;
-
-    Element* mouseReleaseShadowHost = mouseReleaseNode->shadowHost();
-    if (mouseReleaseShadowHost && mouseReleaseShadowHost == mousePressNode->shadowHost()) {
-        // We want to dispatch the click to the shadow tree host element to give listeners the illusion that the
-        // shadom tree is a single element. For example, we want to give the illusion that <input type="range">
-        // is a single element even though it is a composition of multiple shadom tree elements.
-        return mouseReleaseShadowHost;
-    }
-    return nullptr;
-}
-
 bool EventHandler::handleMouseReleaseEvent(const PlatformMouseEvent& platformMouseEvent)
 {
     RefPtr<FrameView> protector(m_frame.view());
@@ -2028,7 +2010,8 @@
 
     bool contextMenuEvent = platformMouseEvent.button() == RightButton;
 
-    Node* nodeToClick = targetNodeForClickEvent(m_clickNode.get(), mouseEvent.targetNode());
+    Node* targetNode = mouseEvent.targetNode();
+    Node* nodeToClick = (m_clickNode && targetNode) ? commonAncestorCrossingShadowBoundary(*m_clickNode, *targetNode) : nullptr;
     bool swallowClickEvent = m_clickCount > 0 && !contextMenuEvent && nodeToClick && !dispatchMouseEvent(eventNames().clickEvent, nodeToClick, true, m_clickCount, platformMouseEvent, true);
 
     if (m_resizeLayer) {