[web] Fall-back to a DOM node if Shadow DOM is unavailable. (flutter/engine#26647)
This commit is contained in:
@@ -499,6 +499,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/dom_renderer.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/engine_canvas.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/font_change_util.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/frame_reference.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/host_node.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/backdrop_filter.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/bitmap_canvas.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/canvas.dart
|
||||
|
||||
@@ -224,6 +224,7 @@ part 'engine/dom_renderer.dart';
|
||||
part 'engine/engine_canvas.dart';
|
||||
part 'engine/font_change_util.dart';
|
||||
part 'engine/frame_reference.dart';
|
||||
part 'engine/host_node.dart';
|
||||
part 'engine/html/backdrop_filter.dart';
|
||||
part 'engine/html/canvas.dart';
|
||||
part 'engine/html/clip.dart';
|
||||
|
||||
@@ -14,7 +14,7 @@ class DomRenderer {
|
||||
|
||||
TextMeasurementService.initialize(
|
||||
rulerCacheCapacity: 10,
|
||||
root: _glassPaneShadow!,
|
||||
root: _glassPaneShadow!.node,
|
||||
);
|
||||
|
||||
assert(() {
|
||||
@@ -160,9 +160,9 @@ class DomRenderer {
|
||||
html.Element? get glassPaneElement => _glassPaneElement;
|
||||
html.Element? _glassPaneElement;
|
||||
|
||||
/// The ShadowRoot of the [glassPaneElement].
|
||||
html.ShadowRoot? get glassPaneShadow => _glassPaneShadow;
|
||||
html.ShadowRoot? _glassPaneShadow;
|
||||
/// The [HostNode] of the [glassPaneElement], which contains the whole Flutter app.
|
||||
HostNode? get glassPaneShadow => _glassPaneShadow;
|
||||
HostNode? _glassPaneShadow;
|
||||
|
||||
final html.Element rootElement = html.document.body!;
|
||||
|
||||
@@ -262,113 +262,6 @@ class DomRenderer {
|
||||
static const String defaultCssFont =
|
||||
'$defaultFontStyle $defaultFontWeight ${defaultFontSize}px $defaultFontFamily';
|
||||
|
||||
// Applies the required global CSS to an incoming [html.CssStyleSheet] `sheet`.
|
||||
void _applyCssRulesToSheet(html.CssStyleSheet sheet) {
|
||||
final bool isWebKit = browserEngine == BrowserEngine.webkit;
|
||||
final bool isFirefox = browserEngine == BrowserEngine.firefox;
|
||||
// TODO(butterfly): use more efficient CSS selectors; descendant selectors
|
||||
// are slow. More info:
|
||||
//
|
||||
// https://csswizardry.com/2011/09/writing-efficient-css-selectors/
|
||||
|
||||
// This undoes browser's default layout attributes for paragraphs. We
|
||||
// compute paragraph layout ourselves.
|
||||
if (isFirefox) {
|
||||
// For firefox set line-height, otherwise textx at same font-size will
|
||||
// measure differently in ruler.
|
||||
sheet.insertRule(
|
||||
'flt-ruler-host p, flt-scene p '
|
||||
'{ margin: 0; line-height: 100%;}',
|
||||
sheet.cssRules.length);
|
||||
} else {
|
||||
sheet.insertRule(
|
||||
'flt-ruler-host p, flt-scene p '
|
||||
'{ margin: 0; }',
|
||||
sheet.cssRules.length);
|
||||
}
|
||||
|
||||
// This undoes browser's default painting and layout attributes of range
|
||||
// input, which is used in semantics.
|
||||
sheet.insertRule('''
|
||||
flt-semantics input[type=range] {
|
||||
appearance: none;
|
||||
-webkit-appearance: none;
|
||||
width: 100%;
|
||||
position: absolute;
|
||||
border: none;
|
||||
top: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
}''', sheet.cssRules.length);
|
||||
|
||||
if (isWebKit) {
|
||||
sheet.insertRule(
|
||||
'flt-semantics input[type=range]::-webkit-slider-thumb {'
|
||||
' -webkit-appearance: none;'
|
||||
'}',
|
||||
sheet.cssRules.length);
|
||||
}
|
||||
|
||||
if (isFirefox) {
|
||||
sheet.insertRule(
|
||||
'input::-moz-selection {'
|
||||
' background-color: transparent;'
|
||||
'}',
|
||||
sheet.cssRules.length);
|
||||
sheet.insertRule(
|
||||
'textarea::-moz-selection {'
|
||||
' background-color: transparent;'
|
||||
'}',
|
||||
sheet.cssRules.length);
|
||||
} else {
|
||||
// On iOS, the invisible semantic text field has a visible cursor and
|
||||
// selection highlight. The following 2 CSS rules force everything to be
|
||||
// transparent.
|
||||
sheet.insertRule(
|
||||
'input::selection {'
|
||||
' background-color: transparent;'
|
||||
'}',
|
||||
sheet.cssRules.length);
|
||||
sheet.insertRule(
|
||||
'textarea::selection {'
|
||||
' background-color: transparent;'
|
||||
'}',
|
||||
sheet.cssRules.length);
|
||||
}
|
||||
sheet.insertRule('''
|
||||
flt-semantics input,
|
||||
flt-semantics textarea,
|
||||
flt-semantics [contentEditable="true"] {
|
||||
caret-color: transparent;
|
||||
}
|
||||
''', sheet.cssRules.length);
|
||||
|
||||
// By default on iOS, Safari would highlight the element that's being tapped
|
||||
// on using gray background. This CSS rule disables that.
|
||||
if (isWebKit) {
|
||||
sheet.insertRule('''
|
||||
$_glassPaneTagName * {
|
||||
-webkit-tap-highlight-color: transparent;
|
||||
}
|
||||
''', sheet.cssRules.length);
|
||||
}
|
||||
|
||||
// This css prevents an autofill overlay brought by the browser during
|
||||
// text field autofill by delaying the transition effect.
|
||||
// See: https://github.com/flutter/flutter/issues/61132.
|
||||
if (browserHasAutofillOverlay()) {
|
||||
sheet.insertRule('''
|
||||
.transparentTextEditing:-webkit-autofill,
|
||||
.transparentTextEditing:-webkit-autofill:hover,
|
||||
.transparentTextEditing:-webkit-autofill:focus,
|
||||
.transparentTextEditing:-webkit-autofill:active {
|
||||
-webkit-transition-delay: 99999s;
|
||||
}
|
||||
''', sheet.cssRules.length);
|
||||
}
|
||||
}
|
||||
|
||||
void reset() {
|
||||
final bool isWebKit = browserEngine == BrowserEngine.webkit;
|
||||
|
||||
@@ -376,7 +269,10 @@ $_glassPaneTagName * {
|
||||
_styleElement = html.StyleElement();
|
||||
html.document.head!.append(_styleElement!);
|
||||
final html.CssStyleSheet sheet = _styleElement!.sheet as html.CssStyleSheet;
|
||||
_applyCssRulesToSheet(sheet);
|
||||
applyGlobalCssRulesToSheet(sheet,
|
||||
browserEngine: browserEngine,
|
||||
hasAutofillOverlay: browserHasAutofillOverlay(),
|
||||
);
|
||||
|
||||
final html.BodyElement bodyElement = html.document.body!;
|
||||
|
||||
@@ -458,22 +354,13 @@ $_glassPaneTagName * {
|
||||
..bottom = '0'
|
||||
..left = '0';
|
||||
|
||||
// Create a Shadow Root under the glass panel, and attach everything there,
|
||||
// instead of directly underneath the glass panel.
|
||||
final html.ShadowRoot glassPaneElementShadowRoot = glassPaneElement.attachShadow(<String, String>{
|
||||
'mode': 'open',
|
||||
'delegatesFocus': 'true',
|
||||
});
|
||||
_glassPaneShadow = glassPaneElementShadowRoot;
|
||||
|
||||
// This must be appended to the body, so we can create a host node properly.
|
||||
bodyElement.append(glassPaneElement);
|
||||
|
||||
final html.StyleElement shadowRootStyleElement = html.StyleElement();
|
||||
// The shadowRootStyleElement must be appended to the DOM, or its `sheet` will be null later...
|
||||
glassPaneElementShadowRoot.append(shadowRootStyleElement);
|
||||
|
||||
final html.CssStyleSheet shadowRootStyleSheet = shadowRootStyleElement.sheet as html.CssStyleSheet;
|
||||
_applyCssRulesToSheet(shadowRootStyleSheet); // TODO: Apply only rules for the shadow root
|
||||
// Create a [HostNode] under the glass pane element, and attach everything
|
||||
// there, instead of directly underneath the glass panel.
|
||||
final HostNode glassPaneElementHostNode = _createHostNode(glassPaneElement);
|
||||
_glassPaneShadow = glassPaneElementHostNode;
|
||||
|
||||
// Don't allow the scene to receive pointer events.
|
||||
_sceneHostElement = createElement('flt-scene-host')
|
||||
@@ -492,7 +379,7 @@ $_glassPaneTagName * {
|
||||
.instance.semanticsHelper
|
||||
.prepareAccessibilityPlaceholder();
|
||||
|
||||
glassPaneElementShadowRoot.nodes.addAll([
|
||||
glassPaneElementHostNode.nodes.addAll([
|
||||
semanticsHostElement,
|
||||
_accessibilityPlaceholder,
|
||||
_sceneHostElement!,
|
||||
@@ -630,6 +517,16 @@ $_glassPaneTagName * {
|
||||
EnginePlatformDispatcher.instance._updateLocales();
|
||||
}
|
||||
|
||||
// Creates a [HostNode] into a `root` [html.Element].
|
||||
HostNode _createHostNode(html.Element root) {
|
||||
if (js_util.getProperty(root, 'attachShadow') != null) {
|
||||
return ShadowDomHostNode(root);
|
||||
} else {
|
||||
// attachShadow not available, fall back to ElementHostNode.
|
||||
return ElementHostNode(root);
|
||||
}
|
||||
}
|
||||
|
||||
/// The framework specifies semantics in physical pixels, but CSS uses
|
||||
/// logical pixels. To compensate, we inject an inverse scale at the root
|
||||
/// level.
|
||||
@@ -814,6 +711,117 @@ $_glassPaneTagName * {
|
||||
void debugPlainTextLayout() => _debugFrameStatistics!.plainTextLayouts++;
|
||||
}
|
||||
|
||||
// Applies the required global CSS to an incoming [html.CssStyleSheet] `sheet`.
|
||||
void applyGlobalCssRulesToSheet(html.CssStyleSheet sheet, {
|
||||
required BrowserEngine browserEngine,
|
||||
required bool hasAutofillOverlay,
|
||||
String glassPaneTagName = DomRenderer._glassPaneTagName,
|
||||
}) {
|
||||
final bool isWebKit = browserEngine == BrowserEngine.webkit;
|
||||
final bool isFirefox = browserEngine == BrowserEngine.firefox;
|
||||
// TODO(web): use more efficient CSS selectors; descendant selectors are slow.
|
||||
// More info: https://csswizardry.com/2011/09/writing-efficient-css-selectors
|
||||
|
||||
// This undoes browser's default layout attributes for paragraphs. We
|
||||
// compute paragraph layout ourselves.
|
||||
if (isFirefox) {
|
||||
// For firefox set line-height, otherwise textx at same font-size will
|
||||
// measure differently in ruler.
|
||||
sheet.insertRule(
|
||||
'flt-ruler-host p, flt-scene p '
|
||||
'{ margin: 0; line-height: 100%;}',
|
||||
sheet.cssRules.length);
|
||||
} else {
|
||||
sheet.insertRule(
|
||||
'flt-ruler-host p, flt-scene p '
|
||||
'{ margin: 0; }',
|
||||
sheet.cssRules.length);
|
||||
}
|
||||
|
||||
// This undoes browser's default painting and layout attributes of range
|
||||
// input, which is used in semantics.
|
||||
sheet.insertRule('''
|
||||
flt-semantics input[type=range] {
|
||||
appearance: none;
|
||||
-webkit-appearance: none;
|
||||
width: 100%;
|
||||
position: absolute;
|
||||
border: none;
|
||||
top: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
}''', sheet.cssRules.length);
|
||||
|
||||
if (isWebKit) {
|
||||
sheet.insertRule(
|
||||
'flt-semantics input[type=range]::-webkit-slider-thumb {'
|
||||
' -webkit-appearance: none;'
|
||||
'}',
|
||||
sheet.cssRules.length);
|
||||
}
|
||||
|
||||
if (isFirefox) {
|
||||
sheet.insertRule(
|
||||
'input::-moz-selection {'
|
||||
' background-color: transparent;'
|
||||
'}',
|
||||
sheet.cssRules.length);
|
||||
sheet.insertRule(
|
||||
'textarea::-moz-selection {'
|
||||
' background-color: transparent;'
|
||||
'}',
|
||||
sheet.cssRules.length);
|
||||
} else {
|
||||
// On iOS, the invisible semantic text field has a visible cursor and
|
||||
// selection highlight. The following 2 CSS rules force everything to be
|
||||
// transparent.
|
||||
sheet.insertRule(
|
||||
'input::selection {'
|
||||
' background-color: transparent;'
|
||||
'}',
|
||||
sheet.cssRules.length);
|
||||
sheet.insertRule(
|
||||
'textarea::selection {'
|
||||
' background-color: transparent;'
|
||||
'}',
|
||||
sheet.cssRules.length);
|
||||
}
|
||||
sheet.insertRule('''
|
||||
flt-semantics input,
|
||||
flt-semantics textarea,
|
||||
flt-semantics [contentEditable="true"] {
|
||||
caret-color: transparent;
|
||||
}
|
||||
''', sheet.cssRules.length);
|
||||
|
||||
// By default on iOS, Safari would highlight the element that's being tapped
|
||||
// on using gray background. This CSS rule disables that.
|
||||
if (isWebKit) {
|
||||
sheet.insertRule('''
|
||||
$glassPaneTagName * {
|
||||
-webkit-tap-highlight-color: transparent;
|
||||
}
|
||||
''', sheet.cssRules.length);
|
||||
}
|
||||
|
||||
// This css prevents an autofill overlay brought by the browser during
|
||||
// text field autofill by delaying the transition effect.
|
||||
// See: https://github.com/flutter/flutter/issues/61132.
|
||||
if (browserHasAutofillOverlay()) {
|
||||
sheet.insertRule('''
|
||||
.transparentTextEditing:-webkit-autofill,
|
||||
.transparentTextEditing:-webkit-autofill:hover,
|
||||
.transparentTextEditing:-webkit-autofill:focus,
|
||||
.transparentTextEditing:-webkit-autofill:active {
|
||||
-webkit-transition-delay: 99999s;
|
||||
}
|
||||
''', sheet.cssRules.length);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// Miscellaneous statistics collecting during a single frame's execution.
|
||||
///
|
||||
/// This is useful when profiling the app. This class should only be used when
|
||||
|
||||
185
engine/src/flutter/lib/web_ui/lib/src/engine/host_node.dart
Normal file
185
engine/src/flutter/lib/web_ui/lib/src/engine/host_node.dart
Normal file
@@ -0,0 +1,185 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
part of engine;
|
||||
|
||||
/// The interface required to host a flutter app in the DOM, and its tests.
|
||||
///
|
||||
/// Consider this as the intersection in functionality between [html.ShadowRoot]
|
||||
/// (preferred Flutter rendering method) and [html.Document] (fallback).
|
||||
///
|
||||
/// Not to be confused with [html.DocumentOrShadowRoot].
|
||||
abstract class HostNode {
|
||||
/// Retrieves the [html.Element] that currently has focus.
|
||||
///
|
||||
/// See:
|
||||
/// * [Document.activeElement](https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElement)
|
||||
html.Element? get activeElement;
|
||||
|
||||
/// Adds a node to the end of the child [nodes] list of this node.
|
||||
///
|
||||
/// If the node already exists in this document, it will be removed from its
|
||||
/// current parent node, then added to this node.
|
||||
///
|
||||
/// This method is more efficient than `nodes.add`, and is the preferred
|
||||
/// way of appending a child node.
|
||||
///
|
||||
/// See:
|
||||
/// * [Node.appendChild](https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild)
|
||||
html.Node append(html.Node node);
|
||||
|
||||
/// Returns true if this node contains the specified node.
|
||||
/// See:
|
||||
/// * [Node.contains](https://developer.mozilla.org/en-US/docs/Web/API/Node.contains)
|
||||
bool contains(html.Node? other);
|
||||
|
||||
/// Returns the currently wrapped [html.Node].
|
||||
html.Node get node;
|
||||
|
||||
/// A modifiable list of this node's children.
|
||||
List<html.Node> get nodes;
|
||||
|
||||
/// Finds the first descendant element of this document that matches the
|
||||
/// specified group of selectors.
|
||||
///
|
||||
/// [selectors] should be a string using CSS selector syntax.
|
||||
///
|
||||
/// ```dart
|
||||
/// var element1 = document.querySelector('.className');
|
||||
/// var element2 = document.querySelector('#id');
|
||||
/// ```
|
||||
///
|
||||
/// For details about CSS selector syntax, see the
|
||||
/// [CSS selector specification](http://www.w3.org/TR/css3-selectors/).
|
||||
///
|
||||
/// See:
|
||||
/// * [Document.querySelector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector)
|
||||
html.Element? querySelector(String selectors);
|
||||
|
||||
/// Finds all descendant elements of this document that match the specified
|
||||
/// group of selectors.
|
||||
///
|
||||
/// [selectors] should be a string using CSS selector syntax.
|
||||
///
|
||||
/// ```dart
|
||||
/// var items = document.querySelectorAll('.itemClassName');
|
||||
/// ```
|
||||
///
|
||||
/// For details about CSS selector syntax, see the
|
||||
/// [CSS selector specification](http://www.w3.org/TR/css3-selectors/).
|
||||
///
|
||||
/// See:
|
||||
/// * [Document.querySelectorAll](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll)
|
||||
List<html.Node> querySelectorAll(String selectors);
|
||||
}
|
||||
|
||||
/// A [HostNode] implementation, backed by a [html.ShadowRoot].
|
||||
///
|
||||
/// This is the preferred flutter implementation, but it might not be supported
|
||||
/// by all browsers yet.
|
||||
///
|
||||
/// The constructor might throw when calling `attachShadow`, if ShadowDOM is not
|
||||
/// supported in the current environment. In this case, a fallback [ElementHostNode]
|
||||
/// should be created instead.
|
||||
class ShadowDomHostNode implements HostNode {
|
||||
late html.ShadowRoot _shadow;
|
||||
|
||||
/// Build a HostNode by attaching a [html.ShadowRoot] to the `root` element.
|
||||
///
|
||||
/// This also calls [applyGlobalCssRulesToSheet], defined in dom_renderer.
|
||||
ShadowDomHostNode(html.Element root) {
|
||||
assert(
|
||||
root.isConnected ?? true,
|
||||
'The `root` of a ShadowDomHostNode must be connected to the Document object or a ShadowRoot.',
|
||||
);
|
||||
|
||||
_shadow = root.attachShadow(<String, String>{
|
||||
'mode': 'open',
|
||||
'delegatesFocus': 'true',
|
||||
});
|
||||
|
||||
final html.StyleElement shadowRootStyleElement = html.StyleElement();
|
||||
// The shadowRootStyleElement must be appended to the DOM, or its `sheet` will be null later.
|
||||
_shadow.append(shadowRootStyleElement);
|
||||
|
||||
// TODO: Apply only rules for the shadow root
|
||||
applyGlobalCssRulesToSheet(
|
||||
shadowRootStyleElement.sheet as html.CssStyleSheet,
|
||||
browserEngine: browserEngine,
|
||||
hasAutofillOverlay: browserHasAutofillOverlay(),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
html.Element? get activeElement => _shadow.activeElement;
|
||||
|
||||
@override
|
||||
html.Element? querySelector(String selectors) {
|
||||
return _shadow.querySelector(selectors);
|
||||
}
|
||||
|
||||
@override
|
||||
List<html.Node> querySelectorAll(String selectors) {
|
||||
return _shadow.querySelectorAll(selectors);
|
||||
}
|
||||
|
||||
@override
|
||||
html.Node append(html.Node node) {
|
||||
return _shadow.append(node);
|
||||
}
|
||||
|
||||
@override
|
||||
bool contains(html.Node? other) {
|
||||
return _shadow.contains(other);
|
||||
}
|
||||
|
||||
@override
|
||||
html.Node get node => _shadow;
|
||||
|
||||
@override
|
||||
List<html.Node> get nodes => _shadow.nodes;
|
||||
}
|
||||
|
||||
/// A [HostNode] implementation, backed by a [html.Element].
|
||||
///
|
||||
/// This is a fallback implementation, in case [ShadowDomHostNode] fails when
|
||||
/// being constructed.
|
||||
class ElementHostNode implements HostNode {
|
||||
late html.Element _element;
|
||||
|
||||
/// Build a HostNode by attaching a child [html.Element] to the `root` element.
|
||||
ElementHostNode(html.Element root) {
|
||||
_element = html.document.createElement('flt-element-host-node');
|
||||
root.append(_element);
|
||||
}
|
||||
|
||||
@override
|
||||
html.Element? get activeElement => _element.ownerDocument?.activeElement;
|
||||
|
||||
@override
|
||||
html.Element? querySelector(String selectors) {
|
||||
return _element.querySelector(selectors);
|
||||
}
|
||||
|
||||
@override
|
||||
List<html.Node> querySelectorAll(String selectors) {
|
||||
return _element.querySelectorAll(selectors);
|
||||
}
|
||||
|
||||
@override
|
||||
html.Node append(html.Node node) {
|
||||
return _element.append(node);
|
||||
}
|
||||
|
||||
@override
|
||||
bool contains(html.Node? other) {
|
||||
return _element.contains(other);
|
||||
}
|
||||
|
||||
@override
|
||||
html.Node get node => _element;
|
||||
|
||||
@override
|
||||
List<html.Node> get nodes => _element.nodes;
|
||||
}
|
||||
@@ -36,7 +36,7 @@ class RulerHost {
|
||||
..width = '0'
|
||||
..height = '0';
|
||||
|
||||
(root ?? domRenderer.glassPaneShadow!).append(_rulerHost);
|
||||
(root ?? domRenderer.glassPaneShadow!.node).append(_rulerHost);
|
||||
registerHotRestartListener(dispose);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,11 +26,9 @@ const String transparentTextEditingClass = 'transparentTextEditing';
|
||||
|
||||
void _emptyCallback(dynamic _) {}
|
||||
|
||||
/// The default root that hosts all DOM required for text editing when a11y is not enabled.
|
||||
///
|
||||
/// This is something similar to [html.Document]. Currently, it's a [html.ShadowRoot].
|
||||
/// The default [HostNode] that hosts all DOM required for text editing when a11y is not enabled.
|
||||
@visibleForTesting
|
||||
html.ShadowRoot get defaultTextEditingRoot => domRenderer.glassPaneShadow!;
|
||||
HostNode get defaultTextEditingRoot => domRenderer.glassPaneShadow!;
|
||||
|
||||
/// These style attributes are constant throughout the life time of an input
|
||||
/// element.
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
@JS()
|
||||
library dom_renderer_test; // We need this to mess with the ShadowDOM.
|
||||
|
||||
import 'dart:html' as html;
|
||||
|
||||
import 'package:js/js.dart';
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/src/engine.dart';
|
||||
@@ -15,14 +18,17 @@ void main() {
|
||||
void testMain() {
|
||||
test('populates flt-renderer and flt-build-mode', () {
|
||||
DomRenderer();
|
||||
expect(html.document.body!.attributes['flt-renderer'], 'html (requested explicitly)');
|
||||
expect(html.document.body!.attributes['flt-renderer'],
|
||||
'html (requested explicitly)');
|
||||
expect(html.document.body!.attributes['flt-build-mode'], 'debug');
|
||||
});
|
||||
|
||||
test('creating elements works', () {
|
||||
final DomRenderer renderer = DomRenderer();
|
||||
final html.Element element = renderer.createElement('div');
|
||||
expect(element, isNotNull);
|
||||
});
|
||||
|
||||
test('can append children to parents', () {
|
||||
final DomRenderer renderer = DomRenderer();
|
||||
final html.Element parent = renderer.createElement('div');
|
||||
@@ -30,18 +36,21 @@ void testMain() {
|
||||
renderer.append(parent, child);
|
||||
expect(parent.children, hasLength(1));
|
||||
});
|
||||
|
||||
test('can set text on elements', () {
|
||||
final DomRenderer renderer = DomRenderer();
|
||||
final html.Element element = renderer.createElement('div');
|
||||
renderer.setText(element, 'Hello World');
|
||||
expect(element.text, 'Hello World');
|
||||
});
|
||||
|
||||
test('can set attributes on elements', () {
|
||||
final DomRenderer renderer = DomRenderer();
|
||||
final html.Element element = renderer.createElement('div');
|
||||
renderer.setElementAttribute(element, 'id', 'foo');
|
||||
expect(element.id, 'foo');
|
||||
});
|
||||
|
||||
test('can add classes to elements', () {
|
||||
final DomRenderer renderer = DomRenderer();
|
||||
final html.Element element = renderer.createElement('div');
|
||||
@@ -49,6 +58,7 @@ void testMain() {
|
||||
renderer.addElementClass(element, 'bar');
|
||||
expect(element.classes, <String>['foo', 'bar']);
|
||||
});
|
||||
|
||||
test('can remove classes from elements', () {
|
||||
final DomRenderer renderer = DomRenderer();
|
||||
final html.Element element = renderer.createElement('div');
|
||||
@@ -58,12 +68,14 @@ void testMain() {
|
||||
renderer.removeElementClass(element, 'foo');
|
||||
expect(element.classes, <String>['bar']);
|
||||
});
|
||||
|
||||
test('can set style properties on elements', () {
|
||||
final DomRenderer renderer = DomRenderer();
|
||||
final html.Element element = renderer.createElement('div');
|
||||
DomRenderer.setElementStyle(element, 'color', 'red');
|
||||
expect(element.style.color, 'red');
|
||||
});
|
||||
|
||||
test('can remove style properties from elements', () {
|
||||
final DomRenderer renderer = DomRenderer();
|
||||
final html.Element element = renderer.createElement('div');
|
||||
@@ -72,12 +84,14 @@ void testMain() {
|
||||
DomRenderer.setElementStyle(element, 'color', null);
|
||||
expect(element.style.color, '');
|
||||
});
|
||||
|
||||
test('elements can have children', () {
|
||||
final DomRenderer renderer = DomRenderer();
|
||||
final html.Element element = renderer.createElement('div');
|
||||
renderer.createElement('div', parent: element);
|
||||
expect(element.children, hasLength(1));
|
||||
});
|
||||
|
||||
test('can detach elements', () {
|
||||
final DomRenderer renderer = DomRenderer();
|
||||
final html.Element element = renderer.createElement('div');
|
||||
@@ -117,4 +131,37 @@ void testMain() {
|
||||
isNotEmpty,
|
||||
);
|
||||
});
|
||||
|
||||
test('renders a shadowRoot by default', () {
|
||||
final DomRenderer renderer = DomRenderer();
|
||||
|
||||
HostNode hostNode = renderer.glassPaneShadow!;
|
||||
|
||||
expect(hostNode.node, isA<html.ShadowRoot>());
|
||||
});
|
||||
|
||||
test('starts without shadowDom available too', () {
|
||||
final dynamic oldAttachShadow = attachShadow;
|
||||
expect(oldAttachShadow, isNotNull);
|
||||
|
||||
attachShadow = null; // Break ShadowDOM
|
||||
|
||||
final DomRenderer renderer = DomRenderer();
|
||||
|
||||
HostNode hostNode = renderer.glassPaneShadow!;
|
||||
|
||||
expect(hostNode.node, isA<html.Element>());
|
||||
expect(
|
||||
(hostNode.node as html.Element).tagName,
|
||||
equalsIgnoringCase('flt-element-host-node'),
|
||||
);
|
||||
|
||||
attachShadow = oldAttachShadow; // Restore ShadowDOM
|
||||
});
|
||||
}
|
||||
|
||||
@JS('Element.prototype.attachShadow')
|
||||
external dynamic get attachShadow;
|
||||
|
||||
@JS('Element.prototype.attachShadow')
|
||||
external set attachShadow(dynamic x);
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// 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/src/engine.dart';
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
internalBootstrapBrowserTest(() => testMain);
|
||||
}
|
||||
|
||||
void testMain() {
|
||||
final html.Element rootNode = html.document.createElement('div');
|
||||
html.document.body!.append(rootNode);
|
||||
|
||||
group('ShadowDomHostNode', () {
|
||||
final HostNode hostNode = ShadowDomHostNode(rootNode);
|
||||
|
||||
test('Initializes and attaches a shadow root', () {
|
||||
expect(hostNode.node, isA<html.ShadowRoot>());
|
||||
expect((hostNode.node as html.ShadowRoot).host, rootNode);
|
||||
expect(hostNode.node, rootNode.shadowRoot);
|
||||
});
|
||||
|
||||
test('Attaches a stylesheet to the shadow root', () {
|
||||
final html.Element firstChild =
|
||||
(hostNode.node as html.ShadowRoot).children.first;
|
||||
|
||||
expect(firstChild.tagName, equalsIgnoringCase('style'));
|
||||
});
|
||||
|
||||
_runDomTests(hostNode);
|
||||
});
|
||||
|
||||
group('ElementHostNode', () {
|
||||
final HostNode hostNode = ElementHostNode(rootNode);
|
||||
|
||||
test('Initializes and attaches a child element', () {
|
||||
expect(hostNode.node, isA<html.Element>());
|
||||
expect((hostNode.node as html.Element).shadowRoot, isNull);
|
||||
expect(hostNode.node.parent, rootNode);
|
||||
});
|
||||
|
||||
_runDomTests(hostNode);
|
||||
});
|
||||
}
|
||||
|
||||
// The common test suite that all types of HostNode implementations need to pass.
|
||||
void _runDomTests(HostNode hostNode) {
|
||||
group('DOM operations', () {
|
||||
final html.Element target = html.document.createElement('div')..id = 'yep';
|
||||
|
||||
setUp(() {
|
||||
hostNode.nodes.addAll([
|
||||
html.document.createElement('div'),
|
||||
target,
|
||||
html.document.createElement('span'),
|
||||
html.document.createElement('div'),
|
||||
]);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
hostNode.nodes.clear();
|
||||
});
|
||||
|
||||
test('querySelector', () {
|
||||
final html.Element? found = hostNode.querySelector('#yep');
|
||||
|
||||
expect(identical(found, target), isTrue);
|
||||
});
|
||||
|
||||
test('.contains and .append', () {
|
||||
final html.Element another = html.document.createElement('div')
|
||||
..id = 'another';
|
||||
|
||||
expect(hostNode.contains(target), isTrue);
|
||||
expect(hostNode.contains(another), isFalse);
|
||||
expect(hostNode.contains(null), isFalse);
|
||||
|
||||
hostNode.append(another);
|
||||
expect(hostNode.contains(another), isTrue);
|
||||
});
|
||||
|
||||
test('querySelectorAll', () {
|
||||
final List<html.Node> found = hostNode.querySelectorAll('div');
|
||||
|
||||
expect(found.length, 3);
|
||||
expect(found[1], target);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -87,7 +87,7 @@ void _testEngineSemanticsOwner() {
|
||||
|
||||
// Synthesize a click on the placeholder.
|
||||
final html.Element placeholder =
|
||||
appShadowRoot.querySelector('flt-semantics-placeholder')!;
|
||||
appHostNode.querySelector('flt-semantics-placeholder')!;
|
||||
|
||||
expect(placeholder.isConnected, isTrue);
|
||||
|
||||
@@ -113,7 +113,7 @@ void _testEngineSemanticsOwner() {
|
||||
expect(semantics().semanticsEnabled, isFalse);
|
||||
|
||||
final html.Element placeholder =
|
||||
appShadowRoot.querySelector('flt-semantics-placeholder')!;
|
||||
appHostNode.querySelector('flt-semantics-placeholder')!;
|
||||
|
||||
expect(placeholder.isConnected, isTrue);
|
||||
|
||||
@@ -382,9 +382,9 @@ void _testContainer() {
|
||||
</sem>''');
|
||||
|
||||
final html.Element parentElement =
|
||||
appShadowRoot.querySelector('flt-semantics')!;
|
||||
appHostNode.querySelector('flt-semantics')!;
|
||||
final html.Element container =
|
||||
appShadowRoot.querySelector('flt-semantics-container')!;
|
||||
appHostNode.querySelector('flt-semantics-container')!;
|
||||
|
||||
if (isMacOrIOS) {
|
||||
expect(parentElement.style.top, '0px');
|
||||
@@ -430,9 +430,9 @@ void _testContainer() {
|
||||
</sem>''');
|
||||
|
||||
final html.Element parentElement =
|
||||
appShadowRoot.querySelector('flt-semantics')!;
|
||||
appHostNode.querySelector('flt-semantics')!;
|
||||
final html.Element container =
|
||||
appShadowRoot.querySelector('flt-semantics-container')!;
|
||||
appHostNode.querySelector('flt-semantics-container')!;
|
||||
|
||||
expect(parentElement.style.transform, 'matrix(1, 0, 0, 1, 10, 10)');
|
||||
expect(parentElement.style.transformOrigin, '0px 0px 0px');
|
||||
@@ -476,9 +476,9 @@ void _testContainer() {
|
||||
}
|
||||
|
||||
final html.Element parentElement =
|
||||
appShadowRoot.querySelector('flt-semantics')!;
|
||||
appHostNode.querySelector('flt-semantics')!;
|
||||
final html.Element container =
|
||||
appShadowRoot.querySelector('flt-semantics-container')!;
|
||||
appHostNode.querySelector('flt-semantics-container')!;
|
||||
|
||||
if (isMacOrIOS) {
|
||||
expect(parentElement.style.top, '0px');
|
||||
@@ -834,7 +834,7 @@ void _testIncrementables() {
|
||||
<input aria-valuenow="1" aria-valuetext="d" aria-valuemax="2" aria-valuemin="1">
|
||||
</sem>''');
|
||||
|
||||
final html.InputElement input = appShadowRoot.querySelector('input') as html.InputElement;
|
||||
final html.InputElement input = appHostNode.querySelector('input') as html.InputElement;
|
||||
input.value = '2';
|
||||
input.dispatchEvent(html.Event('change'));
|
||||
|
||||
@@ -868,7 +868,7 @@ void _testIncrementables() {
|
||||
<input aria-valuenow="1" aria-valuetext="d" aria-valuemax="1" aria-valuemin="0">
|
||||
</sem>''');
|
||||
|
||||
final html.InputElement input = appShadowRoot.querySelector('input') as html.InputElement;
|
||||
final html.InputElement input = appHostNode.querySelector('input') as html.InputElement;
|
||||
input.value = '0';
|
||||
input.dispatchEvent(html.Event('change'));
|
||||
|
||||
@@ -962,13 +962,13 @@ void _testTextField() {
|
||||
semantics().updateSemantics(builder.build());
|
||||
|
||||
final html.Element textField =
|
||||
appShadowRoot.querySelector('input[data-semantics-role="text-field"]')!;
|
||||
appHostNode.querySelector('input[data-semantics-role="text-field"]')!;
|
||||
|
||||
expect(appShadowRoot.activeElement, isNot(textField));
|
||||
expect(appHostNode.activeElement, isNot(textField));
|
||||
|
||||
textField.focus();
|
||||
|
||||
expect(appShadowRoot.activeElement, textField);
|
||||
expect(appHostNode.activeElement, textField);
|
||||
expect(await logger.idLog.first, 0);
|
||||
expect(await logger.actionLog.first, ui.SemanticsAction.tap);
|
||||
|
||||
|
||||
@@ -18,14 +18,7 @@ import '../../matchers.dart';
|
||||
/// so we don't have to hardcode html.document across the test. (The host of a
|
||||
/// normal flutter app used to be html.document, but now that the app is wrapped
|
||||
/// in a Shadow DOM, that's not the case anymore.)
|
||||
///
|
||||
/// A [html.ShadowRoot] quacks very similarly to a [html.Document], but
|
||||
/// unfortunately they don't share any class/implement any interface that let us
|
||||
/// use them interchangeably.
|
||||
///
|
||||
/// This flutterRoot can be changed to return ShadowRoot or Document without
|
||||
/// the need to modify (most of) your code.
|
||||
html.ShadowRoot get appShadowRoot => domRenderer.glassPaneShadow!;
|
||||
HostNode get appHostNode => domRenderer.glassPaneShadow!;
|
||||
|
||||
/// CSS style applied to the root of the semantics tree.
|
||||
// TODO(yjbanov): this should be handled internally by [expectSemanticsTree].
|
||||
@@ -351,14 +344,14 @@ class SemanticsTester {
|
||||
/// Verifies the HTML structure of the current semantics tree.
|
||||
void expectSemanticsTree(String semanticsHtml) {
|
||||
expect(
|
||||
canonicalizeHtml(appShadowRoot.querySelector('flt-semantics')!.outerHtml!),
|
||||
canonicalizeHtml(appHostNode.querySelector('flt-semantics')!.outerHtml!),
|
||||
canonicalizeHtml(semanticsHtml),
|
||||
);
|
||||
}
|
||||
|
||||
/// Finds the first HTML element in the semantics tree used for scrolling.
|
||||
html.Element? findScrollable() {
|
||||
return appShadowRoot.querySelectorAll('flt-semantics').cast<html.Element?>().firstWhere(
|
||||
return appHostNode.querySelectorAll('flt-semantics').cast<html.Element?>().firstWhere(
|
||||
(html.Element? element) =>
|
||||
element!.style.overflow == 'hidden' ||
|
||||
element.style.overflowY == 'scroll' ||
|
||||
|
||||
@@ -75,14 +75,14 @@ void testMain() {
|
||||
|
||||
createTextFieldSemantics(value: 'hello');
|
||||
|
||||
final html.Element textField = appShadowRoot
|
||||
final html.Element textField = appHostNode
|
||||
.querySelector('input[data-semantics-role="text-field"]')!;
|
||||
|
||||
expect(appShadowRoot.activeElement, isNot(textField));
|
||||
expect(appHostNode.activeElement, isNot(textField));
|
||||
|
||||
textField.focus();
|
||||
|
||||
expect(appShadowRoot.activeElement, textField);
|
||||
expect(appHostNode.activeElement, textField);
|
||||
expect(await logger.idLog.first, 0);
|
||||
expect(await logger.actionLog.first, ui.SemanticsAction.tap);
|
||||
|
||||
@@ -98,7 +98,7 @@ void testMain() {
|
||||
..semanticsEnabled = true;
|
||||
|
||||
expect(html.document.activeElement, html.document.body);
|
||||
expect(appShadowRoot.activeElement, null);
|
||||
expect(appHostNode.activeElement, null);
|
||||
|
||||
int changeCount = 0;
|
||||
int actionCount = 0;
|
||||
@@ -122,7 +122,7 @@ void testMain() {
|
||||
|
||||
TextField textField = textFieldSemantics.debugRoleManagerFor(Role.textField) as TextField;
|
||||
expect(html.document.activeElement, domRenderer.glassPaneElement);
|
||||
expect(appShadowRoot.activeElement, strategy.domElement);
|
||||
expect(appHostNode.activeElement, strategy.domElement);
|
||||
expect(textField.editableElement, strategy.domElement);
|
||||
expect((textField.editableElement as dynamic).value, 'hello');
|
||||
expect(textField.editableElement.getAttribute('aria-label'), 'greeting');
|
||||
@@ -138,7 +138,7 @@ void testMain() {
|
||||
);
|
||||
|
||||
expect(html.document.activeElement, html.document.body);
|
||||
expect(appShadowRoot.activeElement, null);
|
||||
expect(appHostNode.activeElement, null);
|
||||
expect(strategy.domElement, null);
|
||||
expect((textField.editableElement as dynamic).value, 'bye');
|
||||
expect(textField.editableElement.getAttribute('aria-label'), 'farewell');
|
||||
@@ -160,7 +160,7 @@ void testMain() {
|
||||
..semanticsEnabled = true;
|
||||
|
||||
expect(html.document.activeElement, html.document.body);
|
||||
expect(appShadowRoot.activeElement, null);
|
||||
expect(appHostNode.activeElement, null);
|
||||
|
||||
strategy.enable(
|
||||
singlelineConfig,
|
||||
@@ -175,12 +175,12 @@ void testMain() {
|
||||
final TextField textField = textFieldSemantics.debugRoleManagerFor(Role.textField) as TextField;
|
||||
expect(textField.editableElement, strategy.domElement);
|
||||
expect(html.document.activeElement, domRenderer.glassPaneElement);
|
||||
expect(appShadowRoot.activeElement, strategy.domElement);
|
||||
expect(appHostNode.activeElement, strategy.domElement);
|
||||
|
||||
// The input should not refocus after blur.
|
||||
textField.editableElement.blur();
|
||||
expect(html.document.activeElement, html.document.body);
|
||||
expect(appShadowRoot.activeElement, null);
|
||||
expect(appHostNode.activeElement, null);
|
||||
strategy.disable();
|
||||
semantics().semanticsEnabled = false;
|
||||
});
|
||||
@@ -206,18 +206,18 @@ void testMain() {
|
||||
);
|
||||
expect(strategy.domElement, isNotNull);
|
||||
expect(html.document.activeElement, domRenderer.glassPaneElement);
|
||||
expect(appShadowRoot.activeElement, strategy.domElement);
|
||||
expect(appHostNode.activeElement, strategy.domElement);
|
||||
|
||||
strategy.disable();
|
||||
expect(strategy.domElement, isNull);
|
||||
|
||||
// It doesn't remove the DOM element.
|
||||
final TextField textField = textFieldSemantics.debugRoleManagerFor(Role.textField) as TextField;
|
||||
expect(appShadowRoot.contains(textField.editableElement), isTrue);
|
||||
expect(appHostNode.contains(textField.editableElement), isTrue);
|
||||
// Editing element is not enabled.
|
||||
expect(strategy.isEnabled, isFalse);
|
||||
expect(html.document.activeElement, html.document.body);
|
||||
expect(appShadowRoot.activeElement, null);
|
||||
expect(appHostNode.activeElement, null);
|
||||
semantics().semanticsEnabled = false;
|
||||
});
|
||||
|
||||
@@ -238,12 +238,12 @@ void testMain() {
|
||||
);
|
||||
expect(strategy.domElement, isNotNull);
|
||||
expect(html.document.activeElement, domRenderer.glassPaneElement);
|
||||
expect(appShadowRoot.activeElement, strategy.domElement);
|
||||
expect(appHostNode.activeElement, strategy.domElement);
|
||||
|
||||
// Blur the element without telling the framework.
|
||||
strategy.activeDomElement.blur();
|
||||
expect(html.document.activeElement, html.document.body);
|
||||
expect(appShadowRoot.activeElement, null);
|
||||
expect(appHostNode.activeElement, null);
|
||||
|
||||
// The input will have focus after editing state is set and semantics updated.
|
||||
strategy.setEditingState(EditingState(text: 'foo'));
|
||||
@@ -262,7 +262,7 @@ void testMain() {
|
||||
isFocused: true,
|
||||
);
|
||||
expect(html.document.activeElement, domRenderer.glassPaneElement);
|
||||
expect(appShadowRoot.activeElement, strategy.domElement);
|
||||
expect(appHostNode.activeElement, strategy.domElement);
|
||||
|
||||
strategy.disable();
|
||||
semantics().semanticsEnabled = false;
|
||||
@@ -287,7 +287,7 @@ void testMain() {
|
||||
final html.TextAreaElement textArea = strategy.domElement as html.TextAreaElement;
|
||||
|
||||
expect(html.document.activeElement, domRenderer.glassPaneElement);
|
||||
expect(appShadowRoot.activeElement, strategy.domElement);
|
||||
expect(appHostNode.activeElement, strategy.domElement);
|
||||
|
||||
strategy.enable(
|
||||
singlelineConfig,
|
||||
@@ -297,11 +297,11 @@ void testMain() {
|
||||
|
||||
textArea.blur();
|
||||
expect(html.document.activeElement, html.document.body);
|
||||
expect(appShadowRoot.activeElement, null);
|
||||
expect(appHostNode.activeElement, null);
|
||||
|
||||
strategy.disable();
|
||||
// It doesn't remove the textarea from the DOM.
|
||||
expect(appShadowRoot.contains(textArea), isTrue);
|
||||
expect(appHostNode.contains(textArea), isTrue);
|
||||
// Editing element is not enabled.
|
||||
expect(strategy.isEnabled, isFalse);
|
||||
semantics().semanticsEnabled = false;
|
||||
@@ -393,12 +393,12 @@ void testMain() {
|
||||
expect(tester.apply().length, 3);
|
||||
|
||||
expect(html.document.activeElement, domRenderer.glassPaneElement);
|
||||
expect(appShadowRoot.activeElement, tester.getTextField(1).editableElement);
|
||||
expect(appHostNode.activeElement, tester.getTextField(1).editableElement);
|
||||
expect(strategy.domElement, tester.getTextField(1).editableElement);
|
||||
|
||||
createTwoFieldSemantics(tester, focusFieldId: 2);
|
||||
expect(tester.apply().length, 3);
|
||||
expect(appShadowRoot.activeElement, tester.getTextField(2).editableElement);
|
||||
expect(appHostNode.activeElement, tester.getTextField(2).editableElement);
|
||||
expect(strategy.domElement, tester.getTextField(2).editableElement);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user