Title: [274365] trunk
Revision
274365
Author
[email protected]
Date
2021-03-12 11:03:26 -0800 (Fri, 12 Mar 2021)

Log Message

[selectors] :focus-visible matches body after keyboard event
https://bugs.webkit.org/show_bug.cgi?id=223113

Reviewed by Antti Koivisto.

LayoutTests/imported/w3c:

* web-platform-tests/css/selectors/focus-visible-001.html: Modify the test to verify
that only the element matches :focus-visible (and not the body).
* web-platform-tests/css/selectors/focus-visible-019-expected.txt: Added.
* web-platform-tests/css/selectors/focus-visible-019.html: Added new test to check script focus in keyboard event,
and that again only the element matches :focus-visible (and not the body).

Source/WebCore:

Fix the bug with some changes in EventHandler::internalKeyEvent().
When you use TAB (or other key) the |element| variable in this method is the document body,
however that element is not focused (element->focused() is false).
Before this patch we were marking the element as matchin :focus-visible,
however we shouldn't do that if the element is not focused (added a condition to avoid doing that).

Apart from that this patch also fixes a related issue, if a keyboard event handler is changing focus via script
there's a part of this method that takes care of updating the |element| variable.
In that case we have to remove :focus-visible flag from the previous element, and add it to the new one.

Test: imported/w3c/web-platform-tests/css/selectors/focus-visible-001.html
Test: imported/w3c/web-platform-tests/css/selectors/focus-visible-019.html

* page/EventHandler.cpp:
(WebCore::EventHandler::internalKeyEvent):

LayoutTests:

* platform/ios/TestExpectations: Skip new test for iOS.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (274364 => 274365)


--- trunk/LayoutTests/ChangeLog	2021-03-12 18:49:22 UTC (rev 274364)
+++ trunk/LayoutTests/ChangeLog	2021-03-12 19:03:26 UTC (rev 274365)
@@ -1,3 +1,12 @@
+2021-03-12  Manuel Rego Casasnovas  <[email protected]>
+
+        [selectors] :focus-visible matches body after keyboard event
+        https://bugs.webkit.org/show_bug.cgi?id=223113
+
+        Reviewed by Antti Koivisto.
+
+        * platform/ios/TestExpectations: Skip new test for iOS.
+
 2021-03-12  Robert Jenner  <[email protected]>
 
         [MacOS wk2] imported/w3c/web-platform-tests/media-source/SourceBuffer-abort-updating.html is a flakey failure

Modified: trunk/LayoutTests/imported/w3c/ChangeLog (274364 => 274365)


