Thanks to visit codestin.com
Credit goes to phabricator.services.mozilla.com

Page MenuHomePhabricator

Bug 1240285 - Forward focus only if label is not focusable r=edgar
ClosedPublic

Authored by dgraham on Mar 20 2020, 5:53 PM.
Tags
None
Referenced Files
Unknown Object (File)
Nov 17 2025, 4:42 PM
Unknown Object (File)
Oct 16 2025, 1:11 PM
Unknown Object (File)
Jun 24 2025, 8:47 PM
Unknown Object (File)
May 30 2025, 4:24 PM
Unknown Object (File)
May 29 2025, 11:25 PM
Unknown Object (File)
May 29 2025, 7:35 PM
Unknown Object (File)
May 29 2025, 4:56 PM
Unknown Object (File)
May 28 2025, 1:29 AM
Tokens
"Yellow Medal" token, awarded by keithamus."Dat Boi" token, awarded by muan."Burninate" token, awarded by koddsson.

Details

Summary

Focus should remain on the label rather than forwarded to the labeled
element.

Co-Authored-By: Mu-An Chiou <[email protected]>

Diff Detail

Event Timeline

phab-bot changed the visibility from "Custom Policy" to "Public (No Login Required)".
phab-bot changed the edit policy from "Custom Policy" to "Restricted Project (Project)".
phab-bot removed a project: secure-revision.
mats added a subscriber: mats.

This patch seems to regress Testcase #3. The current behavior when clicking the button is to focus the target text field (in both Firefox and Chrome). With this patch, the button is focused and nothing happens after that.
Did this patch pass all tests on Try? If so, please add a test that would have detected that regression. Please also add some tests for the bug you're fixing (i.e. with a tabindex on the label)

This revision now requires changes to proceed.Mar 20 2020, 7:40 PM

Looking at chromium code it seems we should still try to focus the control if the label is not focusable itself, or such: https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/html/forms/html_label_element.cc;l=234;drc=ee4cff87f02e46e1fbbdaef0aa123e05761b35e8. WebKit has similar code.

So we should probably keep the override but do something like:

{
  nsIFrame* frame = GetPrimaryFrame(FlushType::Frames));
  if (frame && frame->IsFocusable()) {
    return nsGenericHTMLElement::Focus(aOptions, aCallerType, aError);
  }
}

if (RefPtr<Element> elem = GetLabeledElement()) {
  return elem->Focus(aOptions, aCallerType, aError);
}

Or such.

I pushed this to try on behalf of Mu-an. Hopefully there are some pre-existing tests that start failing with this change. https://treeherder.mozilla.org/#/jobs?repo=try&revision=7513d59996433b09d6a38e60bdcc8fa4b4e0ec3e

Happy to help pushing an updated patch or so on.

@emilio We applied your suggestion and added WPT tests for the expected focus forwarding behavior. The test for an empty tabindex attribute currently fails in Firefox and passes in Chrome.

Can you push this branch again to Try? Thanks for your help!

Thanks @edgar!

That looks pretty good. The only thing that's making things orange and are not flaky is this test you're adding (the empty tabindex subtest).

Per the discussion in matrix, it seems this is an expected failure and Firefox shouldn't consider empty tabindex as focusable. If so what needs to get done is to annotate this test as failing, pointing to a bug.

This would be a matter of creating a file called testing/web-platform/meta/html/semantics/forms/the-label-element/forward-focus-to-associated-element.html.ini with something like this as contents:

[forward-focus-to-associated-element.html]
  [focusing a label with empty tabindex should forward focus to the labelable element]
    expected: FAIL
    bug: <link-to-relevant-bug>

That should make the test pass locally when you do ./mach wpt testing/web-platform/tests/html/semantics/forms/the-label-element/forward-focus-to-associated-element.html (and also on automation).

With that I think this should be good to land, though I suggested the implementation so @edgar should probably be the one to stamp it if it looks good to him :)

We added the expected test failure file and the label tests pass locally. According to the HTML spec, invalid tabindex values—non-integer or empty—fall back to platform focus behavior, and labels are not focusable.

The user agent should follow platform conventions to determine if the element should be considered as a focusable area

https://html.spec.whatwg.org/#the-tabindex-attribute

We found this report describing the tabindex focus issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1128054.

Thanks again for all of your help getting this branch merged.

LGTM, r+ with comment addressed.
@dgraham, thanks for the patch.
@emilio, @mats, thanks for the comments.

testing/web-platform/tests/html/semantics/forms/the-label-element/forward-focus-to-associated-element.html
39

Could you also add a check for label that shouldn't get the focus, something like other tests do:
label.addEventListener("focus", t.unreached_func("Label should not receive focus"));

49

Here too.

dgraham added inline comments.
testing/web-platform/tests/html/semantics/forms/the-label-element/forward-focus-to-associated-element.html
39

We added these assertions to the latest amended commit.

Thank you! Removing mats from the reviewer list because his feedback was addressed.

Do you have commit access? If not, you can add the "check-in needed" tag, and it'll get pushed for you, or I can push it too if you want.

This revision is now accepted and ready to land.Mar 25 2020, 5:27 PM

Also the commit message should be adjusted as we're no longer just removing it.

dgraham retitled this revision from Bug 1240285 - Remove label focus overload. r=edgar to Bug 1240285 - Forward focus only if label is not focusable r=edgar.
dgraham marked an inline comment as done.