Skip to content

Define timing of focus fixup rule precisely when triggered by style changes. #8225

Closed

Description

@emilio

I was looking into implementing the focus fixup rule in Gecko, and per spec it's not clear when it should happen when it happens as a response to style changes.

Consider this testcase:

<!doctype html>
<input>
<script>
onload = function() {
  let input = document.querySelector("input");
  input.focus();
  console.log("focus", document.activeElement);
  input.style.visibility = "hidden";
  console.log("style tweak", document.activeElement);
  input.getBoundingClientRect();
  console.log("gbcr", document.activeElement);
  requestAnimationFrame(() => requestAnimationFrame(() => {
    console.log("raf", document.activeElement);
  }));
}
</script>

Naively, reading the current spec, I'd expect the element to be blurred after the style update (basically, as a result of getBoundingClientRect()). It seems Blink at least does it at a well defined point in the "update the rendering" steps (which is reasonable too).

I thought WebKit implemented this but at least the Safari version I have access to behaves like Firefox.

I think I'd be fine making this a proper step in the "update the rendering", it seems odd that getBoundingClientRect() would fire a blur event.

@mfreed7 / @josepharhar, it seems blink sometimes applies the focus fixup rule sync (when making the form control disabled for example), but in the case of style changes it does it async. I think I'd rather have a single point in update-the-rendering where this happens but I'm curious if there's any reason to do it sync sometimes. Also I wonder when are you doing it r/n?

Activity

  1. added
    topic: focus
    on Aug 27, 2022
  2. emilio commented on Aug 27, 2022

    @emilio
    ContributorAuthor

    @nt1m / @rniwa, do you know the status of this in WebKit? I recall seeing some patches in flight to implement this long ago, but it doesn't seem to work, was it reverted or something?

  3. nt1m commented on Aug 27, 2022

    @nt1m
    Member

    I think I tried to implement this in the update rendering steps: https://bugs.webkit.org/show_bug.cgi?id=237273#c1 , it's the easiest way (since you don't have to find every single place where "is this element focusable?" changes and hook it up there).

    It works, although the spec implies we should do synchronously and not async, so I just left it there for now.

  4. nt1m commented on Aug 27, 2022

    @nt1m
    Member

    (As for the current state, we don't implement the focus fixup rule for most part, aside for finding the right element to focus when showing/hiding a dialog)

  5. mfreed7 commented on Aug 29, 2022

    @mfreed7
    Contributor

    See this Chromium bug: https://bugs.chromium.org/p/chromium/issues/detail?id=1275097. It's currently done in a semi-hacky way, e.g. with timers.

    @Loirooriol tried to fix this up at one point and make it synchronous, so perhaps he has more comments.

    It feels odd to do this in "update the rendering", but that is likely the most performant place to do it. I'm afraid of the patch above - it'll likely slow things down considerably, since we'll be forcing style updates more often.

  6. Loirooriol commented on Aug 29, 2022

    @Loirooriol
    Contributor

    Yeah I tried to make it synchronous, but since style changes can affect focusability, then things like document.activeElement, document.querySelector(":focus"), etc. need to flush styles, which seems bad for perf. And several tests failed, so probably not web compatible, but I don't remember the details.

    Then I agree that doing it in "update the rendering" seems more reasonable.

  7. added
    agenda+
    on Sep 1, 2022
  8. emilio commented on Sep 1, 2022

    @emilio
    ContributorAuthor

    So, there seemed to be consensus today on doing this at some point during update the rendering. I sent a patch updating the tests here: https://bugzilla.mozilla.org/show_bug.cgi?id=1788741

    However, that uncovers a case where all browsers are interoperable in removing focus synchronously, which is focused element removal.

    Not sure to what extent we want that to be special, do we want to keep that behavior? It might be reasonable.

  9. removed
    agenda+
    on Sep 1, 2022
  10. mfreed7 commented on Sep 2, 2022

    @mfreed7
    Contributor

    So, there seemed to be consensus today on doing this at some point during update the rendering. I sent a patch updating the tests here: https://bugzilla.mozilla.org/show_bug.cgi?id=1788741

    However, that uncovers a case where all browsers are interoperable in removing focus synchronously, which is focused element removal.

    Not sure to what extent we want that to be special, do we want to keep that behavior? It might be reasonable.

    Interestingly, we just discussed this case w.r.t. the pop-up API.

    I'm in favor of keeping that behavior (synchronously fire events for focused element removal), given that it's interoperable, and changing it scares me.

  11. Loirooriol commented on Sep 2, 2022

    @Loirooriol
    Contributor

    Worth noting that Blink also blurs synchronously when losing the tabindex:

    <!DOCTYPE html>
    <div id="test" tabindex="1"></div>
    <script>
    test.focus();
    console.log(document.activeElement); // test
    test.removeAttribute("tabindex");
    console.log(document.activeElement); // document.body
    </script>

    But no interoperability here so changing may be fine.

  12. added a commit that references this issue on Oct 16, 2022
    14eb00c
  13. added a commit that references this issue on Nov 4, 2022
    c4533d4
  14. added a commit that references this issue on Jan 18, 2023
    6a5109c
  15. added a commit that references this issue on Jan 27, 2023
    e233336
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: focus

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions