| 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 // TODO(jacobr): use _Lists.dart to remove some of the duplicated |
| 8 // functionality. |
| 7 class _ChildrenElementList extends ListBase<Element> { | 9 class _ChildrenElementList extends ListBase<Element> { |
| 8 // Raw Element. | 10 // Raw Element. |
| 9 final Element _element; | 11 final Element _element; |
| 10 final HtmlCollection _childElements; | 12 final HtmlCollection _childElements; |
| 11 | 13 |
| 12 _ChildrenElementList._wrap(Element element) | 14 _ChildrenElementList._wrap(Element element) |
| 13 : _childElements = element.$dom_children, | 15 : _childElements = element.$dom_children, |
| 14 _element = element; | 16 _element = element; |
| 15 | 17 |
| 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 |
| 16 bool contains(Element element) => _childElements.contains(element); | 40 bool contains(Element element) => _childElements.contains(element); |
| 17 | 41 |
| 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 } |
| 18 | 81 |
| 19 bool get isEmpty { | 82 bool get isEmpty { |
| 20 return _element.$dom_firstElementChild == null; | 83 return _element.$dom_firstElementChild == null; |
| 21 } | 84 } |
| 22 | 85 |
| 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 |
| 23 int get length { | 118 int get length { |
| 24 return _childElements.length; | 119 return _childElements.length; |
| 25 } | 120 } |
| 26 | 121 |
| 27 Element operator [](int index) { | 122 Element operator [](int index) { |
| 28 return _childElements[index]; | 123 return _childElements[index]; |
| 29 } | 124 } |
| 30 | 125 |
| 31 void operator []=(int index, Element value) { | 126 void operator []=(int index, Element value) { |
| 32 _element.$dom_replaceChild(value, _childElements[index]); | 127 _element.$dom_replaceChild(value, _childElements[index]); |
| 33 } | 128 } |
| 34 | 129 |
| 35 void set length(int newLength) { | 130 void set length(int newLength) { |
| 36 // TODO(jacobr): remove children when length is reduced. | 131 // TODO(jacobr): remove children when length is reduced. |
| 37 throw new UnsupportedError('Cannot resize element lists'); | 132 throw new UnsupportedError(''); |
| 38 } | 133 } |
| 39 | 134 |
| 40 Element add(Element value) { | 135 Element add(Element value) { |
| 41 _element.append(value); | 136 _element.append(value); |
| 42 return value; | 137 return value; |
| 43 } | 138 } |
| 44 | 139 |
| 45 Iterator<Element> get iterator => toList().iterator; | 140 Iterator<Element> get iterator => toList().iterator; |
| 46 | 141 |
| 47 void addAll(Iterable<Element> iterable) { | 142 void addAll(Iterable<Element> iterable) { |
| 48 if (iterable is _ChildNodeListLazy) { | 143 if (iterable is _ChildNodeListLazy) { |
| 49 iterable = new List.from(iterable); | 144 iterable = new List.from(iterable); |
| 50 } | 145 } |
| 51 | 146 |
| 52 for (Element element in iterable) { | 147 for (Element element in iterable) { |
| 53 _element.append(element); | 148 _element.append(element); |
| 54 } | 149 } |
| 55 } | 150 } |
| 56 | 151 |
| 152 Iterable<Element> get reversed { |
| 153 return _childElements.reversed; |
| 154 } |
| 155 |
| 57 void sort([int compare(Element a, Element b)]) { | 156 void sort([int compare(Element a, Element b)]) { |
| 58 throw new UnsupportedError('Cannot sort element lists'); | 157 throw new UnsupportedError('TODO(jacobr): should we impl?'); |
| 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); |
| 59 } | 167 } |
| 60 | 168 |
| 61 void setRange(int start, int end, Iterable<Element> iterable, | 169 void setRange(int start, int end, Iterable<Element> iterable, |
| 62 [int skipCount = 0]) { | 170 [int skipCount = 0]) { |
| 63 throw new UnimplementedError(); | 171 throw new UnimplementedError(); |
| 64 } | 172 } |
| 65 | 173 |
| 66 void replaceRange(int start, int end, Iterable<Element> iterable) { | 174 void replaceRange(int start, int end, Iterable<Element> iterable) { |
| 67 throw new UnimplementedError(); | 175 throw new UnimplementedError(); |
| 68 } | 176 } |
| 69 | 177 |
| 70 void fillRange(int start, int end, [Element fillValue]) { | 178 void fillRange(int start, int end, [Element fillValue]) { |
| 71 throw new UnimplementedError(); | 179 throw new UnimplementedError(); |
| 72 } | 180 } |
| 73 | 181 |
| 74 void remove(Object object) { | 182 void remove(Object object) { |
| 75 if (object is Element) { | 183 if (object is Element) { |
| 76 Element element = object; | 184 Element element = object; |
| 77 if (identical(element.parentNode, _element)) { | 185 if (identical(element.parentNode, _element)) { |
| 78 _element.$dom_removeChild(element); | 186 _element.$dom_removeChild(element); |
| 79 } | 187 } |
| 80 } | 188 } |
| 81 } | 189 } |
| 82 | 190 |
| 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 |
| 83 void insert(int index, Element element) { | 221 void insert(int index, Element element) { |
| 84 if (index < 0 || index > length) { | 222 if (index < 0 || index > length) { |
| 85 throw new RangeError.range(index, 0, length); | 223 throw new RangeError.range(index, 0, length); |
| 86 } | 224 } |
| 87 if (index == length) { | 225 if (index == length) { |
| 88 _element.append(element); | 226 _element.append(element); |
| 89 } else { | 227 } else { |
| 90 _element.insertBefore(element, this[index]); | 228 _element.insertBefore(element, this[index]); |
| 91 } | 229 } |
| 92 } | 230 } |
| 93 | 231 |
| 232 void insertAll(int index, Iterable<Element> iterable) { |
| 233 throw new UnimplementedError(); |
| 234 } |
| 235 |
| 94 void setAll(int index, Iterable<Element> iterable) { | 236 void setAll(int index, Iterable<Element> iterable) { |
| 95 throw new UnimplementedError(); | 237 throw new UnimplementedError(); |
| 96 } | 238 } |
| 97 | 239 |
| 98 void clear() { | 240 void clear() { |
| 99 // It is unclear if we want to keep non element nodes? | 241 // It is unclear if we want to keep non element nodes? |
| 100 _element.text = ''; | 242 _element.text = ''; |
| 101 } | 243 } |
| 102 | 244 |
| 103 Element removeAt(int index) { | 245 Element removeAt(int index) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 126 Element get last { | 268 Element get last { |
| 127 Element result = _element.$dom_lastElementChild; | 269 Element result = _element.$dom_lastElementChild; |
| 128 if (result == null) throw new StateError("No elements"); | 270 if (result == null) throw new StateError("No elements"); |
| 129 return result; | 271 return result; |
| 130 } | 272 } |
| 131 | 273 |
| 132 Element get single { | 274 Element get single { |
| 133 if (length > 1) throw new StateError("More than one element"); | 275 if (length > 1) throw new StateError("More than one element"); |
| 134 return first; | 276 return first; |
| 135 } | 277 } |
| 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 } |
| 136 } | 289 } |
| 137 | 290 |
| 138 // TODO(jacobr): this is an inefficient implementation but it is hard to see | 291 // TODO(jacobr): this is an inefficient implementation but it is hard to see |
| 139 // a better option given that we cannot quite force NodeList to be an | 292 // a better option given that we cannot quite force NodeList to be an |
| 140 // ElementList as there are valid cases where a NodeList JavaScript object | 293 // ElementList as there are valid cases where a NodeList JavaScript object |
| 141 // contains Node objects that are not Elements. | 294 // contains Node objects that are not Elements. |
| 142 class _FrozenElementList extends ListBase<Element> { | 295 class _FrozenElementList extends ListBase { |
| 143 final List<Node> _nodeList; | 296 final List<Node> _nodeList; |
| 144 | 297 |
| 145 _FrozenElementList._wrap(this._nodeList); | 298 _FrozenElementList._wrap(this._nodeList); |
| 146 | 299 |
| 147 int get length => _nodeList.length; | 300 int get length => _nodeList.length; |
| 148 | 301 |
| 149 Element operator [](int index) => _nodeList[index]; | 302 Element operator [](int index) => _nodeList[index]; |
| 150 | 303 |
| 151 void operator []=(int index, Element value) { | 304 void operator []=(int index, Element value) { |
| 152 throw new UnsupportedError('Cannot modify list'); | 305 throw new UnsupportedError(''); |
| 153 } | 306 } |
| 154 | 307 |
| 155 void set length(int newLength) { | 308 void set length(int newLength) { |
| 156 throw new UnsupportedError('Cannot modify list'); | 309 _nodeList.length = newLength; |
| 157 } | 310 } |
| 158 | 311 |
| 159 void sort([Comparator<Element> compare]) { | 312 void add(Element value) { |
| 160 throw new UnsupportedError('Cannot sort list'); | 313 throw new UnsupportedError(''); |
| 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(''); |
| 161 } | 359 } |
| 162 | 360 |
| 163 Element get first => _nodeList.first; | 361 Element get first => _nodeList.first; |
| 164 | 362 |
| 165 Element get last => _nodeList.last; | 363 Element get last => _nodeList.last; |
| 166 | 364 |
| 167 Element get single => _nodeList.single; | 365 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 } |
| 168 } | 373 } |
| 169 | 374 |
| 170 class _ElementCssClassSet extends CssClassSet { | 375 class _ElementCssClassSet extends CssClassSet { |
| 171 | 376 |
| 172 final Element _element; | 377 final Element _element; |
| 173 | 378 |
| 174 _ElementCssClassSet(this._element); | 379 _ElementCssClassSet(this._element); |
| 175 | 380 |
| 176 Set<String> readClasses() { | 381 Set<String> readClasses() { |
| 177 var s = new LinkedHashSet<String>(); | 382 var s = new LinkedHashSet<String>(); |
| (...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 772 const ScrollAlignment._internal(this._value); | 977 const ScrollAlignment._internal(this._value); |
| 773 toString() => 'ScrollAlignment.$_value'; | 978 toString() => 'ScrollAlignment.$_value'; |
| 774 | 979 |
| 775 /// Attempt to align the element to the top of the scrollable area. | 980 /// Attempt to align the element to the top of the scrollable area. |
| 776 static const TOP = const ScrollAlignment._internal('TOP'); | 981 static const TOP = const ScrollAlignment._internal('TOP'); |
| 777 /// Attempt to center the element in the scrollable area. | 982 /// Attempt to center the element in the scrollable area. |
| 778 static const CENTER = const ScrollAlignment._internal('CENTER'); | 983 static const CENTER = const ScrollAlignment._internal('CENTER'); |
| 779 /// Attempt to align the element to the bottom of the scrollable area. | 984 /// Attempt to align the element to the bottom of the scrollable area. |
| 780 static const BOTTOM = const ScrollAlignment._internal('BOTTOM'); | 985 static const BOTTOM = const ScrollAlignment._internal('BOTTOM'); |
| 781 } | 986 } |
| OLD | NEW |