Bug 233688 - [selectors] :focus-visible should stop matching after blur
Summary: [selectors] :focus-visible should stop matching after blur
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: CSS (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Manuel Rego Casasnovas
URL:
Keywords: InRadar
Depends on:
Blocks: 185859
  Show dependency treegraph
 
Reported: 2021-11-30 23:56 PST by Manuel Rego Casasnovas
Modified: 2021-12-02 00:07 PST (History)
9 users (show)

See Also:


Attachments
Patch (4.99 KB, patch)
2021-11-30 23:59 PST, Manuel Rego Casasnovas
no flags Details | Formatted Diff | Diff
Patch (6.47 KB, patch)
2021-12-01 06:46 PST, Manuel Rego Casasnovas
no flags Details | Formatted Diff | Diff
Patch (7.55 KB, patch)
2021-12-01 08:22 PST, Manuel Rego Casasnovas
no flags Details | Formatted Diff | Diff
Patch (8.05 KB, patch)
2021-12-01 22:02 PST, Manuel Rego Casasnovas
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Manuel Rego Casasnovas 2021-11-30 23:56:11 PST
I've been checking the failures on the last patch at https://bugs.webkit.org/show_bug.cgi?id=221925 and it looks like we introduced a bug in r286134 (https://bugs.webkit.org/show_bug.cgi?id=233178) and :focus-visible doesn't stop matching after blur.
Comment 1 Manuel Rego Casasnovas 2021-11-30 23:59:38 PST
Created attachment 445534 [details]
Patch
Comment 2 EWS Watchlist 2021-12-01 00:00:45 PST
This patch modifies the imported WPT tests. Please ensure that any changes on the tests (not coming from a WPT import) are exported to WPT. Please see https://trac.webkit.org/wiki/WPTExportProcess
Comment 3 Antti Koivisto 2021-12-01 00:11:21 PST
Comment on attachment 445534 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=445534&action=review

> Source/WebCore/style/ElementRuleCollector.cpp:161
> +    if (matchesFocusPseudoClass(element()) || matchesFocusVisiblePseudoClass(element()))

We should understand why matchesFocusPseudoClass is false while matchesFocusVisiblePseudoClass is true. It makes no logical sense, every element with visible focus should have focus.
Comment 4 Manuel Rego Casasnovas 2021-12-01 06:46:17 PST
Created attachment 445571 [details]
Patch
Comment 5 Manuel Rego Casasnovas 2021-12-01 06:47:51 PST
Thanks for the quick review.

(In reply to Antti Koivisto from comment #3)
> Comment on attachment 445534 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=445534&action=review
> 
> > Source/WebCore/style/ElementRuleCollector.cpp:161
> > +    if (matchesFocusPseudoClass(element()) || matchesFocusVisiblePseudoClass(element()))
> 
> We should understand why matchesFocusPseudoClass is false while
> matchesFocusVisiblePseudoClass is true. It makes no logical sense, every
> element with visible focus should have focus.

Yeah you're right, the actual problem was in Element::setFocus().
We are using there Style::PseudoClassChangeInvalidation for :focus, but that was used before focus-visible flag was set, and thus the flags were out of sync during invalidation.

The new version of the patch fixes that. PTAL.
Comment 6 Antti Koivisto 2021-12-01 07:34:58 PST
Comment on attachment 445571 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=445571&action=review

> Source/WebCore/dom/Element.cpp:-847
> -    Style::PseudoClassChangeInvalidation styleInvalidation(*this, CSSSelector::PseudoClassFocusVisible);

I see direct call to setHasFocusVisible in EventHandler::internalKeyEvent(). Would that miss invalidation now?

Does that case have a test?
Comment 7 Manuel Rego Casasnovas 2021-12-01 08:20:42 PST
Comment on attachment 445571 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=445571&action=review

>> Source/WebCore/dom/Element.cpp:-847
>> -    Style::PseudoClassChangeInvalidation styleInvalidation(*this, CSSSelector::PseudoClassFocusVisible);
> 
> I see direct call to setHasFocusVisible in EventHandler::internalKeyEvent(). Would that miss invalidation now?
> 
> Does that case have a test?

Yes there's a test and it actually failed due to that.
Comment 8 Manuel Rego Casasnovas 2021-12-01 08:22:47 PST
Created attachment 445577 [details]
Patch
Comment 9 Antti Koivisto 2021-12-01 09:38:03 PST
Comment on attachment 445577 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=445577&action=review

> Source/WebCore/dom/Element.cpp:855
> -    Style::PseudoClassChangeInvalidation styleInvalidation(*this, CSSSelector::PseudoClassFocusVisible);
> +    if (invalidation == Invalidation::PseudoClass)
> +        Style::PseudoClassChangeInvalidation styleInvalidation(*this, CSSSelector::PseudoClassFocusVisible);
>      document().userActionElements().setHasFocusVisible(*this, flag);

This won't work, PseudoClassChangeInvalidation needs to be scoped around the the mutation (setting focus visible flag) as invalidation needs to run in both the tree before and after the mutation (from constructor and destructor). Surprised this doesn't break tests.

I don't think you need to do this change though. The only change that is needed is to call setHasFocusVisible within the scope of focusStyleInvalidation.
Comment 10 Antti Koivisto 2021-12-01 09:41:39 PST
Well, I suppose you actually need to do something like this get consistent scopes.

Easiest thing to do is to just add PseudoClassChangeInvalidation to the caller of setHasFocusVisible.
Comment 11 Manuel Rego Casasnovas 2021-12-01 22:02:24 PST
Created attachment 445671 [details]
Patch
Comment 12 Manuel Rego Casasnovas 2021-12-01 22:03:09 PST
(In reply to Antti Koivisto from comment #10)
> Easiest thing to do is to just add PseudoClassChangeInvalidation to the
> caller of setHasFocusVisible.

Ok, I've done this in a new version of the patch. Thanks again for the review.
Comment 13 EWS 2021-12-02 00:06:31 PST
Committed r286415 (244761@main): <https://commits.webkit.org/244761@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 445671 [details].
Comment 14 Radar WebKit Bug Importer 2021-12-02 00:07:25 PST
<rdar://problem/85958613>