Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 8b49cd0

Browse files
author
nturgut
committed
removing changes on the input value. only keeping onscreen keyboard changes
1 parent 3ca9fe9 commit 8b49cd0

File tree

3 files changed

+103
-163
lines changed

3 files changed

+103
-163
lines changed

lib/web_ui/lib/src/engine.dart

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ part 'engine/text/word_break_properties.dart';
129129
part 'engine/text/word_breaker.dart';
130130
part 'engine/text_editing/autofill_hint.dart';
131131
part 'engine/text_editing/input_type.dart';
132+
part 'engine/text_editing/text_capitalization.dart';
132133
part 'engine/text_editing/text_editing.dart';
133134
part 'engine/util.dart';
134135
part 'engine/validators.dart';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
part of engine;
6+
7+
/// Controls the capitalization of the text.
8+
///
9+
/// This corresponds to Flutter's [TextCapitalization].
10+
///
11+
/// Uses `text-transform` css property.
12+
/// See: https://developer.mozilla.org/en-US/docs/Web/CSS/text-transform
13+
enum TextCapitalization {
14+
/// Uppercase for the first letter of each word.
15+
words,
16+
17+
/// Currently not implemented on Flutter Web. Uppercase for the first letter
18+
/// of each sentence.
19+
sentences,
20+
21+
/// Uppercase for each letter.
22+
characters,
23+
24+
/// Lowercase for each letter.
25+
none,
26+
}
27+
28+
/// Helper class for text capitalization.
29+
///
30+
/// Uses `text-transform` css property.
31+
/// See: https://developer.mozilla.org/en-US/docs/Web/CSS/text-transform
32+
class TextCapitalizationConfig {
33+
final TextCapitalization textCapitalization;
34+
35+
static final RegExp wordExp = new RegExp(r"(\w+)");
36+
static final RegExp whiteSpaceExp = new RegExp(r"(\s+)");
37+
38+
const TextCapitalizationConfig.defaultCapitalization()
39+
: textCapitalization = TextCapitalization.none;
40+
41+
// TODO: support sentence level text capitalization.
42+
TextCapitalizationConfig.fromInputConfiguration(String inputConfiguration)
43+
: this.textCapitalization =
44+
inputConfiguration == 'TextCapitalization.words'
45+
? TextCapitalization.words
46+
: (inputConfiguration == 'TextCapitalization.characters')
47+
? TextCapitalization.characters
48+
: TextCapitalization.none;
49+
50+
/// Sets `autocapitalize` attribute on input elements.
51+
///
52+
/// This attribute is only available for mobile browsers.
53+
///
54+
/// Note that in mobile browsers the onscreen keyboards provide sentence
55+
/// level capitalization as default as apposed to no capitalization on desktop
56+
/// browser.
57+
///
58+
/// See: https://developers.google.com/web/updates/2015/04/autocapitalize
59+
/// https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autocapitalize
60+
void setAutocapitalizeAttribute(html.HtmlElement domElement) {
61+
String autocapitalize = '';
62+
switch (textCapitalization) {
63+
case TextCapitalization.words:
64+
// TODO: There is a bug for `words` level capitalization in IOS now.
65+
// For now go back to default. Remove the check after bug is resolved.
66+
// https://bugs.webkit.org/show_bug.cgi?id=148504
67+
if(browserEngine == BrowserEngine.webkit) {
68+
autocapitalize = 'sentences';
69+
} else {
70+
autocapitalize = 'words';
71+
}
72+
break;
73+
case TextCapitalization.characters:
74+
autocapitalize = 'characters';
75+
break;
76+
case TextCapitalization.sentences:
77+
autocapitalize = 'sentences';
78+
break;
79+
case TextCapitalization.none:
80+
default:
81+
autocapitalize = 'off';
82+
break;
83+
}
84+
if (domElement is html.InputElement) {
85+
html.InputElement element = domElement;
86+
element.setAttribute('autocapitalize', autocapitalize);
87+
} else if (domElement is html.TextAreaElement) {
88+
html.TextAreaElement element = domElement;
89+
element.setAttribute('autocapitalize', autocapitalize);
90+
}
91+
}
92+
}

lib/web_ui/lib/src/engine/text_editing/text_editing.dart

