Skip to content

Commit 95be76a

Browse files
authored
[web] Migrate framework away from dart:html and package:js (flutter#128580)
Use `package:web` and `dart:js_interop` instead. Part of flutter#127030
1 parent 52c4db8 commit 95be76a

File tree

6 files changed

+56
-47
lines changed

6 files changed

+56
-47
lines changed

dev/bots/allowlist.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ const Set<String> kCorePackageAllowList = <String>{
4848
'test_api',
4949
'vector_math',
5050
'vm_service',
51+
'web',
5152
'webdriver',
5253
};

packages/flutter/lib/src/foundation/_capabilities_web.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// For now, we're hiding dart:js_interop's `@JS` to avoid a conflict with
6-
// package:js' `@JS`. In the future, we should be able to remove package:js
7-
// altogether and just import dart:js_interop.
8-
import 'dart:js_interop' hide JS;
9-
import 'package:js/js.dart';
5+
import 'dart:js_interop';
106

117
// This value is set by the engine. It is used to determine if the application is
128
// using canvaskit.

packages/flutter/lib/src/services/dom.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// For now, we're hiding dart:js_interop's `@JS` to avoid a conflict with
6-
// package:js' `@JS`. In the future, we should be able to remove package:js
7-
// altogether and just import dart:js_interop.
8-
import 'dart:js_interop' hide JS;
9-
import 'package:js/js.dart';
5+
import 'dart:js_interop';
106

117
/// This file includes static interop helpers for Flutter Web.
128
// TODO(joshualitt): This file will eventually be removed,

packages/flutter/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ dependencies:
99
# To update these, use "flutter update-packages --force-upgrade".
1010
characters: 1.3.0
1111
collection: 1.17.2
12-
js: 0.6.7
1312
material_color_utilities: 0.5.0
1413
meta: 1.9.1
1514
vector_math: 2.1.4
15+
web: 0.1.3-beta
1616
sky_engine:
1717
sdk: flutter
1818

@@ -72,4 +72,4 @@ dev_dependencies:
7272
webkit_inspection_protocol: 1.2.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
7373
yaml: 3.1.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
7474

75-
# PUBSPEC CHECKSUM: dd07
75+
# PUBSPEC CHECKSUM: d32a

packages/flutter/test/painting/_test_http_request.dart

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,22 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// For now, we're hiding dart:js_interop's `@JS` to avoid a conflict with
6-
// package:js' `@JS`. In the future, we should be able to remove package:js
7-
// altogether and just import dart:js_interop.
8-
import 'dart:js_interop' hide JS;
5+
import 'dart:js_interop';
96

107
import 'package:flutter/src/services/dom.dart';
11-
import 'package:js/js.dart';
12-
import 'package:js/js_util.dart' as js_util;
138

149
/// Defines a new property on an Object.
1510
@JS('Object.defineProperty')
1611
external JSVoid objectDefineProperty(JSAny o, JSString symbol, JSAny desc);
1712

1813
void createGetter(JSAny mock, String key, JSAny? Function() get) {
1914
objectDefineProperty(
20-
mock,
21-
key.toJS,
22-
js_util.jsify(
23-
<String, Object>{
24-
'get': () { return get(); }.toJS
25-
}
26-
) as JSAny);
15+
mock,
16+
key.toJS,
17+
<String, JSFunction>{
18+
'get': (() => get()).toJS,
19+
}.jsify()!,
20+
);
2721
}
2822

2923
@JS()
@@ -51,13 +45,12 @@ class TestHttpRequest {
5145
);
5246
// TODO(srujzs): This is needed for when we reify JS types. Right now, JSAny
5347
// is a typedef for Object?, but when we reify, it'll be its own type.
54-
// ignore: unnecessary_cast
5548
final JSAny mock = _mock as JSAny;
56-
createGetter(mock, 'headers', () => js_util.jsify(headers) as JSAny);
49+
createGetter(mock, 'headers', () => headers.jsify());
5750
createGetter(mock,
58-
'responseHeaders', () => js_util.jsify(responseHeaders) as JSAny);
51+
'responseHeaders', () => responseHeaders.jsify());
5952
createGetter(mock, 'status', () => status.toJS);
60-
createGetter(mock, 'response', () => js_util.jsify(response) as JSAny);
53+
createGetter(mock, 'response', () => response.jsify());
6154
}
6255

