Skip to content

Commit 83b72ba

Browse files
authored
[web] Use TrustedTypes from pkg web. (flutter#6273)
During the `package:web` migration, some packages that needed the TrustedTypes API defined those as custom JS-interop. In `google_identity_services_web`, the names of the JS-interop types clashed with those from the incoming package:web, breaking the build. In `google_maps_flutter_web`, the whole definition code is redundant now that the standard API is exposed through package:web. Part of: flutter#117022
1 parent 4200177 commit 83b72ba

File tree

9 files changed

+45
-128
lines changed

9 files changed

+45
-128
lines changed

packages/google_identity_services_web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.1+1
2+
3+
* Uses `TrustedTypes` from `web: ^0.5.1`.
4+
15
## 0.3.1
26

37
* Updates web code to package `web: ^0.5.0`.

packages/google_identity_services_web/example/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ description: An example for the google_identity_services_web package, OneTap.
33
publish_to: 'none'
44

55
environment:
6-
flutter: ">=3.16.0"
7-
sdk: ">=3.2.0 <4.0.0"
6+
sdk: ^3.3.0
7+
flutter: ">=3.19.0"
88

99
dependencies:
1010
flutter:
1111
sdk: flutter
1212
google_identity_services_web:
1313
path: ../
1414
http: ">=0.13.0 <2.0.0"
15-
web: ^0.5.0
15+
web: ^0.5.1
1616

1717
dev_dependencies:
1818
build_runner: ^2.1.10 # To extract README excerpts only.

packages/google_identity_services_web/lib/src/js_interop/package_web_tweaks.dart

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,20 @@ extension NullableTrustedTypesGetter on web.Window {
1717
///
1818
/// See: https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API
1919
@JS('trustedTypes')
20-
external TrustedTypePolicyFactory? get nullableTrustedTypes;
21-
22-
/// Bindings to window.trustedTypes.
23-
///
24-
/// This will crash if accessed in a browser that doesn't support the
25-
/// Trusted Types API.
26-
///
27-
/// See: https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API
28-
@JS('trustedTypes')
29-
external TrustedTypePolicyFactory get trustedTypes;
20+
external web.TrustedTypePolicyFactory? get nullableTrustedTypes;
3021
}
3122

32-
/// This extension allows setting a TrustedScriptURL as the src of a script element,
33-
/// which currently only accepts a string.
23+
/// Allows setting a TrustedScriptURL as the src of a script element.
3424
extension TrustedTypeSrcAttribute on web.HTMLScriptElement {
3525
@JS('src')
36-
external set trustedSrc(TrustedScriptURL value);
37-
}
38-
39-
// TODO(kevmoo): drop all of this once `pkg:web` publishes `0.5.1`.
40-
41-
/// Bindings to a JS TrustedScriptURL.
42-
///
43-
/// See: https://developer.mozilla.org/en-US/docs/Web/API/TrustedScriptURL
44-
extension type TrustedScriptURL._(JSObject _) implements JSObject {}
45-
46-
/// Bindings to a JS TrustedTypePolicyFactory.
47-
///
48-
/// See: https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicyFactory
49-
extension type TrustedTypePolicyFactory._(JSObject _) implements JSObject {
50-
///
51-
external TrustedTypePolicy createPolicy(
52-
String policyName, [
53-
TrustedTypePolicyOptions policyOptions,
54-
]);
26+
external set trustedSrc(web.TrustedScriptURL value);
5527
}
5628

57-
/// Bindings to a JS TrustedTypePolicy.
58-
///
59-
/// See: https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicy
60-
extension type TrustedTypePolicy._(JSObject _) implements JSObject {
61-
///
29+
/// Allows creating a script URL only from a string, with no arguments.
30+
extension CreateScriptUrlNoArgs on web.TrustedTypePolicy {
31+
/// Allows calling `createScriptURL` with only the `input` argument.
6232
@JS('createScriptURL')
63-
external TrustedScriptURL createScriptURLNoArgs(
33+
external web.TrustedScriptURL createScriptURLNoArgs(
6434
String input,
6535
);
6636
}
67-
68-
/// Bindings to a JS TrustedTypePolicyOptions (anonymous).
69-
///
70-
/// See: https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicyFactory/createPolicy#policyoptions
71-
extension type TrustedTypePolicyOptions._(JSObject _) implements JSObject {
72-
///
73-
external factory TrustedTypePolicyOptions({
74-
JSFunction createScriptURL,
75-
});
76-
}

packages/google_identity_services_web/lib/src/js_loader.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ Future<void> loadWebSdk({
2525
onGoogleLibraryLoad = () => completer.complete();
2626

2727
// If TrustedTypes are available, prepare a trusted URL.
28-
TrustedScriptURL? trustedUrl;
28+
web.TrustedScriptURL? trustedUrl;
2929
if (web.window.nullableTrustedTypes != null) {
3030
web.console.debug(
3131
'TrustedTypes available. Creating policy: $trustedTypePolicyName'.toJS,
3232
);
3333
try {
34-
final TrustedTypePolicy policy = web.window.trustedTypes.createPolicy(
34+
final web.TrustedTypePolicy policy = web.window.trustedTypes.createPolicy(
3535
trustedTypePolicyName,
36-
TrustedTypePolicyOptions(
36+
web.TrustedTypePolicyOptions(
3737
createScriptURL: ((JSString url) => _url).toJS,
3838
));
3939
trustedUrl = policy.createScriptURLNoArgs(_url);

packages/google_identity_services_web/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ name: google_identity_services_web
22
description: A Dart JS-interop layer for Google Identity Services. Google's new sign-in SDK for Web that supports multiple types of credentials.
33
repository: https://github.com/flutter/packages/tree/main/packages/google_identity_services_web
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_identiy_services_web%22
5-
version: 0.3.1
5+
version: 0.3.1+1
66

77
environment:
88
sdk: ^3.3.0
99

1010
dependencies:
1111
meta: ^1.3.0
12-
web: ^0.5.0
12+
web: ^0.5.1
1313

1414
dev_dependencies:
1515
path: ^1.8.1

packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.5.6+2
2+
3+
* Uses `TrustedTypes` from `web: ^0.5.1`.
4+
15
## 0.5.6+1
26

37
* Fixes an issue where `dart:js_interop` object literal factories did not

packages/google_maps_flutter/google_maps_flutter_web/lib/src/convert.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ final gmaps.LatLng _nullGmapsLatLng = gmaps.LatLng(0, 0);
99
final gmaps.LatLngBounds _nullGmapsLatLngBounds =
1010
gmaps.LatLngBounds(_nullGmapsLatLng, _nullGmapsLatLng);
1111

12+
// The TrustedType Policy used by this plugin. Used to sanitize InfoWindow contents.
13+
TrustedTypePolicy? _gmapsTrustedTypePolicy;
14+
1215
// Converts a [Color] into a valid CSS value #RRGGBB.
1316
String _getCssColor(Color color) {
1417
return '#${color.value.toRadixString(16).padLeft(8, '0').substring(2)}';
@@ -222,17 +225,17 @@ gmaps.InfoWindowOptions? _infoWindowOptionsFromMarker(Marker marker) {
222225
// Firefox and Safari don't support Trusted Types yet.
223226
// See https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicyFactory#browser_compatibility
224227
if (window.nullableTrustedTypes != null) {
225-
final GoogleMapsTrustedTypePolicy trustedTypePolicy =
226-
window.nullableTrustedTypes!.getGoogleMapsTrustedTypesPolicy(
227-
GoogleMapsTrustedTypePolicyOptions(
228-
createHTML: (String html, JSAny? arguments) {
229-
return sanitizeHtml(html);
228+
_gmapsTrustedTypePolicy ??= window.trustedTypes.createPolicy(
229+
'google_maps_flutter_sanitize',
230+
TrustedTypePolicyOptions(
231+
createHTML: (String html) {
232+
return sanitizeHtml(html).toJS;
230233
}.toJS,
231234
),
232235
);
233236

234237
snippet.trustedInnerHTML =
235-
trustedTypePolicy.createHTML(markerSnippet, null);
238+
_gmapsTrustedTypePolicy!.createHTMLNoArgs(markerSnippet);
236239
} else {
237240
// `sanitizeHtml` is used to clean the (potential) user input from (potential)
238241
// XSS attacks through the contents of the marker InfoWindow.

packages/google_maps_flutter/google_maps_flutter_web/lib/src/dom_window_extension.dart

Lines changed: 11 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
library;
1010

1111
import 'dart:js_interop';
12-
1312
import 'package:web/web.dart' as web;
1413

1514
/// This extension gives [web.Window] a nullable getter to the `trustedTypes`
@@ -21,75 +20,22 @@ extension NullableTrustedTypesGetter on web.Window {
2120
///
2221
/// See: https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API
2322
@JS('trustedTypes')
24-
external GoogleMapsTrustedTypePolicyFactory? get nullableTrustedTypes;
25-
}
26-
27-
// TODO(ditman): remove this extension type when we depend on package:web 0.5.1
28-
/// This extension exists as a stop gap until `package:web 0.5.1` is released.
29-
/// That version provides the `TrustedTypes` API.
30-
@JS('TrustedTypePolicyFactory')
31-
extension type GoogleMapsTrustedTypePolicyFactory._(JSObject _)
32-
implements JSObject {
33-
/// The `TrustedTypePolicy` for Google Maps Flutter.
34-
static GoogleMapsTrustedTypePolicy? _policy;
35-
36-
@JS('createPolicy')
37-
external GoogleMapsTrustedTypePolicy _createPolicy(
38-
String policyName, [
39-
GoogleMapsTrustedTypePolicyOptions policyOptions,
40-
]);
41-
42-
/// Get a new [GoogleMapsTrustedTypePolicy].
43-
///
44-
/// If a policy already exists, it will be returned.
45-
/// Otherwise, a new policy is created.
46-
///
47-
/// Because of we only cache one _policy, this method
48-
/// specifically hardcoded to the GoogleMaps use case.
49-
GoogleMapsTrustedTypePolicy getGoogleMapsTrustedTypesPolicy(
50-
GoogleMapsTrustedTypePolicyOptions policyOptions,
51-
) {
52-
const String policyName = 'google_maps_flutter_sanitize';
53-
_policy ??= _createPolicy(policyName, policyOptions);
54-
55-
return _policy!;
56-
}
23+
external web.TrustedTypePolicyFactory? get nullableTrustedTypes;
5724
}
5825

59-
// TODO(ditman): remove this extension type when we depend on package:web 0.5.1
60-
/// This extension exists as a stop gap until `package:web 0.5.1` is released.
61-
/// That version provides the `TrustedTypes` API.
62-
@JS('TrustedTypePolicy')
63-
extension type GoogleMapsTrustedTypePolicy._(JSObject _) implements JSObject {
64-
/// Create a new `TrustedHTML` instance with the given [input] and [arguments].
65-
external GoogleMapsTrustedHTML createHTML(
66-
String input,
67-
JSAny? arguments,
68-
);
69-
}
70-
71-
// TODO(ditman): remove this extension type when we depend on package:web 0.5.1
72-
/// This extension exists as a stop gap until `package:web 0.5.1` is released.
73-
/// That version provides the `TrustedTypes` API.
74-
@JS('TrustedTypePolicyOptions')
75-
extension type GoogleMapsTrustedTypePolicyOptions._(JSObject _)
76-
implements JSObject {
77-
/// Create a new `TrustedTypePolicyOptions` instance.
78-
external factory GoogleMapsTrustedTypePolicyOptions({
79-
JSFunction createHTML,
80-
});
81-
}
82-
83-
// TODO(ditman): remove this extension type when we depend on package:web 0.5.1
84-
/// This extension exists as a stop gap until `package:web 0.5.1` is released.
85-
/// That version provides the `TrustedTypes` API.
86-
@JS('TrustedHTML')
87-
extension type GoogleMapsTrustedHTML._(JSObject _) implements JSObject {}
88-
8926
/// This extension provides a setter for the [web.HTMLElement] `innerHTML` property,
9027
/// that accepts trusted HTML only.
9128
extension TrustedInnerHTML on web.HTMLElement {
9229
/// Set the inner HTML of this element to the given [trustedHTML].
9330
@JS('innerHTML')
94-
external set trustedInnerHTML(GoogleMapsTrustedHTML trustedHTML);
31+
external set trustedInnerHTML(web.TrustedHTML trustedHTML);
32+
}
33+
34+
/// Allows creating a TrustedHTML object from a string, with no arguments.
35+
extension CreateHTMLNoArgs on web.TrustedTypePolicy {
36+
/// Allows calling `createHTML` with only the `input` argument.
37+
@JS('createHTML')
38+
external web.TrustedHTML createHTMLNoArgs(
39+
String input,
40+
);
9541
}

packages/google_maps_flutter/google_maps_flutter_web/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: google_maps_flutter_web
22
description: Web platform implementation of google_maps_flutter
33
repository: https://github.com/flutter/packages/tree/main/packages/google_maps_flutter/google_maps_flutter_web
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
5-
version: 0.5.6+1
5+
version: 0.5.6+2
66

77
environment:
88
sdk: ^3.3.0
@@ -26,7 +26,7 @@ dependencies:
2626
google_maps_flutter_platform_interface: ^2.5.0
2727
sanitize_html: ^2.0.0
2828
stream_transform: ^2.0.0
29-
web: ^0.5.0
29+
web: ^0.5.1
3030

3131
dev_dependencies:
3232
flutter_test:

0 commit comments

Comments
 (0)