+10-163
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class EngineAutofillForm {
116116
final Map<String, dynamic> autofillInfo = field['autofill'];
117117
final AutofillInfo autofill = AutofillInfo.fromFrameworkMessage(
118118
autofillInfo,
119-
textCapitalization: TextCapitalizationUtil.fromInputConfiguration(
119+
textCapitalization: TextCapitalizationConfig.fromInputConfiguration(
120120
field['textCapitalization']));
121121

122122
// The focused text editing element will not be created here.
@@ -241,7 +241,7 @@ class AutofillInfo {
241241
///
242242
/// On the other hand for the multi element forms, for the input elements
243243
/// other the focused field, we need to use this information.
244-
final TextCapitalizationUtil textCapitalization;
244+
final TextCapitalizationConfig textCapitalization;
245245

246246
/// Attribute used for autofill.
247247
///
@@ -251,8 +251,8 @@ class AutofillInfo {
251251
final String hint;
252252

253253
factory AutofillInfo.fromFrameworkMessage(Map<String, dynamic> autofill,
254-
{TextCapitalizationUtil textCapitalization =
255-
const TextCapitalizationUtil.defaultCapitalization()}) {
254+
{TextCapitalizationConfig textCapitalization =
255+
const TextCapitalizationConfig.defaultCapitalization()}) {
256256
assert(autofill != null); // ignore: unnecessary_null_comparison
257257
final String uniqueIdentifier = autofill['uniqueIdentifier']!;
258258
final List<dynamic> hintsList = autofill['hints'];
@@ -332,18 +332,18 @@ class EditingState {
332332
/// [domElement] can be a [InputElement] or a [TextAreaElement] depending on
333333
/// the [InputType] of the text field.
334334
factory EditingState.fromDomElement(html.HtmlElement? domElement,
335-
{TextCapitalizationUtil textCapitalization =
336-
const TextCapitalizationUtil.defaultCapitalization()}) {
335+
{TextCapitalizationConfig textCapitalization =
336+
const TextCapitalizationConfig.defaultCapitalization()}) {
337337
if (domElement is html.InputElement) {
338338
html.InputElement element = domElement;
339339
return EditingState(
340-
text: textCapitalization.capitializeTextValue(element.value),
340+
text: element.value,
341341
baseOffset: element.selectionStart,
342342
extentOffset: element.selectionEnd);
343343
} else if (domElement is html.TextAreaElement) {
344344
html.TextAreaElement element = domElement;
345345
return EditingState(
346-
text: textCapitalization.capitializeTextValue(element.value),
346+
text: element.value,
347347
baseOffset: element.selectionStart,
348348
extentOffset: element.selectionEnd);
349349
} else {
@@ -439,7 +439,7 @@ class InputConfiguration {
439439
inputAction = flutterInputConfiguration['inputAction'],
440440
obscureText = flutterInputConfiguration['obscureText'],
441441
autocorrect = flutterInputConfiguration['autocorrect'],
442-
textCapitalization = TextCapitalizationUtil.fromInputConfiguration(
442+
textCapitalization = TextCapitalizationConfig.fromInputConfiguration(
443443
flutterInputConfiguration['textCapitalization']),
444444
autofill = flutterInputConfiguration.containsKey('autofill')
445445
? AutofillInfo.fromFrameworkMessage(
@@ -471,7 +471,7 @@ class InputConfiguration {
471471

472472
final EngineAutofillForm? autofillGroup;
473473

474-
final TextCapitalizationUtil textCapitalization;
474+
final TextCapitalizationConfig textCapitalization;
475475
}
476476

477477
typedef _OnChangeCallback = void Function(EditingState? editingState);
@@ -626,8 +626,6 @@ abstract class DefaultTextEditingStrategy implements TextEditingStrategy {
626626
domElement.setAttribute('type', 'password');
627627
}
628628

629-
inputConfig.textCapitalization.setStyleAttribute(domElement);
630-
631629
inputConfig.autofill?.applyToDomElement(domElement, focusedElement: true);
632630

633631
final String autocorrectValue = inputConfig.autocorrect! ? 'on' : 'off';
@@ -1489,154 +1487,3 @@ class EditableTextGeometry {
14891487
..transform = cssTransform;
14901488
}
14911489
}
1492-
1493-
/// Controls the capitalization of the text.
1494-
///
1495-
/// This corresponds to Flutter's [TextCapitalization].
1496-
///
1497-
/// Uses `text-transform` css property.
1498-
/// See: https://developer.mozilla.org/en-US/docs/Web/CSS/text-transform
1499-
enum TextCapitalization {
1500-
/// Uppercase for the first letter of each word.
1501-
words,
1502-
1503-
/// Currently not implemented on Flutter Web. Uppercase for the first letter
1504-
/// of each sentence.
1505-
sentences,
1506-
1507-
/// Uppercase for each letter.
1508-
characters,
1509-
1510-
/// Lowercase for each letter.
1511-
none,
1512-
}
1513-
1514-
/// Helper class for text capitalization.
1515-
///
1516-
/// Uses `text-transform` css property.
1517-
/// See: https://developer.mozilla.org/en-US/docs/Web/CSS/text-transform
1518-
class TextCapitalizationUtil {
1519-
final TextCapitalization textCapitalization;
1520-
1521-
static final RegExp wordExp = new RegExp(r"(\w+)");
1522-
static final RegExp whiteSpaceExp = new RegExp(r"(\s+)");
1523-
1524-
const TextCapitalizationUtil.defaultCapitalization()
1525-
: textCapitalization = TextCapitalization.none;
1526-
1527-
// TODO: support sentence level text capitalization.
1528-
TextCapitalizationUtil.fromInputConfiguration(String inputConfiguration)
1529-
: this.textCapitalization =
1530-
inputConfiguration == 'TextCapitalization.words'
1531-
? TextCapitalization.words
1532-
: (inputConfiguration == 'TextCapitalization.characters')
1533-
? TextCapitalization.characters
1534-
: TextCapitalization.none;
1535-
1536-
void setStyleAttribute(html.HtmlElement domElement) {
1537-
final html.CssStyleDeclaration elementStyle = domElement.style;
1538-
1539-
switch (textCapitalization) {
1540-
case TextCapitalization.words:
1541-
elementStyle.textTransform = 'capitalize';
1542-
break;
1543-
case TextCapitalization.characters:
1544-
elementStyle.textTransform = 'uppercase';
1545-
break;
1546-
case TextCapitalization.sentences:
1547-
case TextCapitalization.none:
1548-
default:
1549-
elementStyle.textTransform = 'lowercase';
1550-
break;
1551-
}
1552-
}
1553-
1554-
/// Change the capitalization of the focused text field's value depending on
1555-
/// the [TextCapitalization].
1556-
///
1557-
/// For [TextCapitalization.words], this method makes all first letter of each
1558-
/// word uppercase.
1559-
///
1560-
/// For [TextCapitalization.characters], this method makes all letters of each
1561-
/// word uppercase.
1562-
///
1563-
/// For [TextCapitalization.sentence] is not supported for now.
1564-
String capitializeTextValue(String value) {
1565-
if (value.isEmpty) {
1566-
return value;
1567-
}
1568-
switch (textCapitalization) {
1569-
case TextCapitalization.words:
1570-
final Iterable<RegExpMatch> wordMatches = wordExp.allMatches(value);
1571-
final List<String> words = wordMatches.map((RegExpMatch match) {
1572-
final String? word = match.group(0);
1573-
return (word == null) ? '' : word;
1574-
}).toList();
1575-
final Iterable<RegExpMatch> whiteSpaceMatches =
1576-
whiteSpaceExp.allMatches(value);
1577-
final List<String> whiteSpaces =
1578-
whiteSpaceMatches.map((RegExpMatch match) {
1579-
final String? word = match.group(0);
1580-
return (word == null) ? '' : word;
1581-
}).toList();
1582-
final StringBuffer textValueBuffer = new StringBuffer();
1583-
for (int i = 0; i < words.length && !words[i].isEmpty; i++) {
1584-
final String word = words[i];
1585-
textValueBuffer.write('${word[0].toUpperCase()}${word.substring(1)}');
1586-
if (whiteSpaces.length > i) {
1587-
textValueBuffer.write(whiteSpaces[i]);
1588-
}
1589-
}
1590-
return textValueBuffer.toString();
1591-
case TextCapitalization.characters:
1592-
return value.toUpperCase();
1593-
case TextCapitalization.sentences:
1594-
case TextCapitalization.none:
1595-
default:
1596-
return value;
1597-
}
1598-
}
1599-
1600-
/// Sets `autocapitalize` attribute on input elements.
1601-
///
1602-
/// This attribute is only available for mobile browsers.
1603-
///
1604-
/// Note that in mobile browsers the onscreen keyboards provide sentence
1605-
/// level capitalization as default as apposed to no capitalization on desktop
1606-
/// browser.
1607-
///
1608-
/// See: https://developers.google.com/web/updates/2015/04/autocapitalize
1609-
/// https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autocapitalize
1610-
void setAutocapitalizeAttribute(html.HtmlElement domElement) {
1611-
String autocapitalize = '';
1612-
switch (textCapitalization) {
1613-
case TextCapitalization.words:
1614-
// TODO: There is a bug for `words` level capitalization in IOS now.
1615-
// For now go back to default. Remove the check after bug is resolved.
1616-
// https://bugs.webkit.org/show_bug.cgi?id=148504
1617-
if(browserEngine == BrowserEngine.webkit) {
1618-
autocapitalize = 'sentences';
1619-
} else {
1620-
autocapitalize = 'words';
1621-
}
1622-
break;
1623-
case TextCapitalization.characters:
1624-
autocapitalize = 'characters';
1625-
break;
1626-
case TextCapitalization.sentences:
1627-
autocapitalize = 'sentences';
1628-
break;
1629-
case TextCapitalization.none:
1630-
default:
1631-
autocapitalize = 'off';
1632-
break;
1633-
}
1634-
if (domElement is html.InputElement) {
1635-
html.InputElement element = domElement;
1636-
element.setAttribute('autocapitalize', autocapitalize);
1637-
} else if (domElement is html.TextAreaElement) {
1638-
html.TextAreaElement element = domElement;
1639-
element.setAttribute('autocapitalize', autocapitalize);
1640-
}
1641-
}
1642-
}

0 commit comments

Comments
 (0)