| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 class _ChildrenNodeList implements NodeList { | 5 class _ChildrenNodeList implements NodeList { |
| 6 // Raw node. | 6 // Raw node. |
| 7 final _node; | 7 final _node; |
| 8 final _childNodes; | 8 final _childNodes; |
| 9 | 9 |
| 10 _ChildrenNodeList._wrap(var node) | 10 _ChildrenNodeList._wrap(var node) |
| 11 : _childNodes = node.childNodes, | 11 : _childNodes = node.childNodes, |
| 12 _node = node; | 12 _node = node; |
| 13 | 13 |
| 14 List<Node> _toList() { | 14 List<Node> _toList() { |
| 15 final output = new List(_childNodes.length); | 15 final output = new List(_childNodes.length); |
| 16 for (int i = 0, len = _childNodes.length; i < len; i++) { | 16 for (int i = 0, len = _childNodes.length; i < len; i++) { |
| 17 output[i] = LevelDom.wrapNode(_childNodes[i]); | 17 output[i] = LevelDom.wrapNode(_childNodes[i]); |
| 18 } | 18 } |
| 19 return output; | 19 return output; |
| 20 } | 20 } |
| 21 | 21 |
| 22 Node get first() { | 22 Node get first() { |
| 23 return LevelDom.wrapNode(_node.firstChild); | 23 return LevelDom.wrapNode(_node.firstChild); |
| 24 } | 24 } |
| 25 | 25 |
| 26 void forEach(void f(Node element)) => _toList().forEach(f); | 26 void forEach(void f(Node element)) => _toList().forEach(f); |
| 27 | 27 |
| 28 Collection map(f(Node element)) => _toList().map(f); | 28 Collection map(f(Node element)) => _toList().map(f); |
| 29 | 29 |
| 30 Collection<Node> filter(bool f(Node element)) => _toList().filter(f); | 30 NodeList filter(bool f(Node element)) => new _NodeList(_toList().filter(f)); |
| 31 | 31 |
| 32 bool every(bool f(Node element)) { | 32 bool every(bool f(Node element)) { |
| 33 for(Node element in this) { | 33 for(Node element in this) { |
| 34 if (!f(element)) { | 34 if (!f(element)) { |
| 35 return false; | 35 return false; |
| 36 } | 36 } |
| 37 }; | 37 }; |
| 38 return true; | 38 return true; |
| 39 } | 39 } |
| 40 | 40 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 void setRange(int start, int length, List from, [int startFrom = 0]) => | 100 void setRange(int start, int length, List from, [int startFrom = 0]) => |
| 101 Lists.setRange(this, start, length, from, startFrom); | 101 Lists.setRange(this, start, length, from, startFrom); |
| 102 | 102 |
| 103 void removeRange(int start, int length) => | 103 void removeRange(int start, int length) => |
| 104 Lists.removeRange(this, start, length, (i) => this[i].remove()); | 104 Lists.removeRange(this, start, length, (i) => this[i].remove()); |
| 105 | 105 |
| 106 void insertRange(int start, int length, [initialValue = null]) { | 106 void insertRange(int start, int length, [initialValue = null]) { |
| 107 throw const NotImplementedException(); | 107 throw const NotImplementedException(); |
| 108 } | 108 } |
| 109 | 109 |
| 110 List getRange(int start, int length) => Lists.getRange(this, start, length); | 110 NodeList getRange(int start, int length) => |
| 111 new _NodeList(Lists.getRange(this, start, length)); |
| 111 | 112 |
| 112 int indexOf(Node element, [int start = 0]) { | 113 int indexOf(Node element, [int start = 0]) { |
| 113 return Lists.indexOf(this, element, start, this.length); | 114 return Lists.indexOf(this, element, start, this.length); |
| 114 } | 115 } |
| 115 | 116 |
| 116 int lastIndexOf(Node element, [int start = null]) { | 117 int lastIndexOf(Node element, [int start = null]) { |
| 117 if (start === null) start = length - 1; | 118 if (start === null) start = length - 1; |
| 118 return Lists.lastIndexOf(this, element, start); | 119 return Lists.lastIndexOf(this, element, start); |
| 119 } | 120 } |
| 120 | 121 |
| 121 void clear() { | 122 void clear() { |
| 122 _node.textContent = ''; | 123 _node.textContent = ''; |
| 123 } | 124 } |
| 124 | 125 |
| 125 Node removeLast() { | 126 Node removeLast() { |
| 126 final last = this.last(); | 127 final last = this.last(); |
| 127 if (last != null) { | 128 if (last != null) { |
| 128 _node.removeChild(LevelDom.unwrap(last)); | 129 _node.removeChild(LevelDom.unwrap(last)); |
| 129 } | 130 } |
| 130 return last; | 131 return last; |
| 131 } | 132 } |
| 132 | 133 |
| 133 Node last() { | 134 Node last() { |
| 134 return LevelDom.wrapNode(_node.lastChild); | 135 return LevelDom.wrapNode(_node.lastChild); |
| 135 } | 136 } |
| 136 } | 137 } |
| 137 | 138 |
| 139 // TODO(nweiz): when all implementations we target have the same name for the |
| 140 // coreimpl implementation of List<Node>, extend that rather than wrapping. |
| 141 class _NodeList implements NodeList { |
| 142 List<Node> _list; |
| 143 |
| 144 _NodeList(List<Node> this._list); |
| 145 |
| 146 Iterator<Node> iterator() => _list.iterator(); |
| 147 |
| 148 void forEach(void f(Node element)) => _list.forEach(f); |
| 149 |
| 150 Collection map(f(Node element)) => _list.map(f); |
| 151 |
| 152 NodeList filter(bool f(Node element)) => new _NodeList(_list.filter(f)); |
| 153 |
| 154 bool every(bool f(Node element)) => _list.every(f); |
| 155 |
| 156 bool some(bool f(Node element)) => _list.some(f); |
| 157 |
| 158 bool isEmpty() => _list.isEmpty(); |
| 159 |
| 160 int get length() => _list.length; |
| 161 |
| 162 Node operator [](int index) => _list[index]; |
| 163 |
| 164 void operator []=(int index, Node value) { _list[index] = value; } |
| 165 |
| 166 void set length(int newLength) { _list.length = newLength; } |
| 167 |
| 168 void add(Node value) => _list.add(value); |
| 169 |
| 170 void addLast(Node value) => _list.addLast(value); |
| 171 |
| 172 void addAll(Collection<Node> collection) => _list.addAll(collection); |
| 173 |
| 174 void sort(int compare(Node a, Node b)) => _list.sort(compare); |
| 175 |
| 176 int indexOf(Node element, [int start = 0]) => _list.indexOf(element, start); |
| 177 |
| 178 int lastIndexOf(Node element, [int start = 0]) => |
| 179 _list.lastIndexOf(element, start); |
| 180 |
| 181 void clear() => _list.clear(); |
| 182 |
| 183 Node removeLast() => _list.removeLast(); |
| 184 |
| 185 Node last() => _list.last(); |
| 186 |
| 187 NodeList getRange(int start, int length) => |
| 188 new _NodeList(_list.getRange(start, length)); |
| 189 |
| 190 void setRange(int start, int length, List<Node> from, [int startFrom = 0]) => |
| 191 _list.setRange(start, length, from, startFrom); |
| 192 |
| 193 void removeRange(int start, int length) => _list.removeRange(start, length); |
| 194 |
| 195 void insertRange(int start, int length, [Node initialValue = null]) => |
| 196 _list.insertRange(start, length, initialValue); |
| 197 |
| 198 Node get first() => _list[0]; |
| 199 } |
| 200 |
| 138 class NodeWrappingImplementation extends EventTargetWrappingImplementation imple
ments Node { | 201 class NodeWrappingImplementation extends EventTargetWrappingImplementation imple
ments Node { |
| 139 NodeList _nodes; | 202 NodeList _nodes; |
| 140 | 203 |
| 141 NodeWrappingImplementation._wrap(ptr) : super._wrap(ptr); | 204 NodeWrappingImplementation._wrap(ptr) : super._wrap(ptr); |
| 142 | 205 |
| 143 void set nodes(Collection<Node> value) { | 206 void set nodes(Collection<Node> value) { |
| 144 // Copy list first since we don't want liveness during iteration. | 207 // Copy list first since we don't want liveness during iteration. |
| 145 List copy = new List.from(value); | 208 List copy = new List.from(value); |
| 146 nodes.clear(); | 209 nodes.clear(); |
| 147 nodes.addAll(copy); | 210 nodes.addAll(copy); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 // array. | 261 // array. |
| 199 Node insertBefore(Node newChild, Node refChild) { | 262 Node insertBefore(Node newChild, Node refChild) { |
| 200 return LevelDom.wrapNode(_ptr.insertBefore( | 263 return LevelDom.wrapNode(_ptr.insertBefore( |
| 201 LevelDom.unwrap(newChild), LevelDom.unwrap(refChild))); | 264 LevelDom.unwrap(newChild), LevelDom.unwrap(refChild))); |
| 202 } | 265 } |
| 203 | 266 |
| 204 Node clone(bool deep) { | 267 Node clone(bool deep) { |
| 205 return LevelDom.wrapNode(_ptr.cloneNode(deep)); | 268 return LevelDom.wrapNode(_ptr.cloneNode(deep)); |
| 206 } | 269 } |
| 207 } | 270 } |
| OLD | NEW |