| 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 (var 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 var 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 length, List from, [int startFrom = 0]) { | |
| 64 throw const NotImplementedException(); | |
| 65 } | |
| 66 | |
| 67 void removeRange(int start, int length) { | |
| 68 _filtered.getRange(start, length).forEach((el) => el.remove()); | |
| 69 } | |
| 70 | |
| 71 void insertRange(int start, int length, [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 var last = this.last(); | |
| 83 if (last != null) { | |
| 84 last.remove(); | |
| 85 } | |
| 86 return last; | |
| 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 length) => | |
| 98 _filtered.getRange(start, length); | |
| 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 class EmptyStyleDeclaration extends CSSStyleDeclarationWrappingImplementation { | |
| 111 // This can't call super(), since that's a factory constructor | |
| 112 EmptyStyleDeclaration() | |
| 113 : super._wrap(dom.document.createElement('div').style); | |
| 114 | |
| 115 void set cssText(String value) { | |
| 116 throw new UnsupportedOperationException( | |
| 117 "Can't modify a frozen style declaration."); | |
| 118 } | |
| 119 | |
| 120 String removeProperty(String propertyName) { | |
| 121 throw new UnsupportedOperationException( | |
| 122 "Can't modify a frozen style declaration."); | |
| 123 } | |
| 124 | |
| 125 void setProperty(String propertyName, String value, [String priority]) { | |
| 126 throw new UnsupportedOperationException( | |
| 127 "Can't modify a frozen style declaration."); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 Future<CSSStyleDeclaration> _emptyStyleFuture() { | |
| 132 return _createMeasurementFuture(() => new EmptyStyleDeclaration(), | |
| 133 new Completer<CSSStyleDeclaration>()); | |
| 134 } | |
| 135 | |
| 136 class EmptyElementRect implements ElementRect { | |
| 137 final ClientRect client = const SimpleClientRect(0, 0, 0, 0); | |
| 138 final ClientRect offset = const SimpleClientRect(0, 0, 0, 0); | |
| 139 final ClientRect scroll = const SimpleClientRect(0, 0, 0, 0); | |
| 140 final ClientRect bounding = const SimpleClientRect(0, 0, 0, 0); | |
| 141 final List<ClientRect> clientRects = const <ClientRect>[]; | |
| 142 | |
| 143 const EmptyElementRect(); | |
| 144 } | |
| 145 | |
| 146 class DocumentFragmentWrappingImplementation extends NodeWrappingImplementation
implements DocumentFragment { | |
| 147 ElementList _elements; | |
| 148 | |
| 149 DocumentFragmentWrappingImplementation._wrap(ptr) : super._wrap(ptr) {} | |
| 150 | |
| 151 /** @domName Document.createDocumentFragment */ | |
| 152 factory DocumentFragmentWrappingImplementation() { | |
| 153 return new DocumentFragmentWrappingImplementation._wrap( | |
| 154 dom.document.createDocumentFragment()); | |
| 155 } | |
| 156 | |
| 157 factory DocumentFragmentWrappingImplementation.html(String html) { | |
| 158 var fragment = new DocumentFragment(); | |
| 159 fragment.innerHTML = html; | |
| 160 return fragment; | |
| 161 } | |
| 162 | |
| 163 factory DocumentFragmentWrappingImplementation.xml(String xml) { | |
| 164 var fragment = new DocumentFragment(); | |
| 165 var e = new XMLElement.tag("xml"); | |
| 166 e.innerHTML = xml; | |
| 167 | |
| 168 // Copy list first since we don't want liveness during iteration. | |
| 169 List nodes = new List.from(e.nodes); | |
| 170 fragment.nodes.addAll(nodes); | |
| 171 return fragment; | |
| 172 } | |
| 173 | |
| 174 factory DocumentFragmentWrappingImplementation.svg(String svg) { | |
| 175 var fragment = new DocumentFragment(); | |
| 176 var e = new SVGSVGElement(); | |
| 177 e.innerHTML = svg; | |
| 178 | |
| 179 // Copy list first since we don't want liveness during iteration. | |
| 180 List nodes = new List.from(e.nodes); | |
| 181 fragment.nodes.addAll(nodes); | |
| 182 return fragment; | |
| 183 } | |
| 184 | |
| 185 ElementList get elements() { | |
| 186 if (_elements == null) { | |
| 187 _elements = new FilteredElementList(this); | |
| 188 } | |
| 189 return _elements; | |
| 190 } | |
| 191 | |
| 192 void set elements(Collection<Element> value) { | |
| 193 // Copy list first since we don't want liveness during iteration. | |
| 194 List copy = new List.from(value); | |
| 195 final elements = this.elements; | |
| 196 elements.clear(); | |
| 197 elements.addAll(copy); | |
| 198 } | |
| 199 | |
| 200 String get innerHTML() { | |
| 201 var e = new Element.tag("div"); | |
| 202 e.nodes.add(this.clone(true)); | |
| 203 return e.innerHTML; | |
| 204 } | |
| 205 | |
| 206 String get outerHTML() => innerHTML; | |
| 207 | |
| 208 // TODO(nweiz): Do we want to support some variant of innerHTML for XML and/or | |
| 209 // SVG strings? | |
| 210 void set innerHTML(String value) { | |
| 211 this.nodes.clear(); | |
| 212 | |
| 213 var e = new Element.tag("div"); | |
| 214 e.innerHTML = value; | |
| 215 | |
| 216 // Copy list first since we don't want liveness during iteration. | |
| 217 List nodes = new List.from(e.nodes); | |
| 218 this.nodes.addAll(nodes); | |
| 219 } | |
| 220 | |
| 221 Node _insertAdjacentNode(String where, Node node) { | |
| 222 switch (where.toLowerCase()) { | |
| 223 case "beforebegin": return null; | |
| 224 case "afterend": return null; | |
| 225 case "afterbegin": | |
| 226 this.insertBefore(node, nodes.first); | |
| 227 return node; | |
| 228 case "beforeend": | |
| 229 this.nodes.add(node); | |
| 230 return node; | |
| 231 default: | |
| 232 throw new IllegalArgumentException("Invalid position ${where}"); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 Element insertAdjacentElement([String where = null, Element element = null]) | |
| 237 => this._insertAdjacentNode(where, element); | |
| 238 | |
| 239 void insertAdjacentText([String where = null, String text = null]) { | |
| 240 this._insertAdjacentNode(where, new Text(text)); | |
| 241 } | |
| 242 | |
| 243 void insertAdjacentHTML( | |
| 244 [String position_OR_where = null, String text = null]) { | |
| 245 this._insertAdjacentNode( | |
| 246 position_OR_where, new DocumentFragment.html(text)); | |
| 247 } | |
| 248 | |
| 249 ElementEvents get on() { | |
| 250 if (_on === null) { | |
| 251 _on = new ElementEventsImplementation._wrap(_ptr); | |
| 252 } | |
| 253 return _on; | |
| 254 } | |
| 255 | |
| 256 Future<ElementRect> get rect() { | |
| 257 return _createMeasurementFuture(() => const EmptyElementRect(), | |
| 258 new Completer<ElementRect>()); | |
| 259 } | |
| 260 | |
| 261 Element query(String selectors) => | |
| 262 LevelDom.wrapElement(_ptr.querySelector(selectors)); | |
| 263 | |
| 264 ElementList queryAll(String selectors) => | |
| 265 LevelDom.wrapElementList(_ptr.querySelectorAll(selectors)); | |
| 266 | |
| 267 // If we can come up with a semi-reasonable default value for an Element | |
| 268 // getter, we'll use it. In general, these return the same values as an | |
| 269 // element that has no parent. | |
| 270 String get contentEditable() => "false"; | |
| 271 bool get isContentEditable() => false; | |
| 272 bool get draggable() => false; | |
| 273 bool get hidden() => false; | |
| 274 bool get spellcheck() => false; | |
| 275 int get tabIndex() => -1; | |
| 276 String get id() => ""; | |
| 277 String get title() => ""; | |
| 278 String get tagName() => ""; | |
| 279 String get webkitdropzone() => ""; | |
| 280 Element get firstElementChild() => elements.first(); | |
| 281 Element get lastElementChild() => elements.last(); | |
| 282 Element get nextElementSibling() => null; | |
| 283 Element get previousElementSibling() => null; | |
| 284 Element get offsetParent() => null; | |
| 285 Element get parent() => null; | |
| 286 Map<String, String> get attributes() => const {}; | |
| 287 CSSClassSet get classes() => null; | |
| 288 Map<String, String> get dataAttributes() => const {}; | |
| 289 CSSStyleDeclaration get style() => new EmptyStyleDeclaration(); | |
| 290 Future<CSSStyleDeclaration> get computedStyle() => | |
| 291 _emptyStyleFuture(); | |
| 292 Future<CSSStyleDeclaration> getComputedStyle(String pseudoElement) => | |
| 293 _emptyStyleFuture(); | |
| 294 bool matchesSelector([String selectors]) => false; | |
| 295 | |
| 296 // Imperative Element methods are made into no-ops, as they are on parentless | |
| 297 // elements. | |
| 298 void blur() {} | |
| 299 void focus() {} | |
| 300 void scrollByLines([int lines]) {} | |
| 301 void scrollByPages([int pages]) {} | |
| 302 void scrollIntoView([bool centerIfNeeded]) {} | |
| 303 | |
| 304 // Setters throw errors rather than being no-ops because we aren't going to | |
| 305 // retain the values that were set, and erroring out seems clearer. | |
| 306 void set attributes(Map<String, String> value) { | |
| 307 throw new UnsupportedOperationException( | |
| 308 "Attributes can't be set for document fragments."); | |
| 309 } | |
| 310 | |
| 311 void set classes(Collection<String> value) { | |
| 312 throw new UnsupportedOperationException( | |
| 313 "Classes can't be set for document fragments."); | |
| 314 } | |
| 315 | |
| 316 void set dataAttributes(Map<String, String> value) { | |
| 317 throw new UnsupportedOperationException( | |
| 318 "Data attributes can't be set for document fragments."); | |
| 319 } | |
| 320 | |
| 321 void set contentEditable(String value) { | |
| 322 throw new UnsupportedOperationException( | |
| 323 "Content editable can't be set for document fragments."); | |
| 324 } | |
| 325 | |
| 326 String get dir() { | |
| 327 throw new UnsupportedOperationException( | |
| 328 "Document fragments don't support text direction."); | |
| 329 } | |
| 330 | |
| 331 void set dir(String value) { | |
| 332 throw new UnsupportedOperationException( | |
| 333 "Document fragments don't support text direction."); | |
| 334 } | |
| 335 | |
| 336 void set draggable(bool value) { | |
| 337 throw new UnsupportedOperationException( | |
| 338 "Draggable can't be set for document fragments."); | |
| 339 } | |
| 340 | |
| 341 void set hidden(bool value) { | |
| 342 throw new UnsupportedOperationException( | |
| 343 "Hidden can't be set for document fragments."); | |
| 344 } | |
| 345 | |
| 346 void set id(String value) { | |
| 347 throw new UnsupportedOperationException( | |
| 348 "ID can't be set for document fragments."); | |
| 349 } | |
| 350 | |
| 351 String get lang() { | |
| 352 throw new UnsupportedOperationException( | |
| 353 "Document fragments don't support language."); | |
| 354 } | |
| 355 | |
| 356 void set lang(String value) { | |
| 357 throw new UnsupportedOperationException( | |
| 358 "Document fragments don't support language."); | |
| 359 } | |
| 360 | |
| 361 void set scrollLeft(int value) { | |
| 362 throw new UnsupportedOperationException( | |
| 363 "Document fragments don't support scrolling."); | |
| 364 } | |
| 365 | |
| 366 void set scrollTop(int value) { | |
| 367 throw new UnsupportedOperationException( | |
| 368 "Document fragments don't support scrolling."); | |
| 369 } | |
| 370 | |
| 371 void set spellcheck(bool value) { | |
| 372 throw new UnsupportedOperationException( | |
| 373 "Spellcheck can't be set for document fragments."); | |
| 374 } | |
| 375 | |
| 376 void set tabIndex(int value) { | |
| 377 throw new UnsupportedOperationException( | |
| 378 "Tab index can't be set for document fragments."); | |
| 379 } | |
| 380 | |
| 381 void set title(String value) { | |
| 382 throw new UnsupportedOperationException( | |
| 383 "Title can't be set for document fragments."); | |
| 384 } | |
| 385 | |
| 386 void set webkitdropzone(String value) { | |
| 387 throw new UnsupportedOperationException( | |
| 388 "WebKit drop zone can't be set for document fragments."); | |
| 389 } | |
| 390 | |
| 391 DocumentFragment clone(bool deep) => super.clone(deep); | |
| 392 } | |
| OLD | NEW |