| 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 /** | |
| 6 * Lazy implementation of the child nodes of an element that does not request | |
| 7 * the actual child nodes of an element until strictly necessary greatly | |
| 8 * improving performance for the typical cases where it is not required. | |
| 9 */ | |
| 10 class _ChildNodeListLazy implements NodeList { | |
| 11 final _NodeImpl _this; | |
| 12 | |
| 13 _ChildNodeListLazy(this._this); | |
| 14 | |
| 15 | |
| 16 $if DART2JS | |
| 17 _NodeImpl get first() => JS('_NodeImpl', '#.firstChild', _this); | |
| 18 _NodeImpl last() => JS('_NodeImpl', '#.lastChild', _this); | |
| 19 $else | |
| 20 _NodeImpl get first() => _this.$dom_firstChild; | |
| 21 _NodeImpl last() => _this.$dom_lastChild; | |
| 22 $endif | |
| 23 | |
| 24 void add(_NodeImpl value) { | |
| 25 _this.$dom_appendChild(value); | |
| 26 } | |
| 27 | |
| 28 void addLast(_NodeImpl value) { | |
| 29 _this.$dom_appendChild(value); | |
| 30 } | |
| 31 | |
| 32 | |
| 33 void addAll(Collection<_NodeImpl> collection) { | |
| 34 for (_NodeImpl node in collection) { | |
| 35 _this.$dom_appendChild(node); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 _NodeImpl removeLast() { | |
| 40 final result = last(); | |
| 41 if (result != null) { | |
| 42 _this.$dom_removeChild(result); | |
| 43 } | |
| 44 return result; | |
| 45 } | |
| 46 | |
| 47 void clear() { | |
| 48 _this.text = ''; | |
| 49 } | |
| 50 | |
| 51 void operator []=(int index, _NodeImpl value) { | |
| 52 _this.$dom_replaceChild(value, this[index]); | |
| 53 } | |
| 54 | |
| 55 Iterator<Node> iterator() => _this.$dom_childNodes.iterator(); | |
| 56 | |
| 57 // TODO(jacobr): We can implement these methods much more efficiently by | |
| 58 // looking up the nodeList only once instead of once per iteration. | |
| 59 void forEach(void f(Node element)) => _Collections.forEach(this, f); | |
| 60 | |
| 61 Collection map(f(Node element)) => _Collections.map(this, [], f); | |
| 62 | |
| 63 Collection<Node> filter(bool f(Node element)) => | |
| 64 new _NodeListWrapper(_Collections.filter(this, <Node>[], f)); | |
| 65 | |
| 66 bool every(bool f(Node element)) => _Collections.every(this, f); | |
| 67 | |
| 68 bool some(bool f(Node element)) => _Collections.some(this, f); | |
| 69 | |
| 70 bool isEmpty() => this.length == 0; | |
| 71 | |
| 72 // From List<Node>: | |
| 73 | |
| 74 // TODO(jacobr): this could be implemented for child node lists. | |
| 75 // The exception we throw here is misleading. | |
| 76 void sort(int compare(Node a, Node b)) { | |
| 77 throw new UnsupportedOperationException("Cannot sort immutable List."); | |
| 78 } | |
| 79 | |
| 80 int indexOf(Node element, [int start = 0]) => | |
| 81 _Lists.indexOf(this, element, start, this.length); | |
| 82 | |
| 83 int lastIndexOf(Node element, [int start = 0]) => | |
| 84 _Lists.lastIndexOf(this, element, start); | |
| 85 | |
| 86 // FIXME: implement these. | |
| 87 void setRange(int start, int rangeLength, List<Node> from, [int startFrom]) { | |
| 88 throw new UnsupportedOperationException( | |
| 89 "Cannot setRange on immutable List."); | |
| 90 } | |
| 91 void removeRange(int start, int rangeLength) { | |
| 92 throw new UnsupportedOperationException( | |
| 93 "Cannot removeRange on immutable List."); | |
| 94 } | |
| 95 void insertRange(int start, int rangeLength, [Node initialValue]) { | |
| 96 throw new UnsupportedOperationException( | |
| 97 "Cannot insertRange on immutable List."); | |
| 98 } | |
| 99 NodeList getRange(int start, int rangeLength) => | |
| 100 new _NodeListWrapper(_Lists.getRange(this, start, rangeLength, <Node>[])); | |
| 101 | |
| 102 // -- end List<Node> mixins. | |
| 103 | |
| 104 // TODO(jacobr): benchmark whether this is more efficient or whether caching | |
| 105 // a local copy of $dom_childNodes is more efficient. | |
| 106 int get length() => _this.$dom_childNodes.length; | |
| 107 | |
| 108 _NodeImpl operator[](int index) => _this.$dom_childNodes[index]; | |
| 109 } | |
| 110 | |
| 111 class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { | |
| 112 _ChildNodeListLazy get nodes() { | |
| 113 return new _ChildNodeListLazy(this); | |
| 114 } | |
| 115 | |
| 116 void set nodes(Collection<Node> value) { | |
| 117 // Copy list first since we don't want liveness during iteration. | |
| 118 // TODO(jacobr): there is a better way to do this. | |
| 119 List copy = new List.from(value); | |
| 120 text = ''; | |
| 121 for (Node node in copy) { | |
| 122 $dom_appendChild(node); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 // TODO(jacobr): should we throw an exception if parent is already null? | |
| 127 _NodeImpl remove() { | |
| 128 if (this.parent != null) { | |
| 129 final _NodeImpl parent = this.parent; | |
| 130 parent.$dom_removeChild(this); | |
| 131 } | |
| 132 return this; | |
| 133 } | |
| 134 | |
| 135 _NodeImpl replaceWith(Node otherNode) { | |
| 136 try { | |
| 137 final _NodeImpl parent = this.parent; | |
| 138 parent.$dom_replaceChild(otherNode, this); | |
| 139 } catch (e) { | |
| 140 | |
| 141 }; | |
| 142 return this; | |
| 143 } | |
| 144 | |
| 145 $!MEMBERS | |
| 146 } | |
| OLD | NEW |