Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(362)

Side by Side Diff: tools/dom/templates/html/impl/impl_Element.darttemplate

Issue 13852008: Removing redundant collection methods from DOM collections. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/dom/src/WrappedList.dart ('k') | tools/dom/templates/html/impl/impl_Node.darttemplate » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // TODO(jacobr): use _Lists.dart to remove some of the duplicated
8 // functionality.
9 class _ChildrenElementList extends ListBase<Element> { 7 class _ChildrenElementList extends ListBase<Element> {
10 // Raw Element. 8 // Raw Element.
11 final Element _element; 9 final Element _element;
12 final HtmlCollection _childElements; 10 final HtmlCollection _childElements;
13 11
14 _ChildrenElementList._wrap(Element element) 12 _ChildrenElementList._wrap(Element element)
15 : _childElements = element.$dom_children, 13 : _childElements = element.$dom_children,
16 _element = element; 14 _element = element;
17 15
18 List<Element> toList({ bool growable: true }) {
19 List<Element> output;
20 if (growable) {
21 output = <Element>[];
22 output.length = _childElements.length;
23 } else {
24 output = new List<Element>(_childElements.length);
25 }
26 for (int i = 0, len = _childElements.length; i < len; i++) {
27 output[i] = _childElements[i];
28 }
29 return output;
30 }
31
32 Set<Element> toSet() {
33 final output = new Set<Element>();
34 for (int i = 0, len = _childElements.length; i < len; i++) {
35 output.add(_childElements[i]);
36 }
37 return output;
38 }
39
40 bool contains(Element element) => _childElements.contains(element); 16 bool contains(Element element) => _childElements.contains(element);
41 17
42 void forEach(void f(Element element)) {
43 for (Element element in _childElements) {
44 f(element);
45 }
46 }
47
48 bool every(bool f(Element element)) {
49 for (Element element in this) {
50 if (!f(element)) {
51 return false;
52 }
53 }
54 return true;
55 }
56
57 bool any(bool f(Element element)) {
58 for (Element element in this) {
59 if (f(element)) {
60 return true;
61 }
62 }
63 return false;
64 }
65
66 String join([String separator = ""]) {
67 return _childElements.join(separator);
68 }
69
70 Iterable map(f(Element element)) {
71 return _childElements.map(f);
72 }
73
74 Iterable<Element> where(bool f(Element element)) {
75 return _childElements.where(f);
76 }
77
78 Iterable expand(Iterable f(Element element)) {
79 return _childElements.expand(f);
80 }
81 18
82 bool get isEmpty { 19 bool get isEmpty {
83 return _element.$dom_firstElementChild == null; 20 return _element.$dom_firstElementChild == null;
84 } 21 }
85 22
86 Iterable<Element> take(int n) {
87 return _childElements.take(n);
88 }
89
90 Iterable<Element> takeWhile(bool test(Element value)) {
91 return _childElements.takeWhile(test);
92 }
93
94 Iterable<Element> skip(int n) {
95 return _childElements.skip(n);
96 }
97
98 Iterable<Element> skipWhile(bool test(Element value)) {
99 return _childElements.skipWhile(test);
100 }
101
102 Element firstWhere(bool test(Element value), {Element orElse()}) {
103 return _childElements.firstWhere(test, orElse: orElse);
104 }
105
106 Element lastWhere(bool test(Element value), {Element orElse()}) {
107 return _childElements.lastWhere(test, orElse: orElse);
108 }
109
110 Element singleWhere(bool test(Element value)) {
111 return _childElements.singleWhere(test);
112 }
113
114 Element elementAt(int index) {
115 return this[index];
116 }
117
118 int get length { 23 int get length {
119 return _childElements.length; 24 return _childElements.length;
120 } 25 }
121 26
122 Element operator [](int index) { 27 Element operator [](int index) {
123 return _childElements[index]; 28 return _childElements[index];
124 } 29 }
125 30
126 void operator []=(int index, Element value) { 31 void operator []=(int index, Element value) {
127 _element.$dom_replaceChild(value, _childElements[index]); 32 _element.$dom_replaceChild(value, _childElements[index]);
128 } 33 }
129 34
130 void set length(int newLength) { 35 void set length(int newLength) {
131 // TODO(jacobr): remove children when length is reduced. 36 // TODO(jacobr): remove children when length is reduced.
132 throw new UnsupportedError(''); 37 throw new UnsupportedError('Cannot resize element lists');
133 } 38 }
134 39
135 Element add(Element value) { 40 Element add(Element value) {
136 _element.append(value); 41 _element.append(value);
137 return value; 42 return value;
138 } 43 }
139 44
140 Iterator<Element> get iterator => toList().iterator; 45 Iterator<Element> get iterator => toList().iterator;
141 46
142 void addAll(Iterable<Element> iterable) { 47 void addAll(Iterable<Element> iterable) {
143 if (iterable is _ChildNodeListLazy) { 48 if (iterable is _ChildNodeListLazy) {
144 iterable = new List.from(iterable); 49 iterable = new List.from(iterable);
145 } 50 }
146 51
147 for (Element element in iterable) { 52 for (Element element in iterable) {
148 _element.append(element); 53 _element.append(element);
149 } 54 }
150 } 55 }
151 56
152 Iterable<Element> get reversed {
153 return _childElements.reversed;
154 }
155
156 void sort([int compare(Element a, Element b)]) { 57 void sort([int compare(Element a, Element b)]) {
157 throw new UnsupportedError('TODO(jacobr): should we impl?'); 58 throw new UnsupportedError('Cannot sort element lists');
158 }
159
160 Element reduce(Element combine(Element value, Element element)) {
161 return _childElements.reduce(combine);
162 }
163
164 dynamic fold(dynamic initialValue,
165 dynamic combine(dynamic previousValue, Element element)) {
166 return _childElements.fold(initialValue, combine);
167 } 59 }
168 60
169 void setRange(int start, int end, Iterable<Element> iterable, 61 void setRange(int start, int end, Iterable<Element> iterable,
170 [int skipCount = 0]) { 62 [int skipCount = 0]) {
171 throw new UnimplementedError(); 63 throw new UnimplementedError();
172 } 64 }
173 65
174 void replaceRange(int start, int end, Iterable<Element> iterable) { 66 void replaceRange(int start, int end, Iterable<Element> iterable) {
175 throw new UnimplementedError(); 67 throw new UnimplementedError();
176 } 68 }
177 69
178 void fillRange(int start, int end, [Element fillValue]) { 70 void fillRange(int start, int end, [Element fillValue]) {
179 throw new UnimplementedError(); 71 throw new UnimplementedError();
180 } 72 }
181 73
182 void remove(Object object) { 74 void remove(Object object) {
183 if (object is Element) { 75 if (object is Element) {
184 Element element = object; 76 Element element = object;
185 if (identical(element.parentNode, _element)) { 77 if (identical(element.parentNode, _element)) {
186 _element.$dom_removeChild(element); 78 _element.$dom_removeChild(element);
187 } 79 }
188 } 80 }
189 } 81 }
190 82
191 void removeWhere(bool test(Element element)) {
192 _childElements.removeWhere(test);
193 }
194
195 void retainWhere(bool test(Element element)) {
196 _childElements.retainWhere(test);
197 }
198
199 void removeRange(int start, int end) {
200 throw new UnimplementedError();
201 }
202
203 Iterable getRange(int start, int end) {
204 throw new UnimplementedError();
205 }
206
207 List sublist(int start, [int end]) {
208 if (end == null) end = length;
209 return new _FrozenElementList._wrap(Lists.getRange(this, start, end, []));
210 }
211
212 int indexOf(Element element, [int start = 0]) {
213 return Lists.indexOf(this, element, start, this.length);
214 }
215
216 int lastIndexOf(Element element, [int start = null]) {
217 if (start == null) start = length - 1;
218 return Lists.lastIndexOf(this, element, start);
219 }
220
221 void insert(int index, Element element) { 83 void insert(int index, Element element) {
222 if (index < 0 || index > length) { 84 if (index < 0 || index > length) {
223 throw new RangeError.range(index, 0, length); 85 throw new RangeError.range(index, 0, length);
224 } 86 }
225 if (index == length) { 87 if (index == length) {
226 _element.append(element); 88 _element.append(element);
227 } else { 89 } else {
228 _element.insertBefore(element, this[index]); 90 _element.insertBefore(element, this[index]);
229 } 91 }
230 } 92 }
231 93
232 void insertAll(int index, Iterable<Element> iterable) {
233 throw new UnimplementedError();
234 }
235
236 void setAll(int index, Iterable<Element> iterable) { 94 void setAll(int index, Iterable<Element> iterable) {
237 throw new UnimplementedError(); 95 throw new UnimplementedError();
238 } 96 }
239 97
240 void clear() { 98 void clear() {
241 // It is unclear if we want to keep non element nodes? 99 // It is unclear if we want to keep non element nodes?
242 _element.text = ''; 100 _element.text = '';
243 } 101 }
244 102
245 Element removeAt(int index) { 103 Element removeAt(int index) {
(...skipping 22 matching lines...) Expand all
268 Element get last { 126 Element get last {
269 Element result = _element.$dom_lastElementChild; 127 Element result = _element.$dom_lastElementChild;
270 if (result == null) throw new StateError("No elements"); 128 if (result == null) throw new StateError("No elements");
271 return result; 129 return result;
272 } 130 }
273 131
274 Element get single { 132 Element get single {
275 if (length > 1) throw new StateError("More than one element"); 133 if (length > 1) throw new StateError("More than one element");
276 return first; 134 return first;
277 } 135 }
278
279 Map<int, Element> asMap() {
280 return _childElements.asMap();
281 }
282
283 String toString() {
284 StringBuffer buffer = new StringBuffer('[');
285 buffer.writeAll(this, ', ');
286 buffer.write(']');
287 return buffer.toString();
288 }
289 } 136 }
290 137
291 // TODO(jacobr): this is an inefficient implementation but it is hard to see 138 // TODO(jacobr): this is an inefficient implementation but it is hard to see
292 // a better option given that we cannot quite force NodeList to be an 139 // a better option given that we cannot quite force NodeList to be an
293 // ElementList as there are valid cases where a NodeList JavaScript object 140 // ElementList as there are valid cases where a NodeList JavaScript object
294 // contains Node objects that are not Elements. 141 // contains Node objects that are not Elements.
295 class _FrozenElementList extends ListBase { 142 class _FrozenElementList extends ListBase<Element> {
296 final List<Node> _nodeList; 143 final List<Node> _nodeList;
297 144
298 _FrozenElementList._wrap(this._nodeList); 145 _FrozenElementList._wrap(this._nodeList);
299 146
300 int get length => _nodeList.length; 147 int get length => _nodeList.length;
301 148
302 Element operator [](int index) => _nodeList[index]; 149 Element operator [](int index) => _nodeList[index];
303 150
304 void operator []=(int index, Element value) { 151 void operator []=(int index, Element value) {
305 throw new UnsupportedError(''); 152 throw new UnsupportedError('Cannot modify list');
306 } 153 }
307 154
308 void set length(int newLength) { 155 void set length(int newLength) {
309 _nodeList.length = newLength; 156 throw new UnsupportedError('Cannot modify list');
310 } 157 }
311 158
312 void add(Element value) { 159 void sort([Comparator<Element> compare]) {
313 throw new UnsupportedError(''); 160 throw new UnsupportedError('Cannot sort list');
314 }
315
316 void addAll(Iterable<Element> iterable) {
317 throw new UnsupportedError('');
318 }
319
320 void sort([int compare(Element a, Element b)]) {
321 throw new UnsupportedError('');
322 }
323
324 void setRange(int start, int end, Iterable<Element> iterable,
325 [int skipCount = 0]) {
326 throw new UnsupportedError('');
327 }
328
329 void removeRange(int start, int end) {
330 throw new UnsupportedError('');
331 }
332
333 List<Element> sublist(int start, [int end]) {
334 return new _FrozenElementList._wrap(_nodeList.sublist(start, end));
335 }
336
337 void clear() {
338 throw new UnsupportedError('');
339 }
340
341 Element removeAt(int index) {
342 throw new UnsupportedError('');
343 }
344
345 Element removeLast() {
346 throw new UnsupportedError('');
347 }
348
349 void remove(Object element) {
350 throw new UnsupportedError('');
351 }
352
353 void removeWhere(bool test(Element element)) {
354 throw new UnsupportedError('');
355 }
356
357 void retainWhere(bool test(Element element)) {
358 throw new UnsupportedError('');
359 } 161 }
360 162
361 Element get first => _nodeList.first; 163 Element get first => _nodeList.first;
362 164
363 Element get last => _nodeList.last; 165 Element get last => _nodeList.last;
364 166
365 Element get single => _nodeList.single; 167 Element get single => _nodeList.single;
366
367 String toString() {
368 StringBuffer buffer = new StringBuffer('[');
369 buffer.writeAll(this, ', ');
370 buffer.write(']');
371 return buffer.toString();
372 }
373 } 168 }
374 169
375 class _ElementCssClassSet extends CssClassSet { 170 class _ElementCssClassSet extends CssClassSet {
376 171
377 final Element _element; 172 final Element _element;
378 173
379 _ElementCssClassSet(this._element); 174 _ElementCssClassSet(this._element);
380 175
381 Set<String> readClasses() { 176 Set<String> readClasses() {
382 var s = new LinkedHashSet<String>(); 177 var s = new LinkedHashSet<String>();
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after
977 const ScrollAlignment._internal(this._value); 772 const ScrollAlignment._internal(this._value);
978 toString() => 'ScrollAlignment.$_value'; 773 toString() => 'ScrollAlignment.$_value';
979 774
980 /// Attempt to align the element to the top of the scrollable area. 775 /// Attempt to align the element to the top of the scrollable area.
981 static const TOP = const ScrollAlignment._internal('TOP'); 776 static const TOP = const ScrollAlignment._internal('TOP');
982 /// Attempt to center the element in the scrollable area. 777 /// Attempt to center the element in the scrollable area.
983 static const CENTER = const ScrollAlignment._internal('CENTER'); 778 static const CENTER = const ScrollAlignment._internal('CENTER');
984 /// Attempt to align the element to the bottom of the scrollable area. 779 /// Attempt to align the element to the bottom of the scrollable area.
985 static const BOTTOM = const ScrollAlignment._internal('BOTTOM'); 780 static const BOTTOM = const ScrollAlignment._internal('BOTTOM');
986 } 781 }
OLDNEW
« no previous file with comments | « tools/dom/src/WrappedList.dart ('k') | tools/dom/templates/html/impl/impl_Node.darttemplate » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698