[web] Migrate Flutter Web DOM usage to JS static interop - 32. (flutter/engine#33362)
This commit is contained in:
@@ -102,6 +102,7 @@ extension DomHTMLDocumentExtension on DomHTMLDocument {
|
||||
Iterable<DomElement> getElementsByTagName(String tag) =>
|
||||
createDomListWrapper<DomElement>(js_util
|
||||
.callMethod<_DomList>(this, 'getElementsByTagName', <Object>[tag]));
|
||||
external DomElement? get activeElement;
|
||||
}
|
||||
|
||||
@JS('document')
|
||||
@@ -149,6 +150,7 @@ extension DomEventExtension on DomEvent {
|
||||
if (bubbles != null) bubbles,
|
||||
if (cancelable != null) cancelable
|
||||
]);
|
||||
external bool get defaultPrevented;
|
||||
}
|
||||
|
||||
DomEvent createDomEvent(String type, String name) {
|
||||
@@ -242,6 +244,18 @@ extension DomElementExtension on DomElement {
|
||||
int get scrollLeft => js_util.getProperty(this, 'scrollLeft').round();
|
||||
set scrollLeft(int value) =>
|
||||
js_util.setProperty<num>(this, 'scrollLeft', value.round());
|
||||
external DomTokenList get classList;
|
||||
external set className(String value);
|
||||
external void blur();
|
||||
List<DomNode> getElementsByTagName(String tag) =>
|
||||
js_util.callMethod<List<Object?>>(
|
||||
this, 'getElementsByTagName', <Object>[tag]).cast<DomNode>();
|
||||
List<DomNode> getElementsByClassName(String className) =>
|
||||
js_util.callMethod<List<Object?>>(
|
||||
this, 'getElementsByClassName', <Object>[className]).cast<DomNode>();
|
||||
external void click();
|
||||
external bool hasAttribute(String name);
|
||||
external DomNodeList get childNodes;
|
||||
}
|
||||
|
||||
@JS()
|
||||
@@ -313,6 +327,10 @@ extension DomCSSStyleDeclarationExtension on DomCSSStyleDeclaration {
|
||||
set overflowY(String value) => setProperty('overflow-y', value);
|
||||
set overflowX(String value) => setProperty('overflow-x', value);
|
||||
set outline(String value) => setProperty('outline', value);
|
||||
set resize(String value) => setProperty('resize', value);
|
||||
set alignContent(String value) => setProperty('align-content', value);
|
||||
set textAlign(String value) => setProperty('text-align', value);
|
||||
set font(String value) => setProperty('font', value);
|
||||
String get width => getPropertyValue('width');
|
||||
String get height => getPropertyValue('height');
|
||||
String get position => getPropertyValue('position');
|
||||
@@ -372,6 +390,10 @@ extension DomCSSStyleDeclarationExtension on DomCSSStyleDeclaration {
|
||||
String get overflowY => getPropertyValue('overflow-y');
|
||||
String get overflowX => getPropertyValue('overflow-x');
|
||||
String get outline => getPropertyValue('outline');
|
||||
String get resize => getPropertyValue('resize');
|
||||
String get alignContent => getPropertyValue('align-content');
|
||||
String get textAlign => getPropertyValue('text-align');
|
||||
String get font => getPropertyValue('font');
|
||||
|
||||
external String getPropertyValue(String property);
|
||||
void setProperty(String propertyName, String value, [String? priority]) {
|
||||
@@ -764,6 +786,16 @@ DomHTMLTextAreaElement createDomHTMLTextAreaElement() =>
|
||||
extension DomHTMLTextAreaElementExtension on DomHTMLTextAreaElement {
|
||||
external set value(String? value);
|
||||
external void select();
|
||||
external set placeholder(String? value);
|
||||
external set name(String value);
|
||||
external int? get selectionStart;
|
||||
external int? get selectionEnd;
|
||||
external String? get value;
|
||||
void setSelectionRange(int start, int end, [String? direction]) =>
|
||||
js_util.callMethod(this, 'setSelectionRange',
|
||||
<Object>[start, end, if (direction != null) direction]);
|
||||
external String get name;
|
||||
external String get placeholder;
|
||||
}
|
||||
|
||||
@JS()
|
||||
@@ -1036,11 +1068,69 @@ extension DomHTMLInputElementExtension on DomHTMLInputElement {
|
||||
external String? get value;
|
||||
external bool? get disabled;
|
||||
external set disabled(bool? value);
|
||||
external set placeholder(String? value);
|
||||
external set name(String? value);
|
||||
external set autocomplete(String value);
|
||||
external int? get selectionStart;
|
||||
external int? get selectionEnd;
|
||||
void setSelectionRange(int start, int end, [String? direction]) =>
|
||||
js_util.callMethod(this, 'setSelectionRange',
|
||||
<Object>[start, end, if (direction != null) direction]);
|
||||
external String get autocomplete;
|
||||
external String? get name;
|
||||
external String? get type;
|
||||
external String get placeholder;
|
||||
}
|
||||
|
||||
DomHTMLInputElement createDomHTMLInputElement() =>
|
||||
domDocument.createElement('input') as DomHTMLInputElement;
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomTokenList {}
|
||||
|
||||
extension DomTokenListExtension on DomTokenList {
|
||||
external void add(String value);
|
||||
external bool contains(String token);
|
||||
}
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomHTMLFormElement extends DomHTMLElement {}
|
||||
|
||||
extension DomHTMLFormElementExtension on DomHTMLFormElement {
|
||||
external set noValidate(bool? value);
|
||||
external set method(String? value);
|
||||
external set action(String? value);
|
||||
}
|
||||
|
||||
DomHTMLFormElement createDomHTMLFormElement() =>
|
||||
domDocument.createElement('form') as DomHTMLFormElement;
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomNodeList {}
|
||||
|
||||
extension DomNodeListExtension on DomNodeList {
|
||||
external DomNode? item(int index);
|
||||
}
|
||||
|
||||
// A helper class for managing a subscription. On construction it will add an
|
||||
// event listener of the requested type to the target. Calling [cancel] will
|
||||
// remove the listener. Caller is still responsible for calling [allowInterop]
|
||||
// on the listener before creating the subscription.
|
||||
class DomSubscription {
|
||||
final String type;
|
||||
final DomEventTarget target;
|
||||
final DomEventListener listener;
|
||||
|
||||
DomSubscription(this.target, this.type, this.listener) {
|
||||
target.addEventListener(type, listener);
|
||||
}
|
||||
|
||||
void cancel() => target.removeEventListener(type, listener);
|
||||
}
|
||||
|
||||
class DomPoint {
|
||||
final num x;
|
||||
final num y;
|
||||
|
||||
@@ -403,8 +403,7 @@ mixin _WheelEventListenerMixin on _BaseAdapter {
|
||||
..fontSize = 'initial'
|
||||
..display = 'none';
|
||||
domDocument.body!.append(probe);
|
||||
String fontSize =
|
||||
domWindow.getComputedStyle(probe).getPropertyValue('font-size');
|
||||
String fontSize = domWindow.getComputedStyle(probe).fontSize;
|
||||
double? res;
|
||||
if (fontSize.contains('px')) {
|
||||
fontSize = fontSize.replaceAll('px', '');
|
||||
|
||||
@@ -2,13 +2,12 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:html' as html;
|
||||
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
import '../browser_detection.dart';
|
||||
import '../dom.dart';
|
||||
import '../platform_dispatcher.dart';
|
||||
import '../safe_browser_api.dart';
|
||||
import '../text_editing/text_editing.dart';
|
||||
import 'semantics.dart';
|
||||
|
||||
@@ -137,9 +136,14 @@ class SemanticsTextEditingStrategy extends DefaultTextEditingStrategy {
|
||||
}
|
||||
|
||||
// Subscribe to text and selection changes.
|
||||
subscriptions.add(activeDomElement.onInput.listen(handleChange));
|
||||
subscriptions.add(activeDomElement.onKeyDown.listen(maybeSendAction));
|
||||
subscriptions.add(html.document.onSelectionChange.listen(handleChange));
|
||||
subscriptions.add(
|
||||
DomSubscription(activeDomElement, 'input', allowInterop(handleChange)));
|
||||
subscriptions.add(
|
||||
DomSubscription(activeDomElement, 'keydown',
|
||||
allowInterop(maybeSendAction)));
|
||||
subscriptions.add(
|
||||
DomSubscription(domDocument, 'selectionchange',
|
||||
allowInterop(handleChange)));
|
||||
preventDefaultForMouseEvents();
|
||||
}
|
||||
|
||||
@@ -210,13 +214,13 @@ class TextField extends RoleManager {
|
||||
: super(Role.textField, semanticsObject) {
|
||||
editableElement =
|
||||
semanticsObject.hasFlag(ui.SemanticsFlag.isMultiline)
|
||||
? html.TextAreaElement()
|
||||
: html.InputElement();
|
||||
? createDomHTMLTextAreaElement()
|
||||
: createDomHTMLInputElement();
|
||||
_setupDomElement();
|
||||
}
|
||||
|
||||
/// The element used for editing, e.g. `<input>`, `<textarea>`.
|
||||
late final html.HtmlElement editableElement;
|
||||
late final DomHTMLElement editableElement;
|
||||
|
||||
void _setupDomElement() {
|
||||
// On iOS, even though the semantic text field is transparent, the cursor
|
||||
@@ -245,7 +249,7 @@ class TextField extends RoleManager {
|
||||
..left = '0'
|
||||
..width = '${semanticsObject.rect!.width}px'
|
||||
..height = '${semanticsObject.rect!.height}px';
|
||||
semanticsObject.element.append(editableElement as DomNode);
|
||||
semanticsObject.element.append(editableElement);
|
||||
|
||||
switch (browserEngine) {
|
||||
case BrowserEngine.blink:
|
||||
@@ -267,14 +271,15 @@ class TextField extends RoleManager {
|
||||
/// When in browser gesture mode, the focus is forwarded to the framework as
|
||||
/// a tap to initialize editing.
|
||||
void _initializeForBlink() {
|
||||
editableElement.addEventListener('focus', (html.Event event) {
|
||||
if (semanticsObject.owner.gestureMode != GestureMode.browserGestures) {
|
||||
return;
|
||||
}
|
||||
editableElement.addEventListener(
|
||||
'focus', allowInterop((DomEvent event) {
|
||||
if (semanticsObject.owner.gestureMode != GestureMode.browserGestures) {
|
||||
return;
|
||||
}
|
||||
|
||||
EnginePlatformDispatcher.instance.invokeOnSemanticsAction(
|
||||
semanticsObject.id, ui.SemanticsAction.tap, null);
|
||||
});
|
||||
EnginePlatformDispatcher.instance.invokeOnSemanticsAction(
|
||||
semanticsObject.id, ui.SemanticsAction.tap, null);
|
||||
}));
|
||||
}
|
||||
|
||||
/// Safari on iOS reports text field activation via touch events.
|
||||
@@ -291,19 +296,21 @@ class TextField extends RoleManager {
|
||||
num? lastTouchStartOffsetX;
|
||||
num? lastTouchStartOffsetY;
|
||||
|
||||
editableElement.addEventListener('touchstart', (html.Event event) {
|
||||
final html.TouchEvent touchEvent = event as html.TouchEvent;
|
||||
lastTouchStartOffsetX = touchEvent.changedTouches!.last.client.x;
|
||||
lastTouchStartOffsetY = touchEvent.changedTouches!.last.client.y;
|
||||
}, true);
|
||||
editableElement.addEventListener('touchstart',
|
||||
allowInterop((DomEvent event) {
|
||||
final DomTouchEvent touchEvent = event as DomTouchEvent;
|
||||
lastTouchStartOffsetX = touchEvent.changedTouches!.last.clientX;
|
||||
lastTouchStartOffsetY = touchEvent.changedTouches!.last.clientY;
|
||||
}), true);
|
||||
|
||||
editableElement.addEventListener('touchend', (html.Event event) {
|
||||
final html.TouchEvent touchEvent = event as html.TouchEvent;
|
||||
editableElement.addEventListener(
|
||||
'touchend', allowInterop((DomEvent event) {
|
||||
final DomTouchEvent touchEvent = event as DomTouchEvent;
|
||||
|
||||
if (lastTouchStartOffsetX != null) {
|
||||
assert(lastTouchStartOffsetY != null);
|
||||
final num offsetX = touchEvent.changedTouches!.last.client.x;
|
||||
final num offsetY = touchEvent.changedTouches!.last.client.y;
|
||||
final num offsetX = touchEvent.changedTouches!.last.clientX;
|
||||
final num offsetY = touchEvent.changedTouches!.last.clientY;
|
||||
|
||||
// This should match the similar constant defined in:
|
||||
//
|
||||
@@ -323,7 +330,7 @@ class TextField extends RoleManager {
|
||||
|
||||
lastTouchStartOffsetX = null;
|
||||
lastTouchStartOffsetY = null;
|
||||
}, true);
|
||||
}), true);
|
||||
}
|
||||
|
||||
bool _hasFocused = false;
|
||||
@@ -360,7 +367,7 @@ class TextField extends RoleManager {
|
||||
SemanticsTextEditingStrategy.instance.activate(this);
|
||||
needsDomFocusRequest = true;
|
||||
}
|
||||
if (html.document.activeElement != editableElement) {
|
||||
if (domDocument.activeElement != editableElement) {
|
||||
needsDomFocusRequest = true;
|
||||
}
|
||||
// Focused elements should have full text editing state applied.
|
||||
@@ -371,7 +378,7 @@ class TextField extends RoleManager {
|
||||
// Only apply text, because this node is not focused.
|
||||
editingState.applyTextToDomElement(editableElement);
|
||||
|
||||
if (_hasFocused && html.document.activeElement == editableElement) {
|
||||
if (_hasFocused && domDocument.activeElement == editableElement) {
|
||||
// Unlike `editableElement.focus()` we don't need to schedule `blur`
|
||||
// post-update because `document.activeElement` implies that the
|
||||
// element is already attached to the DOM. If it's not, it can't
|
||||
@@ -385,7 +392,7 @@ class TextField extends RoleManager {
|
||||
// Schedule focus post-update to make sure the element is attached to
|
||||
// the document. Otherwise focus() has no effect.
|
||||
semanticsObject.owner.addOneTimePostUpdateCallback(() {
|
||||
if (html.document.activeElement != editableElement) {
|
||||
if (domDocument.activeElement != editableElement) {
|
||||
editableElement.focus();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:html' as html;
|
||||
|
||||
import '../browser_detection.dart';
|
||||
import '../dom.dart';
|
||||
|
||||
/// Various types of inputs used in text fields.
|
||||
///
|
||||
@@ -69,10 +68,10 @@ abstract class EngineInputType {
|
||||
String? get inputmodeAttribute;
|
||||
|
||||
/// Create the appropriate DOM element for this input type.
|
||||
html.HtmlElement createDomElement() => html.InputElement();
|
||||
DomHTMLElement createDomElement() => createDomHTMLInputElement();
|
||||
|
||||
/// Given a [domElement], set attributes that are specific to this input type.
|
||||
void configureInputMode(html.HtmlElement domElement) {
|
||||
void configureInputMode(DomHTMLElement domElement) {
|
||||
if (inputmodeAttribute == null) {
|
||||
return;
|
||||
}
|
||||
@@ -156,5 +155,5 @@ class MultilineInputType extends EngineInputType {
|
||||
String? get inputmodeAttribute => null;
|
||||
|
||||
@override
|
||||
html.HtmlElement createDomElement() => html.TextAreaElement();
|
||||
DomHTMLElement createDomElement() => createDomHTMLTextAreaElement();
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import 'package:meta/meta.dart';
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
import '../browser_detection.dart';
|
||||
import '../dom.dart';
|
||||
import '../embedder.dart';
|
||||
import '../host_node.dart';
|
||||
import '../platform_dispatcher.dart';
|
||||
@@ -54,10 +55,10 @@ HostNode get defaultTextEditingRoot => flutterViewEmbedder.glassPaneShadow!;
|
||||
/// element.
|
||||
///
|
||||
/// They are assigned once during the creation of the DOM element.
|
||||
void _setStaticStyleAttributes(html.HtmlElement domElement) {
|
||||
domElement.classes.add(HybridTextEditing.textEditingClass);
|
||||
void _setStaticStyleAttributes(DomHTMLElement domElement) {
|
||||
domElement.classList.add(HybridTextEditing.textEditingClass);
|
||||
|
||||
final html.CssStyleDeclaration elementStyle = domElement.style;
|
||||
final DomCSSStyleDeclaration elementStyle = domElement.style;
|
||||
elementStyle
|
||||
..whiteSpace = 'pre-wrap'
|
||||
..alignContent = 'center'
|
||||
@@ -77,7 +78,7 @@ void _setStaticStyleAttributes(html.HtmlElement domElement) {
|
||||
..transformOrigin = '0 0 0';
|
||||
|
||||
if (browserHasAutofillOverlay()) {
|
||||
domElement.classes.add(transparentTextEditingClass);
|
||||
domElement.classList.add(transparentTextEditingClass);
|
||||
}
|
||||
|
||||
// This property makes the input's blinking cursor transparent.
|
||||
@@ -96,9 +97,9 @@ void _setStaticStyleAttributes(html.HtmlElement domElement) {
|
||||
/// element.
|
||||
///
|
||||
/// They are assigned once during the creation of the DOM element.
|
||||
void _hideAutofillElements(html.HtmlElement domElement,
|
||||
void _hideAutofillElements(DomHTMLElement domElement,
|
||||
{bool isOffScreen = false}) {
|
||||
final html.CssStyleDeclaration elementStyle = domElement.style;
|
||||
final DomCSSStyleDeclaration elementStyle = domElement.style;
|
||||
elementStyle
|
||||
..whiteSpace = 'pre-wrap'
|
||||
..alignContent = 'center'
|
||||
@@ -122,7 +123,7 @@ void _hideAutofillElements(html.HtmlElement domElement,
|
||||
}
|
||||
|
||||
if (browserHasAutofillOverlay()) {
|
||||
domElement.classes.add(transparentTextEditingClass);
|
||||
domElement.classList.add(transparentTextEditingClass);
|
||||
}
|
||||
|
||||
/// This property makes the input's blinking cursor transparent.
|
||||
@@ -142,9 +143,9 @@ class EngineAutofillForm {
|
||||
this.formIdentifier = '',
|
||||
});
|
||||
|
||||
final html.FormElement formElement;
|
||||
final DomHTMLFormElement formElement;
|
||||
|
||||
final Map<String, html.HtmlElement>? elements;
|
||||
final Map<String, DomHTMLElement>? elements;
|
||||
|
||||
final Map<String, AutofillInfo>? items;
|
||||
|
||||
@@ -181,17 +182,17 @@ class EngineAutofillForm {
|
||||
// If there is only one text field in the autofill model, `fields` will be
|
||||
// null. `focusedElementAutofill` contains the information about the one
|
||||
// text field.
|
||||
final Map<String, html.HtmlElement> elements = <String, html.HtmlElement>{};
|
||||
final Map<String, DomHTMLElement> elements = <String, DomHTMLElement>{};
|
||||
final Map<String, AutofillInfo> items = <String, AutofillInfo>{};
|
||||
final html.FormElement formElement = html.FormElement();
|
||||
final DomHTMLFormElement formElement = createDomHTMLFormElement();
|
||||
|
||||
// Validation is in the framework side.
|
||||
formElement.noValidate = true;
|
||||
formElement.method = 'post';
|
||||
formElement.action = '#';
|
||||
formElement.addEventListener('submit', (html.Event e) {
|
||||
formElement.addEventListener('submit', allowInterop((DomEvent e) {
|
||||
e.preventDefault();
|
||||
});
|
||||
}));
|
||||
|
||||
_hideAutofillElements(formElement);
|
||||
|
||||
@@ -221,7 +222,7 @@ class EngineAutofillForm {
|
||||
field.readJson('inputType').readString('name'),
|
||||
);
|
||||
|
||||
final html.HtmlElement htmlElement = engineInputType.createDomElement();
|
||||
final DomHTMLElement htmlElement = engineInputType.createDomElement();
|
||||
autofill.editingState.applyToDomElement(htmlElement);
|
||||
autofill.applyToDomElement(htmlElement);
|
||||
_hideAutofillElements(htmlElement);
|
||||
@@ -251,12 +252,12 @@ class EngineAutofillForm {
|
||||
|
||||
// If a form with the same Autofill elements is already on the dom, remove
|
||||
// it from DOM.
|
||||
final html.FormElement? form = formsOnTheDom[formIdentifier];
|
||||
final DomHTMLFormElement? form = formsOnTheDom[formIdentifier];
|
||||
form?.remove();
|
||||
|
||||
// In order to submit the form when Framework sends a `TextInput.commit`
|
||||
// message, we add a submit button to the form.
|
||||
final html.InputElement submitButton = html.InputElement();
|
||||
final DomHTMLInputElement submitButton = createDomHTMLInputElement();
|
||||
_hideAutofillElements(submitButton, isOffScreen: true);
|
||||
submitButton.className = 'submitBtn';
|
||||
submitButton.type = 'submit';
|
||||
@@ -271,9 +272,9 @@ class EngineAutofillForm {
|
||||
);
|
||||
}
|
||||
|
||||
void placeForm(html.HtmlElement mainTextEditingElement) {
|
||||
void placeForm(DomHTMLElement mainTextEditingElement) {
|
||||
formElement.append(mainTextEditingElement);
|
||||
defaultTextEditingRoot.append(formElement);
|
||||
defaultTextEditingRoot.append(formElement as html.Node);
|
||||
}
|
||||
|
||||
void storeForm() {
|
||||
@@ -291,33 +292,32 @@ class EngineAutofillForm {
|
||||
/// listeners with no exceptions are added during
|
||||
/// [TextEditingStrategy.addEventHandlers] method call and all
|
||||
/// listeners are removed during [TextEditingStrategy.disable] method call.
|
||||
List<StreamSubscription<html.Event>> addInputEventListeners() {
|
||||
List<DomSubscription> addInputEventListeners() {
|
||||
final Iterable<String> keys = elements!.keys;
|
||||
final List<StreamSubscription<html.Event>> subscriptions =
|
||||
<StreamSubscription<html.Event>>[];
|
||||
final List<DomSubscription> subscriptions = <DomSubscription>[];
|
||||
|
||||
void addSubscriptionForKey(String key) {
|
||||
final html.Element element = elements![key]!;
|
||||
final DomElement element = elements![key]!;
|
||||
subscriptions.add(
|
||||
element.onInput.listen((html.Event e) {
|
||||
if (items![key] == null) {
|
||||
throw StateError(
|
||||
'AutofillInfo must have a valid uniqueIdentifier.');
|
||||
} else {
|
||||
final AutofillInfo autofillInfo = items![key]!;
|
||||
handleChange(element, autofillInfo);
|
||||
}
|
||||
})
|
||||
);
|
||||
DomSubscription(element, 'input',
|
||||
allowInterop((DomEvent e) {
|
||||
if (items![key] == null) {
|
||||
throw StateError(
|
||||
'AutofillInfo must have a valid uniqueIdentifier.');
|
||||
} else {
|
||||
final AutofillInfo autofillInfo = items![key]!;
|
||||
handleChange(element, autofillInfo);
|
||||
}
|
||||
})));
|
||||
}
|
||||
|
||||
keys.forEach(addSubscriptionForKey);
|
||||
return subscriptions;
|
||||
}
|
||||
|
||||
void handleChange(html.Element domElement, AutofillInfo autofillInfo) {
|
||||
void handleChange(DomElement domElement, AutofillInfo autofillInfo) {
|
||||
final EditingState newEditingState = EditingState.fromDomElement(
|
||||
domElement as html.HtmlElement?);
|
||||
domElement as DomHTMLElement);
|
||||
|
||||
_sendAutofillEditingState(autofillInfo.uniqueIdentifier, newEditingState);
|
||||
}
|
||||
@@ -409,12 +409,12 @@ class AutofillInfo {
|
||||
);
|
||||
}
|
||||
|
||||
void applyToDomElement(html.HtmlElement domElement,
|
||||
void applyToDomElement(DomHTMLElement domElement,
|
||||
{bool focusedElement = false}) {
|
||||
final String? autofillHint = this.autofillHint;
|
||||
final String? placeholder = this.placeholder;
|
||||
if (domElement is html.InputElement) {
|
||||
final html.InputElement element = domElement;
|
||||
if (domInstanceOfString(domElement, 'HTMLInputElement')) {
|
||||
final DomHTMLInputElement element = domElement as DomHTMLInputElement;
|
||||
if (placeholder != null) {
|
||||
element.placeholder = placeholder;
|
||||
}
|
||||
@@ -428,15 +428,16 @@ class AutofillInfo {
|
||||
}
|
||||
}
|
||||
element.autocomplete = autofillHint ?? 'on';
|
||||
} else if (domElement is html.TextAreaElement) {
|
||||
} else if (domInstanceOfString(domElement, 'HTMLTextAreaElement')) {
|
||||
final DomHTMLTextAreaElement element = domElement as DomHTMLTextAreaElement;
|
||||
if (placeholder != null) {
|
||||
domElement.placeholder = placeholder;
|
||||
element.placeholder = placeholder;
|
||||
}
|
||||
if (autofillHint != null) {
|
||||
domElement.name = autofillHint;
|
||||
domElement.id = autofillHint;
|
||||
element.name = autofillHint;
|
||||
element.id = autofillHint;
|
||||
}
|
||||
domElement.setAttribute('autocomplete', autofillHint ?? 'on');
|
||||
element.setAttribute('autocomplete', autofillHint ?? 'on');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -704,15 +705,16 @@ class EditingState {
|
||||
///
|
||||
/// [domElement] can be a [InputElement] or a [TextAreaElement] depending on
|
||||
/// the [InputType] of the text field.
|
||||
factory EditingState.fromDomElement(html.HtmlElement? domElement) {
|
||||
if (domElement is html.InputElement) {
|
||||
final html.InputElement element = domElement;
|
||||
factory EditingState.fromDomElement(DomHTMLElement? domElement) {
|
||||
if (domInstanceOfString(domElement, 'HTMLInputElement')) {
|
||||
final DomHTMLInputElement element = domElement! as DomHTMLInputElement;
|
||||
return EditingState(
|
||||
text: element.value,
|
||||
baseOffset: element.selectionStart,
|
||||
extentOffset: element.selectionEnd);
|
||||
} else if (domElement is html.TextAreaElement) {
|
||||
final html.TextAreaElement element = domElement;
|
||||
} else if (domInstanceOfString(domElement, 'HTMLTextAreaElement')) {
|
||||
final DomHTMLTextAreaElement element = domElement! as
|
||||
DomHTMLTextAreaElement;
|
||||
return EditingState(
|
||||
text: element.value,
|
||||
baseOffset: element.selectionStart,
|
||||
@@ -807,13 +809,14 @@ class EditingState {
|
||||
/// See also:
|
||||
///
|
||||
/// * [applyTextToDomElement], which is used for non-focused elements.
|
||||
void applyToDomElement(html.HtmlElement? domElement) {
|
||||
if (domElement is html.InputElement) {
|
||||
final html.InputElement element = domElement;
|
||||
void applyToDomElement(DomHTMLElement? domElement) {
|
||||
if (domInstanceOfString(domElement, 'HTMLInputElement')) {
|
||||
final DomHTMLInputElement element = domElement! as DomHTMLInputElement;
|
||||
element.value = text;
|
||||
element.setSelectionRange(baseOffset!, extentOffset!);
|
||||
} else if (domElement is html.TextAreaElement) {
|
||||
final html.TextAreaElement element = domElement;
|
||||
} else if (domInstanceOfString(domElement, 'HTMLTextAreaElement')) {
|
||||
final DomHTMLTextAreaElement element = domElement! as
|
||||
DomHTMLTextAreaElement;
|
||||
element.value = text;
|
||||
element.setSelectionRange(baseOffset!, extentOffset!);
|
||||
} else {
|
||||
@@ -828,12 +831,13 @@ class EditingState {
|
||||
/// See also:
|
||||
///
|
||||
/// * [applyToDomElement], which is used for focused elements.
|
||||
void applyTextToDomElement(html.HtmlElement? domElement) {
|
||||
if (domElement is html.InputElement) {
|
||||
final html.InputElement element = domElement;
|
||||
void applyTextToDomElement(DomHTMLElement? domElement) {
|
||||
if (domInstanceOfString(domElement, 'HTMLInputElement')) {
|
||||
final DomHTMLInputElement element = domElement! as DomHTMLInputElement;
|
||||
element.value = text;
|
||||
} else if (domElement is html.TextAreaElement) {
|
||||
final html.TextAreaElement element = domElement;
|
||||
} else if (domInstanceOfString(domElement, 'HTMLTextAreaElement')) {
|
||||
final DomHTMLTextAreaElement element = domElement! as
|
||||
DomHTMLTextAreaElement;
|
||||
element.value = text;
|
||||
} else {
|
||||
throw UnsupportedError('Unsupported DOM element type');
|
||||
@@ -1088,13 +1092,13 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
|
||||
bool isEnabled = false;
|
||||
|
||||
/// The DOM element used for editing, if any.
|
||||
html.HtmlElement? domElement;
|
||||
DomHTMLElement? domElement;
|
||||
|
||||
/// Same as [domElement] but null-checked.
|
||||
///
|
||||
/// This must only be called in places that know for sure that a DOM element
|
||||
/// is currently available for editing.
|
||||
html.HtmlElement get activeDomElement {
|
||||
DomHTMLElement get activeDomElement {
|
||||
assert(
|
||||
domElement != null,
|
||||
'The DOM element of this text editing strategy is not currently active.',
|
||||
@@ -1120,8 +1124,7 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
|
||||
OnChangeCallback? onChange;
|
||||
OnActionCallback? onAction;
|
||||
|
||||
final List<StreamSubscription<html.Event>> subscriptions =
|
||||
<StreamSubscription<html.Event>>[];
|
||||
final List<DomSubscription> subscriptions = <DomSubscription>[];
|
||||
|
||||
bool get hasAutofillGroup => inputConfiguration.autofillGroup != null;
|
||||
|
||||
@@ -1129,7 +1132,7 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
|
||||
bool get appendedToForm => _appendedToForm;
|
||||
bool _appendedToForm = false;
|
||||
|
||||
html.FormElement? get focusedFormElement =>
|
||||
DomHTMLFormElement? get focusedFormElement =>
|
||||
inputConfiguration.autofillGroup?.formElement;
|
||||
|
||||
@override
|
||||
@@ -1151,7 +1154,7 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
|
||||
// DOM later, when the first location information arrived.
|
||||
// Otherwise, on Blink based Desktop browsers, the autofill menu appears
|
||||
// on top left of the screen.
|
||||
defaultTextEditingRoot.append(activeDomElement);
|
||||
defaultTextEditingRoot.append(activeDomElement as html.Node);
|
||||
_appendedToForm = false;
|
||||
}
|
||||
|
||||
@@ -1203,21 +1206,24 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
|
||||
}
|
||||
|
||||
// Subscribe to text and selection changes.
|
||||
subscriptions.add(activeDomElement.onInput.listen(handleChange));
|
||||
subscriptions.add(DomSubscription(activeDomElement, 'input',
|
||||
allowInterop(handleChange)));
|
||||
|
||||
subscriptions.add(activeDomElement.onKeyDown.listen(maybeSendAction));
|
||||
subscriptions.add(DomSubscription(activeDomElement, 'keydown',
|
||||
allowInterop(maybeSendAction)));
|
||||
|
||||
subscriptions.add(html.document.onSelectionChange.listen(handleChange));
|
||||
subscriptions.add(DomSubscription(domDocument, 'selectionchange',
|
||||
allowInterop(handleChange)));
|
||||
|
||||
activeDomElement.addEventListener('beforeinput', handleBeforeInput);
|
||||
activeDomElement.addEventListener('beforeinput',
|
||||
allowInterop(handleBeforeInput));
|
||||
|
||||
addCompositionEventHandlers(activeDomElement);
|
||||
addCompositionEventHandlers(activeDomElement as html.HtmlElement);
|
||||
|
||||
// Refocus on the activeDomElement after blur, so that user can keep editing the
|
||||
// text field.
|
||||
subscriptions.add(activeDomElement.onBlur.listen((_) {
|
||||
activeDomElement.focus();
|
||||
}));
|
||||
subscriptions.add(DomSubscription(activeDomElement, 'blur',
|
||||
allowInterop((_) { activeDomElement.focus(); })));
|
||||
|
||||
preventDefaultForMouseEvents();
|
||||
}
|
||||
@@ -1252,7 +1258,7 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
|
||||
subscriptions[i].cancel();
|
||||
}
|
||||
subscriptions.clear();
|
||||
removeCompositionEventHandlers(activeDomElement);
|
||||
removeCompositionEventHandlers(activeDomElement as html.HtmlElement);
|
||||
|
||||
// If focused element is a part of a form, it needs to stay on the DOM
|
||||
// until the autofill context of the form is finalized.
|
||||
@@ -1287,7 +1293,7 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
|
||||
_appendedToForm = true;
|
||||
}
|
||||
|
||||
void handleChange(html.Event event) {
|
||||
void handleChange(DomEvent event) {
|
||||
assert(isEnabled);
|
||||
|
||||
EditingState newEditingState = EditingState.fromDomElement(activeDomElement);
|
||||
@@ -1309,7 +1315,7 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
|
||||
}
|
||||
}
|
||||
|
||||
void handleBeforeInput(html.Event event) {
|
||||
void handleBeforeInput(DomEvent event) {
|
||||
// In some cases the beforeinput event is not fired such as when the selection
|
||||
// of a text field is updated. In this case only the oninput event is fired.
|
||||
// We still want a delta generated in these cases so we can properly update
|
||||
@@ -1343,9 +1349,12 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
|
||||
}
|
||||
}
|
||||
|
||||
void maybeSendAction(html.Event event) {
|
||||
if (event is html.KeyboardEvent && event.keyCode == _kReturnKeyCode) {
|
||||
onAction!(inputConfiguration.inputAction);
|
||||
void maybeSendAction(DomEvent e) {
|
||||
if (domInstanceOfString(e, 'KeyboardEvent')) {
|
||||
final DomKeyboardEvent event = e as DomKeyboardEvent;
|
||||
if (event.keyCode == _kReturnKeyCode) {
|
||||
onAction!(inputConfiguration.inputAction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1382,17 +1391,20 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
|
||||
/// selection conflicts with selection sent from the framework, which creates
|
||||
/// flickering during selection by mouse.
|
||||
void preventDefaultForMouseEvents() {
|
||||
subscriptions.add(activeDomElement.onMouseDown.listen((_) {
|
||||
subscriptions.add(
|
||||
DomSubscription(activeDomElement, 'mousedown', allowInterop((_) {
|
||||
_.preventDefault();
|
||||
}));
|
||||
})));
|
||||
|
||||
subscriptions.add(activeDomElement.onMouseUp.listen((_) {
|
||||
subscriptions.add(
|
||||
DomSubscription(activeDomElement, 'mouseup', allowInterop((_) {
|
||||
_.preventDefault();
|
||||
}));
|
||||
})));
|
||||
|
||||
subscriptions.add(activeDomElement.onMouseMove.listen((_) {
|
||||
subscriptions.add(
|
||||
DomSubscription(activeDomElement, 'mousemove', allowInterop((_) {
|
||||
_.preventDefault();
|
||||
}));
|
||||
})));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1463,7 +1475,8 @@ class IOSTextEditingStrategy extends GloballyPositionedTextEditingStrategy {
|
||||
if (hasAutofillGroup) {
|
||||
placeForm();
|
||||
}
|
||||
inputConfig.textCapitalization.setAutocapitalizeAttribute(activeDomElement);
|
||||
inputConfig.textCapitalization.setAutocapitalizeAttribute(activeDomElement
|
||||
as html.HtmlElement);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -1484,21 +1497,26 @@ class IOSTextEditingStrategy extends GloballyPositionedTextEditingStrategy {
|
||||
}
|
||||
|
||||
// Subscribe to text and selection changes.
|
||||
subscriptions.add(activeDomElement.onInput.listen(handleChange));
|
||||
subscriptions.add(DomSubscription(activeDomElement, 'input',
|
||||
allowInterop(handleChange)));
|
||||
|
||||
subscriptions.add(activeDomElement.onKeyDown.listen(maybeSendAction));
|
||||
subscriptions.add(DomSubscription(activeDomElement, 'keydown',
|
||||
allowInterop(maybeSendAction)));
|
||||
|
||||
subscriptions.add(html.document.onSelectionChange.listen(handleChange));
|
||||
subscriptions.add(DomSubscription(domDocument, 'selectionchange',
|
||||
allowInterop(handleChange)));
|
||||
|
||||
activeDomElement.addEventListener('beforeinput', handleBeforeInput);
|
||||
activeDomElement.addEventListener('beforeinput',
|
||||
allowInterop(handleBeforeInput));
|
||||
|
||||
addCompositionEventHandlers(activeDomElement);
|
||||
addCompositionEventHandlers(activeDomElement as html.HtmlElement);
|
||||
|
||||
// Position the DOM element after it is focused.
|
||||
subscriptions.add(activeDomElement.onFocus.listen((_) {
|
||||
// Cancel previous timer if exists.
|
||||
_schedulePlacement();
|
||||
}));
|
||||
subscriptions.add(DomSubscription(activeDomElement, 'focus',
|
||||
allowInterop((_) {
|
||||
// Cancel previous timer if exists.
|
||||
_schedulePlacement();
|
||||
})));
|
||||
|
||||
_addTapListener();
|
||||
|
||||
@@ -1521,14 +1539,15 @@ class IOSTextEditingStrategy extends GloballyPositionedTextEditingStrategy {
|
||||
// In order to detect this, we measure how much time has passed since the
|
||||
// input field was activated. If the time is too short, we re-focus the
|
||||
// input element.
|
||||
subscriptions.add(activeDomElement.onBlur.listen((_) {
|
||||
final bool isFastCallback = blurWatch.elapsed < _blurFastCallbackInterval;
|
||||
if (windowHasFocus && isFastCallback) {
|
||||
activeDomElement.focus();
|
||||
} else {
|
||||
owner.sendTextConnectionClosedToFrameworkIfAny();
|
||||
}
|
||||
}));
|
||||
subscriptions.add(DomSubscription(activeDomElement, 'blur',
|
||||
allowInterop((_) {
|
||||
final bool isFastCallback = blurWatch.elapsed < _blurFastCallbackInterval;
|
||||
if (windowHasFocus && isFastCallback) {
|
||||
activeDomElement.focus();
|
||||
} else {
|
||||
owner.sendTextConnectionClosedToFrameworkIfAny();
|
||||
}
|
||||
})));
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -1564,7 +1583,7 @@ class IOSTextEditingStrategy extends GloballyPositionedTextEditingStrategy {
|
||||
/// [_positionInputElementTimer] timer is restarted. The element will be
|
||||
/// placed to its correct position after [_delayBeforePlacement].
|
||||
void _addTapListener() {
|
||||
subscriptions.add(activeDomElement.onClick.listen((_) {
|
||||
subscriptions.add(DomSubscription(activeDomElement, 'click', allowInterop((_) {
|
||||
// Check if the element is already positioned. If not this does not fall
|
||||
// under `The user was using the long press, now they want to enter text
|
||||
// via keyboard` journey.
|
||||
@@ -1575,7 +1594,7 @@ class IOSTextEditingStrategy extends GloballyPositionedTextEditingStrategy {
|
||||
// Re-configure the timer to place the element.
|
||||
_schedulePlacement();
|
||||
}
|
||||
}));
|
||||
})));
|
||||
}
|
||||
|
||||
void _schedulePlacement() {
|
||||
@@ -1615,9 +1634,10 @@ class AndroidTextEditingStrategy extends GloballyPositionedTextEditingStrategy {
|
||||
if (hasAutofillGroup) {
|
||||
placeForm();
|
||||
} else {
|
||||
defaultTextEditingRoot.append(activeDomElement);
|
||||
defaultTextEditingRoot.append(activeDomElement as html.Node);
|
||||
}
|
||||
inputConfig.textCapitalization.setAutocapitalizeAttribute(activeDomElement);
|
||||
inputConfig.textCapitalization.setAutocapitalizeAttribute(
|
||||
activeDomElement as html.HtmlElement);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -1628,27 +1648,35 @@ class AndroidTextEditingStrategy extends GloballyPositionedTextEditingStrategy {
|
||||
}
|
||||
|
||||
// Subscribe to text and selection changes.
|
||||
subscriptions.add(activeDomElement.onInput.listen(handleChange));
|
||||
subscriptions.add(
|
||||
DomSubscription(activeDomElement, 'input', allowInterop(handleChange)));
|
||||
|
||||
subscriptions.add(activeDomElement.onKeyDown.listen(maybeSendAction));
|
||||
subscriptions.add(
|
||||
DomSubscription(activeDomElement, 'keydown',
|
||||
allowInterop(maybeSendAction)));
|
||||
|
||||
subscriptions.add(html.document.onSelectionChange.listen(handleChange));
|
||||
subscriptions.add(
|
||||
DomSubscription(domDocument, 'selectionchange',
|
||||
allowInterop(handleChange)));
|
||||
|
||||
activeDomElement.addEventListener('beforeinput', handleBeforeInput);
|
||||
activeDomElement.addEventListener('beforeinput',
|
||||
allowInterop(handleBeforeInput));
|
||||
|
||||
addCompositionEventHandlers(activeDomElement);
|
||||
addCompositionEventHandlers(activeDomElement as html.HtmlElement);
|
||||
|
||||
subscriptions.add(activeDomElement.onBlur.listen((_) {
|
||||
if (windowHasFocus) {
|
||||
// Chrome on Android will hide the onscreen keyboard when you tap outside
|
||||
// the text box. Instead, we want the framework to tell us to hide the
|
||||
// keyboard via `TextInput.clearClient` or `TextInput.hide`. Therefore
|
||||
// refocus as long as [windowHasFocus] is true.
|
||||
activeDomElement.focus();
|
||||
} else {
|
||||
owner.sendTextConnectionClosedToFrameworkIfAny();
|
||||
}
|
||||
}));
|
||||
subscriptions.add(
|
||||
DomSubscription(activeDomElement, 'blur',
|
||||
allowInterop((_) {
|
||||
if (windowHasFocus) {
|
||||
// Chrome on Android will hide the onscreen keyboard when you tap outside
|
||||
// the text box. Instead, we want the framework to tell us to hide the
|
||||
// keyboard via `TextInput.clearClient` or `TextInput.hide`. Therefore
|
||||
// refocus as long as [windowHasFocus] is true.
|
||||
activeDomElement.focus();
|
||||
} else {
|
||||
owner.sendTextConnectionClosedToFrameworkIfAny();
|
||||
}
|
||||
})));
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -1686,13 +1714,17 @@ class FirefoxTextEditingStrategy extends GloballyPositionedTextEditingStrategy {
|
||||
}
|
||||
|
||||
// Subscribe to text and selection changes.
|
||||
subscriptions.add(activeDomElement.onInput.listen(handleChange));
|
||||
subscriptions.add(
|
||||
DomSubscription(activeDomElement, 'input', allowInterop(handleChange)));
|
||||
|
||||
subscriptions.add(activeDomElement.onKeyDown.listen(maybeSendAction));
|
||||
subscriptions.add(
|
||||
DomSubscription(
|
||||
activeDomElement, 'keydown', allowInterop(maybeSendAction)));
|
||||
|
||||
activeDomElement.addEventListener('beforeinput', handleBeforeInput);
|
||||
activeDomElement.addEventListener('beforeinput',
|
||||
allowInterop(handleBeforeInput));
|
||||
|
||||
addCompositionEventHandlers(activeDomElement);
|
||||
addCompositionEventHandlers(activeDomElement as html.HtmlElement);
|
||||
|
||||
// Detects changes in text selection.
|
||||
//
|
||||
@@ -1707,20 +1739,30 @@ class FirefoxTextEditingStrategy extends GloballyPositionedTextEditingStrategy {
|
||||
//
|
||||
// After each keyup, the start/end values of the selection is compared to
|
||||
// the previously saved editing state.
|
||||
subscriptions.add(activeDomElement.onKeyUp.listen((html.KeyboardEvent event) {
|
||||
handleChange(event);
|
||||
}));
|
||||
subscriptions.add(
|
||||
DomSubscription(
|
||||
activeDomElement,
|
||||
'keyup',
|
||||
allowInterop((DomEvent event) {
|
||||
handleChange(event);
|
||||
})));
|
||||
|
||||
// In Firefox the context menu item "Select All" does not work without
|
||||
// listening to onSelect. On the other browsers onSelectionChange is
|
||||
// enough for covering "Select All" functionality.
|
||||
subscriptions.add(activeDomElement.onSelect.listen(handleChange));
|
||||
subscriptions.add(
|
||||
DomSubscription(
|
||||
activeDomElement, 'select', allowInterop(handleChange)));
|
||||
|
||||
// Refocus on the activeDomElement after blur, so that user can keep editing the
|
||||
// text field.
|
||||
subscriptions.add(activeDomElement.onBlur.listen((_) {
|
||||
_postponeFocus();
|
||||
}));
|
||||
subscriptions.add(
|
||||
DomSubscription(
|
||||
activeDomElement,
|
||||
'blur',
|
||||
allowInterop((_) {
|
||||
_postponeFocus();
|
||||
})));
|
||||
|
||||
preventDefaultForMouseEvents();
|
||||
}
|
||||
@@ -1940,9 +1982,9 @@ class TextInputFinishAutofillContext extends TextInputCommand {
|
||||
/// Called when the form is finalized with save option `true`.
|
||||
/// See: https://github.com/flutter/flutter/blob/bf9f3a3dcfea3022f9cf2dfc3ab10b120b48b19d/packages/flutter/lib/src/services/text_input.dart#L1277
|
||||
void saveForms() {
|
||||
formsOnTheDom.forEach((String identifier, html.FormElement form) {
|
||||
final html.InputElement submitBtn =
|
||||
form.getElementsByClassName('submitBtn').first as html.InputElement;
|
||||
formsOnTheDom.forEach((String identifier, DomHTMLFormElement form) {
|
||||
final DomHTMLInputElement submitBtn =
|
||||
form.getElementsByClassName('submitBtn').first as DomHTMLInputElement;
|
||||
submitBtn.click();
|
||||
});
|
||||
}
|
||||
@@ -1951,7 +1993,7 @@ void saveForms() {
|
||||
///
|
||||
/// Called when the form is finalized.
|
||||
void cleanForms() {
|
||||
for (final html.FormElement form in formsOnTheDom.values) {
|
||||
for (final DomHTMLFormElement form in formsOnTheDom.values) {
|
||||
form.remove();
|
||||
}
|
||||
formsOnTheDom.clear();
|
||||
@@ -2125,8 +2167,8 @@ final HybridTextEditing textEditing = HybridTextEditing();
|
||||
/// save or cancel them.
|
||||
///
|
||||
/// See: https://github.com/flutter/flutter/blob/bf9f3a3dcfea3022f9cf2dfc3ab10b120b48b19d/packages/flutter/lib/src/services/text_input.dart#L1277
|
||||
final Map<String, html.FormElement> formsOnTheDom =
|
||||
<String, html.FormElement>{};
|
||||
final Map<String, DomHTMLFormElement> formsOnTheDom =
|
||||
<String, DomHTMLFormElement>{};
|
||||
|
||||
/// Should be used as a singleton to provide support for text editing in
|
||||
/// Flutter Web.
|
||||
@@ -2260,9 +2302,9 @@ class EditableTextStyle {
|
||||
String get cssFont =>
|
||||
'$fontWeight ${fontSize}px ${canonicalizeFontFamily(fontFamily)}';
|
||||
|
||||
void applyToDomElement(html.HtmlElement domElement) {
|
||||
void applyToDomElement(DomHTMLElement domElement) {
|
||||
domElement.style
|
||||
..textAlign = align
|
||||
..textAlign = align!
|
||||
..font = cssFont;
|
||||
}
|
||||
}
|
||||
@@ -2317,7 +2359,7 @@ class EditableTextGeometry {
|
||||
/// example, if the editable DOM element is nested inside the semantics
|
||||
/// tree the semantics tree provides the placement parameters, in which
|
||||
/// case this method should not be used.
|
||||
void applyToDomElement(html.HtmlElement domElement) {
|
||||
void applyToDomElement(DomHTMLElement domElement) {
|
||||
final String cssTransform = float64ListToCssTransform(globalTransform);
|
||||
domElement.style
|
||||
..width = '${width}px'
|
||||
|
||||
@@ -215,7 +215,7 @@ void testMain() {
|
||||
|
||||
// It doesn't remove the DOM element.
|
||||
final TextField textField = textFieldSemantics.debugRoleManagerFor(Role.textField)! as TextField;
|
||||
expect(appHostNode.contains(textField.editableElement), isTrue);
|
||||
expect(appHostNode.contains(textField.editableElement as html.Node), isTrue);
|
||||
// Editing element is not enabled.
|
||||
expect(strategy.isEnabled, isFalse);
|
||||
expect(html.document.activeElement, html.document.body);
|
||||
|
||||
@@ -12,6 +12,7 @@ import 'package:test/test.dart';
|
||||
|
||||
import 'package:ui/src/engine.dart' show flutterViewEmbedder;
|
||||
import 'package:ui/src/engine/browser_detection.dart';
|
||||
import 'package:ui/src/engine/dom.dart';
|
||||
import 'package:ui/src/engine/initialization.dart';
|
||||
import 'package:ui/src/engine/services.dart';
|
||||
import 'package:ui/src/engine/text_editing/autofill_hint.dart';
|
||||
@@ -114,7 +115,8 @@ Future<void> testMain() async {
|
||||
expect(input.getAttribute('type'), null);
|
||||
|
||||
// Input is appended to the right point of the DOM.
|
||||
expect(defaultTextEditingRoot.contains(editingStrategy!.domElement), isTrue);
|
||||
expect(defaultTextEditingRoot.contains(editingStrategy!.domElement as
|
||||
Node?), isTrue);
|
||||
|
||||
editingStrategy!.disable();
|
||||
expect(
|
||||
@@ -262,14 +264,14 @@ Future<void> testMain() async {
|
||||
);
|
||||
expect(defaultTextEditingRoot.querySelectorAll('textarea'), hasLength(1));
|
||||
|
||||
final TextAreaElement textarea =
|
||||
defaultTextEditingRoot.querySelector('textarea')! as TextAreaElement;
|
||||
final DomHTMLTextAreaElement textarea =
|
||||
defaultTextEditingRoot.querySelector('textarea')! as DomHTMLTextAreaElement;
|
||||
// Now the textarea should have focus.
|
||||
expect(defaultTextEditingRoot.activeElement, textarea);
|
||||
expect(editingStrategy!.domElement, textarea);
|
||||
|
||||
textarea.value = 'foo\nbar';
|
||||
textarea.dispatchEvent(Event.eventType('Event', 'input'));
|
||||
textarea.dispatchEvent(createDomEvent('Event', 'input'));
|
||||
textarea.setSelectionRange(4, 6);
|
||||
document.dispatchEvent(Event.eventType('Event', 'selectionchange'));
|
||||
// Can read textarea state correctly (and preserves new lines).
|
||||
@@ -367,7 +369,7 @@ Future<void> testMain() async {
|
||||
// No input action so far.
|
||||
expect(lastInputAction, isNull);
|
||||
|
||||
final KeyboardEvent event = dispatchKeyboardEvent(
|
||||
final DomKeyboardEvent event = dispatchKeyboardEvent(
|
||||
editingStrategy!.domElement!,
|
||||
'keydown',
|
||||
keyCode: _kReturnKeyCode,
|
||||
@@ -611,7 +613,7 @@ Future<void> testMain() async {
|
||||
const MethodCall show = MethodCall('TextInput.show');
|
||||
sendFrameworkMessage(codec.encodeMethodCall(show));
|
||||
|
||||
final Element element = textEditing!.strategy.domElement!;
|
||||
final DomElement element = textEditing!.strategy.domElement!;
|
||||
expect(element.getAttribute('readonly'), 'readonly');
|
||||
|
||||
// Update the read-only config.
|
||||
@@ -1310,7 +1312,7 @@ Future<void> testMain() async {
|
||||
const MethodCall show = MethodCall('TextInput.show');
|
||||
sendFrameworkMessage(codec.encodeMethodCall(show));
|
||||
|
||||
final Element domElement = textEditing!.strategy.domElement!;
|
||||
final DomElement domElement = textEditing!.strategy.domElement!;
|
||||
|
||||
checkInputEditingState(domElement, 'abcd', 2, 3);
|
||||
|
||||
@@ -1370,7 +1372,7 @@ Future<void> testMain() async {
|
||||
});
|
||||
sendFrameworkMessage(codec.encodeMethodCall(setEditingState));
|
||||
|
||||
final HtmlElement domElement = textEditing!.strategy.domElement!;
|
||||
final DomHTMLElement domElement = textEditing!.strategy.domElement!;
|
||||
|
||||
checkInputEditingState(domElement, 'abcd', 2, 3);
|
||||
|
||||
@@ -1393,12 +1395,11 @@ Future<void> testMain() async {
|
||||
if (browserEngine == BrowserEngine.blink ||
|
||||
browserEngine == BrowserEngine.samsung ||
|
||||
browserEngine == BrowserEngine.webkit) {
|
||||
expect(textEditing!.strategy.domElement!.classes,
|
||||
contains('transparentTextEditing'));
|
||||
expect(textEditing!.strategy.domElement!.classList.contains('transparentTextEditing'),
|
||||
isTrue);
|
||||
} else {
|
||||
expect(
|
||||
textEditing!.strategy.domElement!.classes.any(
|
||||
(String element) => element.toString() == 'transparentTextEditing'),
|
||||
textEditing!.strategy.domElement!.classList.contains('transparentTextEditing'),
|
||||
isFalse);
|
||||
}
|
||||
|
||||
@@ -1433,7 +1434,7 @@ Future<void> testMain() async {
|
||||
const MethodCall show = MethodCall('TextInput.show');
|
||||
sendFrameworkMessage(codec.encodeMethodCall(show));
|
||||
|
||||
final HtmlElement domElement = textEditing!.strategy.domElement!;
|
||||
final DomHTMLElement domElement = textEditing!.strategy.domElement!;
|
||||
|
||||
checkInputEditingState(domElement, 'abcd', 2, 3);
|
||||
|
||||
@@ -1455,7 +1456,7 @@ Future<void> testMain() async {
|
||||
test('Canonicalizes font family', () {
|
||||
showKeyboard(inputType: 'text');
|
||||
|
||||
final HtmlElement input = textEditing!.strategy.domElement!;
|
||||
final DomHTMLElement input = textEditing!.strategy.domElement!;
|
||||
|
||||
MethodCall setStyle;
|
||||
|
||||
@@ -1562,7 +1563,7 @@ Future<void> testMain() async {
|
||||
input.setSelectionRange(2, 5);
|
||||
if (browserEngine == BrowserEngine.firefox) {
|
||||
final Event keyup = KeyboardEvent('keyup');
|
||||
textEditing!.strategy.domElement!.dispatchEvent(keyup);
|
||||
textEditing!.strategy.domElement!.dispatchEvent(keyup as DomEvent);
|
||||
} else {
|
||||
document.dispatchEvent(Event.eventType('Event', 'selectionchange'));
|
||||
}
|
||||
@@ -1614,7 +1615,7 @@ Future<void> testMain() async {
|
||||
input.setSelectionRange(2, 5);
|
||||
if (browserEngine == BrowserEngine.firefox) {
|
||||
final Event keyup = KeyboardEvent('keyup');
|
||||
textEditing!.strategy.domElement!.dispatchEvent(keyup);
|
||||
textEditing!.strategy.domElement!.dispatchEvent(keyup as DomEvent);
|
||||
} else {
|
||||
document.dispatchEvent(Event.eventType('Event', 'selectionchange'));
|
||||
}
|
||||
@@ -1747,7 +1748,8 @@ Future<void> testMain() async {
|
||||
Matrix4.translationValues(10.0, 20.0, 30.0).storage.toList());
|
||||
sendFrameworkMessage(codec.encodeMethodCall(setSizeAndTransform));
|
||||
|
||||
final TextAreaElement textarea = textEditing!.strategy.domElement! as TextAreaElement;
|
||||
final DomHTMLTextAreaElement textarea = textEditing!.strategy.domElement!
|
||||
as DomHTMLTextAreaElement;
|
||||
checkTextAreaEditingState(textarea, '', 0, 0);
|
||||
|
||||
// Can set editing state and preserve new lines.
|
||||
@@ -1764,11 +1766,11 @@ Future<void> testMain() async {
|
||||
|
||||
textarea.value = 'something\nelse';
|
||||
|
||||
textarea.dispatchEvent(Event.eventType('Event', 'input'));
|
||||
textarea.dispatchEvent(createDomEvent('Event', 'input'));
|
||||
textarea.setSelectionRange(2, 5);
|
||||
if (browserEngine == BrowserEngine.firefox) {
|
||||
textEditing!.strategy.domElement!
|
||||
.dispatchEvent(KeyboardEvent('keyup'));
|
||||
.dispatchEvent(createDomEvent('Event', 'keyup'));
|
||||
} else {
|
||||
document.dispatchEvent(Event.eventType('Event', 'selectionchange'));
|
||||
}
|
||||
@@ -1923,7 +1925,7 @@ Future<void> testMain() async {
|
||||
inputAction: 'TextInputAction.next',
|
||||
);
|
||||
|
||||
final KeyboardEvent event = dispatchKeyboardEvent(
|
||||
final DomKeyboardEvent event = dispatchKeyboardEvent(
|
||||
textEditing!.strategy.domElement!,
|
||||
'keydown',
|
||||
keyCode: _kReturnKeyCode,
|
||||
@@ -1963,12 +1965,13 @@ Future<void> testMain() async {
|
||||
|
||||
expect(autofillForm.formIdentifier, 'field1*field2*field3');
|
||||
|
||||
final FormElement form = autofillForm.formElement;
|
||||
final DomHTMLFormElement form = autofillForm.formElement;
|
||||
// Note that we also add a submit button. Therefore the form element has
|
||||
// 3 child nodes.
|
||||
expect(form.childNodes, hasLength(3));
|
||||
|
||||
final InputElement firstElement = form.childNodes.first as InputElement;
|
||||
final DomHTMLInputElement firstElement = form.childNodes.item(0)! as
|
||||
DomHTMLInputElement;
|
||||
// Autofill value is applied to the element.
|
||||
expect(firstElement.name,
|
||||
BrowserAutofillHints.instance.flutterToEngine('password'));
|
||||
@@ -1989,7 +1992,7 @@ Future<void> testMain() async {
|
||||
expect(firstElement.selectionEnd, 0);
|
||||
|
||||
// Element is hidden.
|
||||
final CssStyleDeclaration css = firstElement.style;
|
||||
final CssStyleDeclaration css = firstElement.style as CssStyleDeclaration;
|
||||
expect(css.color, 'transparent');
|
||||
expect(css.backgroundColor, 'transparent');
|
||||
|
||||
@@ -1997,11 +2000,9 @@ Future<void> testMain() async {
|
||||
if (browserEngine == BrowserEngine.blink ||
|
||||
browserEngine == BrowserEngine.samsung ||
|
||||
browserEngine == BrowserEngine.webkit) {
|
||||
expect(firstElement.classes, contains('transparentTextEditing'));
|
||||
expect(firstElement.classList.contains('transparentTextEditing'), isTrue);
|
||||
} else {
|
||||
expect(
|
||||
firstElement.classes.any(
|
||||
(String element) => element.toString() == 'transparentTextEditing'),
|
||||
expect(firstElement.classList.contains('transparentTextEditing'),
|
||||
isFalse);
|
||||
}
|
||||
});
|
||||
@@ -2027,12 +2028,12 @@ Future<void> testMain() async {
|
||||
EngineAutofillForm.fromFrameworkMessage(
|
||||
createAutofillInfo('username', 'field1'), fields)!;
|
||||
|
||||
final InputElement testInputElement = InputElement();
|
||||
final DomHTMLInputElement testInputElement = createDomHTMLInputElement();
|
||||
autofillForm.placeForm(testInputElement);
|
||||
|
||||
// The focused element is appended to the form, form also has the button
|
||||
// so in total it shoould have 4 elements.
|
||||
final FormElement form = autofillForm.formElement;
|
||||
final DomHTMLFormElement form = autofillForm.formElement;
|
||||
expect(form.childNodes, hasLength(4));
|
||||
|
||||
final FormElement formOnDom = defaultTextEditingRoot.querySelector('form')! as FormElement;
|
||||
@@ -2059,10 +2060,10 @@ Future<void> testMain() async {
|
||||
expect(autofillForm.items, isEmpty);
|
||||
expect(autofillForm.formElement, isNotNull);
|
||||
|
||||
final FormElement form = autofillForm.formElement;
|
||||
final DomHTMLFormElement form = autofillForm.formElement;
|
||||
// Submit button is added to the form.
|
||||
expect(form.childNodes, isNotEmpty);
|
||||
final InputElement inputElement = form.childNodes.first as InputElement;
|
||||
final InputElement inputElement = form.childNodes.item(0)! as InputElement;
|
||||
expect(inputElement.type, 'submit');
|
||||
|
||||
// The submit button should have class `submitBtn`.
|
||||
@@ -2105,7 +2106,7 @@ Future<void> testMain() async {
|
||||
final AutofillInfo autofillInfo = AutofillInfo.fromFrameworkMessage(
|
||||
createAutofillInfo(testHint, testId));
|
||||
|
||||
final InputElement testInputElement = InputElement();
|
||||
final DomHTMLInputElement testInputElement = createDomHTMLInputElement();
|
||||
autofillInfo.applyToDomElement(testInputElement);
|
||||
|
||||
// Hint sent from the framework is converted to the hint compatible with
|
||||
@@ -2128,7 +2129,7 @@ Future<void> testMain() async {
|
||||
final AutofillInfo autofillInfo = AutofillInfo.fromFrameworkMessage(
|
||||
createAutofillInfo(testHint, testId));
|
||||
|
||||
final TextAreaElement testInputElement = TextAreaElement();
|
||||
final DomHTMLTextAreaElement testInputElement = createDomHTMLTextAreaElement();
|
||||
autofillInfo.applyToDomElement(testInputElement);
|
||||
|
||||
// Hint sent from the framework is converted to the hint compatible with
|
||||
@@ -2145,7 +2146,7 @@ Future<void> testMain() async {
|
||||
final AutofillInfo autofillInfo = AutofillInfo.fromFrameworkMessage(
|
||||
createAutofillInfo(testPasswordHint, testId));
|
||||
|
||||
final InputElement testInputElement = InputElement();
|
||||
final DomHTMLInputElement testInputElement = createDomHTMLInputElement();
|
||||
autofillInfo.applyToDomElement(testInputElement);
|
||||
|
||||
// Hint sent from the framework is converted to the hint compatible with
|
||||
@@ -2163,7 +2164,7 @@ Future<void> testMain() async {
|
||||
final AutofillInfo autofillInfo = AutofillInfo.fromFrameworkMessage(
|
||||
createAutofillInfo(null, testId));
|
||||
|
||||
final InputElement testInputElement = InputElement();
|
||||
final DomHTMLInputElement testInputElement = createDomHTMLInputElement();
|
||||
autofillInfo.applyToDomElement(testInputElement);
|
||||
|
||||
expect(testInputElement.autocomplete,'on');
|
||||
@@ -2174,7 +2175,7 @@ Future<void> testMain() async {
|
||||
final AutofillInfo autofillInfo = AutofillInfo.fromFrameworkMessage(
|
||||
createAutofillInfo(null, testId));
|
||||
|
||||
final TextAreaElement testInputElement = TextAreaElement();
|
||||
final DomHTMLTextAreaElement testInputElement = createDomHTMLTextAreaElement();
|
||||
autofillInfo.applyToDomElement(testInputElement);
|
||||
|
||||
expect(testInputElement.getAttribute('autocomplete'),'on');
|
||||
@@ -2185,7 +2186,7 @@ Future<void> testMain() async {
|
||||
final AutofillInfo autofillInfo = AutofillInfo.fromFrameworkMessage(
|
||||
createAutofillInfo(null, testId, placeholder: 'enter your password'));
|
||||
|
||||
final TextAreaElement testInputElement = TextAreaElement();
|
||||
final DomHTMLTextAreaElement testInputElement = createDomHTMLTextAreaElement();
|
||||
autofillInfo.applyToDomElement(testInputElement);
|
||||
|
||||
expect(testInputElement.getAttribute('autocomplete'),'on');
|
||||
@@ -2229,7 +2230,7 @@ Future<void> testMain() async {
|
||||
_editingState =
|
||||
EditingState(text: 'Test', baseOffset: 1, extentOffset: 2);
|
||||
|
||||
_editingState.applyToDomElement(input);
|
||||
_editingState.applyToDomElement(input as DomHTMLInputElement);
|
||||
|
||||
expect(input.value, 'Test');
|
||||
expect(input.selectionStart, 1);
|
||||
@@ -2249,7 +2250,7 @@ Future<void> testMain() async {
|
||||
_editingState =
|
||||
EditingState(text: 'Test', baseOffset: 1, extentOffset: 2);
|
||||
|
||||
_editingState.applyToDomElement(textArea);
|
||||
_editingState.applyToDomElement(textArea as DomHTMLTextAreaElement);
|
||||
|
||||
expect(textArea.value, 'Test');
|
||||
expect(textArea.selectionStart, 1);
|
||||
@@ -2262,7 +2263,7 @@ Future<void> testMain() async {
|
||||
input.selectionStart = 1;
|
||||
input.selectionEnd = 2;
|
||||
|
||||
_editingState = EditingState.fromDomElement(input);
|
||||
_editingState = EditingState.fromDomElement(input as DomHTMLInputElement);
|
||||
|
||||
expect(_editingState.text, 'Test');
|
||||
expect(_editingState.baseOffset, 1);
|
||||
@@ -2282,7 +2283,8 @@ Future<void> testMain() async {
|
||||
input.selectionStart = 1;
|
||||
input.selectionEnd = 2;
|
||||
|
||||
_editingState = EditingState.fromDomElement(input);
|
||||
_editingState = EditingState.fromDomElement(input as
|
||||
DomHTMLTextAreaElement);
|
||||
|
||||
expect(_editingState.text, 'Test');
|
||||
expect(_editingState.baseOffset, 1);
|
||||
@@ -2296,12 +2298,12 @@ Future<void> testMain() async {
|
||||
input.selectionStart = 1;
|
||||
input.selectionEnd = 2;
|
||||
|
||||
final EditingState editingState1 = EditingState.fromDomElement(input);
|
||||
final EditingState editingState2 = EditingState.fromDomElement(input);
|
||||
final EditingState editingState1 = EditingState.fromDomElement(input as DomHTMLInputElement);
|
||||
final EditingState editingState2 = EditingState.fromDomElement(input as DomHTMLInputElement);
|
||||
|
||||
input.setSelectionRange(1, 3);
|
||||
|
||||
final EditingState editingState3 = EditingState.fromDomElement(input);
|
||||
final EditingState editingState3 = EditingState.fromDomElement(input as DomHTMLInputElement);
|
||||
|
||||
expect(editingState1 == editingState2, isTrue);
|
||||
expect(editingState1 != editingState3, isTrue);
|
||||
@@ -2408,8 +2410,8 @@ Future<void> testMain() async {
|
||||
});
|
||||
}
|
||||
|
||||
KeyboardEvent dispatchKeyboardEvent(
|
||||
EventTarget target,
|
||||
DomKeyboardEvent dispatchKeyboardEvent(
|
||||
DomEventTarget target,
|
||||
String type, {
|
||||
required int keyCode,
|
||||
}) {
|
||||
@@ -2421,7 +2423,7 @@ KeyboardEvent dispatchKeyboardEvent(
|
||||
'cancelable': true,
|
||||
}
|
||||
];
|
||||
final KeyboardEvent event = js_util.callConstructor<KeyboardEvent>(
|
||||
final DomKeyboardEvent event = js_util.callConstructor<DomKeyboardEvent>(
|
||||
jsKeyboardEvent,
|
||||
js_util.jsify(eventArgs) as List<Object?>?,
|
||||
);
|
||||
@@ -2465,10 +2467,10 @@ void cleanTestFlags() {
|
||||
}
|
||||
|
||||
void checkInputEditingState(
|
||||
Element? element, String text, int start, int end) {
|
||||
DomElement? element, String text, int start, int end) {
|
||||
expect(element, isNotNull);
|
||||
expect(element, isA<InputElement>());
|
||||
final InputElement input = element! as InputElement;
|
||||
expect(domInstanceOfString(element, 'HTMLInputElement'), true);
|
||||
final DomHTMLInputElement input = element! as DomHTMLInputElement;
|
||||
expect(defaultTextEditingRoot.activeElement, input);
|
||||
expect(input.value, text);
|
||||
expect(input.selectionStart, start);
|
||||
@@ -2477,20 +2479,20 @@ void checkInputEditingState(
|
||||
|
||||
/// In case of an exception backup DOM element(s) can still stay on the DOM.
|
||||
void clearBackUpDomElementIfExists() {
|
||||
final List<Node> domElementsToRemove = <Node>[];
|
||||
final List<DomElement> domElementsToRemove = <DomElement>[];
|
||||
if (defaultTextEditingRoot.querySelectorAll('input').isNotEmpty) {
|
||||
domElementsToRemove.addAll(defaultTextEditingRoot.querySelectorAll('input'));
|
||||
domElementsToRemove.addAll(defaultTextEditingRoot.querySelectorAll('input').cast<DomElement>());
|
||||
}
|
||||
if (defaultTextEditingRoot.querySelectorAll('textarea').isNotEmpty) {
|
||||
domElementsToRemove.addAll(defaultTextEditingRoot.querySelectorAll('textarea'));
|
||||
domElementsToRemove.addAll(defaultTextEditingRoot.querySelectorAll('textarea').cast<DomElement>());
|
||||
}
|
||||
domElementsToRemove.forEach(_removeNode);
|
||||
}
|
||||
|
||||
void _removeNode(Node n)=> n.remove();
|
||||
void _removeNode(DomElement n)=> n.remove();
|
||||
|
||||
void checkTextAreaEditingState(
|
||||
TextAreaElement textarea,
|
||||
DomHTMLTextAreaElement textarea,
|
||||
String text,
|
||||
int start,
|
||||
int end,
|
||||
|
||||
Reference in New Issue
Block a user