Skip to content

Commit 0414680

Browse files
authored
[web] Stop iOS auto-zoom by sizing semantic inputs at 16px (flutter#192328)
Fixes flutter#192327 and flutter#129233. **Problem** 1. The semantic `<input>` and `<textarea>` never get a font size, so they fall back to the browser default, 11px on iOS. iOS auto-zooms any focused editable element under 16px, so tapping a `TextField` zoomed the page to 1.45x and left it there. 2. The framework's own text style never reached the element either. It arrives before the element is attached, so it is stored, and `activate()` replayed it before enabling the strategy, which made `updateElementStyle` skip writing it. Font size, family, weight and spacing were all dropped. That is flutter#129233. **Fix** Set the size on the element itself, to `max(frameworkFontSize, 16)` on iOS, at element creation and after every style sync. It cannot live in the global stylesheet because a later style update writes the framework font as an inline `font` shorthand, which outranks any stylesheet rule. It assigns rather than only raises because on the first-focus path that shorthand is skipped and nothing else sets a size, so raising alone would leave a 24px field at 16px. Nothing visible changes: the semantics tree is painted at `filter: opacity(0%)`. Guarded by `isIosSafari`; Android was device-tested and does not auto-zoom. **Demo** | | | |---|---| | Before | https://flutter-demo-50-before.web.app | | After | https://flutter-demo-50-after.web.app | **iOS auto-zoom.** On an iPhone with semantics enabled, tap the **12px field**. A default Material field is already 16px and cannot trigger the bug. Before, `vvScale` jumps to 1.45 and stays until you pinch out. After, it stays 1.00. **flutter#129233.** In desktop Chrome, click into the **24px field**, then run: ```js [...document.querySelectorAll('flt-semantics-host input, flt-semantics-host textarea')] .map((e, i) => `${i} inline=${e.style.fontSize || 'NONE'} computed=${getComputedStyle(e).fontSize}`) .join('\n') ``` Index 3 is the 24px field. Before: `inline=NONE computed=13.3333px`. After: `inline=24px computed=24px`. Only the focused field carries a style, so the others correctly report `NONE`.
1 parent 6af46e7 commit 0414680

2 files changed

Lines changed: 182 additions & 1 deletion

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/semantics/text_field.dart‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,51 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:math' as math;
6+
57
import 'package:ui/ui.dart' as ui;
68
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;
79

10+
import '../browser_detection.dart';
811
import '../dom.dart';
912
import '../platform_dispatcher.dart';
1013
import '../text_editing/input_type.dart';
1114
import '../text_editing/text_editing.dart';
1215
import 'semantics.dart';
1316

17+
/// The smallest font size that does not make iOS zoom the page.
18+
///
19+
/// iOS zooms when it focuses an editable element whose font size is below this,
20+
/// and leaves the page zoomed afterwards.
21+
///
22+
/// See: https://github.com/flutter/flutter/issues/192327
23+
const double _iosMinimumEditableFontSize = 16.0;
24+
25+
/// Raises [element]'s font size to [_iosMinimumEditableFontSize] on iOS when
26+
/// [frameworkFontSize] is smaller than that, or absent.
27+
///
28+
/// iOS zooms the whole page when it focuses an editable element below the
29+
/// threshold, and leaves it zoomed. The element is invisible, so enlarging a
30+
/// small size changes nothing the user sees, and a larger framework size is kept
31+
/// as it is.
32+
///
33+
/// Called from two places. At element creation there is no framework style yet
34+
/// and the element would fall back to the browser default, 11px on iOS. After
35+
/// each style sync the framework size has just been written by
36+
/// [EditableTextStyle.applyToDomElement] and may be below the threshold again.
37+
///
38+
/// Assigning `font-size` as a longhand after that inline `font` shorthand
39+
/// overrides only the size and leaves the rest of the shorthand alone. It also
40+
/// has to be set on the element rather than in the global stylesheet, because an
41+
/// inline declaration outranks any stylesheet rule.
42+
void _applyIosMinimumFontSize(DomHTMLElement element, double? frameworkFontSize) {
43+
if (!isIosSafari) {
44+
return;
45+
}
46+
final double effectiveFontSize = math.max(_iosMinimumEditableFontSize, frameworkFontSize ?? 0.0);
47+
element.style.fontSize = '${effectiveFontSize}px';
48+
}
49+
1450
/// Text editing used by accesibility mode.
1551
///
1652
/// [SemanticsTextEditingStrategy] assumes the caller will own the creation,
@@ -90,8 +126,11 @@ class SemanticsTextEditingStrategy extends DefaultTextEditingStrategy {
90126

91127
activeTextField = textField;
92128
domElement = textField.editableElement;
93-
_syncStyle();
129+
// Enable before syncing the style.
130+
// [DefaultTextEditingStrategy.updateElementStyle] only writes to the element
131+
// once editing is enabled, so syncing first drops the style silently.
94132
super.enable(inputConfig!, onChange: onChange!, onAction: onAction!);
133+
_syncStyle();
95134
}
96135

97136
/// Detaches the DOM element owned by [textField] from this text editing
@@ -253,6 +292,7 @@ class SemanticsTextEditingStrategy extends DefaultTextEditingStrategy {
253292
return;
254293
}
255294
super.updateElementStyle(_queuedStyle!);
295+
_applyIosMinimumFontSize(activeDomElement, _queuedStyle!.fontSize);
256296
}
257297
}
258298

