OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of $LIBRARYNAME; | 5 part of $LIBRARYNAME; |
6 | 6 |
7 /** | 7 /** |
8 * Lazy implementation of the child nodes of an element that does not request | 8 * Lazy implementation of the child nodes of an element that does not request |
9 * the actual child nodes of an element until strictly necessary greatly | 9 * the actual child nodes of an element until strictly necessary greatly |
10 * improving performance for the typical cases where it is not required. | 10 * improving performance for the typical cases where it is not required. |
11 */ | 11 */ |
12 class _ChildNodeListLazy extends ListBase<Node> implements NodeListWrapper { | 12 class _ChildNodeListLazy extends ListBase<Node> implements NodeListWrapper { |
13 final Node _this; | 13 final Node _this; |
14 | 14 |
15 _ChildNodeListLazy(this._this); | 15 _ChildNodeListLazy(this._this); |
16 | 16 |
17 | 17 |
18 $if DART2JS | |
19 Node get first { | 18 Node get first { |
20 Node result = JS('Node|Null', '#.firstChild', _this); | 19 Node result = JS('Node|Null', '#.firstChild', _this); |
21 if (result == null) throw new StateError("No elements"); | 20 if (result == null) throw new StateError("No elements"); |
22 return result; | 21 return result; |
23 } | 22 } |
24 Node get last { | 23 Node get last { |
25 Node result = JS('Node|Null', '#.lastChild', _this); | 24 Node result = JS('Node|Null', '#.lastChild', _this); |
26 if (result == null) throw new StateError("No elements"); | 25 if (result == null) throw new StateError("No elements"); |
27 return result; | 26 return result; |
28 } | 27 } |
29 Node get single { | 28 Node get single { |
30 int l = this.length; | 29 int l = this.length; |
31 if (l == 0) throw new StateError("No elements"); | 30 if (l == 0) throw new StateError("No elements"); |
32 if (l > 1) throw new StateError("More than one element"); | 31 if (l > 1) throw new StateError("More than one element"); |
33 return JS('Node|Null', '#.firstChild', _this); | 32 return JS('Node|Null', '#.firstChild', _this); |
34 } | 33 } |
35 $else | |
36 Node get first { | |
37 Node result = _this.firstChild; | |
38 if (result == null) throw new StateError("No elements"); | |
39 return result; | |
40 } | |
41 Node get last { | |
42 Node result = _this.lastChild; | |
43 if (result == null) throw new StateError("No elements"); | |
44 return result; | |
45 } | |
46 Node get single { | |
47 int l = this.length; | |
48 if (l == 0) throw new StateError("No elements"); | |
49 if (l > 1) throw new StateError("More than one element"); | |
50 return _this.firstChild; | |
51 } | |
52 $endif | |
53 | 34 |
54 void add(Node value) { | 35 void add(Node value) { |
55 _this.append(value); | 36 _this.append(value); |
56 } | 37 } |
57 | 38 |
58 void addAll(Iterable<Node> iterable) { | 39 void addAll(Iterable<Node> iterable) { |
59 if (iterable is _ChildNodeListLazy) { | 40 if (iterable is _ChildNodeListLazy) { |
60 _ChildNodeListLazy otherList = iterable; | 41 _ChildNodeListLazy otherList = iterable; |
61 if (!identical(otherList._this, _this)) { | 42 if (!identical(otherList._this, _this)) { |
62 // Optimized route for copying between nodes. | 43 // Optimized route for copying between nodes. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 var result = this[index]; | 88 var result = this[index]; |
108 if (result != null) { | 89 if (result != null) { |
109 _this._removeChild(result); | 90 _this._removeChild(result); |
110 } | 91 } |
111 return result; | 92 return result; |
112 } | 93 } |
113 | 94 |
114 bool remove(Object object) { | 95 bool remove(Object object) { |
115 if (object is! Node) return false; | 96 if (object is! Node) return false; |
116 Node node = object; | 97 Node node = object; |
117 $if JSINTEROP | |
118 // We aren't preserving identity of nodes in JSINTEROP mode | |
119 if (_this != node.parentNode) return false; | |
120 $else | |
121 if (!identical(_this, node.parentNode)) return false; | 98 if (!identical(_this, node.parentNode)) return false; |
122 $endif | |
123 _this._removeChild(node); | 99 _this._removeChild(node); |
124 return true; | 100 return true; |
125 } | 101 } |
126 | 102 |
127 void _filter(bool test(Node node), bool removeMatching) { | 103 void _filter(bool test(Node node), bool removeMatching) { |
128 // This implementation of removeWhere/retainWhere is more efficient | 104 // This implementation of removeWhere/retainWhere is more efficient |
129 // than the default in ListBase. Child nodes can be removed in constant | 105 // than the default in ListBase. Child nodes can be removed in constant |
130 // time. | 106 // time. |
131 Node child = _this.firstChild; | 107 Node child = _this.firstChild; |
132 while (child != null) { | 108 while (child != null) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 | 170 |
195 Node operator[](int index) => _this.childNodes[index]; | 171 Node operator[](int index) => _this.childNodes[index]; |
196 | 172 |
197 List<Node> get rawList => _this.childNodes; | 173 List<Node> get rawList => _this.childNodes; |
198 } | 174 } |
199 | 175 |
200 | 176 |
201 $(ANNOTATIONS)$(NATIVESPEC)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS
{ | 177 $(ANNOTATIONS)$(NATIVESPEC)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS
{ |
202 | 178 |
203 // Custom element created callback. | 179 // Custom element created callback. |
204 $if DART2JS | |
205 Node._created() : super._created(); | 180 Node._created() : super._created(); |
206 $else | |
207 Node._created() : super._created(); | |
208 $endif | |
209 | 181 |
210 /** | 182 /** |
211 * A modifiable list of this node's children. | 183 * A modifiable list of this node's children. |
212 */ | 184 */ |
213 List<Node> get nodes { | 185 List<Node> get nodes { |
214 return new _ChildNodeListLazy(this); | 186 return new _ChildNodeListLazy(this); |
215 } | 187 } |
216 | 188 |
217 set nodes(Iterable<Node> value) { | 189 set nodes(Iterable<Node> value) { |
218 // Copy list first since we don't want liveness during iteration. | 190 // Copy list first since we don't want liveness during iteration. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 } | 255 } |
284 | 256 |
285 /** | 257 /** |
286 * Print out a String representation of this Node. | 258 * Print out a String representation of this Node. |
287 */ | 259 */ |
288 String toString() { | 260 String toString() { |
289 String value = nodeValue; // Fetch DOM Node property once. | 261 String value = nodeValue; // Fetch DOM Node property once. |
290 return value == null ? super.toString() : value; | 262 return value == null ? super.toString() : value; |
291 } | 263 } |
292 | 264 |
293 $if DARTIUM | |
294 /** | 265 /** |
295 * A list of this node's children. | 266 * A list of this node's children. |
296 * | 267 * |
297 * ## Other resources | |
298 * | |
299 * * [Node.childNodes](https://developer.mozilla.org/en-US/docs/Web/API/Node.c
hildNodes) | |
300 * from MDN. | |
301 */ | |
302 @DomName('Node.childNodes') | |
303 @DocsEditable() | |
304 List<Node> get childNodes => _blink.BlinkNode.instance.childNodes_Getter_(this
); | |
305 $else | |
306 /** | |
307 * A list of this node's children. | |
308 * | |
309 * ## Other resources | 268 * ## Other resources |
310 * | 269 * |
311 * * [Node.childNodes](https://developer.mozilla.org/en-US/docs/Web/API/Node.c
hildNodes) | 270 * * [Node.childNodes](https://developer.mozilla.org/en-US/docs/Web/API/Node.c
hildNodes) |
312 * from MDN. | 271 * from MDN. |
313 */ | 272 */ |
314 @DomName('Node.childNodes') | 273 @DomName('Node.childNodes') |
315 @DocsEditable() | 274 @DocsEditable() |
316 @Returns('NodeList') | 275 @Returns('NodeList') |
317 @Creates('NodeList') | 276 @Creates('NodeList') |
318 final List<Node> childNodes; | 277 final List<Node> childNodes; |
319 | 278 |
320 $endif | |
321 $!MEMBERS | 279 $!MEMBERS |
322 } | 280 } |
OLD | NEW |