Skip to content

Commit 330744c

Browse files
fswgemini-code-assist[bot]antfitch
authored
add more examples to package:web migration (#7040)
I think adding a few more examples and info will help with the migration process. --- - [x] I’ve reviewed the contributor guide and applied the relevant portions to this PR. - [x] This PR doesn't contain automatically generated corrections or text (Grammarly, LLMs, and similar). - [x] This PR follows the [Google Developer Documentation Style Guidelines](https://developers.google.com/style) — for example, it doesn't use _i.e._ or _e.g._, and it avoids _I_ and _we_ (first person). - [x] This PR uses [semantic line breaks](https://github.com/dart-lang/site-shared/blob/main/doc/writing-for-dart-and-flutter-websites.md#semantic-line-breaks) of 80 characters or fewer. --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Amanda Fitch <18406675+antfitch@users.noreply.github.com>
1 parent 03b021e commit 330744c

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/content/interop/js-interop/package-web.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,80 @@ Node append(Node node) native;
156156
`native` is an internal keyword that means the same as `external` in this
157157
context.
158158

159+
### `Element` and `HTMLElement`
160+
161+
`Element` is now `HTMLElement` but the extension type `Element` also exists as
162+
a base type for `HTMLElement`, `SVGElement`, etc.
163+
See [Element MDN documentation][].
164+
`querySelector` returns an Element as it can return other subtypes of `Element`
165+
besides `HTMLElement` like `SVGElement`. A downcast to `HTMLElement` is needed
166+
if you need to access its methods.
167+
168+
```dart
169+
element.querySelector('#selectme')!.className = 'test'; // valid in both
170+
171+
element.querySelector('#selectme')!.style.color = 'red'; // Remove
172+
(element.querySelector('#selectme') as HTMLElement).style.color = 'red'; // Add
173+
```
174+
175+
### List operations
176+
177+
Unlike `dart:html`, `package:web` methods like `Element.querySelectorAll` and
178+
`Element.children` return values that don't implement `List`. If a `List` is
179+
required, you'll need to wrap it with a class.
180+
181+
For immutable operations, you can use `JSImmutableListWrapper`:
182+
183+
```dart
184+
final anchors = document.querySelectorAll('a');
185+
for (final anchor in anchors) {} // Remove
186+
for (final anchor in JSImmutableListWrapper(anchors)) {} //Add
187+
188+
for (final child in parent.children) {} // Remove
189+
for (final child in JSImmutableListWrapper(parent.children)) {} // Add
190+
```
191+
192+
You need to add implementation for simple mutable operations like `removeWhere`:
193+
194+
```dart
195+
parent.children.removeWhere(test); // Remove
196+
197+
for (var i = parent.children.length - 1; i >= 0; --i) {
198+
if (test(parent.children.item(i)!)) {
199+
parent.children.item(i)!.remove();
200+
}
201+
} // Add
202+
203+
```
204+
205+
### Common DOM manipulation examples
206+
207+
Here are some common examples of trivial changes that need to be made when
208+
migrating from `dart:html` to `package:web`:
209+
210+
```dart
211+
element.querySelector('#selector')?.innerHtml = 'something'; // Remove
212+
element.querySelector('#selector')?.innerHTML = 'something'.toJS; // Add
213+
214+
element.parent.classes.add('class'); // Remove
215+
element.parentElement.classList.add('class'); // Add
216+
217+
element.appendHtml(html); // Remove
218+
element.insertAdjacentHTML('beforeend', html.toJS); // Add
219+
220+
var checkbox = CheckboxInputElement(); // Remove
221+
var checkbox = HTMLInputElement()..type='checkbox'; // Add
222+
223+
element.querySelectorAll('a').classes.add('link'); // Remove
224+
for (final a in JSImmutableListWrapper(element.querySelectorAll('a'))) {
225+
a.classList.add('a');
226+
} // Add
227+
```
228+
229+
{% comment %}
230+
TODO: add more examples
231+
{% endcomment -%}
232+
159233
### Type tests
160234

161235
It's common for code that uses `dart:html` to utilize runtime checks like `is`.
@@ -175,6 +249,9 @@ section of the JS types page covers alternatives in detail.
175249
```dart
176250
obj is Window; // Remove
177251
obj.instanceOfString('Window'); // Add
252+
253+
element is InputElement; // Remove
254+
element.isA<HTMLInputElement>(); // Add
178255
```
179256

180257
### Type signatures
@@ -325,3 +402,4 @@ Do we have any other package migrations to show off here?
325402
[restricts]: /interop/js-interop/js-types#requirements-on-external-declarations-and-function-tojs
326403
[#54507]: {{site.repo.dart.sdk}}/issues/54507
327404
[mocking tutorial]: /interop/js-interop/mock
405+
[Element MDN documentation]: https://developer.mozilla.org/en-US/docs/Web/API/Element

0 commit comments

Comments
 (0)