Title: [292400] trunk
Revision
292400
Author
[email protected]
Date
2022-04-05 10:04:57 -0700 (Tue, 05 Apr 2022)

Log Message

[macOS][selectors] :focus-visible matching on button focused via script (after clicking on a different button)
https://bugs.webkit.org/show_bug.cgi?id=236782
<rdar://problem/89382543>

Reviewed by Antti Koivisto.

LayoutTests/imported/w3c:

Add specific test for <button> element.

* web-platform-tests/css/selectors/focus-visible-script-focus-008-b-expected.txt: Added.
* web-platform-tests/css/selectors/focus-visible-script-focus-008-b.html: Added.

Source/WebCore:

Document::m_latestFocusTrigger was not being updated after clicking on a button (or other form controls)
on platforms different than GTK and WPE.
This is because HTMLFormControlElement::isMouseFocusable() returns false on such platforms.

This issue is causing problems with :focus-visible, as the button is not actually focused via mouse click,
if you move the focus via script afterwards, the new focused element always match :focus-visible.

This patch is a workaround to solve that issue, by updating m_latestFocusTrigger on Document
even if the element is not actually focused.
That way :focus-visible doesn't match if there's a script focus afterwards.

Fix :focus-visible after clicking on a button on Mac

Test: imported/w3c/web-platform-tests/css/selectors/focus-visible-script-focus-008-b.html

* dom/Document.h:
(WebCore::Document::setLatestFocusTrigger):
* page/EventHandler.cpp:
(WebCore::EventHandler::dispatchMouseEvent):

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/imported/w3c/ChangeLog (292399 => 292400)


--- trunk/LayoutTests/imported/w3c/ChangeLog	2022-04-05 16:57:35 UTC (rev 292399)
+++ trunk/LayoutTests/imported/w3c/ChangeLog	2022-04-05 17:04:57 UTC (rev 292400)
@@ -1,3 +1,16 @@
+2022-04-05  Manuel Rego Casasnovas  <[email protected]>
+
+        [macOS][selectors] :focus-visible matching on button focused via script (after clicking on a different button)
+        https://bugs.webkit.org/show_bug.cgi?id=236782
+        <rdar://problem/89382543>
+
+        Reviewed by Antti Koivisto.
+
+        Add specific test for <button> element.
+
+        * web-platform-tests/css/selectors/focus-visible-script-focus-008-b-expected.txt: Added.
+        * web-platform-tests/css/selectors/focus-visible-script-focus-008-b.html: Added.
+
 2022-04-05  Alan Bujtas  <[email protected]>
 
         [CSS-Contain] Add support for contain: inline-size

Added: trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-script-focus-008-b-expected.txt (0 => 292400)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-script-focus-008-b-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-script-focus-008-b-expected.txt	2022-04-05 17:04:57 UTC (rev 292400)
@@ -0,0 +1,10 @@
+This test checks that a script focus after a mouse click on a button that does not match :focus visible, does NOT match :focus-visible.
+
+Click on the button that says "Click me".
+If the element that says "Focused" has a red outline then the test result is FAILURE, if it has a green background then the test result is SUCCESS.
+Click me
+Focused
+
+PASS ":focus-visible" should be a valid selector
+PASS Script focus after mouse click on a button that does not match :focus-visible, does NOT match :focus-visible
+

Added: trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-script-focus-008-b.html (0 => 292400)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-script-focus-008-b.html	                        (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-script-focus-008-b.html	2022-04-05 17:04:57 UTC (rev 292400)
@@ -0,0 +1,64 @@
+<!DOCTYPE html>
+<meta charset="utf-8" />
+<title>CSS Test (Selectors): Script focus after mouse click on a button that does not match :focus-visible, does NOT match :focus-visible</title>
+<link rel="author" title="Manuel Rego Casasnovas" href=""
+<link rel="help" href="" />
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<style>
+  #warning {
+    display: none;
+    background: red;
+  }
+
+  @supports not selector(:focus-visible) {
+    #instructions {
+      display: none;
+    }
+
+    #warning {
+      display: block;
+    }
+  }
+
+  :focus-visible {
+    outline: solid thick red;
+  }
+
+  :focus:not(:focus-visible) {
+    background-color: lime;
+  }
+</style>
+
+<p>This test checks that a script focus after a mouse click on a button that does not match <code>:focus visible</code>, does NOT match <code>:focus-visible</code>.</p>
+<ol id="instructions">
+  <li>Click on the button that says "Click me".</li>
+  <li>If the element that says "Focused" has a red outline then the test result is FAILURE, if it has a green background then the test result is SUCCESS.</li>
+</ol>
+<p id="warning">Your user-agent does not support <code>:focus-visible</code> pseudo-class, please SKIP this test.</p>
+
+<button id="initial">Click me</button>
+<div id="target" tabindex="0">Focused</div>
+
+<script>
+  setup({ explicit_done: true });
+
+  // Check that :focus-visible is supported.
+  test_valid_selector(':focus-visible');
+
+  initial.addEventListener("click", () => {
+    target.focus();
+  });
+
+  async_test(function(t) {
+    target.addEventListener("focus", t.step_func_done(function() {
+      assert_equals(getComputedStyle(target).backgroundColor, "rgb(0, 255, 0)", `backgroundColor for ${target.tagName}#${target.id} should be lime`);
+      assert_not_equals(getComputedStyle(target).outlineColor, "rgb(255, 0, 0)", `outlineColor for ${target.tagName}#${target.id} should NOT be red`);
+    }));
+
+    test_driver.click(initial).then(() => done());
+  }, "Script focus after mouse click on a button that does not match :focus-visible, does NOT match :focus-visible");
+</script>

Modified: trunk/Source/WebCore/ChangeLog (292399 => 292400)


--- trunk/Source/WebCore/ChangeLog	2022-04-05 16:57:35 UTC (rev 292399)
+++ trunk/Source/WebCore/ChangeLog	2022-04-05 17:04:57 UTC (rev 292400)
@@ -1,3 +1,31 @@
+2022-04-05  Manuel Rego Casasnovas  <[email protected]>
+
+        [macOS][selectors] :focus-visible matching on button focused via script (after clicking on a different button)
+        https://bugs.webkit.org/show_bug.cgi?id=236782
+        <rdar://problem/89382543>
+
+        Reviewed by Antti Koivisto.
+
+        Document::m_latestFocusTrigger was not being updated after clicking on a button (or other form controls)
+        on platforms different than GTK and WPE.
+        This is because HTMLFormControlElement::isMouseFocusable() returns false on such platforms.
+
+        This issue is causing problems with :focus-visible, as the button is not actually focused via mouse click,
+        if you move the focus via script afterwards, the new focused element always match :focus-visible.
+
+        This patch is a workaround to solve that issue, by updating m_latestFocusTrigger on Document
+        even if the element is not actually focused.
+        That way :focus-visible doesn't match if there's a script focus afterwards.
+
+        Fix :focus-visible after clicking on a button on Mac
+
+        Test: imported/w3c/web-platform-tests/css/selectors/focus-visible-script-focus-008-b.html
+
+        * dom/Document.h:
+        (WebCore::Document::setLatestFocusTrigger):
+        * page/EventHandler.cpp:
+        (WebCore::EventHandler::dispatchMouseEvent):
+
 2022-04-05  Simon Fraser  <[email protected]>
 
         Pass an IOSurfacePool to ImageBuffer::releaseBufferToPool()

Modified: trunk/Source/WebCore/dom/Document.h (292399 => 292400)


--- trunk/Source/WebCore/dom/Document.h	2022-04-05 16:57:35 UTC (rev 292399)
+++ trunk/Source/WebCore/dom/Document.h	2022-04-05 17:04:57 UTC (rev 292400)
@@ -797,6 +797,7 @@
     WEBCORE_EXPORT bool setFocusedElement(Element*, const FocusOptions& = { });
     Element* focusedElement() const { return m_focusedElement.get(); }
     bool wasLastFocusByClick() const { return m_latestFocusTrigger == FocusTrigger::Click; }
+    void setLatestFocusTrigger(FocusTrigger trigger) { m_latestFocusTrigger = trigger; }
     UserActionElementSet& userActionElements()  { return m_userActionElements; }
     const UserActionElementSet& userActionElements() const { return m_userActionElements; }
 

Modified: trunk/Source/WebCore/page/EventHandler.cpp (292399 => 292400)


--- trunk/Source/WebCore/page/EventHandler.cpp	2022-04-05 16:57:35 UTC (rev 292399)
+++ trunk/Source/WebCore/page/EventHandler.cpp	2022-04-05 17:04:57 UTC (rev 292400)
@@ -2795,6 +2795,15 @@
     if (!element && isInsideScrollbar(platformMouseEvent.position()))
         return false;
 
+#if (!PLATFORM(GTK) && !PLATFORM(WPE))
+    // This is a workaround related to :focus-visible (see webkit.org/b/236782).
+    // Form control elements are not mouse focusable on some platforms (see HTMLFormControlElement::isMouseFocusable())
+    // which makes us behave differently than other browsers when a button is clicked,
+    // because the button is not actually focused so we don't set the latest FocusTrigger.
+    if (m_elementUnderMouse && !m_elementUnderMouse->isMouseFocusable() && is<HTMLFormControlElement>(m_elementUnderMouse))
+        m_frame.document()->setLatestFocusTrigger(FocusTrigger::Click);
+#endif
+
     // If focus shift is blocked, we eat the event.
     auto* page = m_frame.page();
     if (page && !CheckedRef(page->focusController())->setFocusedElement(element.get(), m_frame, { { }, { }, { }, FocusTrigger::Click, { } }))
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to