[web] Migrate Flutter Web DOM usage to JS static interop - 37. (flutter/engine#33373)

This commit is contained in:
joshualitt
2022-06-22 12:43:56 -07:00
committed by GitHub
parent 20def47959
commit 05f07b159f
18 changed files with 179 additions and 141 deletions

View File

@@ -46,6 +46,7 @@ extension DomWindowExtension on DomWindow {
elt,
if (pseudoElt != null) pseudoElt
]) as DomCSSStyleDeclaration;
external DomScreen? get screen;
}
@JS()
@@ -1093,6 +1094,14 @@ DomTouchEvent createDomTouchEvent(String type, [Map<dynamic, dynamic>? init]) =>
js_util.callConstructor(domGetConstructor('TouchEvent')!,
<Object>[type, if (init != null) js_util.jsify(init)]);
@JS()
@staticInterop
class DomCompositionEvent {}
extension DomCompositionEventExtension on DomCompositionEvent {
external String? get data;
}
@JS()
@staticInterop
class DomHTMLInputElement extends DomHTMLElement {}
@@ -1218,6 +1227,35 @@ class DomStyleSheet {}
@staticInterop
class DomCSSStyleSheet extends DomStyleSheet {}
extension DomCSSStyleSheetExtension on DomCSSStyleSheet {
List<DomCSSRule> get cssRules =>
js_util.getProperty<List<Object?>>(this, 'cssRules').cast<DomCSSRule>();
int insertRule(String rule, [int? index]) => js_util
.callMethod(this, 'insertRule', <Object>[rule, if (index != null) index]);
}
@JS()
@staticInterop
class DomCSSRule {}
@JS()
@staticInterop
class DomScreen {}
extension DomScreenExtension on DomScreen {
external DomScreenOrientation? get orientation;
}
@JS()
@staticInterop
class DomScreenOrientation extends DomEventTarget {}
extension DomScreenOrientationExtension on DomScreenOrientation {
Future<dynamic> lock(String orientation) => js_util
.promiseToFuture(js_util.callMethod(this, 'lock', <String>[orientation]));
external void unlock();
}
// 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]

View File

