Skip to content

Commit

Permalink
Editable text fix (flutter#12249)
Browse files Browse the repository at this point in the history
* enabling spellcheck for text editing

* Fixing errors in the input box, which arrises when the fontweight is null.

* Rollback the spellcheck change. Still not tested in safari mobile.

* Carrying the assignment of fontweight from index to EditingStyle constructor. This reduce # of null checks to one.
  • Loading branch information
nturgut committed Sep 12, 2019
1 parent cce4b5b commit 9c84659
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 23 deletions.
18 changes: 9 additions & 9 deletions lib/web_ui/lib/src/engine/text_editing.dart
Expand Up @@ -667,13 +667,11 @@ class HybridTextEditing {
/// to its CSS equivalent value.
/// Converts index of TextAlign to enum value.
_editingStyle = _EditingStyle(
textDirection: ui.TextDirection.values[textDirectionIndex],
fontSize: style['fontSize'],
textAlign: ui.TextAlign.values[textAlignIndex],
fontFamily: style['fontFamily'],
fontWeight:
fontWeightIndexToCss(fontWeightIndex: style['fontWeightIndex']),
);
textDirection: ui.TextDirection.values[textDirectionIndex],
fontSize: style['fontSize'],
textAlign: ui.TextAlign.values[textAlignIndex],
fontFamily: style['fontFamily'],
fontWeightIndex: style['fontWeightIndex']);
}

/// Size and transform of the editable text on the page.
Expand Down Expand Up @@ -767,8 +765,10 @@ class _EditingStyle {
@required this.fontSize,
@required this.textAlign,
@required this.fontFamily,
this.fontWeight,
});
@required fontWeightIndex,
}) : this.fontWeight = (fontWeightIndex != null)
? fontWeightIndexToCss(fontWeightIndex: fontWeightIndex)
: 'normal';

/// This information will be used for changing the style of the hidden input
/// element, which will match it's size to the size of the editable widget.
Expand Down
84 changes: 70 additions & 14 deletions lib/web_ui/test/text_editing_test.dart
Expand Up @@ -472,22 +472,12 @@ void main() {
textEditing.handleTextInput(codec.encodeMethodCall(setClient));

final MethodCall setSizeAndTransform =
MethodCall('TextInput.setEditableSizeAndTransform', <String, dynamic>{
'width': 150,
'height': 50,
'transform':
Matrix4.translationValues(10.0, 20.0, 30.0).storage.toList()
});
configureSetSizeAndTransformMethodCall(150, 50,
Matrix4.translationValues(10.0, 20.0, 30.0).storage.toList());
textEditing.handleTextInput(codec.encodeMethodCall(setSizeAndTransform));

const MethodCall setStyle =
MethodCall('TextInput.setStyle', <String, dynamic>{
'fontSize': 12,
'fontFamily': 'sans-serif',
'textAlignIndex': 4,
'fontWeightIndex': 4,
'textDirectionIndex': 1,
});
final MethodCall setStyle =
configureSetStyleMethodCall(12, 'sans-serif', 4, 4, 1);
textEditing.handleTextInput(codec.encodeMethodCall(setStyle));

const MethodCall setEditingState =
Expand Down Expand Up @@ -522,6 +512,52 @@ void main() {
expect(spy.messages, isEmpty);
});

test('input font set succesfully with null fontWeightIndex', () {
final MethodCall setClient = MethodCall(
'TextInput.setClient', <dynamic>[123, flutterSinglelineConfig]);
textEditing.handleTextInput(codec.encodeMethodCall(setClient));

final MethodCall setSizeAndTransform =
configureSetSizeAndTransformMethodCall(150, 50,
Matrix4.translationValues(10.0, 20.0, 30.0).storage.toList());
textEditing.handleTextInput(codec.encodeMethodCall(setSizeAndTransform));

final MethodCall setStyle = configureSetStyleMethodCall(
12, 'sans-serif', 4, null /* fontWeightIndex */, 1);
textEditing.handleTextInput(codec.encodeMethodCall(setStyle));

const MethodCall setEditingState =
MethodCall('TextInput.setEditingState', <String, dynamic>{
'text': 'abcd',
'selectionBase': 2,
'selectionExtent': 3,
});
textEditing.handleTextInput(codec.encodeMethodCall(setEditingState));

const MethodCall show = MethodCall('TextInput.show');
textEditing.handleTextInput(codec.encodeMethodCall(show));

final HtmlElement domElement = textEditing.editingElement.domElement;

checkInputEditingState(domElement, 'abcd', 2, 3);

// Check if the location and styling is correct.
expect(
domElement.getBoundingClientRect(),
Rectangle<double>.fromPoints(const Point<double>(10.0, 20.0),
const Point<double>(160.0, 70.0)));
expect(domElement.style.transform,
'matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 10, 20, 30, 1)');
expect(
textEditing.editingElement.domElement.style.font, '12px sans-serif');

const MethodCall clearClient = MethodCall('TextInput.clearClient');
textEditing.handleTextInput(codec.encodeMethodCall(clearClient));

// Confirm that [HybridTextEditing] didn't send any messages.
expect(spy.messages, isEmpty);
});

test('Syncs the editing state back to Flutter', () {
final MethodCall setClient = MethodCall(
'TextInput.setClient', <dynamic>[123, flutterSinglelineConfig]);
Expand Down Expand Up @@ -642,6 +678,26 @@ void main() {
});
}

MethodCall configureSetStyleMethodCall(int fontSize, String fontFamily,
int textAlignIndex, int fontWeightIndex, int textDirectionIndex) {
return MethodCall('TextInput.setStyle', <String, dynamic>{
'fontSize': fontSize,
'fontFamily': fontFamily,
'textAlignIndex': textAlignIndex,
'fontWeightIndex': fontWeightIndex,
'textDirectionIndex': textDirectionIndex,
});
}

MethodCall configureSetSizeAndTransformMethodCall(
int width, int height, List<double> transform) {
return MethodCall('TextInput.setEditableSizeAndTransform', <String, dynamic>{
'width': width,
'height': height,
'transform': transform
});
}

void checkInputEditingState(
InputElement input, String text, int start, int end) {
expect(document.activeElement, input);
Expand Down

0 comments on commit 9c84659

Please sign in to comment.