6356
late DomXMLHttpRequestMock _mock;

packages/flutter/test/widgets/selectable_region_context_menu_test.dart

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,61 @@
55
@TestOn('browser') // This file contains web-only library.
66
library;
77

8-
import 'dart:html' as html;
8+
import 'dart:js_interop';
99

1010
import 'package:flutter/foundation.dart';
1111
import 'package:flutter/material.dart';
1212
import 'package:flutter/rendering.dart';
1313
import 'package:flutter_test/flutter_test.dart';
14+
import 'package:web/web.dart' as web;
15+
16+
extension on JSString {
17+
external JSBoolean includes(JSString searchString);
18+
}
19+
extension on web.HTMLCollection {
20+
Iterable<web.Element> get iterable => _genIterable(length, (JSNumber i) => item(i));
21+
}
22+
extension on web.CSSRuleList {
23+
Iterable<web.CSSRule> get iterable => _genIterable(length, (JSNumber i) => item(i));
24+
}
25+
26+
typedef ItemGetter<T> = T? Function(JSNumber index);
27+
Iterable<T> _genIterable<T>(JSNumber length, ItemGetter<T> getItem) {
28+
return Iterable<T>.generate(length.toDart.toInt(), (int index) => getItem(index.toJS)!);
29+
}
1430

1531
void main() {
16-
html.Element? element;
32+
web.HTMLElement? element;
1733
PlatformSelectableRegionContextMenu.debugOverrideRegisterViewFactory = (String viewType, Object Function(int viewId) fn, {bool isVisible = true}) {
18-
element = fn(0) as html.Element;
34+
element = fn(0) as web.HTMLElement;
1935
// The element needs to be attached to the document body to receive mouse
2036
// events.
21-
html.document.body!.append(element!);
37+
web.document.body!.append(element);
2238
};
2339
// This force register the dom element.
2440
PlatformSelectableRegionContextMenu(child: const Placeholder());
2541
PlatformSelectableRegionContextMenu.debugOverrideRegisterViewFactory = null;
2642

2743
test('DOM element is set up correctly', () async {
2844
expect(element, isNotNull);
29-
expect(element!.style.width, '100%');
30-
expect(element!.style.height, '100%');
31-
expect(element!.classes.length, 1);
32-
final String className = element!.classes.first;
45+
expect(element!.style.width, '100%'.toJS);
46+
expect(element!.style.height, '100%'.toJS);
47+
expect(element!.classList.length, 1.toJS);
48+
final JSString className = element!.className;
3349

34-
expect(html.document.head!.children, isNotEmpty);
50+
expect(web.document.head!.children.iterable, isNotEmpty);
3551
bool foundStyle = false;
36-
for (final html.Element element in html.document.head!.children) {
37-
if (element is! html.StyleElement) {
52+
for (final web.Element element in web.document.head!.children.iterable) {
53+
if (element.tagName != 'STYLE'.toJS) {
3854
continue;
3955
}
40-
final html.CssStyleSheet sheet = element.sheet! as html.CssStyleSheet;
41-
foundStyle = sheet.rules!.any((html.CssRule rule) => rule.cssText!.contains(className));
56+
final web.CSSRuleList? rules = (element as web.HTMLStyleElement).sheet?.rules;
57+
if (rules != null) {
58+
foundStyle = rules.iterable.any((web.CSSRule rule) => rule.cssText.includes(className).toDart);
59+
}
60+
if (foundStyle) {
61+
break;
62+
}
4263
}
4364
expect(foundStyle, isTrue);
4465
});
@@ -62,11 +83,13 @@ void main() {
6283

6384
// Dispatch right click.
6485
element!.dispatchEvent(
65-
html.MouseEvent(
66-
'mousedown',
67-
button: 2,
68-
clientX: 200,
69-
clientY: 300,
86+
web.MouseEvent(
87+
'mousedown'.toJS,
88+
web.MouseEventInit(
89+
button: 2.toJS,
90+
clientX: 200.toJS,
91+
clientY: 300.toJS,
92+
),
7093
),
7194
);
7295
final RenderSelectionSpy renderSelectionSpy = tester.renderObject<RenderSelectionSpy>(find.byKey(spy));

0 commit comments

Comments
 (0)