@@ -3,7 +3,6 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:html' as html;
import 'package:ui/ui.dart' as ui;
@@ -52,22 +51,18 @@ class FlutterViewEmbedder {
// The tag name for the root view of the flutter app (glass-pane)
static const String _glassPaneTagName = 'flt-glass-pane';
/// Fires when browser language preferences change.
static const html.EventStreamProvider<html.Event> languageChangeEvent =
html.EventStreamProvider<html.Event>('languagechange');
/// Listens to window resize events.
StreamSubscription<html.Event>? _resizeSubscription;
/// Listens to window resize events
DomSubscription? _resizeSubscription;
/// Listens to window locale events.
StreamSubscription<html.Event>? _localeSubscription;
DomSubscription? _localeSubscription;
/// Contains Flutter-specific CSS rules, such as default margins and
/// paddings.
html.StyleElement? _styleElement;
DomHTMLStyleElement? _styleElement;
/// Configures the screen, such as scaling.
html.MetaElement? _viewportMeta;
DomHTMLMetaElement? _viewportMeta;
/// The element that contains the [sceneElement].
///
@@ -76,12 +71,12 @@ class FlutterViewEmbedder {
///
/// This element is inserted after the [semanticsHostElement] so that
/// platform views take precedence in DOM event handling.
html.Element? get sceneHostElement => _sceneHostElement;
html.Element? _sceneHostElement;
DomElement? get sceneHostElement => _sceneHostElement;
DomElement? _sceneHostElement;
/// A child element of body outside the shadowroot that hosts
/// global resources such svg filters and clip paths when using webkit.
html.Element? _resourcesHost;
DomElement? _resourcesHost;
/// The element that contains the semantics tree.
///
@@ -96,18 +91,18 @@ class FlutterViewEmbedder {
///
/// This element is inserted before the [semanticsHostElement] so that
/// platform views take precedence in DOM event handling.
html.Element? get semanticsHostElement => _semanticsHostElement;
html.Element? _semanticsHostElement;
DomElement? get semanticsHostElement => _semanticsHostElement;
DomElement? _semanticsHostElement;
/// The last scene element rendered by the [render] method.
html.Element? get sceneElement => _sceneElement;
html.Element? _sceneElement;
DomElement? get sceneElement => _sceneElement;
DomElement? _sceneElement;
/// This is state persistent across hot restarts that indicates what
/// to clear. Delay removal of old visible state to make the
/// transition appear smooth.
static const String _staleHotRestartStore = '__flutter_state';
List<html.Element?>? _staleHotRestartState;
List<DomElement?>? _staleHotRestartState;
/// Creates a container for DOM elements that need to be cleaned up between
/// hot restarts.
@@ -115,11 +110,11 @@ class FlutterViewEmbedder {
/// If a contains already exists, reuses the existing one.
void _setupHotRestart() {
// This persists across hot restarts to clear stale DOM.
_staleHotRestartState = getJsProperty<List<html.Element?>?>(html.window, _staleHotRestartStore);
_staleHotRestartState = getJsProperty<List<DomElement?>?>(domWindow, _staleHotRestartStore);
if (_staleHotRestartState == null) {
_staleHotRestartState = <html.Element?>[];
_staleHotRestartState = <DomElement?>[];
setJsProperty(
html.window, _staleHotRestartStore, _staleHotRestartState);
domWindow, _staleHotRestartStore, _staleHotRestartState);
}
}
@@ -130,7 +125,7 @@ class FlutterViewEmbedder {
registerHotRestartListener(() {
_resizeSubscription?.cancel();
_localeSubscription?.cancel();
_staleHotRestartState!.addAll(<html.Element?>[
_staleHotRestartState!.addAll(<DomElement?>[
_glassPaneElement,
_styleElement,
_viewportMeta,
@@ -140,7 +135,7 @@ class FlutterViewEmbedder {
void _clearOnHotRestart() {
if (_staleHotRestartState!.isNotEmpty) {
for (final html.Element? element in _staleHotRestartState!) {
for (final DomElement? element in _staleHotRestartState!) {
element?.remove();
}
_staleHotRestartState!.clear();
@@ -151,7 +146,7 @@ class FlutterViewEmbedder {
/// already in the right place, skip DOM mutation. This is both faster and
/// more correct, because moving DOM nodes loses internal state, such as
/// text selection.
void addSceneToSceneHost(html.Element? sceneElement) {
void addSceneToSceneHost(DomElement? sceneElement) {
if (sceneElement != _sceneElement) {
_sceneElement?.remove();
_sceneElement = sceneElement;
@@ -169,14 +164,14 @@ class FlutterViewEmbedder {
/// which captures semantics input events. The semantics DOM tree must be a
/// child of the glass pane element so that events bubble up to the glass pane
/// if they are not handled by semantics.
html.Element? get glassPaneElement => _glassPaneElement;
html.Element? _glassPaneElement;
DomElement? get glassPaneElement => _glassPaneElement;
DomElement? _glassPaneElement;
/// The [HostNode] of the [glassPaneElement], which contains the whole Flutter app.
HostNode? get glassPaneShadow => _glassPaneShadow;
HostNode? _glassPaneShadow;
final html.Element rootElement = html.document.body!;
final DomElement rootElement = domDocument.body!;
static const String defaultFontStyle = 'normal';
static const String defaultFontWeight = 'normal';
@@ -189,11 +184,11 @@ class FlutterViewEmbedder {
final bool isWebKit = browserEngine == BrowserEngine.webkit;
_styleElement?.remove();
_styleElement = html.StyleElement();
_styleElement = createDomHTMLStyleElement();
_resourcesHost?.remove();
_resourcesHost = null;
html.document.head!.append(_styleElement!);
final html.CssStyleSheet sheet = _styleElement!.sheet! as html.CssStyleSheet;
domDocument.head!.append(_styleElement!);
final DomCSSStyleSheet sheet = _styleElement!.sheet! as DomCSSStyleSheet;
applyGlobalCssRulesToSheet(
sheet,
browserEngine: browserEngine,
@@ -238,8 +233,8 @@ class FlutterViewEmbedder {
// engine are complete.
bodyElement.spellcheck = false;
for (final html.Element viewportMeta
in html.document.head!.querySelectorAll('meta[name="viewport"]')) {
for (final DomElement viewportMeta
in domDocument.head!.querySelectorAll('meta[name="viewport"]')) {
if (assertionsEnabled) {
// Filter out the meta tag that the engine placed on the page. This is
// to avoid UI flicker during hot restart. Hot restart will clean up the
@@ -260,18 +255,18 @@ class FlutterViewEmbedder {
// variables, so this will be null upon hot restart. Instead, this tag is
// removed by _clearOnHotRestart.
_viewportMeta?.remove();
_viewportMeta = html.MetaElement()
_viewportMeta = createDomHTMLMetaElement()
..setAttribute('flt-viewport', '')
..name = 'viewport'
..content = 'width=device-width, initial-scale=1.0, '
'maximum-scale=1.0, user-scalable=no';
html.document.head!.append(_viewportMeta!);
domDocument.head!.append(_viewportMeta!);
// IMPORTANT: the glass pane element must come after the scene element in the DOM node list so
// it can intercept input events.
_glassPaneElement?.remove();
final DomElement glassPaneElement = domDocument.createElement(_glassPaneTagName);
_glassPaneElement = glassPaneElement as html.Element;
_glassPaneElement = glassPaneElement;
glassPaneElement.style
..position = 'absolute'
..top = '0'
@@ -285,12 +280,11 @@ class FlutterViewEmbedder {
// Create a [HostNode] under the glass pane element, and attach everything
// there, instead of directly underneath the glass panel.
final HostNode glassPaneElementHostNode = _createHostNode(glassPaneElement
as html.Element);
final HostNode glassPaneElementHostNode = _createHostNode(glassPaneElement);
_glassPaneShadow = glassPaneElementHostNode;
// Don't allow the scene to receive pointer events.
_sceneHostElement = html.document.createElement('flt-scene-host')
_sceneHostElement = domDocument.createElement('flt-scene-host')
..style.pointerEvents = 'none';
/// CanvasKit uses a static scene element that never gets replaced, so it's
@@ -298,24 +292,24 @@ class FlutterViewEmbedder {
/// system is reset due to hot restart or in a test.
if (useCanvasKit) {
skiaSceneHost = createDomElement('flt-scene');
addSceneToSceneHost(skiaSceneHost as html.Element?);
addSceneToSceneHost(skiaSceneHost);
}
final html.Element semanticsHostElement =
html.document.createElement('flt-semantics-host');
final DomElement semanticsHostElement =
domDocument.createElement('flt-semantics-host');
semanticsHostElement.style
..position = 'absolute'
..transformOrigin = '0 0 0';
_semanticsHostElement = semanticsHostElement;
updateSemanticsScreenProperties();
final html.Element _accessibilityPlaceholder = EngineSemanticsOwner
final DomElement _accessibilityPlaceholder = EngineSemanticsOwner
.instance.semanticsHelper
.prepareAccessibilityPlaceholder() as html.Element;
.prepareAccessibilityPlaceholder();
glassPaneElementHostNode.appendAll(<DomNode>[
_accessibilityPlaceholder as DomNode,
_sceneHostElement! as DomNode,
_accessibilityPlaceholder,
_sceneHostElement!,
// The semantic host goes last because hit-test order-wise it must be
// first. If semantics goes under the scene host, platform views will
@@ -327,7 +321,7 @@ class FlutterViewEmbedder {
// elements transparent. This way, if a platform view appears among other
// interactive Flutter widgets, as long as those widgets do not intersect
// with the platform view, the platform view will be reachable.
semanticsHostElement as DomNode,
semanticsHostElement,
]);
// When debugging semantics, make the scene semi-transparent so that the
@@ -339,7 +333,7 @@ class FlutterViewEmbedder {
PointerBinding.initInstance(glassPaneElement);
KeyboardBinding.initInstance(glassPaneElement);
if (html.window.visualViewport == null && isWebKit) {
if (domWindow.visualViewport == null && isWebKit) {
// Older Safari versions sometimes give us bogus innerWidth/innerHeight
// values when the page loads. When it changes the values to correct ones
// it does not notify of the change via `onResize`. As a workaround, we
@@ -353,13 +347,13 @@ class FlutterViewEmbedder {
// Firefox returns correct values for innerHeight, innerWidth.
// Firefox also triggers html.window.onResize therefore this timer does
// not need to be set up for Firefox.
final int initialInnerWidth = html.window.innerWidth!;
final int initialInnerWidth = domWindow.innerWidth!;
// Counts how many times screen size was checked. It is checked up to 5
// times.
int checkCount = 0;
Timer.periodic(const Duration(milliseconds: 100), (Timer t) {
checkCount += 1;
if (initialInnerWidth != html.window.innerWidth) {
if (initialInnerWidth != domWindow.innerWidth) {
// Window size changed. Notify.
t.cancel();
_metricsDidChange(null);
@@ -370,24 +364,25 @@ class FlutterViewEmbedder {
});
}
if (html.window.visualViewport != null) {
_resizeSubscription =
html.window.visualViewport!.onResize.listen(_metricsDidChange);
if (domWindow.visualViewport != null) {
_resizeSubscription = DomSubscription(domWindow.visualViewport!, 'resize',
allowInterop(_metricsDidChange));
} else {
_resizeSubscription = html.window.onResize.listen(_metricsDidChange);
_resizeSubscription = DomSubscription(domWindow, 'resize',
allowInterop(_metricsDidChange));
}
_localeSubscription =
languageChangeEvent.forTarget(html.window).listen(_languageDidChange);
_localeSubscription = DomSubscription(domWindow, 'languagechange',
allowInterop(_languageDidChange));
EnginePlatformDispatcher.instance.updateLocales();
}
// Creates a [HostNode] into a `root` [html.Element].
HostNode _createHostNode(html.Element root) {
// Creates a [HostNode] into a `root` [DomElement].
HostNode _createHostNode(DomElement root) {
if (getJsProperty<Object?>(root, 'attachShadow') != null) {
return ShadowDomHostNode(root as DomElement);
return ShadowDomHostNode(root);
} else {
// attachShadow not available, fall back to ElementHostNode.
return ElementHostNode(root as DomElement);
return ElementHostNode(root);
}
}
@@ -395,8 +390,8 @@ class FlutterViewEmbedder {
/// logical pixels. To compensate, an inverse scale is injected at the root
/// level.
void updateSemanticsScreenProperties() {
_semanticsHostElement!.style.transform =
'scale(${1 / html.window.devicePixelRatio})';
_semanticsHostElement!.style.setProperty('transform',
'scale(${1 / domWindow.devicePixelRatio})');
}
/// Called immediately after browser window metrics change.
@@ -408,7 +403,7 @@ class FlutterViewEmbedder {
///
/// Note: always check for rotations for a mobile device. Update the physical
/// size if the change is caused by a rotation.
void _metricsDidChange(html.Event? event) {
void _metricsDidChange(DomEvent? event) {
updateSemanticsScreenProperties();
if (isMobile && !window.isRotation() && textEditing.isEditing) {
window.computeOnScreenKeyboardInsets(true);
@@ -422,7 +417,7 @@ class FlutterViewEmbedder {
}
/// Called immediately after browser window language change.
void _languageDidChange(html.Event event) {
void _languageDidChange(DomEvent event) {
EnginePlatformDispatcher.instance.updateLocales();
if (ui.window.onLocaleChanged != null) {
ui.window.onLocaleChanged!();
@@ -451,9 +446,9 @@ class FlutterViewEmbedder {
///
/// See w3c screen api: https://www.w3.org/TR/screen-orientation/
Future<bool> setPreferredOrientation(List<dynamic> orientations) {
final html.Screen screen = html.window.screen!;
final DomScreen screen = domWindow.screen!;
if (!unsafeIsNull(screen)) {
final html.ScreenOrientation? screenOrientation = screen.orientation;
final DomScreenOrientation? screenOrientation = screen.orientation;
if (!unsafeIsNull(screenOrientation)) {
if (orientations.isEmpty) {
screenOrientation!.unlock();
@@ -500,9 +495,9 @@ class FlutterViewEmbedder {
}
/// The element corresponding to the only child of the root surface.
html.Element? get _rootApplicationElement {
final html.Element lastElement = rootElement.children.last;
for (final html.Element child in lastElement.children) {
DomElement? get _rootApplicationElement {
final DomElement lastElement = rootElement.children.last;
for (final DomElement child in lastElement.children) {
if (child.tagName == 'FLT-SCENE') {
return child;
}
@@ -516,37 +511,37 @@ class FlutterViewEmbedder {
/// place it as first element of body(webkit), or as a child of
/// glass pane element for other browsers to make sure url resolution
/// works correctly when content is inside a shadow root.
void addResource(html.Element element) {
void addResource(DomElement element) {
final bool isWebKit = browserEngine == BrowserEngine.webkit;
if (_resourcesHost == null) {
_resourcesHost = html.DivElement()
_resourcesHost = createDomHTMLDivElement()
..style.visibility = 'hidden';
if (isWebKit) {
final html.Node bodyNode = html.document.body!;
final DomNode bodyNode = domDocument.body!;
bodyNode.insertBefore(_resourcesHost!, bodyNode.firstChild);
} else {
_glassPaneShadow!.node.insertBefore(
_resourcesHost! as DomNode, _glassPaneShadow!.node.firstChild);
_resourcesHost!, _glassPaneShadow!.node.firstChild);
}
}
_resourcesHost!.append(element);
}
/// Removes a global resource element.
void removeResource(html.Element? element) {
void removeResource(DomElement? element) {
if (element == null) {
return;
}
assert(element.parent == _resourcesHost);
assert(element.parentNode == _resourcesHost);
element.remove();
}
String get currentHtml => _rootApplicationElement?.outerHtml ?? '';
String get currentHtml => _rootApplicationElement?.outerHTML ?? '';
}
// Applies the required global CSS to an incoming [html.CssStyleSheet] `sheet`.
void applyGlobalCssRulesToSheet(
html.CssStyleSheet sheet, {
DomCSSStyleSheet sheet, {
required BrowserEngine browserEngine,
required bool hasAutofillOverlay,
String glassPaneTagName = FlutterViewEmbedder._glassPaneTagName,

View File

@@ -2,8 +2,6 @@
// 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';
import 'embedder.dart';
@@ -111,7 +109,7 @@ class ShadowDomHostNode implements HostNode {
// TODO(dit): Apply only rules for the shadow root
applyGlobalCssRulesToSheet(
shadowRootStyleElement.sheet! as html.CssStyleSheet,
shadowRootStyleElement.sheet! as DomCSSStyleSheet,
browserEngine: browserEngine,
hasAutofillOverlay: browserHasAutofillOverlay(),
);

View File

@@ -2,8 +2,6 @@
// 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';
@@ -54,7 +52,7 @@ class PersistedColorFilter extends PersistedContainerSurface
@override
void discard() {
super.discard();
flutterViewEmbedder.removeResource(_filterElement as html.Element?);
flutterViewEmbedder.removeResource(_filterElement);
// Do not detach the child container from the root. It is permanently
// attached. The elements are reused together and are detached from the DOM
// together.
@@ -73,7 +71,7 @@ class PersistedColorFilter extends PersistedContainerSurface
@override
void apply() {
flutterViewEmbedder.removeResource(_filterElement as html.Element?);
flutterViewEmbedder.removeResource(_filterElement);
_filterElement = null;
final EngineColorFilter? engineValue = filter as EngineColorFilter?;
if (engineValue == null) {
@@ -139,7 +137,7 @@ class PersistedColorFilter extends PersistedContainerSurface
// Use SVG filter for blend mode.
final SvgFilter svgFilter = svgFilterFromBlendMode(filterColor, colorFilterBlendMode);
_filterElement = svgFilter.element;
flutterViewEmbedder.addResource(_filterElement! as html.Element);
flutterViewEmbedder.addResource(_filterElement!);
style.filter = 'url(#${svgFilter.id})';
if (colorFilterBlendMode == ui.BlendMode.saturation ||
colorFilterBlendMode == ui.BlendMode.multiply ||
@@ -151,7 +149,7 @@ class PersistedColorFilter extends PersistedContainerSurface
void _applyMatrixColorFilter(CkMatrixColorFilter colorFilter) {
final SvgFilter svgFilter = svgFilterFromColorMatrix(colorFilter.matrix);
_filterElement = svgFilter.element;
flutterViewEmbedder.addResource(_filterElement! as html.Element);
flutterViewEmbedder.addResource(_filterElement!);
childContainer!.style.filter = 'url(#${svgFilter.id})';
}

View File

@@ -2,8 +2,6 @@
// 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';
@@ -40,7 +38,7 @@ class PersistedShaderMask extends PersistedContainerSurface
final ui.Rect maskRect;
final ui.BlendMode blendMode;
final ui.FilterQuality filterQuality;
html.Element? _shaderElement;
DomElement? _shaderElement;
final bool isWebKit = browserEngine == BrowserEngine.webkit;
@override
@@ -162,7 +160,7 @@ class PersistedShaderMask extends PersistedContainerSurface
final SvgFilter svgFilter = svgMaskFilterFromImageAndBlendMode(
imageUrl, blendModeTemp, maskRect.width, maskRect.height);
_shaderElement = svgFilter.element as html.Element;
_shaderElement = svgFilter.element;
if (isWebKit) {
_childContainer!.style.filter = 'url(#${svgFilter.id})';
} else {

View File

@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dom.dart';
import 'embedder.dart';
import 'util.dart';
@@ -68,7 +67,7 @@ class MouseCursor {
void activateSystemCursor(String? kind) {
setElementStyle(
flutterViewEmbedder.glassPaneElement! as DomElement,
flutterViewEmbedder.glassPaneElement!,
'cursor',
_mapKindToCssValue(kind),
);

View File

@@ -4,7 +4,6 @@
import 'dart:async';
import 'dart:convert';
import 'dart:html' as html;
import 'dart:typed_data';
import 'package:meta/meta.dart';
@@ -485,7 +484,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
contentManager: platformViewManager,
contentHandler: (DomElement content) {
// Remove cast to [html.Element] after migration.
flutterViewEmbedder.glassPaneElement!.append(content as html.Element);
flutterViewEmbedder.glassPaneElement!.append(content);
},
);
_platformViewMessageHandler!.handlePlatformViewCall(data, callback!);
@@ -608,7 +607,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
rasterizer!.draw(layerScene.layerTree);
} else {
final SurfaceScene surfaceScene = scene as SurfaceScene;
flutterViewEmbedder.addSceneToSceneHost(surfaceScene.webOnlyRootElement as html.Element?);
flutterViewEmbedder.addSceneToSceneHost(surfaceScene.webOnlyRootElement);
}
frameTimingsOnRasterFinish();
}

View File

@@ -2,7 +2,6 @@
// 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 'dart:math' as math;
import 'dart:typed_data';
@@ -1787,7 +1786,7 @@ class EngineSemanticsOwner {
if (_rootSemanticsElement == null) {
final SemanticsObject root = _semanticsTree[0]!;
_rootSemanticsElement = root.element;
flutterViewEmbedder.semanticsHostElement!.append(root.element as html.Node);
flutterViewEmbedder.semanticsHostElement!.append(root.element);
}
_finalizeTree();

View File

@@ -2,8 +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 '../dom.dart';
import '../safe_browser_api.dart';
import 'text_editing.dart';
/// Provides default functionality for listening to HTML composition events.
@@ -29,9 +29,12 @@ mixin CompositionAwareMixin {
/// The name of the browser composition event type that triggers on ending a composition.
static const String _kCompositionEnd = 'compositionend';
late final html.EventListener _compositionStartListener = _handleCompositionStart;
late final html.EventListener _compositionUpdateListener = _handleCompositionUpdate;
late final html.EventListener _compositionEndListener = _handleCompositionEnd;
late final DomEventListener _compositionStartListener =
allowInterop(_handleCompositionStart);
late final DomEventListener _compositionUpdateListener =
allowInterop(_handleCompositionUpdate);
late final DomEventListener _compositionEndListener =
allowInterop(_handleCompositionEnd);
/// The currently composing text in the `domElement`.
///
@@ -40,29 +43,29 @@ mixin CompositionAwareMixin {
/// so it is safe to reference it to get the current composingText.
String? composingText;
void addCompositionEventHandlers(html.HtmlElement domElement) {
void addCompositionEventHandlers(DomHTMLElement domElement) {
domElement.addEventListener(_kCompositionStart, _compositionStartListener);
domElement.addEventListener(_kCompositionUpdate, _compositionUpdateListener);
domElement.addEventListener(_kCompositionEnd, _compositionEndListener);
}
void removeCompositionEventHandlers(html.HtmlElement domElement) {
void removeCompositionEventHandlers(DomHTMLElement domElement) {
domElement.removeEventListener(_kCompositionStart, _compositionStartListener);
domElement.removeEventListener(_kCompositionUpdate, _compositionUpdateListener);
domElement.removeEventListener(_kCompositionEnd, _compositionEndListener);
}
void _handleCompositionStart(html.Event event) {
void _handleCompositionStart(DomEvent event) {
composingText = null;
}
void _handleCompositionUpdate(html.Event event) {
if (event is html.CompositionEvent) {
composingText = event.data;
void _handleCompositionUpdate(DomEvent event) {
if (domInstanceOfString(event, 'CompositionEvent')) {
composingText = (event as DomCompositionEvent).data;
}
}
void _handleCompositionEnd(html.Event event) {
void _handleCompositionEnd(DomEvent event) {
composingText = null;
}

View File

@@ -3,7 +3,6 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:html' as html;
import 'dart:math' as math;
import 'dart:typed_data';
@@ -1218,7 +1217,7 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
activeDomElement.addEventListener('beforeinput',
allowInterop(handleBeforeInput));
addCompositionEventHandlers(activeDomElement as html.HtmlElement);
addCompositionEventHandlers(activeDomElement);
// Refocus on the activeDomElement after blur, so that user can keep editing the
// text field.
@@ -1258,7 +1257,7 @@ abstract class DefaultTextEditingStrategy with CompositionAwareMixin implements
subscriptions[i].cancel();
}
subscriptions.clear();
removeCompositionEventHandlers(activeDomElement as html.HtmlElement);
removeCompositionEventHandlers(activeDomElement);
// 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.
@@ -1508,7 +1507,7 @@ class IOSTextEditingStrategy extends GloballyPositionedTextEditingStrategy {
activeDomElement.addEventListener('beforeinput',
allowInterop(handleBeforeInput));
addCompositionEventHandlers(activeDomElement as html.HtmlElement);
addCompositionEventHandlers(activeDomElement);
// Position the DOM element after it is focused.
subscriptions.add(DomSubscription(activeDomElement, 'focus',
@@ -1661,7 +1660,7 @@ class AndroidTextEditingStrategy extends GloballyPositionedTextEditingStrategy {
activeDomElement.addEventListener('beforeinput',
allowInterop(handleBeforeInput));
addCompositionEventHandlers(activeDomElement as html.HtmlElement);
addCompositionEventHandlers(activeDomElement);
subscriptions.add(
DomSubscription(activeDomElement, 'blur',
@@ -1723,7 +1722,7 @@ class FirefoxTextEditingStrategy extends GloballyPositionedTextEditingStrategy {
activeDomElement.addEventListener('beforeinput',
allowInterop(handleBeforeInput));
addCompositionEventHandlers(activeDomElement as html.HtmlElement);
addCompositionEventHandlers(activeDomElement);
// Detects changes in text selection.
//

View File

@@ -42,9 +42,11 @@ void testMain() {
// as a child of the glassPane, and the slot lives in the glassPane
// shadow root. The slot is the one that has pointer events auto.
final html.Element contents =
flutterViewEmbedder.glassPaneElement!.querySelector('#view-0')!;
flutterViewEmbedder.glassPaneElement!.querySelector('#view-0')! as
html.Element;
final html.Element slot =
flutterViewEmbedder.sceneElement!.querySelector('slot')!;
flutterViewEmbedder.sceneElement!.querySelector('slot')! as
html.Element;
final html.Element contentsHost = contents.parent!;
final html.Element slotHost = slot.parent!;
@@ -119,7 +121,8 @@ void testMain() {
// Transformations happen on the slot element.
final html.Element slotHost =
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!;
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!
as html.Element;
expect(
slotHost.style.transform,
@@ -144,7 +147,8 @@ void testMain() {
dispatcher.rasterizer!.draw(sb.build().layerTree);
final html.Element slotHost =
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!;
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!
as html.Element;
final html.CssStyleDeclaration style = slotHost.style;
expect(style.transform, 'matrix(1, 0, 0, 1, 3, 4)');
@@ -189,7 +193,8 @@ void testMain() {
// Transformations happen on the slot element.
html.Element slotHost =
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!;
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!
as html.Element;
expect(
getTransformChain(slotHost),
@@ -210,7 +215,8 @@ void testMain() {
// Transformations happen on the slot element.
slotHost =
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!;
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!
as html.Element;
expect(
getTransformChain(slotHost),
@@ -241,7 +247,8 @@ void testMain() {
// Transformations happen on the slot element.
final html.Element slotHost =
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!;
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!
as html.Element;
expect(
getTransformChain(slotHost),
@@ -270,7 +277,8 @@ void testMain() {
// Transformations happen on the slot element.
final html.Element slotHost =
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!;
flutterViewEmbedder.sceneElement!.querySelector('flt-platform-view-slot')!
as html.Element;
expect(
getTransformChain(slotHost),
@@ -561,7 +569,8 @@ void testMain() {
}
final html.Node skPathDefs =
flutterViewEmbedder.sceneElement!.querySelector('#sk_path_defs')!;
flutterViewEmbedder.sceneElement!.querySelector('#sk_path_defs')! as
html.Node;
expect(skPathDefs.childNodes, hasLength(0));

View File

@@ -2,8 +2,6 @@
// 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:js/js.dart';
import 'package:js/js_util.dart' as js_util;
import 'package:test/bootstrap/browser.dart';
@@ -59,7 +57,7 @@ void testMain() {
// No <canvas> element should be created.
expect(
flutterViewEmbedder.glassPaneElement!.querySelectorAll<html.Element>('canvas'),
flutterViewEmbedder.glassPaneElement!.querySelectorAll('canvas'),
isEmpty,
);
});

View File

@@ -9,6 +9,7 @@ import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';
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/text_editing/composition_aware_mixin.dart';
import 'package:ui/src/engine/text_editing/input_type.dart';
@@ -68,7 +69,8 @@ Future<void> testMain() async {
final _MockWithCompositionAwareMixin mockWithCompositionAwareMixin =
_MockWithCompositionAwareMixin();
mockWithCompositionAwareMixin.composingText = fakeComposingText;
mockWithCompositionAwareMixin.addCompositionEventHandlers(_inputElement);
mockWithCompositionAwareMixin.addCompositionEventHandlers(_inputElement
as DomHTMLElement);
_inputElement.dispatchEvent(html.Event(_MockWithCompositionAwareMixin._kCompositionEnd));
@@ -81,7 +83,8 @@ Future<void> testMain() async {
final _MockWithCompositionAwareMixin mockWithCompositionAwareMixin =
_MockWithCompositionAwareMixin();
mockWithCompositionAwareMixin.composingText = fakeComposingText;
mockWithCompositionAwareMixin.addCompositionEventHandlers(_inputElement);
mockWithCompositionAwareMixin.addCompositionEventHandlers(_inputElement
as DomHTMLElement);
_inputElement.dispatchEvent(html.Event(_MockWithCompositionAwareMixin._kCompositionStart));
@@ -95,7 +98,8 @@ Future<void> testMain() async {
final _MockWithCompositionAwareMixin mockWithCompositionAwareMixin =
_MockWithCompositionAwareMixin();
mockWithCompositionAwareMixin.composingText = fakeComposingText;
mockWithCompositionAwareMixin.addCompositionEventHandlers(_inputElement);
mockWithCompositionAwareMixin.addCompositionEventHandlers(_inputElement
as DomHTMLElement);
_inputElement.dispatchEvent(html.CompositionEvent(
_MockWithCompositionAwareMixin._kCompositionUpdate,

View File

@@ -80,11 +80,11 @@ void testMain() {
test('should add/remove global resource', () {
final FlutterViewEmbedder embedder = FlutterViewEmbedder();
final html.DivElement resource = html.DivElement();
embedder.addResource(resource);
embedder.addResource(resource as DomHTMLDivElement);
final html.Element? resourceRoot = resource.parent;
expect(resourceRoot, isNotNull);
expect(resourceRoot!.childNodes.length, 1);
embedder.removeResource(resource);
embedder.removeResource(resource as DomHTMLDivElement);
expect(resourceRoot.childNodes.length, 0);
});
@@ -92,7 +92,7 @@ void testMain() {
final FlutterViewEmbedder embedder = FlutterViewEmbedder();
final html.InputElement regularTextField = html.InputElement();
regularTextField.placeholder = 'Now you see me';
embedder.addResource(regularTextField);
embedder.addResource(regularTextField as DomHTMLInputElement);
regularTextField.focus();
html.CssStyleDeclaration? style = domWindow.getComputedStyle(
@@ -104,7 +104,7 @@ void testMain() {
final html.InputElement textField = html.InputElement();
textField.placeholder = 'Now you dont';
textField.classes.add('flt-text-editing');
embedder.addResource(textField);
embedder.addResource(textField as DomHTMLInputElement);
textField.focus();
style = domWindow.getComputedStyle(

View File

@@ -46,7 +46,8 @@ void main() {
void testMain() {
ensureFlutterViewEmbedderInitialized();
final html.Element glassPane = flutterViewEmbedder.glassPaneElement!;
final html.Element glassPane = flutterViewEmbedder.glassPaneElement! as
html.Element;
double dpi = 1.0;
setUp(() {

View File

@@ -1888,7 +1888,9 @@ void _testPlatformView() {
expect(child3Rect.right, 20);
expect(child3Rect.bottom, 60);
final html.Element platformViewElement = flutterViewEmbedder.glassPaneElement!.querySelector('#view-0')!;
final html.Element platformViewElement =
flutterViewEmbedder.glassPaneElement!.querySelector('#view-0')! as
html.Element;
final html.Rectangle<num> platformViewRect = platformViewElement.getBoundingClientRect();
expect(platformViewRect.left, 0);
expect(platformViewRect.top, 15);

View File

@@ -39,7 +39,7 @@ Future<void> testMain() async {
setUp(() async {
SurfaceSceneBuilder.debugForgetFrameScene();
for (final html.Node scene in
flutterViewEmbedder.sceneHostElement!.querySelectorAll('flt-scene')) {
flutterViewEmbedder.sceneHostElement!.querySelectorAll('flt-scene').cast<html.Node>()) {
scene.remove();
}
initWebGl();
@@ -163,8 +163,7 @@ void _renderCirclesScene(BlendMode blendMode) {
builder.addPicture(Offset.zero, circles2);
builder.pop();
flutterViewEmbedder.sceneHostElement!.append(builder.build().webOnlyRootElement!
as html.Element);
flutterViewEmbedder.sceneHostElement!.append(builder.build().webOnlyRootElement!);
}
Picture _drawTestPictureWithText(
@@ -220,6 +219,5 @@ void _renderTextScene(BlendMode blendMode) {
builder.addPicture(Offset.zero, textPicture2);
builder.pop();
flutterViewEmbedder.sceneHostElement!.append(builder.build().webOnlyRootElement!
as html.Element);
flutterViewEmbedder.sceneHostElement!.append(builder.build().webOnlyRootElement!);
}

View File

@@ -461,7 +461,7 @@ void expectPageHtml(String expectedHtml,
/// Currently rendered HTML DOM as an HTML string.
String get currentHtml {
return flutterViewEmbedder.sceneElement?.outerHtml ?? '';
return flutterViewEmbedder.sceneElement?.outerHTML ?? '';
}
class SceneTester {