@@ -305,17 +305,19 @@ class EditingState {
305
305
///
306
306
/// [domElement] can be a [InputElement] or a [TextAreaElement] depending on
307
307
/// the [InputType] of the text field.
308
- factory EditingState .fromDomElement (html.HtmlElement ? domElement) {
308
+ factory EditingState .fromDomElement (html.HtmlElement ? domElement,
309
+ {TextCapitalizationUtil textCapitalization =
310
+ const TextCapitalizationUtil .defaultCapitalization ()}) {
309
311
if (domElement is html.InputElement ) {
310
312
html.InputElement element = domElement;
311
313
return EditingState (
312
- text: element.value,
314
+ text: textCapitalization. capitializeTextValue ( element.value) ,
313
315
baseOffset: element.selectionStart,
314
316
extentOffset: element.selectionEnd);
315
317
} else if (domElement is html.TextAreaElement ) {
316
318
html.TextAreaElement element = domElement;
317
319
return EditingState (
318
- text: element.value,
320
+ text: textCapitalization. capitializeTextValue ( element.value) ,
319
321
baseOffset: element.selectionStart,
320
322
extentOffset: element.selectionEnd);
321
323
} else {
@@ -706,7 +708,8 @@ abstract class DefaultTextEditingStrategy implements TextEditingStrategy {
706
708
void _handleChange (html.Event event) {
707
709
assert (isEnabled);
708
710
709
- EditingState newEditingState = EditingState .fromDomElement (domElement);
711
+ EditingState newEditingState = EditingState .fromDomElement (domElement,
712
+ textCapitalization: _inputConfiguration.textCapitalization);
710
713
711
714
if (newEditingState != _lastEditingState) {
712
715
_lastEditingState = newEditingState;
@@ -1487,6 +1490,12 @@ enum TextCapitalization {
1487
1490
class TextCapitalizationUtil {
1488
1491
final TextCapitalization textCapitalization;
1489
1492
1493
+ static final RegExp wordExp = new RegExp (r"(\w+)" );
1494
+ static final RegExp whiteSpaceExp = new RegExp (r"(\s+)" );
1495
+
1496
+ const TextCapitalizationUtil .defaultCapitalization ()
1497
+ : textCapitalization = TextCapitalization .none;
1498
+
1490
1499
// TODO: support sentence level text capitalization.
1491
1500
TextCapitalizationUtil .fromInputConfiguration (String inputConfiguration)
1492
1501
: this .textCapitalization =
@@ -1513,4 +1522,50 @@ class TextCapitalizationUtil {
1513
1522
break ;
1514
1523
}
1515
1524
}
1525
+
1526
+ /// Change the capitalization of the focused text field's value depending on
1527
+ /// the [TextCapitalization] .
1528
+ ///
1529
+ /// For [TextCapitalization.words] , this method makes all first letter of each
1530
+ /// word uppercase.
1531
+ ///
1532
+ /// For [TextCapitalization.characters] , this method makes all letters of each
1533
+ /// word uppercase.
1534
+ ///
1535
+ /// For [TextCapitalization.sentence] is not supported for now.
1536
+ String capitializeTextValue (String value) {
1537
+ if (value.isEmpty) {
1538
+ return value;
1539
+ }
1540
+ switch (textCapitalization) {
1541
+ case TextCapitalization .words:
1542
+ final Iterable <RegExpMatch > wordMatches = wordExp.allMatches (value);
1543
+ final List <String > words = wordMatches.map ((RegExpMatch match) {
1544
+ final String ? word = match.group (0 );
1545
+ return (word == null ) ? '' : word;
1546
+ }).toList ();
1547
+ final Iterable <RegExpMatch > whiteSpaceMatches =
1548
+ whiteSpaceExp.allMatches (value);
1549
+ final List <String > whiteSpaces =
1550
+ whiteSpaceMatches.map ((RegExpMatch match) {
1551
+ final String ? word = match.group (0 );
1552
+ return (word == null ) ? '' : word;
1553
+ }).toList ();
1554
+ final StringBuffer textValueBuffer = new StringBuffer ();
1555
+ for (int i = 0 ; i < words.length && ! words[i].isEmpty; i++ ) {
1556
+ final String word = words[i];
1557
+ textValueBuffer.write ('${word [0 ].toUpperCase ()}${word .substring (1 )}' );
1558
+ if (whiteSpaces.length > i) {
1559
+ textValueBuffer.write (whiteSpaces[i]);
1560
+ }
1561
+ }
1562
+ return textValueBuffer.toString ();
1563
+ case TextCapitalization .characters:
1564
+ return value.toUpperCase ();
1565
+ case TextCapitalization .sentences:
1566
+ case TextCapitalization .none:
1567
+ default :
1568
+ return value;
1569
+ }
1570
+ }
1516
1571
}
0 commit comments