From cf40f7305723650f3be3ffd48db478b7bafe795d Mon Sep 17 00:00:00 2001
From: Juanjo Tugores
Date: Thu, 5 Sep 2024 11:23:55 -0700
Subject: [PATCH] Fix unexpected ViewFocus events when Text Editing utilities
change focus in the middle of a blur call. (flutter/engine#54965)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
In [some cases][1], text editing utilities re-focus the `` element during a blur event. This causes an unusual sequence of `focusin` and `focusout` events, leading to the engine sending unintended events.
Consider the following HTML code:
```html
```
Clicking input1, then input2, then input3 produces the following console logs:
```
// Input1 is clicked
focusin: focus was gained by â
// Input2 is clicked
focusout: focus is leaving â and it will go to â
focusin: focus was gained by â
// Input3 is clicked
focusout: focus is leaving â and it will go to â
focusin: focus was gained by â
```
Now, let's add a blur handler that changes focus:
```html
```
The log sequence changes and gives the wrong impression that no dom element has focus:
```
// Input1 is clicked
focusin: focus was gained by â
// Input2 is clicked
focusout: focus is leaving â and it will go to â
focusin: focus was gained by â
// Input3 is clicked, but the handler kicks in and instead of the following line being a focusout, it results in a focusin call first.
focusin: focus was gained by â
focusout: focus is leaving â and it will go to null
```
In addition to that, during `focusout` processing, `activeElement` typically points to ``. However, if an element is focused during a `blur` event, `activeElement` points to that focused element. Although, undocumented it can be verified with:
```html
```
We leverage these behaviors to ignore `focusout` events when the document has focus but `activeElement` is not `