--- trunk/LayoutTests/imported/w3c/ChangeLog	2021-03-12 18:49:22 UTC (rev 274364)
+++ trunk/LayoutTests/imported/w3c/ChangeLog	2021-03-12 19:03:26 UTC (rev 274365)
@@ -1,3 +1,16 @@
+2021-03-12  Manuel Rego Casasnovas  <[email protected]>
+
+        [selectors] :focus-visible matches body after keyboard event
+        https://bugs.webkit.org/show_bug.cgi?id=223113
+
+        Reviewed by Antti Koivisto.
+
+        * web-platform-tests/css/selectors/focus-visible-001.html: Modify the test to verify
+        that only the element matches :focus-visible (and not the body).
+        * web-platform-tests/css/selectors/focus-visible-019-expected.txt: Added.
+        * web-platform-tests/css/selectors/focus-visible-019.html: Added new test to check script focus in keyboard event,
+        and that again only the element matches :focus-visible (and not the body).
+
 2021-03-12  Antoine Quint  <[email protected]>
 
         Support animation of perspective-origin property

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-001.html (274364 => 274365)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-001.html	2021-03-12 18:49:22 UTC (rev 274364)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-001.html	2021-03-12 19:03:26 UTC (rev 274365)
@@ -39,10 +39,14 @@
   <div id="el" tabindex="0">Focus me.</div>
   <script>
     async_test(function(t) {
-      el.addEventListener("focus", t.step_func(function() {
+      el.addEventListener("focus", t.step_func_done(function() {
         assert_equals(getComputedStyle(el).outlineColor, "rgb(0, 128, 0)", `outlineColor for ${el.tagName}#${el.id} should be green`);
         assert_not_equals(getComputedStyle(el).backgroundColor, "rgb(255, 0, 0)", `backgroundColor for ${el.tagName}#${el.id} should NOT be red`);
-        t.done();
+
+        let focusVisiblePseudoAll = document.querySelectorAll(':focus-visible');
+        assert_equals(focusVisiblePseudoAll.length, 1, "Only 1 element matches ':focus-visible'");
+        let focusVisiblePseudo = document.querySelector(':focus-visible');
+        assert_equals(el, focusVisiblePseudo, "${el.tagName}#${el.id} matches ':focus-visible'");
       }));
       const tab_key = '\ue004';
       test_driver.send_keys(el, tab_key)

Added: trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-019-expected.txt (0 => 274365)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-019-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-019-expected.txt	2021-03-12 19:03:26 UTC (rev 274365)
@@ -0,0 +1,9 @@
+This test checks that changing focus via script on keyboard event handler matches :focus-visible.
+
+Type any letter with the keyboard without having done anything before.
+If the element that says "Focused" has a red background, then the test result is FAILURE. If the element has a green outline, then the test result is SUCCESS.
+Focused.
+
+PASS ":focus-visible" should be a valid selector
+PASS Focus via script on keyboard event handler matches ':focus-visible'
+

Added: trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-019.html (0 => 274365)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-019.html	                        (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/selectors/focus-visible-019.html	2021-03-12 19:03:26 UTC (rev 274365)
@@ -0,0 +1,64 @@
+<!DOCTYPE html>
+<meta charset="utf-8" />
+<title>CSS Test (Selectors): Script focus on keyboard event handler matches :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=""
+<script src=""
+<style>
+  #warning {
+    display: none;
+    background: red;
+  }
+
+  @supports not (selector(:focus-visible)) {
+    #instructions {
+      display: none;
+    }
+
+    #warning {
+      display: block;
+    }
+  }
+
+  :focus-visible {
+    outline: green solid 5px;
+  }
+
+  :focus:not(:focus-visible) {
+    outline: 0;
+    background-color: red;
+  }
+</style>
+
+<p>This test checks that changing focus via script on keyboard event handler matches <code>:focus-visible</code>.</p>
+<ol id="instructions">
+  <li>Type any letter with the keyboard without having done anything before.</li>
+  <li>If the element that says "Focused" has a red background, then the test result is FAILURE. If the element has a green outline, 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>
+<div id="target" tabindex="0">Focused.</div>
+<script>
+  // Check that :focus-visible is supported.
+  test_valid_selector(':focus-visible');
+
+  document.addEventListener("keydown", () => {
+    target.focus();
+  });
+
+  async_test(function(t) {
+    test_driver.send_keys(document.body, "a").then(t.step_func_done(function() {
+      assert_equals(getComputedStyle(target).outlineColor, "rgb(0, 128, 0)", `outlineColor for ${target.tagName}#${target.id} should be green`);
+      assert_not_equals(getComputedStyle(target).backgroundColor, "rgb(255, 0, 0)", `backgroundColor for ${target.tagName}#${target.id} should NOT be red`);
+
+      let focusVisiblePseudoAll = document.querySelectorAll(':focus-visible');
+      assert_equals(focusVisiblePseudoAll.length, 1, "Only 1 element matches ':focus-visible'");
+      let focusVisiblePseudo = document.querySelector(':focus-visible');
+      assert_equals(target, focusVisiblePseudo, "${target.tagName}#${target.id} matches ':focus-visible'");
+    }));
+  }, "Focus via script on keyboard event handler matches ':focus-visible'");
+</script>

Modified: trunk/LayoutTests/platform/ios/TestExpectations (274364 => 274365)


--- trunk/LayoutTests/platform/ios/TestExpectations	2021-03-12 18:49:22 UTC (rev 274364)
+++ trunk/LayoutTests/platform/ios/TestExpectations	2021-03-12 19:03:26 UTC (rev 274365)
@@ -3395,6 +3395,7 @@
 webkit.org/b/209734 imported/w3c/web-platform-tests/css/selectors/focus-visible-011.html [ Skip ]
 webkit.org/b/209734 imported/w3c/web-platform-tests/css/selectors/focus-visible-012.html [ Skip ]
 webkit.org/b/209734 imported/w3c/web-platform-tests/css/selectors/focus-visible-013.html [ Skip ]
+webkit.org/b/209734 imported/w3c/web-platform-tests/css/selectors/focus-visible-019.html [ Skip ]
 webkit.org/b/209734 imported/w3c/web-platform-tests/css/selectors/hover-002.html [ Skip ]
 
 # Certain versions of iOS use different text security characters.

Modified: trunk/Source/WebCore/ChangeLog (274364 => 274365)


--- trunk/Source/WebCore/ChangeLog	2021-03-12 18:49:22 UTC (rev 274364)
+++ trunk/Source/WebCore/ChangeLog	2021-03-12 19:03:26 UTC (rev 274365)
@@ -1,3 +1,26 @@
+2021-03-12  Manuel Rego Casasnovas  <[email protected]>
+
+        [selectors] :focus-visible matches body after keyboard event
+        https://bugs.webkit.org/show_bug.cgi?id=223113
+
+        Reviewed by Antti Koivisto.
+
+        Fix the bug with some changes in EventHandler::internalKeyEvent().
+        When you use TAB (or other key) the |element| variable in this method is the document body,
+        however that element is not focused (element->focused() is false).
+        Before this patch we were marking the element as matchin :focus-visible,
+        however we shouldn't do that if the element is not focused (added a condition to avoid doing that).
+
+        Apart from that this patch also fixes a related issue, if a keyboard event handler is changing focus via script
+        there's a part of this method that takes care of updating the |element| variable.
+        In that case we have to remove :focus-visible flag from the previous element, and add it to the new one.
+
+        Test: imported/w3c/web-platform-tests/css/selectors/focus-visible-001.html
+        Test: imported/w3c/web-platform-tests/css/selectors/focus-visible-019.html
+
+        * page/EventHandler.cpp:
+        (WebCore::EventHandler::internalKeyEvent):
+
 2021-03-12  Alex Christensen  <[email protected]>
 
         Reduce maximum HashTable entry size to 400 bytes

Modified: trunk/Source/WebCore/page/EventHandler.cpp (274364 => 274365)


--- trunk/Source/WebCore/page/EventHandler.cpp	2021-03-12 18:49:22 UTC (rev 274364)
+++ trunk/Source/WebCore/page/EventHandler.cpp	2021-03-12 19:03:26 UTC (rev 274365)
@@ -3533,11 +3533,16 @@
         keydown->preventDefault();
     keydown->setTarget(element);
 
-    // If the user interacts with the page via the keyboard, the currently focused element should match :focus-visible.
-    // Just typing a modifier key is not considered user interaction with the page, but Shift + a (or Caps Lock + a) is considered an interaction.
-    if (keydown->modifierKeys().isEmpty() || ((keydown->shiftKey() || keydown->capsLockKey()) && !initialKeyEvent.text().isEmpty()))
-        element->setHasFocusVisible(true);
+    auto shouldMatchFocusVisible = [initialKeyEvent, keydown](const Element& element) {
+        if (!element.focused())
+            return false;
 
+        // If the user interacts with the page via the keyboard, the currently focused element should match :focus-visible.
+        // Just typing a modifier key is not considered user interaction with the page, but Shift + a (or Caps Lock + a) is considered an interaction.
+        return keydown->modifierKeys().isEmpty() || ((keydown->shiftKey() || keydown->capsLockKey()) && !initialKeyEvent.text().isEmpty());
+    };
+    element->setHasFocusVisible(shouldMatchFocusVisible(*element));
+
     if (initialKeyEvent.type() == PlatformEvent::RawKeyDown) {
         element->dispatchEvent(keydown);
         // If frame changed as a result of keydown dispatch, then return true to avoid sending a subsequent keypress message to the new frame.
@@ -3583,9 +3588,11 @@
     // Focus may have changed during keydown handling, so refetch element.
     // But if we are dispatching a fake backward compatibility keypress, then we pretend that the keypress happened on the original element.
     if (!keydownResult) {
+        element->setHasFocusVisible(false);
         element = eventTargetElementForDocument(m_frame.document());
         if (!element)
             return false;
+        element->setHasFocusVisible(shouldMatchFocusVisible(*element));
     }
 
     PlatformKeyboardEvent keyPressEvent = initialKeyEvent;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to