Skip to content

Commit

Permalink
[web] Updates package:web dependency to ^0.5.0. (#5791)
Browse files Browse the repository at this point in the history
This PR updates the packages that are using `package:web` to version `^0.5.0`.

Ancilliary changes:

* Bump `environment` to `flutter: ">=3.19.0"` and `sdk: ^3.3.0`.
* Bump version to next `Y`
* Clean-up code that was kept for compatibility with versions of `web: <0.5.0`.

The main exception to this is `package:google_sign_in_web`, which depends on a version of `google_identity_services_web` that has a dependency on package:web that is `<0.5.0`, so that package needs to have a range until `google_identity_services_web` gets published with the new ^0.5.0 dependency.

Co-Authored-By: David Iglesias<[email protected]>
  • Loading branch information
kevmoo authored Feb 20, 2024
1 parent 8bba41b commit b21dce5
Show file tree
Hide file tree
Showing 44 changed files with 500 additions and 290 deletions.
5 changes: 3 additions & 2 deletions packages/cross_file/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT
## 0.3.4

* Updates minimum supported SDK version to Flutter 3.13/Dart 3.1.
* Updates to web code to package `web: ^0.5.0`.
* Updates SDK version to Dart `^3.3.0`.

## 0.3.3+8

Expand Down
8 changes: 2 additions & 6 deletions packages/cross_file/lib/src/types/html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ class XFile extends XFileBase {
super(path) {
if (path == null) {
_browserBlob = _createBlobFromBytes(bytes, mimeType);
// TODO(kevmoo): drop ignore when pkg:web constraint excludes v0.3
// ignore: unnecessary_cast
_path = URL.createObjectURL(_browserBlob! as JSObject);
_path = URL.createObjectURL(_browserBlob!);
} else {
_path = path;
}
Expand Down Expand Up @@ -131,9 +129,7 @@ class XFile extends XFileBase {

// Attempt to re-hydrate the blob from the `path` via a (local) HttpRequest.
// Note that safari hangs if the Blob is >=4GB, so bail out in that case.
// TODO(kevmoo): Remove ignore and fix when the MIN Dart SDK is 3.3
// ignore: unnecessary_non_null_assertion
if (isSafari() && _length != null && _length! >= _fourGigabytes) {
if (isSafari() && _length != null && _length >= _fourGigabytes) {
throw Exception('Safari cannot handle XFiles larger than 4GB.');
}

Expand Down
6 changes: 3 additions & 3 deletions packages/cross_file/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ name: cross_file
description: An abstraction to allow working with files across multiple platforms.
repository: https://github.com/flutter/packages/tree/main/packages/cross_file
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+cross_file%22
version: 0.3.3+8
version: 0.3.4

environment:
sdk: ^3.2.0
sdk: ^3.3.0

dependencies:
meta: ^1.3.0
web: '>=0.3.0 <0.5.0'
web: ^0.5.0

dev_dependencies:
path: ^1.8.1
Expand Down
6 changes: 3 additions & 3 deletions packages/cross_file/test/x_file_html_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ void main() {
test('Stores data as a Blob', () async {
// Read the blob from its path 'natively'
final html.Response response =
(await html.window.fetch(file.path.toJS).toDart)! as html.Response;
await html.window.fetch(file.path.toJS).toDart;

final JSAny? arrayBuffer = await response.arrayBuffer().toDart;
final ByteBuffer data = (arrayBuffer! as JSArrayBuffer).toDart;
final JSAny arrayBuffer = await response.arrayBuffer().toDart;
final ByteBuffer data = (arrayBuffer as JSArrayBuffer).toDart;
expect(data.asUint8List(), equals(bytes));
});

Expand Down
5 changes: 3 additions & 2 deletions packages/file_selector/file_selector_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT
## 0.9.4

* Updates minimum supported SDK version to Flutter 3.13/Dart 3.1.
* Updates web code to package `web: ^0.5.0`.
* Updates SDK version to Dart `^3.3.0`. Flutter `^3.16.0`.

## 0.9.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ void main() {

FileList? createFileList(List<File> files) {
final DataTransfer dataTransfer = DataTransfer();
// Tear-offs of external extension type interop member 'add' are disallowed.
// ignore: prefer_foreach
for (final File e in files) {
// TODO(srujzs): This is necessary in order to support package:web 0.4.0.
// This was not needed with 0.3.0, hence the lint.
// ignore: unnecessary_cast
dataTransfer.items.add(e as JSAny);
dataTransfer.items.add(e);
}
return dataTransfer.files;
}
Expand All @@ -46,13 +45,8 @@ void main() {
});

group('getFiles', () {
final File mockFile1 =
// TODO(srujzs): Remove once typed JSArrays (JSArray<T>) get to `stable`.
// ignore: always_specify_types
File(<Object>['123456'].jsify as JSArray, 'file1.txt');
// TODO(srujzs): Remove once typed JSArrays (JSArray<T>) get to `stable`.
// ignore: always_specify_types
final File mockFile2 = File(<Object>[].jsify as JSArray, 'file2.txt');
final File mockFile1 = File(<JSAny>['123456'.toJS].toJS, 'file1.txt');
final File mockFile2 = File(<JSAny>[].toJS, 'file2.txt');

testWidgets('works', (_) async {
final Future<List<XFile>> futureFiles = domHelper.getFiles(
Expand Down Expand Up @@ -114,32 +108,27 @@ void main() {
testWidgets('sets the <input /> attributes and clicks it', (_) async {
const String accept = '.jpg,.png';
const bool multiple = true;
bool wasClicked = false;

//ignore: unawaited_futures
input.onClick.first.then((_) => wasClicked = true);
final Future<bool> wasClicked = input.onClick.first.then((_) => true);

final Future<List<XFile>> futureFile = domHelper.getFiles(
accept: accept,
multiple: multiple,
input: input,
);

expect(input.matches('body'), true);
expect(input.isConnected, true,
reason: 'input must be injected into the DOM');
expect(input.accept, accept);
expect(input.multiple, multiple);
expect(
wasClicked,
true,
reason:
'The <input /> should be clicked otherwise no dialog will be shown',
);
expect(await wasClicked, true,
reason:
'The <input /> should be clicked otherwise no dialog will be shown');

setFilesAndTriggerChange(<File>[]);
await futureFile;

// It should be already removed from the DOM after the file is resolved.
expect(input.parentElement, isNull);
expect(input.isConnected, isFalse);
});
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/file_selector/file_selector_web/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ name: file_selector_web_integration_tests
publish_to: none

environment:
sdk: ^3.1.0
flutter: ">=3.13.0"
sdk: ^3.3.0
flutter: ">=3.19.0"

dependencies:
file_selector_platform_interface: ^2.6.0
file_selector_web:
path: ../
flutter:
sdk: flutter
web: '>=0.3.0 <0.5.0'
web: ^0.5.0

dev_dependencies:
flutter_test:
Expand Down
11 changes: 4 additions & 7 deletions packages/file_selector/file_selector_web/lib/src/dom_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import 'package:web/helpers.dart';
class DomHelper {
/// Default constructor, initializes the container DOM element.
DomHelper() {
final Element body = querySelector('body')!;
final Element body = document.querySelector('body')!;
body.appendChild(_container);
}

final Element _container = createElementTag('file-selector');
final Element _container = document.createElement('file-selector');

/// Sets the <input /> attributes and waits for a file to be selected.
Future<List<XFile>> getFiles({
Expand All @@ -28,7 +28,7 @@ class DomHelper {
}) {
final Completer<List<XFile>> completer = Completer<List<XFile>>();
final HTMLInputElement inputElement =
input ?? (createElementTag('input') as HTMLInputElement)
input ?? (document.createElement('input') as HTMLInputElement)
..type = 'file';

_container.appendChild(
Expand Down Expand Up @@ -72,10 +72,7 @@ class DomHelper {
}

XFile _convertFileToXFile(File file) => XFile(
// TODO(srujzs): This is necessary in order to support package:web 0.4.0.
// This was not needed with 0.3.0, hence the lint.
// ignore: unnecessary_cast
URL.createObjectURL(file as JSObject),
URL.createObjectURL(file),
name: file.name,
length: file.size,
lastModified: DateTime.fromMillisecondsSinceEpoch(file.lastModified),
Expand Down
8 changes: 4 additions & 4 deletions packages/file_selector/file_selector_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ name: file_selector_web
description: Web platform implementation of file_selector
repository: https://github.com/flutter/packages/tree/main/packages/file_selector/file_selector_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
version: 0.9.3
version: 0.9.4

environment:
sdk: ^3.2.0
flutter: ">=3.16.0"
sdk: ^3.3.0
flutter: ">=3.19.0"

flutter:
plugin:
Expand All @@ -22,7 +22,7 @@ dependencies:
sdk: flutter
flutter_web_plugins:
sdk: flutter
web: '>=0.3.0 <0.5.0'
web: ^0.5.0

dev_dependencies:
flutter_test:
Expand Down
5 changes: 5 additions & 0 deletions packages/google_identity_services_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.3.1

* Updates web code to package `web: ^0.5.0`.
* Updates SDK version to Dart `^3.3.0`. Flutter `^3.19.0`.

## 0.3.0+2

* Adds `fedcm_auto` to `CredentialSelectBy` enum.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ void main() async {
expectConfigValue('login_uri', 'https://www.example.com/login');
expectConfigValue('native_callback', utils.isAJs('function'));
expectConfigValue('cancel_on_tap_outside', isFalse);
// TODO(srujzs): Remove once typed JSArrays (JSArray<T>) get to `stable`.
// ignore: always_specify_types
expectConfigValue('allowed_parent_origin', isA<JSArray>());
expectConfigValue('allowed_parent_origin', isA<JSArray<JSString>>());
expectConfigValue('prompt_parent_id', 'some_dom_id');
expectConfigValue('nonce', 's0m3_r4ndOM_vALu3');
expectConfigValue('context', 'signin');
Expand Down
3 changes: 1 addition & 2 deletions packages/google_identity_services_web/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: google_identity_services_web_example
description: An example for the google_identity_services_web package, OneTap.
publish_to: 'none'
version: 0.0.1

environment:
flutter: ">=3.16.0"
Expand All @@ -13,7 +12,7 @@ dependencies:
google_identity_services_web:
path: ../
http: ">=0.13.0 <2.0.0"
web: ">=0.3.0 <0.5.0"
web: ^0.5.0

dev_dependencies:
build_runner: ^2.1.10 # To extract README excerpts only.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,7 @@ abstract class IdConfiguration {
JSString? context,
JSString? state_cookie_domain,
JSString? ux_mode,
// TODO(srujzs): Remove once typed JSArrays (JSArray<T>) get to `stable`.
// ignore: always_specify_types
JSArray? allowed_parent_origin,
JSArray<JSString>? allowed_parent_origin,
JSFunction? intermediate_iframe_close_callback,
JSBoolean? itp_support,
JSString? login_hint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,75 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/// Provides some useful tweaks to `package:web`.
library package_web_tweaks;

import 'dart:js_interop';

import 'package:web/web.dart' as web;

// TODO(kevmoo): Make this file unnecessary, https://github.com/dart-lang/web/issues/175

/// This extension gives web.window a nullable getter to the `trustedTypes`
/// property, which needs to be used to check for feature support.
extension NullableTrustedTypesGetter on web.Window {
/// (Nullable) Bindings to window.trustedTypes.
///
/// This may be null if the browser doesn't support the Trusted Types API.
///
/// See: https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API
@JS('trustedTypes')
external TrustedTypePolicyFactory? get nullableTrustedTypes;

/// Bindings to window.trustedTypes.
///
/// This will crash if accessed in a browser that doesn't support the
/// Trusted Types API.
///
/// See: https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API
@JS('trustedTypes')
external web.TrustedTypePolicyFactory? get nullableTrustedTypes;
external TrustedTypePolicyFactory get trustedTypes;
}

/// This extension allows a trusted type policy to create a script URL without
/// the `args` parameter (which in Chrome currently fails).
extension CreateScriptUrlWithoutArgs on web.TrustedTypePolicy {
/// This extension allows setting a TrustedScriptURL as the src of a script element,
/// which currently only accepts a string.
extension TrustedTypeSrcAttribute on web.HTMLScriptElement {
@JS('src')
external set trustedSrc(TrustedScriptURL value);
}

// TODO(kevmoo): drop all of this once `pkg:web` publishes `0.5.1`.

/// Bindings to a JS TrustedScriptURL.
///
/// See: https://developer.mozilla.org/en-US/docs/Web/API/TrustedScriptURL
extension type TrustedScriptURL._(JSObject _) implements JSObject {}

/// Bindings to a JS TrustedTypePolicyFactory.
///
/// See: https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicyFactory
extension type TrustedTypePolicyFactory._(JSObject _) implements JSObject {
///
external TrustedTypePolicy createPolicy(
String policyName, [
TrustedTypePolicyOptions policyOptions,
]);
}

/// Bindings to a JS TrustedTypePolicy.
///
/// See: https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicy
extension type TrustedTypePolicy._(JSObject _) implements JSObject {
///
@JS('createScriptURL')
external web.TrustedScriptURL createScriptURLNoArgs(
external TrustedScriptURL createScriptURLNoArgs(
String input,
);
}

/// This extension allows setting a TrustedScriptURL as the src of a script element,
/// which currently only accepts a string.
extension TrustedTypeSrcAttribute on web.HTMLScriptElement {
/// Bindings to a JS TrustedTypePolicyOptions (anonymous).
///
/// See: https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicyFactory/createPolicy#policyoptions
extension type TrustedTypePolicyOptions._(JSObject _) implements JSObject {
///
@JS('src')
external set srcTT(web.TrustedScriptURL value);
external factory TrustedTypePolicyOptions({
JSFunction createScriptURL,
});
}
8 changes: 4 additions & 4 deletions packages/google_identity_services_web/lib/src/js_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ Future<void> loadWebSdk({
onGoogleLibraryLoad = () => completer.complete();

// If TrustedTypes are available, prepare a trusted URL.
web.TrustedScriptURL? trustedUrl;
TrustedScriptURL? trustedUrl;
if (web.window.nullableTrustedTypes != null) {
web.console.debug(
'TrustedTypes available. Creating policy: $trustedTypePolicyName'.toJS,
);
try {
final web.TrustedTypePolicy policy = web.window.trustedTypes.createPolicy(
final TrustedTypePolicy policy = web.window.trustedTypes.createPolicy(
trustedTypePolicyName,
web.TrustedTypePolicyOptions(
TrustedTypePolicyOptions(
createScriptURL: ((JSString url) => _url).toJS,
));
trustedUrl = policy.createScriptURLNoArgs(_url);
Expand All @@ -47,7 +47,7 @@ Future<void> loadWebSdk({
..async = true
..defer = true;
if (trustedUrl != null) {
script.srcTT = trustedUrl;
script.trustedSrc = trustedUrl;
} else {
script.src = _url;
}
Expand Down
Loading

0 comments on commit b21dce5

Please sign in to comment.