-
Notifications
You must be signed in to change notification settings - Fork 44
Mutable List interface for children and childNodes
#490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
77c23f9
c3d91d8
b2c91fd
37605f0
8435092
17ba464
f9d9226
194b07e
8dcf595
c7bc010
5a4a1e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ import 'dart:convert'; | |
| import 'dart:js_interop'; | ||
|
|
||
| import '../dom.dart'; | ||
| import 'lists.dart'; | ||
|
|
||
| export 'cross_origin.dart' | ||
| show CrossOriginContentWindowExtension, CrossOriginWindowExtension; | ||
|
|
@@ -103,3 +104,14 @@ extension UriToURL on Uri { | |
| } | ||
| } | ||
| } | ||
|
|
||
| extension NodeExtension on Node { | ||
| /// Returns [childNodes] as a modifiable [List] | ||
| List<Node> get childNodesAsList => JSLiveNodeListWrapper(this, childNodes); | ||
|
||
| } | ||
|
|
||
| extension ElementExtension on Element { | ||
| /// Returns [children] as a modifiable [List] | ||
| List<Element> get childrenAsList => | ||
| JSLiveNodeListWrapper<Element, HTMLCollection, Element>(this, children); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| import 'dart:collection'; | ||
| import 'dart:js_interop'; | ||
| import '../dom/dom.dart'; | ||
|
|
||
| /// `_JSList` acts as a wrapper around a JS list object providing an interface to | ||
| /// access the list items and list length while also allowing us to specify the | ||
|
|
@@ -69,3 +70,44 @@ class JSImmutableListWrapper<T extends JSObject, U extends JSObject> | |
| @override | ||
| U elementAt(int index) => this[index]; | ||
| } | ||
|
|
||
| /// A wrapper for live node lists. `NodeList` and `HTMLCollection` that are | ||
| /// [live](https://developer.mozilla.org/en-US/docs/Web/API/NodeList#live_vs._static_nodelists) | ||
| /// can be safely modified at runtime. This requires an instance of `P`, a | ||
| /// container that elements would be added to or removed from. | ||
| class JSLiveNodeListWrapper<P extends Node, T extends JSObject, U extends Node> | ||
|
||
| extends JSImmutableListWrapper<T, U> { | ||
| final P parentNode; | ||
|
|
||
| JSLiveNodeListWrapper(this.parentNode, super.original); | ||
|
|
||
| @override | ||
| set length(int value) { | ||
| if (value > length) { | ||
| throw UnsupportedError('Cannot add null to live node List.'); | ||
| } | ||
| for (var i = length - 1; i >= value; i--) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is new compared to the dart:html version. I think that's okay though since we were throwing in the dart:html version anyways.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it is here to reuse JSImmutableListWrapper logic without overriding many methods |
||
| parentNode.removeChild(_jsList.item(i)); | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| void operator []=(int index, U value) { | ||
| RangeError.checkValidRange(index, null, length); | ||
| parentNode.replaceChild(value, _jsList.item(index)); | ||
| } | ||
|
|
||
| @override | ||
| void add(U element) { | ||
| // `ListMixin` implementation only works for lists that allow `null`. | ||
| parentNode.appendChild(element); | ||
| } | ||
|
|
||
| @override | ||
| void removeRange(int start, int end) { | ||
| RangeError.checkValidRange(start, end, length); | ||
| for (var i = 0; i < end - start + 1; i++) { | ||
|
||
| parentNode.removeChild(this[start]); | ||
|
||
| } | ||
| } | ||
| } | ||
fsw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Dart doc comments should be full sentences.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I thought subjectless sentences are allowed? (sorry, I am not native speaker) or do you mean just the dot at the end?