Project: chromium Issues People Development process History Sign in
New issue
Advanced search Search tips
Issue 617371 Implement :focus-within pseudo-class from Selectors Level 4
Starred by 16 users Reported by, Jun 4 2016 Back to list
Status: Fixed
Closed: Today
EstimatedDays: ----
NextAction: ----
OS: All
Pri: 3
Type: Feature

Blocked on:
issue 704902

Sign in to add a comment
UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36

Steps to reproduce the problem:
1. Open the attached testcase in Chrome.
2. Click/tap into the text <input> so as to focus it.

What is the expected behavior?
The <div> should turn green since the <input>, which is a descendant of the <div>,
has become focused, thus causing the :focus pseudo-class to match the <input>,
which should cause the <div> to match :focus-within,
because it now has a descendant which matches :focus.

What went wrong?
The <div> remained white, indicating that the :focus-within pseudo-class in the webpage's CSS didn't start matching the <div>.

Did this work before? No 

Chrome version: 51.0.2704.79  Channel: stable
OS Version: OS X 10.11.5
Flash Version: Shockwave Flash 21.0 r0

455 bytes View Download
Comment 1 by, Jun 4 2016
Components: Blink>Focus Blink>CSS
Labels: -Pri-2 -OS-Mac OS-All Pri-3
Status: Untriaged
Status: Available
The version of Selectors Level 4 that focus-within appears in is still an editor's draft.
Comment 3 by, Jun 30 2016
Components: UI>Accessibility
FYI WebKit already have this
Comment 4 by, Sep 26 2016
Firefox 52 has implemented this:
Labels: Needs-BlinkIntent
Per this will ship in Firefox 52 and Safari 10.1.
Labels: Update-Quarterly
Status: Assigned
I'll give it a try.

Intent thread at:!topic/blink-dev/V3RNBhQelSg
Blockedon: 704902
Labels: Objective
Since this is a high-level tracking bug, adding the label Objective. Per Blink>CSS bug protocols, for clear responsibility, any concrete work on this issue should take place on a dependent bug that is filed under one component only.
Is the spec include anything aboout Shadow DOM?

