OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // TODO(jacobr): use _Lists.dart to remove some of the duplicated |
| 6 // functionality. |
| 7 class _ChildrenElementList implements ElementList { |
| 8 // Raw Element. |
| 9 final _ElementImpl _element; |
| 10 final _HTMLCollectionImpl _childElements; |
| 11 |
| 12 _ChildrenElementList._wrap(_ElementImpl element) |
| 13 : _childElements = element._children, |
| 14 _element = element; |
| 15 |
| 16 List<Element> _toList() { |
| 17 final output = new List(_childElements.length); |
| 18 for (int i = 0, len = _childElements.length; i < len; i++) { |
| 19 output[i] = _childElements[i]; |
| 20 } |
| 21 return output; |
| 22 } |
| 23 |
| 24 _ElementImpl get first() { |
| 25 return _element._firstElementChild; |
| 26 } |
| 27 |
| 28 void forEach(void f(Element element)) { |
| 29 for (_ElementImpl element in _childElements) { |
| 30 f(element); |
| 31 } |
| 32 } |
| 33 |
| 34 ElementList filter(bool f(Element element)) { |
| 35 final output = <Element>[]; |
| 36 forEach((Element element) { |
| 37 if (f(element)) { |
| 38 output.add(element); |
| 39 } |
| 40 }); |
| 41 return new _FrozenElementList._wrap(output); |
| 42 } |
| 43 |
| 44 bool every(bool f(Element element)) { |
| 45 for(Element element in this) { |
| 46 if (!f(element)) { |
| 47 return false; |
| 48 } |
| 49 }; |
| 50 return true; |
| 51 } |
| 52 |
| 53 bool some(bool f(Element element)) { |
| 54 for(Element element in this) { |
| 55 if (f(element)) { |
| 56 return true; |
| 57 } |
| 58 }; |
| 59 return false; |
| 60 } |
| 61 |
| 62 bool isEmpty() { |
| 63 return _element._firstElementChild == null; |
| 64 } |
| 65 |
| 66 int get length() { |
| 67 return _childElements.length; |
| 68 } |
| 69 |
| 70 _ElementImpl operator [](int index) { |
| 71 return _childElements[index]; |
| 72 } |
| 73 |
| 74 void operator []=(int index, _ElementImpl value) { |
| 75 _element._replaceChild(value, _childElements[index]); |
| 76 } |
| 77 |
| 78 void set length(int newLength) { |
| 79 // TODO(jacobr): remove children when length is reduced. |
| 80 throw const UnsupportedOperationException(''); |
| 81 } |
| 82 |
| 83 Element add(_ElementImpl value) { |
| 84 _element._appendChild(value); |
| 85 return value; |
| 86 } |
| 87 |
| 88 Element addLast(_ElementImpl value) => add(value); |
| 89 |
| 90 Iterator<Element> iterator() => _toList().iterator(); |
| 91 |
| 92 void addAll(Collection<_ElementImpl> collection) { |
| 93 for (_ElementImpl element in collection) { |
| 94 _element._appendChild(element); |
| 95 } |
| 96 } |
| 97 |
| 98 void sort(int compare(Element a, Element b)) { |
| 99 throw const UnsupportedOperationException('TODO(jacobr): should we impl?'); |
| 100 } |
| 101 |
| 102 void copyFrom(List<Object> src, int srcStart, int dstStart, int count) { |
| 103 throw 'Not impl yet. todo(jacobr)'; |
| 104 } |
| 105 |
| 106 void setRange(int start, int length, List from, [int startFrom = 0]) { |
| 107 throw const NotImplementedException(); |
| 108 } |
| 109 |
| 110 void removeRange(int start, int length) { |
| 111 throw const NotImplementedException(); |
| 112 } |
| 113 |
| 114 void insertRange(int start, int length, [initialValue = null]) { |
| 115 throw const NotImplementedException(); |
| 116 } |
| 117 |
| 118 List getRange(int start, int length) => |
| 119 new _FrozenElementList._wrap(_Lists.getRange(this, start, length, |
| 120 <Element>[])); |
| 121 |
| 122 int indexOf(Element element, [int start = 0]) { |
| 123 return _Lists.indexOf(this, element, start, this.length); |
| 124 } |
| 125 |
| 126 int lastIndexOf(Element element, [int start = null]) { |
| 127 if (start === null) start = length - 1; |
| 128 return _Lists.lastIndexOf(this, element, start); |
| 129 } |
| 130 |
| 131 void clear() { |
| 132 // It is unclear if we want to keep non element nodes? |
| 133 _element.text = ''; |
| 134 } |
| 135 |
| 136 Element removeLast() { |
| 137 final last = this.last(); |
| 138 if (last != null) { |
| 139 _element._removeChild(last); |
| 140 } |
| 141 return last; |
| 142 } |
| 143 |
| 144 Element last() { |
| 145 return _element.lastElementChild; |
| 146 } |
| 147 } |
| 148 |
| 149 // TODO(jacobr): this is an inefficient implementation but it is hard to see |
| 150 // a better option given that we cannot quite force NodeList to be an |
| 151 // ElementList as there are valid cases where a NodeList JavaScript object |
| 152 // contains Node objects that are not Elements. |
| 153 class _FrozenElementList implements ElementList { |
| 154 final List<Node> _nodeList; |
| 155 |
| 156 _FrozenElementList._wrap(this._nodeList); |
| 157 |
| 158 Element get first() { |
| 159 return _nodeList.first; |
| 160 } |
| 161 |
| 162 void forEach(void f(Element element)) { |
| 163 for (Element el in this) { |
| 164 f(el); |
| 165 } |
| 166 } |
| 167 |
| 168 Collection map(f(Element element)) { |
| 169 final out = []; |
| 170 for (Element el in this) { |
| 171 out.add(f(el)); |
| 172 } |
| 173 return out; |
| 174 } |
| 175 |
| 176 ElementList filter(bool f(Element element)) { |
| 177 final out = new _ElementList([]); |
| 178 for (Element el in this) { |
| 179 if (f(el)) out.add(el); |
| 180 } |
| 181 return out; |
| 182 } |
| 183 |
| 184 bool every(bool f(Element element)) { |
| 185 for(Element element in this) { |
| 186 if (!f(element)) { |
| 187 return false; |
| 188 } |
| 189 }; |
| 190 return true; |
| 191 } |
| 192 |
| 193 bool some(bool f(Element element)) { |
| 194 for(Element element in this) { |
| 195 if (f(element)) { |
| 196 return true; |
| 197 } |
| 198 }; |
| 199 return false; |
| 200 } |
| 201 |
| 202 bool isEmpty() => _nodeList.isEmpty(); |
| 203 |
| 204 int get length() => _nodeList.length; |
| 205 |
| 206 Element operator [](int index) => _nodeList[index]; |
| 207 |
| 208 void operator []=(int index, Element value) { |
| 209 throw const UnsupportedOperationException(''); |
| 210 } |
| 211 |
| 212 void set length(int newLength) { |
| 213 _nodeList.length = newLength; |
| 214 } |
| 215 |
| 216 void add(Element value) { |
| 217 throw const UnsupportedOperationException(''); |
| 218 } |
| 219 |
| 220 void addLast(Element value) { |
| 221 throw const UnsupportedOperationException(''); |
| 222 } |
| 223 |
| 224 Iterator<Element> iterator() => new _FrozenElementListIterator(this); |
| 225 |
| 226 void addAll(Collection<Element> collection) { |
| 227 throw const UnsupportedOperationException(''); |
| 228 } |
| 229 |
| 230 void sort(int compare(Element a, Element b)) { |
| 231 throw const UnsupportedOperationException(''); |
| 232 } |
| 233 |
| 234 void setRange(int start, int length, List from, [int startFrom = 0]) { |
| 235 throw const UnsupportedOperationException(''); |
| 236 } |
| 237 |
| 238 void removeRange(int start, int length) { |
| 239 throw const UnsupportedOperationException(''); |
| 240 } |
| 241 |
| 242 void insertRange(int start, int length, [initialValue = null]) { |
| 243 throw const UnsupportedOperationException(''); |
| 244 } |
| 245 |
| 246 ElementList getRange(int start, int length) => |
| 247 new _FrozenElementList._wrap(_nodeList.getRange(start, length)); |
| 248 |
| 249 int indexOf(Element element, [int start = 0]) => |
| 250 _nodeList.indexOf(element, start); |
| 251 |
| 252 int lastIndexOf(Element element, [int start = null]) => |
| 253 _nodeList.lastIndexOf(element, start); |
| 254 |
| 255 void clear() { |
| 256 throw const UnsupportedOperationException(''); |
| 257 } |
| 258 |
| 259 Element removeLast() { |
| 260 throw const UnsupportedOperationException(''); |
| 261 } |
| 262 |
| 263 Element last() => _nodeList.last(); |
| 264 } |
| 265 |
| 266 class _FrozenElementListIterator implements Iterator<Element> { |
| 267 final _FrozenElementList _list; |
| 268 int _index = 0; |
| 269 |
| 270 _FrozenElementListIterator(this._list); |
| 271 |
| 272 /** |
| 273 * Gets the next element in the iteration. Throws a |
| 274 * [NoMoreElementsException] if no element is left. |
| 275 */ |
| 276 Element next() { |
| 277 if (!hasNext()) { |
| 278 throw const NoMoreElementsException(); |
| 279 } |
| 280 |
| 281 return _list[_index++]; |
| 282 } |
| 283 |
| 284 /** |
| 285 * Returns whether the [Iterator] has elements left. |
| 286 */ |
| 287 bool hasNext() => _index < _list.length; |
| 288 } |
| 289 |
| 290 class _ElementList extends _ListWrapper<Element> implements ElementList { |
| 291 _ElementList(List<Element> list) : super(list); |
| 292 |
| 293 ElementList filter(bool f(Element element)) => |
| 294 new _ElementList(super.filter(f)); |
| 295 |
| 296 ElementList getRange(int start, int length) => |
| 297 new _ElementList(super.getRange(start, length)); |
| 298 } |
| 299 |
| 300 class ElementAttributeMap implements Map<String, String> { |
| 301 |
| 302 final _ElementImpl _element; |
| 303 |
| 304 ElementAttributeMap._wrap(this._element); |
| 305 |
| 306 bool containsValue(String value) { |
| 307 final attributes = _element._attributes; |
| 308 for (int i = 0, len = attributes.length; i < len; i++) { |
| 309 if(value == attributes[i].value) { |
| 310 return true; |
| 311 } |
| 312 } |
| 313 return false; |
| 314 } |
| 315 |
| 316 bool containsKey(String key) { |
| 317 return _element._hasAttribute(key); |
| 318 } |
| 319 |
| 320 String operator [](String key) { |
| 321 return _element._getAttribute(key); |
| 322 } |
| 323 |
| 324 void operator []=(String key, String value) { |
| 325 _element._setAttribute(key, value); |
| 326 } |
| 327 |
| 328 String putIfAbsent(String key, String ifAbsent()) { |
| 329 if (!containsKey(key)) { |
| 330 this[key] = ifAbsent(); |
| 331 } |
| 332 } |
| 333 |
| 334 String remove(String key) { |
| 335 _element._removeAttribute(key); |
| 336 } |
| 337 |
| 338 void clear() { |
| 339 final attributes = _element._attributes; |
| 340 for (int i = attributes.length - 1; i >= 0; i--) { |
| 341 remove(attributes[i].name); |
| 342 } |
| 343 } |
| 344 |
| 345 void forEach(void f(String key, String value)) { |
| 346 final attributes = _element._attributes; |
| 347 for (int i = 0, len = attributes.length; i < len; i++) { |
| 348 final item = attributes[i]; |
| 349 f(item.name, item.value); |
| 350 } |
| 351 } |
| 352 |
| 353 Collection<String> getKeys() { |
| 354 // TODO(jacobr): generate a lazy collection instead. |
| 355 final attributes = _element._attributes; |
| 356 final keys = new List<String>(attributes.length); |
| 357 for (int i = 0, len = attributes.length; i < len; i++) { |
| 358 keys[i] = attributes[i].name; |
| 359 } |
| 360 return keys; |
| 361 } |
| 362 |
| 363 Collection<String> getValues() { |
| 364 // TODO(jacobr): generate a lazy collection instead. |
| 365 final attributes = _element._attributes; |
| 366 final values = new List<String>(attributes.length); |
| 367 for (int i = 0, len = attributes.length; i < len; i++) { |
| 368 values[i] = attributes[i].value; |
| 369 } |
| 370 return values; |
| 371 } |
| 372 |
| 373 /** |
| 374 * The number of {key, value} pairs in the map. |
| 375 */ |
| 376 int get length() { |
| 377 return _element._attributes.length; |
| 378 } |
| 379 |
| 380 /** |
| 381 * Returns true if there is no {key, value} pair in the map. |
| 382 */ |
| 383 bool isEmpty() { |
| 384 return length == 0; |
| 385 } |
| 386 } |
| 387 |
| 388 class _SimpleClientRect implements ClientRect { |
| 389 final num left; |
| 390 final num top; |
| 391 final num width; |
| 392 final num height; |
| 393 num get right() => left + width; |
| 394 num get bottom() => top + height; |
| 395 |
| 396 const _SimpleClientRect(this.left, this.top, this.width, this.height); |
| 397 |
| 398 bool operator ==(ClientRect other) { |
| 399 return other !== null && left == other.left && top == other.top |
| 400 && width == other.width && height == other.height; |
| 401 } |
| 402 |
| 403 String toString() => "($left, $top, $width, $height)"; |
| 404 } |
| 405 |
| 406 // TODO(jacobr): we cannot currently be lazy about calculating the client |
| 407 // rects as we must perform all measurement queries at a safe point to avoid |
| 408 // triggering unneeded layouts. |
| 409 /** |
| 410 * All your element measurement needs in one place |
| 411 * @domName none |
| 412 */ |
| 413 class _ElementRectImpl implements ElementRect { |
| 414 final ClientRect client; |
| 415 final ClientRect offset; |
| 416 final ClientRect scroll; |
| 417 |
| 418 // TODO(jacobr): should we move these outside of ElementRect to avoid the |
| 419 // overhead of computing them every time even though they are rarely used. |
| 420 final _ClientRectImpl _boundingClientRect; |
| 421 final _ClientRectListImpl _clientRects; |
| 422 |
| 423 _ElementRectImpl(_ElementImpl element) : |
| 424 client = new _SimpleClientRect(element._clientLeft, |
| 425 element._clientTop, |
| 426 element._clientWidth, |
| 427 element._clientHeight), |
| 428 offset = new _SimpleClientRect(element._offsetLeft, |
| 429 element._offsetTop, |
| 430 element._offsetWidth, |
| 431 element._offsetHeight), |
| 432 scroll = new _SimpleClientRect(element._scrollLeft, |
| 433 element._scrollTop, |
| 434 element._scrollWidth, |
| 435 element._scrollHeight), |
| 436 _boundingClientRect = element._getBoundingClientRect(), |
| 437 _clientRects = element._getClientRects(); |
| 438 |
| 439 _ClientRectImpl get bounding() => _boundingClientRect; |
| 440 |
| 441 // TODO(jacobr): cleanup. |
| 442 List<ClientRect> get clientRects() { |
| 443 final out = new List(_clientRects.length); |
| 444 for (num i = 0; i < _clientRects.length; i++) { |
| 445 out[i] = _clientRects.item(i); |
| 446 } |
| 447 return out; |
| 448 } |
| 449 } |
| 450 |
| 451 class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { |
| 452 |
| 453 // TODO(jacobr): caching these may hurt performance. |
| 454 ElementAttributeMap _elementAttributeMap; |
| 455 _CssClassSet _cssClassSet; |
| 456 _DataAttributeMap _dataAttributes; |
| 457 |
| 458 /** |
| 459 * @domName Element.hasAttribute, Element.getAttribute, Element.setAttribute, |
| 460 * Element.removeAttribute |
| 461 */ |
| 462 Map<String, String> get attributes() { |
| 463 if (_elementAttributeMap === null) { |
| 464 _elementAttributeMap = new ElementAttributeMap._wrap(this); |
| 465 } |
| 466 return _elementAttributeMap; |
| 467 } |
| 468 |
| 469 void set attributes(Map<String, String> value) { |
| 470 Map<String, String> attributes = this.attributes; |
| 471 attributes.clear(); |
| 472 for (String key in value.getKeys()) { |
| 473 attributes[key] = value[key]; |
| 474 } |
| 475 } |
| 476 |
| 477 void set elements(Collection<Element> value) { |
| 478 final elements = this.elements; |
| 479 elements.clear(); |
| 480 elements.addAll(value); |
| 481 } |
| 482 |
| 483 ElementList get elements() => new _ChildrenElementList._wrap(this); |
| 484 |
| 485 ElementList queryAll(String selectors) => |
| 486 new _FrozenElementList._wrap(_querySelectorAll(selectors)); |
| 487 |
| 488 Set<String> get classes() { |
| 489 if (_cssClassSet === null) { |
| 490 _cssClassSet = new _CssClassSet(this); |
| 491 } |
| 492 return _cssClassSet; |
| 493 } |
| 494 |
| 495 void set classes(Collection<String> value) { |
| 496 _CssClassSet classSet = classes; |
| 497 classSet.clear(); |
| 498 classSet.addAll(value); |
| 499 } |
| 500 |
| 501 Map<String, String> get dataAttributes() { |
| 502 if (_dataAttributes === null) { |
| 503 _dataAttributes = new _DataAttributeMap(attributes); |
| 504 } |
| 505 return _dataAttributes; |
| 506 } |
| 507 |
| 508 void set dataAttributes(Map<String, String> value) { |
| 509 Map<String, String> dataAttributes = this.dataAttributes; |
| 510 dataAttributes.clear(); |
| 511 for (String key in value.getKeys()) { |
| 512 dataAttributes[key] = value[key]; |
| 513 } |
| 514 } |
| 515 |
| 516 Future<ElementRect> get rect() { |
| 517 return _createMeasurementFuture( |
| 518 () => new _ElementRectImpl(this), |
| 519 new Completer<ElementRect>()); |
| 520 } |
| 521 |
| 522 Future<CSSStyleDeclaration> get computedStyle() { |
| 523 // TODO(jacobr): last param should be null, see b/5045788 |
| 524 return getComputedStyle(''); |
| 525 } |
| 526 |
| 527 Future<CSSStyleDeclaration> getComputedStyle(String pseudoElement) { |
| 528 return _createMeasurementFuture(() => |
| 529 window._getComputedStyle(this, pseudoElement), |
| 530 new Completer<CSSStyleDeclaration>()); |
| 531 } |
| 532 $!MEMBERS |
| 533 } |
OLD | NEW |