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 2 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
32 changes: 32 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 @@ -10,6 +10,10 @@ import '../embedder.dart';
import '../util.dart';
import 'slots.dart';

// Keep these values in sync with the ones used in the framework's `HtmlElementView`.
const String _kDefaultVisibleViewType = '_default_document_create_element_visible';
const String _kDefaultInvisibleViewType = '_default_document_create_element_invisible';

/// This class handles the lifecycle of Platform Views in the DOM of a Flutter Web App.
///
/// There are three important parts of Platform Views. This class manages two of
Expand All @@ -25,6 +29,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(
_kDefaultVisibleViewType,
_defaultFactory,
);
registerFactory(
_kDefaultInvisibleViewType,
_defaultFactory,
isVisible: false,
);
}

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

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

DomElement _defaultFactory(
int viewId, {
Object? params,
}) {
params!;
params as _DefaultFactoryParams;
return domDocument.createElement(params.tagName);
}

typedef _DefaultFactoryParams = Map<Object?, Object?>;

extension on _DefaultFactoryParams {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a wacky syntax we can get with extensions, I love it (in small doses) xD

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually removed this because I was able to do the same thing using the existing readX extensions:

extension JsonExtensions on Map<dynamic, dynamic> {

String get tagName => this['tagName']! as String;
}
56 changes: 48 additions & 8 deletions lib/web_ui/test/engine/platform_views/content_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';
import 'package:ui/src/engine.dart';

import '../../common/matchers.dart';
const String _kDefaultVisibleViewType = '_default_document_create_element_visible';
const String _kDefaultInvisibleViewType = '_default_document_create_element_invisible';

void main() {
internalBootstrapBrowserTest(() => testMain);
Expand Down Expand Up @@ -97,13 +98,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 +205,43 @@ void testMain() {
}, throwsA(isA<AssertionError>()));
});
});

test('default factories', () {
final DomElement content0 = contentManager.renderContent(
_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(
_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(
_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);
});
});
}