Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/web_ui/lib/src/engine/platform_views/content_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ import 'slots.dart';
/// This class keeps a registry of `factories`, `contents` so the framework can
/// CRUD Platform Views as needed, regardless of the rendering backend.
class PlatformViewManager {
PlatformViewManager() {
// Register some default factories.
registerFactory(
ui_web.kDefaultVisibleViewType,
_defaultFactory,
);
registerFactory(
ui_web.kDefaultInvisibleViewType,
_defaultFactory,
isVisible: false,
);
}

// The factory functions, indexed by the viewType
final Map<String, Function> _factories = <String, Function>{};

Expand Down Expand Up @@ -223,3 +236,12 @@ class PlatformViewManager {
return result;
}
}

DomElement _defaultFactory(
int viewId, {
Object? params,
}) {
params!;
params as Map<Object?, Object?>;
return domDocument.createElement(params.readString('tagName'));
}
12 changes: 12 additions & 0 deletions lib/web_ui/lib/ui_web/src/ui_web/platform_view_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

import 'package:ui/src/engine.dart';

/// The view type of the factory that creates visible platform view DOM elements.
///
/// There's no need to register this view type with [PlatformViewRegistry]
/// because it is registered by default.
const String kDefaultVisibleViewType = '_default_document_create_element_visible';

/// The view type of the factory that creates invisible platform view DOM elements.
///
/// There's no need to register this view type with [PlatformViewRegistry]
/// because it is registered by default.
const String kDefaultInvisibleViewType = '_default_document_create_element_invisible';

/// A function which takes a unique `id` and some `params` and creates an HTML
/// element.
typedef ParameterizedPlatformViewFactory = Object Function(
Expand Down
56 changes: 47 additions & 9 deletions lib/web_ui/test/engine/platform_views/content_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';
import 'package:ui/src/engine.dart';

import '../../common/matchers.dart';
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;

void main() {
internalBootstrapBrowserTest(() => testMain);
Expand Down Expand Up @@ -97,13 +96,14 @@ void testMain() {
});

test('refuse to render views for unregistered factories', () async {
try {
contentManager.renderContent(unregisteredViewType, viewId, null);
fail('renderContent should have thrown an Assertion error!');
} catch (e) {
expect(e, isAssertionError);
expect((e as AssertionError).message, contains(unregisteredViewType));
}
expect(
() => contentManager.renderContent(unregisteredViewType, viewId, null),
throwsA(const TypeMatcher<AssertionError>().having(
(AssertionError error) => error.message,
'assertion message',
contains(unregisteredViewType),
)),
);
});

test('rendered markup contains required attributes', () async {
Expand Down Expand Up @@ -203,5 +203,43 @@ void testMain() {
}, throwsA(isA<AssertionError>()));
});
});

test('default factories', () {
final DomElement content0 = contentManager.renderContent(
ui_web.kDefaultVisibleViewType,
viewId,
<dynamic, dynamic>{'tagName': 'table'},
);
expect(
contentManager.getViewById(viewId),
content0.querySelector('table'),
);
expect(contentManager.isVisible(viewId), isTrue);
expect(contentManager.isInvisible(viewId), isFalse);

final DomElement content1 = contentManager.renderContent(
ui_web.kDefaultInvisibleViewType,
viewId + 1,
<dynamic, dynamic>{'tagName': 'script'},
);
expect(
contentManager.getViewById(viewId + 1),
content1.querySelector('script'),
);
expect(contentManager.isVisible(viewId + 1), isFalse);
expect(contentManager.isInvisible(viewId + 1), isTrue);

final DomElement content2 = contentManager.renderContent(
ui_web.kDefaultVisibleViewType,
viewId + 2,
<dynamic, dynamic>{'tagName': 'p'},
);
expect(
contentManager.getViewById(viewId + 2),
content2.querySelector('p'),
);
expect(contentManager.isVisible(viewId + 2), isTrue);
expect(contentManager.isInvisible(viewId + 2), isFalse);
});
});
}