How Safari 10.1 treats shadow tree?
The spec explicitly says (
"An element also matches :focus-within if one of its shadow-including descendants matches :focus."

This is pretty similar to the text for for :active or :hover pseudo-classes.

And about "shadow-including descendants" the spec says (
"An object A is a shadow-including descendant of an object B, if A is a descendant of B, or A’s root is a shadow root and A’s root’s host is a shadow-including inclusive descendant of B."

In Safari they have a test like this:
Which adds a green border to all the elements on the page including <body> and <div id="root">.

Also the W3C suite has 5 tests testing Shadow DOM too:

rego@ thanks for the checking!

I've tried Safari TP25 myself, and it passed my casual tests of using :focus-within.
The behavior about Shadow DOM looks reasonable and the specs sound Shadow DOM-ready.

BTW the csswg tests use Shadow V0 API - I'll find time to convert them to V1 APIs :)
23.5 KB View Download
25.0 KB View Download
@kochi, yeah I realized about that. We should change it to V1, I can do it too.
But note that the csswg-test is merging into wpt tomorrow,
so it's probably better to wait a few days. :-)
rego@ it's good to know, and feel free to take it :)

BTW, the screenshot in comment#13 was for what I was playing with on Safari TP25.,css,output (without <slot>),css,output (with <slot>)

Notice the difference that the latter <input> gets green border, as it matches
the :focus-within in the document stylesheet. In the former <input> didn't
get green border, even though it is focused, because it is inside a shadow tree
and the style rule in the document tree didn't reach there.
Labels: NewComponent-Accessibility-Blink NewComponent-Accessibility
Status: Started
The intent thread still lacks 1 LGTM, but an initial patch is ready at:
Project Member Comment 18 by, Apr 12
The following revision refers to this bug:

commit ffc655c99fef91f49ad7906344d51678c3853b74
Author: rego <>
Date: Wed Apr 12 11:31:29 2017

[selectors4] Implement :focus-within pseudo-class

This patch adds support for the new ":focus-within" pseudo-class.
Most of the patch is basically the regular boilerplate code
to add a new selector. Then the interesting changes happen
on ContainerNode.

The patch is covered by a bunch of tests imported from
the W3C test suite (which includes generic tests in addition to
the tests from Firefox and WebKit implementations too).
Apart from some minor changes on current tests to add
the new selector.

Intent-to-ship thread is available at:!topic/blink-dev/V3RNBhQelSg

BUG= 617371 

Cr-Commit-Position: refs/heads/master@{#463992}


Initial support for :focus-within has just landed behind a runtime flag.

The next steps would be (as described in the CL):
* Fix the test on WPT:
* Investigate the common ancestor approach deeply (the same we currently use for :hover).

Project Member Comment 20 by, Apr 17
The following revision refers to this bug:

commit a51a79c74201875de7299e5daf61e0c297d64647
Author: rego <>
Date: Mon Apr 17 17:34:37 2017

[selectors4] Remove :focus-within test from TestExpectations

The test is passing since r463992 but was marked as Failure
in TestExpectations file.

BUG= 617371 

Cr-Commit-Position: refs/heads/master@{#464944}


Comment 21 by, Apr 21 (6 days ago)
Components: Blink>Accessibility
Comment 22 by, Apr 21 (6 days ago)
Components: -UI>Accessibility
Labels: -newcomponent-accessibility-blink -newcomponent-accessibility
Project Member Comment 23 by, Apr 25 (2 days ago)
The following revision refers to this bug:

commit 8789e1ce7cc6848e0cb4ba4bb546828df4fe7ca2
Author: rego <>
Date: Tue Apr 25 12:06:44 2017

[selectors4] Use common ancestor strategy for :focus-within

Following the same approach than what we do for :active and :hover
pseudo-classes, it seems a nice optimization to use the common ancestor
strategy for :focus-within too.

The use case would be for example that you've a from with several inputs
in a deep part of the DOM, and in the body element you've :focus-within
to show some warning while editing the form.
When you switch focus from one input to the next one, we don't need
to invalidate the whole chain up to the body, but it'd be enough
to just do it up to the common ancestor of the old and new focused elements
(in this example the form).

To achieve that Document::SetFocusedElement() has been modified to look
for the common ancestor and then pass it to
This new helper method is also used from FocusController to set
:focus-within flag when the window loses/gains focus.

A new unit test has been added to AffectedByFocusTest.cpp
verifying that we only recalculate styles up to the common ancestor.

BUG= 617371 

Cr-Commit-Position: refs/heads/master@{#466949}


Comment 24 by, Apr 25 (2 days ago)
Common ancestor patch has landed, and I've added a new WPT test in: (still pending to review).

@rune do you think we're still missing something else?
Or could we mark the Runtime Flag as stable for shipping this in M60?
Thanks for your feedback.
Comment 25 by, Apr 25 (2 days ago)
IIUC, the test is about clearing :focus on elements becoming display:none, so it's not really directly a question about :focus-within as :focus-within is just kept in sync with :focus, right?

Comment 26 by, Yesterday (45 hours ago)
> IIUC, the test is about clearing :focus on elements becoming display:none, so it's not really directly a question about :focus-within as :focus-within is just kept in sync with :focus, right?

Yeah that's true, I've splitted it into 2 different tests. Anyway that should be reviewed and merged first on WPT and then they'll be imported into Blink.

@rune my question about enabling the runtime flag still stands. Are you missing anything else regarding :focus-within before we ship the feature? Thanks.
Comment 27 by, Yesterday (43 hours ago)
Nope, I think you can ship.
Project Member Comment 28 by, Today (16 hours ago)
The following revision refers to this bug:

commit 7638b8e164ca3abae4522f98a82bdb1c44b4b4cc
Author: rego <>
Date: Thu Apr 27 12:21:35 2017

[selectors4] Ship ":focus-within" pseudo-class


BUG= 617371 

Cr-Commit-Position: refs/heads/master@{#467646}


Comment 29 by, Today (15 hours ago)
Status: Fixed
Sign in to add a comment