[web] retrieve hostElement for an implicit view (flutter/engine#53296)

This PR updates `ui_web.views.getHostElement` API to also return `hostElement` for implicit view.

Resolves https://github.com/flutter/flutter/issues/150075
This commit is contained in:
Pavel Mazhnik
2024-07-12 01:15:20 +03:00
committed by GitHub
parent ac871bacc2
commit d809a1db38
4 changed files with 39 additions and 5 deletions

View File

@@ -26,7 +26,8 @@ abstract class EmbeddingStrategy {
}
}
/// The host element in which the Flutter view is embedded.
/// The DOM element in which the Flutter view is embedded.
/// This element is the direct parent element of the <flutter-view> element.
DomElement get hostElement;
/// The global event target for the Flutter view.

View File

@@ -99,6 +99,11 @@ class FlutterViewManager {
return _jsViewOptions[viewId];
}
/// Returns the DOM element in which the Flutter view associated to [viewId] is embedded.
DomElement? getHostElement(int viewId) {
return _viewData[viewId]?.embeddingStrategy.hostElement;
}
EngineFlutterView? findViewForElement(DomElement? element) {
const String viewRootSelector =
'${DomManager.flutterViewTagName}[${GlobalHtmlAttributes.flutterViewIdAttributeName}]';

View File

@@ -17,13 +17,19 @@ class FlutterViewManagerProxy {
// The proxied viewManager instance.
final FlutterViewManager _viewManager;
/// Returns the `hostElement` configuration value passed from JS when `viewId` was added.
/// Returns the host element for [viewId].
///
/// In the full-page mode, the host element is the `<body>` element of the page
/// and the view is the one and only [PlatformDispatcher.implicitView].
///
/// In the add-to-app mode, the host element is the value of `hostElement`
/// provided when creating the view.
///
/// This is useful for plugins and apps to have a safe DOM Element where they
/// can add their own custom HTML elements (for example: file inputs for the
/// file_selector plugin).
JSAny? getHostElement(int viewId) {
return _viewManager.getOptions(viewId)?.hostElement as JSAny?;
return _viewManager.getHostElement(viewId) as JSAny?;
}
/// Returns the `initialData` configuration value passed from JS when `viewId` was added.

View File

@@ -36,9 +36,9 @@ Future<void> doTests() async {
}
setUp(() {
view = EngineFlutterView(platformDispatcher, createDomElement('div'));
viewId = view.viewId;
hostElement = createDomElement('div');
view = EngineFlutterView(platformDispatcher, hostElement);
viewId = view.viewId;
});
tearDown(() {
@@ -60,6 +60,28 @@ Future<void> doTests() async {
expect(element, hostElement);
});
test('can retrieve hostElement for an implicit view with default host element', () {
final view = EngineFlutterView.implicit(platformDispatcher, null);
final viewId = view.viewId;
viewManager.registerView(view);
addTearDown(() => viewManager.unregisterView(viewId));
final JSAny? element = views.getHostElement(viewId);
expect(element, domDocument.body);
});
test('can retrieve hostElement for an implicit view with custom host element', () {
final view = EngineFlutterView.implicit(platformDispatcher, hostElement);
final viewId = view.viewId;
viewManager.registerView(view);
addTearDown(() => viewManager.unregisterView(viewId));
final JSAny? element = views.getHostElement(viewId);
expect(element, hostElement);
});
});
group('getInitialData', () {