Repository navigation
Conversation
When the framework inserts a semantics node above an existing one, the engine moves the existing node's DOM element into the new node with append/insertBefore. Chrome fires `blur` on a focused element that is removed from the document, so a focused text field below it is blurred, the text input connection is closed and the framework unfocuses the field. Move already connected children with `moveBefore` when the browser supports it. It moves the element without blurring it. Fixes flutter#195289
There was a problem hiding this comment.
Code Review
This pull request introduces the insertOrMoveBefore helper method to DomNode to allow moving DOM nodes without losing focus on their descendants, and integrates it into SemanticsObject child updates. It also adds a regression test to verify that inserting or removing ancestor nodes does not blur a focused text field. The review feedback suggests simplifying boolean checks in the new helper method to align with Effective Dart guidelines, and conditionally asserting the blur count in the test to handle environments that do not support moveBefore.
| /// the browser supports it. Unlike a remove-and-insert, `moveBefore` keeps the | ||
| /// focus inside [node], so a focused descendant is not blurred. | ||
| void insertOrMoveBefore(DomNode node, DomNode? referenceNode) { | ||
| if (node.isConnected == true && isConnected == true && (this as JSObject).has('moveBefore')) { |
There was a problem hiding this comment.
According to the Effective Dart style guide, boolean expressions should not be compared to true or false directly. Since isConnected is a non-nullable boolean, we can simplify this condition to improve readability and adhere to the style guide.
if (node.isConnected && isConnected && (this as JSObject).has('moveBefore')) {References
- Effective Dart: Style guide recommends not comparing boolean expressions to true or false. (link)
There was a problem hiding this comment.
isConnected is declared as external bool? get isConnected; on DomNode, so the == true is needed: node.isConnected && isConnected would not compile.
|
|
||
| // Insert node 3 between node 0 and node 1, as the framework does when a | ||
| // new sibling (e.g. an overlay or a tooltip) appears next to a route. | ||
| pumpSemantics(withNode3: true); | ||
|
|
||
| expect(blurCount, 0); | ||
| expect(domDocument.activeElement, input); | ||
|
|
||
| // Remove node 3, moving node 1 back under node 0. | ||
| pumpSemantics(withNode3: false); | ||
|
|
||
| expect(blurCount, 0); | ||
| expect(domDocument.activeElement, input); |
There was a problem hiding this comment.
To ensure the test is robust and does not fail when run in environments or browsers that do not yet support moveBefore (where Chrome would naturally trigger a blur event), consider conditionally asserting blurCount == 0 only when moveBefore is supported.
final bool supportsMoveBefore = (domDocument.body! as JSObject).has('moveBefore');
// Insert node 3 between node 0 and node 1, as the framework does when a
// new sibling (e.g. an overlay or a tooltip) appears next to a route.
pumpSemantics(withNode3: true);
if (supportsMoveBefore) {
expect(blurCount, 0);
}
expect(domDocument.activeElement, input);
// Remove node 3, moving node 1 back under node 0.
pumpSemantics(withNode3: false);
if (supportsMoveBefore) {
expect(blurCount, 0);
}
expect(domDocument.activeElement, input);There was a problem hiding this comment.
I would rather keep this assertion unconditional. The web tests pin Chrome 145 (dev/package_lock.yaml), which has moveBefore (since 133), and Chrome is the browser where insertBefore blurs the moved input, so this assertion is the regression check for #195289. Gating it on moveBefore support would make the test pass without checking anything on a browser that lacks it. If the Firefox or Safari runs disagree, I will handle them explicitly.
flutter-zl
left a comment
There was a problem hiding this comment.
LGTM, thanks for the fix!
I verified it with a before/after demo of the issue's scenario, built against the PR's engine changes:
- Before: https://flutter-demo-97-before.web.app — the field blurs and typing stops when the node is inserted.
- After: https://flutter-demo-97-after.web.app — the field keeps focus, 0 blur events, typing continues
Safari has no moveBefore and drops the focus of a moved descendant without firing blur, so keystrokes are lost. Restore the focus after insertBefore.
|
Update after Flutter 3.50.0, in a real app with semantics on. The case that led me here, The sample in #195289 still reproduces on 3.50.0, so this PR is still needed. With
So the description was wrong about Safari. 8f533f9 adds a fallback for browsers without Local runs of |
With semantics enabled, a focused text field loses focus when the framework inserts a semantics node above it (for example when a tooltip or an
OverlayPortalchild appears next to a route on a nested navigator).SemanticsObject.updateChildrenmoves the existing child's element into the new node withappend/insertBefore. Chrome firesbluron a focused element removed from the document, so the text input connection is closed and the framework unfocuses the field. Typing stops, and a double-click in a search field with autocomplete options never shows the selected word.This PR moves children that are already in the document with
moveBeforewhen the browser supports it, which keeps the focus. Browsers withoutmoveBefore(Safari) keep usinginsertBeforeand get the focus back withfocusWithoutScroll()after the move: Safari fires noblurbut still movesdocument.activeElementto<body>, so keystrokes were lost there too.Before/after, with the sample from the issue (type, wait for the node to be inserted after 5 s, type again):
abc,document.activeElementis<body>abcdef,document.activeElementis the<input>Tests: added
does not blur a focused text field when a node is inserted above or removed from its ancestorsandkeeps the focus of a text field moved without moveBefore(deletesElement.prototype.moveBeforefor the test) tosemantics_test.dart. Both fail without the change and pass with it, in Chrome and Safari.test/engine/semantics/andtext_editing_test.dartpass locally in Chrome (dart2js, CanvasKit).Fixes #195289
Pre-launch Checklist
///).