@@ -345,6 +385,12 @@ class SemanticTextField extends SemanticRole {
345385
..left = '0'
346386
..width = '${semanticsObject.rect!.width}px'
347387
..height = '${semanticsObject.rect!.height}px';
388+
389+
// No framework style has arrived yet, so the element falls back to the
390+
// browser default, which is under the threshold. Seed the floor so a focus
391+
// landing before the first style cannot zoom the page.
392+
_applyIosMinimumFontSize(editableElement, null);
393+
348394
append(editableElement);
349395

350396
editableElement.addEventListener(

‎engine/src/flutter/lib/web_ui/test/engine/semantics/text_field_test.dart‎

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,8 @@ void testMain() {
917917
expect(textField.editableElement.getAttribute('form'), isNull);
918918
}, skip: !ui_web.browser.isSafari);
919919
});
920+
921+
_testIosMinimumFontSize();
920922
}
921923

922924
/// Builds the `fields` list of a `TextInputConfiguration` autofill group, the
@@ -995,6 +997,139 @@ SemanticsObject createTextFieldSemantics({
995997
return tester.getSemanticsObject(0);
996998
}
997999

1000+
// Covers both orders in which the framework style and the semantic element can
1001+
// arrive: the style queued before the element is activated, and the style
1002+
// updated after it is already active.
1003+
// See: https://github.com/flutter/flutter/issues/192327
1004+
void _testIosMinimumFontSize() {
1005+
group('$SemanticTextField iOS minimum font size', () {
1006+
late HybridTextEditing testTextEditing;
1007+
late SemanticsTextEditingStrategy strategy;
1008+
1009+
setUp(() {
1010+
// Must be set before the element is created, because the floor is seeded
1011+
// at creation time.
1012+
debugEmulateIosSafari = true;
1013+
testTextEditing = HybridTextEditing();
1014+
SemanticsTextEditingStrategy.ensureInitialized(testTextEditing);
1015+
strategy = SemanticsTextEditingStrategy.instance;
1016+
testTextEditing.debugTextEditingStrategyOverride = strategy;
1017+
testTextEditing.configuration = singlelineConfig;
1018+
semantics()
1019+
..debugOverrideTimestampFunction(() => _testTime)
1020+
..semanticsEnabled = true;
1021+
});
1022+
1023+
tearDown(() {
1024+
if (strategy.isEnabled) {
1025+
strategy.disable();
1026+
}
1027+
cleanForms();
1028+
debugEmulateIosSafari = false;
1029+
semantics().semanticsEnabled = false;
1030+
domDocument.activeElement?.blur();
1031+
});
1032+
1033+
EditableTextStyle styleWithFontSize(double fontSize) => EditableTextStyle(
1034+
textDirection: ui.TextDirection.ltr,
1035+
fontSize: fontSize,
1036+
textAlign: ui.TextAlign.left,
1037+
fontFamily: 'Arial',
1038+
fontWeight: 'normal',
1039+
letterSpacing: null,
1040+
wordSpacing: null,
1041+
lineHeight: null,
1042+
);
1043+
1044+
test('seeds a newly created input with the minimum', () {
1045+
final SemanticsObject node = createTextFieldSemantics(value: 'hello');
1046+
final textField = node.semanticRole! as SemanticTextField;
1047+
1048+
expect(textField.editableElement.style.fontSize, '16px');
1049+
});
1050+
1051+
test('seeds a newly created textarea with the minimum', () {
1052+
testTextEditing.configuration = multilineConfig;
1053+
final SemanticsObject node = createTextFieldSemantics(value: 'hello', isMultiline: true);
1054+
final textField = node.semanticRole! as SemanticTextField;
1055+
1056+
expect(textField.editableElement.tagName.toLowerCase(), 'textarea');
1057+
expect(textField.editableElement.style.fontSize, '16px');
1058+
});
1059+
1060+
test('raises a framework font below the minimum', () {
1061+
strategy.enable(singlelineConfig, onChange: (_, _) {}, onAction: (_) {});
1062+
final SemanticsObject node = createTextFieldSemantics(value: 'hello', isFocused: true);
1063+
final textField = node.semanticRole! as SemanticTextField;
1064+
1065+
strategy.updateElementStyle(styleWithFontSize(12));
1066+
1067+
// The framework shorthand has just written 12px onto the element. Without
1068+
// the floor that value persists past blur and a later refocus would zoom.
1069+
expect(textField.editableElement.style.fontSize, '16px');
1070+
});
1071+
1072+
// Production order: the framework sends the style before `show`, so it is
1073+
// queued while the element is still detached and replayed at activation.
1074+
test('raises a queued framework font below the minimum', () {
1075+
strategy.enable(singlelineConfig, onChange: (_, _) {}, onAction: (_) {});
1076+
strategy.updateElementStyle(styleWithFontSize(12));
1077+
1078+
final SemanticsObject node = createTextFieldSemantics(value: 'hello', isFocused: true);
1079+
final textField = node.semanticRole! as SemanticTextField;
1080+
1081+
expect(textField.editableElement.style.fontSize, '16px');
1082+
});
1083+
1084+
test('preserves a queued framework font above the minimum', () {
1085+
strategy.enable(singlelineConfig, onChange: (_, _) {}, onAction: (_) {});
1086+
strategy.updateElementStyle(styleWithFontSize(24));
1087+
1088+
final SemanticsObject node = createTextFieldSemantics(value: 'hello', isFocused: true);
1089+
final textField = node.semanticRole! as SemanticTextField;
1090+
1091+
expect(textField.editableElement.style.fontSize, '24px');
1092+
// The floor sets `font-size` as a longhand after the framework's `font`
1093+
// shorthand. Assert a sibling property to catch a regression that reached
1094+
// for the shorthand instead and wiped the rest of the style.
1095+
expect(textField.editableElement.style.fontFamily, contains('Arial'));
1096+
expect(textField.editableElement.style.fontWeight, 'normal');
1097+
});
1098+
1099+
test('keeps a framework font above the minimum', () {
1100+
strategy.enable(singlelineConfig, onChange: (_, _) {}, onAction: (_) {});
1101+
final SemanticsObject node = createTextFieldSemantics(value: 'hello', isFocused: true);
1102+
final textField = node.semanticRole! as SemanticTextField;
1103+
1104+
strategy.updateElementStyle(styleWithFontSize(24));
1105+
1106+
expect(textField.editableElement.style.fontSize, '24px');
1107+
});
1108+
});
1109+
1110+
group('$SemanticTextField non-iOS font size', () {
1111+
setUp(() {
1112+
semantics()
1113+
..debugOverrideTimestampFunction(() => _testTime)
1114+
..semanticsEnabled = true;
1115+
});
1116+
1117+
tearDown(() {
1118+
semantics().semanticsEnabled = false;
1119+
domDocument.activeElement?.blur();
1120+
});
1121+
1122+
// Only the iOS minimum is platform-gated. A framework style still reaches
1123+
// the element everywhere; this field is never given one.
1124+
test('does not apply the minimum', () {
1125+
final SemanticsObject node = createTextFieldSemantics(value: 'hello');
1126+
final textField = node.semanticRole! as SemanticTextField;
1127+
1128+
expect(textField.editableElement.style.fontSize, isEmpty);
1129+
});
1130+
}, skip: isIosSafari);
1131+
}
1132+
9981133
/// Emulates sending of a message by the framework to the engine.
9991134
void sendFrameworkMessage(ByteData? message, HybridTextEditing testTextEditing) {
10001135
testTextEditing.channel.handleTextInput(message, (ByteData? data) {});

0 commit comments

Comments
 (0)