| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, 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 class FilteredElementList implements ElementList { | |
| 6 final Node _node; | |
| 7 final NodeList _childNodes; | |
| 8 | |
| 9 FilteredElementList(Node node): _childNodes = node.nodes, _node = node; | |
| 10 | |
| 11 // We can't memoize this, since it's possible that children will be messed | |
| 12 // with externally to this class. | |
| 13 // | |
| 14 // TODO(nweiz): Do we really need to copy the list to make the types work out? | |
| 15 List<Element> get _filtered() => | |
| 16 new List.from(_childNodes.filter((n) => n is Element)); | |
| 17 | |
| 18 // Don't use _filtered.first so we can short-circuit once we find an element. | |
| 19 Element get first() { | |
| 20 for (final node in _childNodes) { | |
| 21 if (node is Element) { | |
| 22 return node; | |
| 23 } | |
| 24 } | |
| 25 return null; | |
| 26 } | |
| 27 | |
| 28 void forEach(void f(Element element)) { | |
| 29 _filtered.forEach(f); | |
| 30 } | |
| 31 | |
| 32 void operator []=(int index, Element value) { | |
| 33 this[index].replaceWith(value); | |
| 34 } | |
| 35 | |
| 36 void set length(int newLength) { | |
| 37 final len = this.length; | |
| 38 if (newLength >= len) { | |
| 39 return; | |
| 40 } else if (newLength < 0) { | |
| 41 throw const IllegalArgumentException("Invalid list length"); | |
| 42 } | |
| 43 | |
| 44 removeRange(newLength - 1, len - newLength); | |
| 45 } | |
| 46 | |
| 47 void add(Element value) { | |
| 48 _childNodes.add(value); | |
| 49 } | |
| 50 | |
| 51 void addAll(Collection<Element> collection) { | |
| 52 collection.forEach(add); | |
| 53 } | |
| 54 | |
| 55 void addLast(Element value) { | |
| 56 add(value); | |
| 57 } | |
| 58 | |
| 59 void sort(int compare(Element a, Element b)) { | |
| 60 throw const UnsupportedOperationException('TODO(jacobr): should we impl?'); | |
| 61 } | |
| 62 | |
| 63 void setRange(int start, int rangeLength, List from, [int startFrom = 0]) { | |
| 64 throw const NotImplementedException(); | |
| 65 } | |
| 66 | |
| 67 void removeRange(int start, int rangeLength) { | |
| 68 _filtered.getRange(start, rangeLength).forEach((el) => el.remove()); | |
| 69 } | |
| 70 | |
| 71 void insertRange(int start, int rangeLength, [initialValue = null]) { | |
| 72 throw const NotImplementedException(); | |
| 73 } | |
| 74 | |
| 75 void clear() { | |
| 76 // Currently, ElementList#clear clears even non-element nodes, so we follow | |
| 77 // that behavior. | |
| 78 _childNodes.clear(); | |
| 79 } | |
| 80 | |
| 81 Element removeLast() { | |
| 82 final result = this.last(); | |
| 83 if (result != null) { | |
| 84 result.remove(); | |
| 85 } | |
| 86 return result; | |
| 87 } | |
| 88 | |
| 89 Collection map(f(Element element)) => _filtered.map(f); | |
| 90 Collection<Element> filter(bool f(Element element)) => _filtered.filter(f); | |
| 91 bool every(bool f(Element element)) => _filtered.every(f); | |
| 92 bool some(bool f(Element element)) => _filtered.some(f); | |
| 93 bool isEmpty() => _filtered.isEmpty(); | |
| 94 int get length() => _filtered.length; | |
| 95 Element operator [](int index) => _filtered[index]; | |
| 96 Iterator<Element> iterator() => _filtered.iterator(); | |
| 97 List<Element> getRange(int start, int rangeLength) => | |
| 98 _filtered.getRange(start, rangeLength); | |
| 99 int indexOf(Element element, [int start = 0]) => | |
| 100 _filtered.indexOf(element, start); | |
| 101 | |
| 102 int lastIndexOf(Element element, [int start = null]) { | |
| 103 if (start === null) start = length - 1; | |
| 104 return _filtered.lastIndexOf(element, start); | |
| 105 } | |
| 106 | |
| 107 Element last() => _filtered.last(); | |
| 108 } | |
| 109 | |
| 110 Future<CSSStyleDeclaration> _emptyStyleFuture() { | |
| 111 return _createMeasurementFuture(() => new Element.tag('div').style, | |
| 112 new Completer<CSSStyleDeclaration>()); | |
| 113 } | |
| 114 | |
| 115 class EmptyElementRect implements ElementRect { | |
| 116 final ClientRect client = const _SimpleClientRect(0, 0, 0, 0); | |
| 117 final ClientRect offset = const _SimpleClientRect(0, 0, 0, 0); | |
| 118 final ClientRect scroll = const _SimpleClientRect(0, 0, 0, 0); | |
| 119 final ClientRect bounding = const _SimpleClientRect(0, 0, 0, 0); | |
| 120 final List<ClientRect> clientRects = const <ClientRect>[]; | |
| 121 | |
| 122 const EmptyElementRect(); | |
| 123 } | |
| 124 | |
| 125 class _FrozenCSSClassSet extends _CssClassSet { | |
| 126 _FrozenCSSClassSet() : super(null); | |
| 127 | |
| 128 void _write(Set s) { | |
| 129 throw const UnsupportedOperationException( | |
| 130 'frozen class set cannot be modified'); | |
| 131 } | |
| 132 Set<String> _read() => new Set<String>(); | |
| 133 | |
| 134 bool get isFrozen() => true; | |
| 135 } | |
| 136 | |
| 137 class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { | |
| 138 ElementList _elements; | |
| 139 | |
| 140 ElementList get elements() { | |
| 141 if (_elements == null) { | |
| 142 _elements = new FilteredElementList(this); | |
| 143 } | |
| 144 return _elements; | |
| 145 } | |
| 146 | |
| 147 // TODO: The type of value should be Collection<Element>. See http://b/5392897 | |
| 148 void set elements(value) { | |
| 149 // Copy list first since we don't want liveness during iteration. | |
| 150 List copy = new List.from(value); | |
| 151 final elements = this.elements; | |
| 152 elements.clear(); | |
| 153 elements.addAll(copy); | |
| 154 } | |
| 155 | |
| 156 _ElementImpl query(String selectors) => $dom_querySelector(selectors); | |
| 157 | |
| 158 List<Element> queryAll(String selectors) => | |
| 159 new _FrozenElementList._wrap($dom_querySelectorAll(selectors)); | |
| 160 | |
| 161 String get innerHTML() { | |
| 162 final e = new Element.tag("div"); | |
| 163 e.nodes.add(this.clone(true)); | |
| 164 return e.innerHTML; | |
| 165 } | |
| 166 | |
| 167 String get outerHTML() => innerHTML; | |
| 168 | |
| 169 // TODO(nweiz): Do we want to support some variant of innerHTML for XML and/or | |
| 170 // SVG strings? | |
| 171 void set innerHTML(String value) { | |
| 172 this.nodes.clear(); | |
| 173 | |
| 174 final e = new Element.tag("div"); | |
| 175 e.innerHTML = value; | |
| 176 | |
| 177 // Copy list first since we don't want liveness during iteration. | |
| 178 List nodes = new List.from(e.nodes); | |
| 179 this.nodes.addAll(nodes); | |
| 180 } | |
| 181 | |
| 182 Node _insertAdjacentNode(String where, Node node) { | |
| 183 switch (where.toLowerCase()) { | |
| 184 case "beforebegin": return null; | |
| 185 case "afterend": return null; | |
| 186 case "afterbegin": | |
| 187 this.insertBefore(node, this.nodes.first); | |
| 188 return node; | |
| 189 case "beforeend": | |
| 190 this.nodes.add(node); | |
| 191 return node; | |
| 192 default: | |
| 193 throw new IllegalArgumentException("Invalid position ${where}"); | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 Element insertAdjacentElement(String where, Element element) | |
| 198 => this._insertAdjacentNode(where, element); | |
| 199 | |
| 200 void insertAdjacentText(String where, String text) { | |
| 201 this._insertAdjacentNode(where, new Text(text)); | |
| 202 } | |
| 203 | |
| 204 void insertAdjacentHTML(String where, String text) { | |
| 205 this._insertAdjacentNode(where, new DocumentFragment.html(text)); | |
| 206 } | |
| 207 | |
| 208 void addText(String text) { | |
| 209 this.insertAdjacentText('beforeend', text); | |
| 210 } | |
| 211 | |
| 212 void addHTML(String text) { | |
| 213 this.insertAdjacentHTML('beforeend', text); | |
| 214 } | |
| 215 | |
| 216 Future<ElementRect> get rect() { | |
| 217 return _createMeasurementFuture(() => const EmptyElementRect(), | |
| 218 new Completer<ElementRect>()); | |
| 219 } | |
| 220 | |
| 221 // If we can come up with a semi-reasonable default value for an Element | |
| 222 // getter, we'll use it. In general, these return the same values as an | |
| 223 // element that has no parent. | |
| 224 String get contentEditable() => "false"; | |
| 225 bool get isContentEditable() => false; | |
| 226 bool get draggable() => false; | |
| 227 bool get hidden() => false; | |
| 228 bool get spellcheck() => false; | |
| 229 bool get translate() => false; | |
| 230 int get tabIndex() => -1; | |
| 231 String get id() => ""; | |
| 232 String get title() => ""; | |
| 233 String get tagName() => ""; | |
| 234 String get webkitdropzone() => ""; | |
| 235 String get webkitRegionOverflow() => ""; | |
| 236 Element get $dom_firstElementChild() => elements.first(); | |
| 237 Element get $dom_lastElementChild() => elements.last(); | |
| 238 Element get nextElementSibling() => null; | |
| 239 Element get previousElementSibling() => null; | |
| 240 Element get offsetParent() => null; | |
| 241 Element get parent() => null; | |
| 242 Map<String, String> get attributes() => const {}; | |
| 243 CSSClassSet get classes() => new _FrozenCSSClassSet(); | |
| 244 Map<String, String> get dataAttributes() => const {}; | |
| 245 CSSStyleDeclaration get style() => new Element.tag('div').style; | |
| 246 Future<CSSStyleDeclaration> get computedStyle() => | |
| 247 _emptyStyleFuture(); | |
| 248 Future<CSSStyleDeclaration> getComputedStyle(String pseudoElement) => | |
| 249 _emptyStyleFuture(); | |
| 250 bool matchesSelector(String selectors) => false; | |
| 251 | |
| 252 // Imperative Element methods are made into no-ops, as they are on parentless | |
| 253 // elements. | |
| 254 void blur() {} | |
| 255 void focus() {} | |
| 256 void click() {} | |
| 257 void scrollByLines(int lines) {} | |
| 258 void scrollByPages(int pages) {} | |
| 259 void scrollIntoView([bool centerIfNeeded]) {} | |
| 260 void webkitRequestFullScreen(int flags) {} | |
| 261 void webkitRequestFullscreen() {} | |
| 262 | |
| 263 // Setters throw errors rather than being no-ops because we aren't going to | |
| 264 // retain the values that were set, and erroring out seems clearer. | |
| 265 void set attributes(Map<String, String> value) { | |
| 266 throw new UnsupportedOperationException( | |
| 267 "Attributes can't be set for document fragments."); | |
| 268 } | |
| 269 | |
| 270 void set classes(Collection<String> value) { | |
| 271 throw new UnsupportedOperationException( | |
| 272 "Classes can't be set for document fragments."); | |
| 273 } | |
| 274 | |
| 275 void set dataAttributes(Map<String, String> value) { | |
| 276 throw new UnsupportedOperationException( | |
| 277 "Data attributes can't be set for document fragments."); | |
| 278 } | |
| 279 | |
| 280 void set contentEditable(String value) { | |
| 281 throw new UnsupportedOperationException( | |
| 282 "Content editable can't be set for document fragments."); | |
| 283 } | |
| 284 | |
| 285 String get dir() { | |
| 286 throw new UnsupportedOperationException( | |
| 287 "Document fragments don't support text direction."); | |
| 288 } | |
| 289 | |
| 290 void set dir(String value) { | |
| 291 throw new UnsupportedOperationException( | |
| 292 "Document fragments don't support text direction."); | |
| 293 } | |
| 294 | |
| 295 void set draggable(bool value) { | |
| 296 throw new UnsupportedOperationException( | |
| 297 "Draggable can't be set for document fragments."); | |
| 298 } | |
| 299 | |
| 300 void set hidden(bool value) { | |
| 301 throw new UnsupportedOperationException( | |
| 302 "Hidden can't be set for document fragments."); | |
| 303 } | |
| 304 | |
| 305 void set id(String value) { | |
| 306 throw new UnsupportedOperationException( | |
| 307 "ID can't be set for document fragments."); | |
| 308 } | |
| 309 | |
| 310 String get lang() { | |
| 311 throw new UnsupportedOperationException( | |
| 312 "Document fragments don't support language."); | |
| 313 } | |
| 314 | |
| 315 void set lang(String value) { | |
| 316 throw new UnsupportedOperationException( | |
| 317 "Document fragments don't support language."); | |
| 318 } | |
| 319 | |
| 320 void set scrollLeft(int value) { | |
| 321 throw new UnsupportedOperationException( | |
| 322 "Document fragments don't support scrolling."); | |
| 323 } | |
| 324 | |
| 325 void set scrollTop(int value) { | |
| 326 throw new UnsupportedOperationException( | |
| 327 "Document fragments don't support scrolling."); | |
| 328 } | |
| 329 | |
| 330 void set spellcheck(bool value) { | |
| 331 throw new UnsupportedOperationException( | |
| 332 "Spellcheck can't be set for document fragments."); | |
| 333 } | |
| 334 | |
| 335 void set translate(bool value) { | |
| 336 throw new UnsupportedOperationException( | |
| 337 "Spellcheck can't be set for document fragments."); | |
| 338 } | |
| 339 | |
| 340 void set tabIndex(int value) { | |
| 341 throw new UnsupportedOperationException( | |
| 342 "Tab index can't be set for document fragments."); | |
| 343 } | |
| 344 | |
| 345 void set title(String value) { | |
| 346 throw new UnsupportedOperationException( | |
| 347 "Title can't be set for document fragments."); | |
| 348 } | |
| 349 | |
| 350 void set webkitdropzone(String value) { | |
| 351 throw new UnsupportedOperationException( | |
| 352 "WebKit drop zone can't be set for document fragments."); | |
| 353 } | |
| 354 | |
| 355 void set webkitRegionOverflow(String value) { | |
| 356 throw new UnsupportedOperationException( | |
| 357 "WebKit region overflow can't be set for document fragments."); | |
| 358 } | |
| 359 | |
| 360 $!MEMBERS | |
| 361 } | |
| OLD | NEW |