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

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

Issue 13963020: Resubmit: "With the collections deriving from ListBase, we no longer need to implement a numbe… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 7 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
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 bool remove(Object object) { 74 bool 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 return true; 79 return true;
188 } 80 }
189 } 81 }
190 return false; 82 return false;
191 } 83 }
192 84
193 void removeWhere(bool test(Element element)) {
194 _childElements.removeWhere(test);
195 }
196
197 void retainWhere(bool test(Element element)) {
198 _childElements.retainWhere(test);
199 }
200
201 void removeRange(int start, int end) {
202 throw new UnimplementedError();
203 }
204
205 Iterable getRange(int start, int end) {
206 throw new UnimplementedError();
207 }
208
209 List sublist(int start, [int end]) {
210 if (end == null) end = length;
211 return new _FrozenElementList._wrap(Lists.getRange(this, start, end, []));
212 }
213
214 int indexOf(Element element, [int start = 0]) {
215 return Lists.indexOf(this, element, start, this.length);
216 }
217
218 int lastIndexOf(Element element, [int start = null]) {
219 if (start == null) start = length - 1;
220 return Lists.lastIndexOf(this, element, start);
221 }
222
223 void insert(int index, Element element) { 85 void insert(int index, Element element) {
224 if (index < 0 || index > length) { 86 if (index < 0 || index > length) {
225 throw new RangeError.range(index, 0, length); 87 throw new RangeError.range(index, 0, length);
226 } 88 }
227 if (index == length) { 89 if (index == length) {
228 _element.append(element); 90 _element.append(element);
229 } else { 91 } else {
230 _element.insertBefore(element, this[index]); 92 _element.insertBefore(element, this[index]);
231 } 93 }
232 } 94 }
233 95
234 void insertAll(int index, Iterable<Element> iterable) {
235 throw new UnimplementedError();
236 }
237
238 void setAll(int index, Iterable<Element> iterable) { 96 void setAll(int index, Iterable<Element> iterable) {
239 throw new UnimplementedError(); 97 throw new UnimplementedError();
240 } 98 }
241 99
242 void clear() { 100 void clear() {
243 // It is unclear if we want to keep non element nodes? 101 // It is unclear if we want to keep non element nodes?
244 _element.text = ''; 102 _element.text = '';
245 } 103 }
246 104
247 Element removeAt(int index) { 105 Element removeAt(int index) {
(...skipping 22 matching lines...) Expand all
270 Element get last { 128 Element get last {
271 Element result = _element.$dom_lastElementChild; 129 Element result = _element.$dom_lastElementChild;
272 if (result == null) throw new StateError("No elements"); 130 if (result == null) throw new StateError("No elements");
273 return result; 131 return result;
274 } 132 }
275 133
276 Element get single { 134 Element get single {
277 if (length > 1) throw new StateError("More than one element"); 135 if (length > 1) throw new StateError("More than one element");
278 return first; 136 return first;
279 } 137 }
280
281 Map<int, Element> asMap() {
282 return _childElements.asMap();
283 }
284
285 String toString() {
286 StringBuffer buffer = new StringBuffer('[');
287 buffer.writeAll(this, ', ');
288 buffer.write(']');
289 return buffer.toString();
290 }
291 } 138 }
292 139
293 // TODO(jacobr): this is an inefficient implementation but it is hard to see 140 // TODO(jacobr): this is an inefficient implementation but it is hard to see
294 // a better option given that we cannot quite force NodeList to be an 141 // a better option given that we cannot quite force NodeList to be an
295 // ElementList as there are valid cases where a NodeList JavaScript object 142 // ElementList as there are valid cases where a NodeList JavaScript object
296 // contains Node objects that are not Elements. 143 // contains Node objects that are not Elements.
297 class _FrozenElementList extends ListBase { 144 class _FrozenElementList<T extends Element> extends ListBase<T> {
298 final List<Node> _nodeList; 145 final List<Node> _nodeList;
299 146
300 _FrozenElementList._wrap(this._nodeList); 147 _FrozenElementList._wrap(this._nodeList);
301 148
302 int get length => _nodeList.length; 149 int get length => _nodeList.length;
303 150
304 Element operator [](int index) => _nodeList[index]; 151 Element operator [](int index) => _nodeList[index];
305 152
306 void operator []=(int index, Element value) { 153 void operator []=(int index, Element value) {
307 throw new UnsupportedError(''); 154 throw new UnsupportedError('Cannot modify list');
308 } 155 }
309 156
310 void set length(int newLength) { 157 void set length(int newLength) {
311 _nodeList.length = newLength; 158 throw new UnsupportedError('Cannot modify list');
312 } 159 }
313 160
314 void add(Element value) { 161 void sort([Comparator<Element> compare]) {
315 throw new UnsupportedError(''); 162 throw new UnsupportedError('Cannot sort list');
316 }
317
318 void addAll(Iterable<Element> iterable) {
319 throw new UnsupportedError('');
320 }
321
322 void sort([int compare(Element a, Element b)]) {
323 throw new UnsupportedError('');
324 }
325
326 void setRange(int start, int end, Iterable<Element> iterable,
327 [int skipCount = 0]) {
328 throw new UnsupportedError('');
329 }
330
331 void removeRange(int start, int end) {
332 throw new UnsupportedError('');
333 }
334
335 List<Element> sublist(int start, [int end]) {
336 return new _FrozenElementList._wrap(_nodeList.sublist(start, end));
337 }
338
339 void clear() {
340 throw new UnsupportedError('');
341 }
342
343 Element removeAt(int index) {
344 throw new UnsupportedError('');
345 }
346
347 Element removeLast() {
348 throw new UnsupportedError('');
349 }
350
351 bool remove(Object element) {
352 throw new UnsupportedError('');
353 }
354
355 void removeWhere(bool test(Element element)) {
356 throw new UnsupportedError('');
357 }
358
359 void retainWhere(bool test(Element element)) {
360 throw new UnsupportedError('');
361 } 163 }
362 164
363 Element get first => _nodeList.first; 165 Element get first => _nodeList.first;
364 166
365 Element get last => _nodeList.last; 167 Element get last => _nodeList.last;
366 168
367 Element get single => _nodeList.single; 169 Element get single => _nodeList.single;
368
369 String toString() {
370 StringBuffer buffer = new StringBuffer('[');
371 buffer.writeAll(this, ', ');
372 buffer.write(']');
373 return buffer.toString();
374 }
375 } 170 }
376 171
377 class _ElementCssClassSet extends CssClassSet { 172 class _ElementCssClassSet extends CssClassSet {
378 173
379 final Element _element; 174 final Element _element;
380 175
381 _ElementCssClassSet(this._element); 176 _ElementCssClassSet(this._element);
382 177
383 Set<String> readClasses() { 178 Set<String> readClasses() {
384 var s = new LinkedHashSet<String>(); 179 var s = new LinkedHashSet<String>();
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after
979 const ScrollAlignment._internal(this._value); 774 const ScrollAlignment._internal(this._value);
980 toString() => 'ScrollAlignment.$_value'; 775 toString() => 'ScrollAlignment.$_value';
981 776
982 /// Attempt to align the element to the top of the scrollable area. 777 /// Attempt to align the element to the top of the scrollable area.
983 static const TOP = const ScrollAlignment._internal('TOP'); 778 static const TOP = const ScrollAlignment._internal('TOP');
984 /// Attempt to center the element in the scrollable area. 779 /// Attempt to center the element in the scrollable area.
985 static const CENTER = const ScrollAlignment._internal('CENTER'); 780 static const CENTER = const ScrollAlignment._internal('CENTER');
986 /// Attempt to align the element to the bottom of the scrollable area. 781 /// Attempt to align the element to the bottom of the scrollable area.
987 static const BOTTOM = const ScrollAlignment._internal('BOTTOM'); 782 static const BOTTOM = const ScrollAlignment._internal('BOTTOM');
988 } 783 }
OLDNEW
« no previous file with comments | « tools/dom/src/WrappedList.dart ('k') | tools/dom/templates/html/impl/impl_HTMLSelectElement.darttemplate » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698