| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 // TODO(jacobr): we could write this method more efficiently if we wanted to |
| 6 // however performance isn't crucial as it is only called when a method is |
| 7 // called from an atypical context (e.g. measurement method called outside of |
| 8 // requestMeasurementFrame or dom manipulation called within |
| 9 // requestMeasurementFrame). |
| 10 bool _nodeInDocument(dom.Node node) { |
| 11 return LevelDom.wrapNode(node)._inDocument; |
| 12 } |
| 13 |
| 5 class _ChildrenNodeList implements NodeList { | 14 class _ChildrenNodeList implements NodeList { |
| 6 // Raw node. | 15 // Raw node. |
| 7 final _node; | 16 final _node; |
| 8 final _childNodes; | 17 final _childNodes; |
| 9 | 18 |
| 10 _ChildrenNodeList._wrap(var node) | 19 _ChildrenNodeList._wrap(var node) |
| 11 : _childNodes = node.childNodes, | 20 : _childNodes = node.childNodes, |
| 12 _node = node; | 21 _node = node; |
| 13 | 22 |
| 14 List<Node> _toList() { | 23 List<Node> _toList() { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 void operator []=(int index, Node value) { | 86 void operator []=(int index, Node value) { |
| 78 _node.replaceChild(LevelDom.unwrap(value), _childNodes[index]); | 87 _node.replaceChild(LevelDom.unwrap(value), _childNodes[index]); |
| 79 } | 88 } |
| 80 | 89 |
| 81 void set length(int newLength) { | 90 void set length(int newLength) { |
| 82 throw new UnsupportedOperationException(''); | 91 throw new UnsupportedOperationException(''); |
| 83 } | 92 } |
| 84 | 93 |
| 85 /** @domName Node.appendChild */ | 94 /** @domName Node.appendChild */ |
| 86 Node add(Node value) { | 95 Node add(Node value) { |
| 96 assert(!_inMeasurementFrame |
| 97 || (!_nodeInDocument(_node) && !value._inDocument)); |
| 87 _node.appendChild(LevelDom.unwrap(value)); | 98 _node.appendChild(LevelDom.unwrap(value)); |
| 88 return value; | 99 return value; |
| 89 } | 100 } |
| 90 | 101 |
| 91 Node addLast(Node value) { | 102 Node addLast(Node value) { |
| 92 _node.appendChild(LevelDom.unwrap(value)); | 103 assert(!_inMeasurementFrame |
| 104 || (!_nodeInDocument(_node) && !value._inDocument)); |
| 105 _node.appendChild(LevelDom.unwrap(value)); |
| 93 return value; | 106 return value; |
| 94 } | 107 } |
| 95 | 108 |
| 96 Iterator<Node> iterator() { | 109 Iterator<Node> iterator() { |
| 97 return _toList().iterator(); | 110 return _toList().iterator(); |
| 98 } | 111 } |
| 99 | 112 |
| 100 void addAll(Collection<Node> collection) { | 113 void addAll(Collection<Node> collection) { |
| 114 assert(!_inMeasurementFrame || !_nodeInDocument(_node)); |
| 101 for (Node node in collection) { | 115 for (Node node in collection) { |
| 116 assert(!_inMeasurementFrame || !node._inDocument); |
| 102 _node.appendChild(LevelDom.unwrap(node)); | 117 _node.appendChild(LevelDom.unwrap(node)); |
| 103 } | 118 } |
| 104 } | 119 } |
| 105 | 120 |
| 106 void sort(int compare(Node a, Node b)) { | 121 void sort(int compare(Node a, Node b)) { |
| 107 throw const UnsupportedOperationException('TODO(jacobr): should we impl?'); | 122 throw const UnsupportedOperationException('TODO(jacobr): should we impl?'); |
| 108 } | 123 } |
| 109 | 124 |
| 110 void copyFrom(List<Object> src, int srcStart, int dstStart, int count) { | 125 void copyFrom(List<Object> src, int srcStart, int dstStart, int count) { |
| 111 throw 'Not impl yet. todo(jacobr)'; | 126 throw 'Not impl yet. todo(jacobr)'; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 int indexOf(Node element, [int start = 0]) { | 187 int indexOf(Node element, [int start = 0]) { |
| 173 return _Lists.indexOf(this, element, start, this.length); | 188 return _Lists.indexOf(this, element, start, this.length); |
| 174 } | 189 } |
| 175 | 190 |
| 176 int lastIndexOf(Node element, [int start = null]) { | 191 int lastIndexOf(Node element, [int start = null]) { |
| 177 if (start === null) start = length - 1; | 192 if (start === null) start = length - 1; |
| 178 return _Lists.lastIndexOf(this, element, start); | 193 return _Lists.lastIndexOf(this, element, start); |
| 179 } | 194 } |
| 180 | 195 |
| 181 void clear() { | 196 void clear() { |
| 197 assert(!_inMeasurementFrame || !_nodeInDocument(_node)); |
| 182 _node.textContent = ''; | 198 _node.textContent = ''; |
| 183 } | 199 } |
| 184 | 200 |
| 185 Node removeLast() { | 201 Node removeLast() { |
| 202 assert(!_inMeasurementFrame || !_nodeInDocument(_node)); |
| 186 final last = this.last(); | 203 final last = this.last(); |
| 187 if (last != null) { | 204 if (last != null) { |
| 188 _node.removeChild(LevelDom.unwrap(last)); | 205 _node.removeChild(LevelDom.unwrap(last)); |
| 189 } | 206 } |
| 190 return last; | 207 return last; |
| 191 } | 208 } |
| 192 | 209 |
| 193 Node last() { | 210 Node last() { |
| 194 return LevelDom.wrapNode(_node.lastChild); | 211 return LevelDom.wrapNode(_node.lastChild); |
| 195 } | 212 } |
| 196 } | 213 } |
| 197 | 214 |
| 198 class NodeWrappingImplementation extends EventTargetWrappingImplementation imple
ments Node { | 215 class NodeWrappingImplementation extends EventTargetWrappingImplementation imple
ments Node { |
| 199 NodeList _nodes; | 216 NodeList _nodes; |
| 200 | 217 |
| 201 NodeWrappingImplementation._wrap(ptr) : super._wrap(ptr); | 218 NodeWrappingImplementation._wrap(ptr) : super._wrap(ptr); |
| 202 | 219 |
| 203 void set nodes(Collection<Node> value) { | 220 void set nodes(Collection<Node> value) { |
| 221 assert(!_inMeasurementFrame || !_inDocument); |
| 204 // Copy list first since we don't want liveness during iteration. | 222 // Copy list first since we don't want liveness during iteration. |
| 205 List copy = new List.from(value); | 223 List copy = new List.from(value); |
| 206 nodes.clear(); | 224 nodes.clear(); |
| 207 nodes.addAll(copy); | 225 nodes.addAll(copy); |
| 208 } | 226 } |
| 209 | 227 |
| 210 NodeList get nodes() { | 228 NodeList get nodes() { |
| 211 if (_nodes === null) { | 229 if (_nodes === null) { |
| 212 _nodes = new _ChildrenNodeList._wrap(_ptr); | 230 _nodes = new _ChildrenNodeList._wrap(_ptr); |
| 213 } | 231 } |
| 214 return _nodes; | 232 return _nodes; |
| 215 } | 233 } |
| 216 | 234 |
| 217 Node get nextNode() => LevelDom.wrapNode(_ptr.nextSibling); | 235 Node get nextNode() => LevelDom.wrapNode(_ptr.nextSibling); |
| 218 | 236 |
| 219 Document get document() => LevelDom.wrapDocument(_ptr.ownerDocument); | 237 Document get document() => LevelDom.wrapDocument(_ptr.ownerDocument); |
| 220 | 238 |
| 221 Node get parent() => LevelDom.wrapNode(_ptr.parentNode); | 239 Node get parent() => LevelDom.wrapNode(_ptr.parentNode); |
| 222 | 240 |
| 223 Node get previousNode() => LevelDom.wrapNode(_ptr.previousSibling); | 241 Node get previousNode() => LevelDom.wrapNode(_ptr.previousSibling); |
| 224 | 242 |
| 225 String get text() => _ptr.textContent; | 243 String get text() => _ptr.textContent; |
| 226 | 244 |
| 227 void set text(String value) { _ptr.textContent = value; } | 245 void set text(String value) { |
| 246 assert(!_inMeasurementFrame || !_inDocument); |
| 247 _ptr.textContent = value; |
| 248 } |
| 228 | 249 |
| 229 // New methods implemented. | 250 // New methods implemented. |
| 230 Node replaceWith(Node otherNode) { | 251 Node replaceWith(Node otherNode) { |
| 252 assert(!_inMeasurementFrame || !_inDocument); |
| 231 try { | 253 try { |
| 232 _ptr.parentNode.replaceChild(LevelDom.unwrap(otherNode), _ptr); | 254 _ptr.parentNode.replaceChild(LevelDom.unwrap(otherNode), _ptr); |
| 233 } catch(var e) { | 255 } catch(var e) { |
| 234 // TODO(jacobr): what should we return on failure? | 256 // TODO(jacobr): what should we return on failure? |
| 235 } | 257 } |
| 236 return this; | 258 return this; |
| 237 } | 259 } |
| 238 | 260 |
| 239 Node remove() { | 261 Node remove() { |
| 262 assert(!_inMeasurementFrame || !_inDocument); |
| 240 // TODO(jacobr): should we throw an exception if parent is already null? | 263 // TODO(jacobr): should we throw an exception if parent is already null? |
| 241 if (_ptr.parentNode !== null) { | 264 if (_ptr.parentNode !== null) { |
| 242 _ptr.parentNode.removeChild(_ptr); | 265 _ptr.parentNode.removeChild(_ptr); |
| 243 } | 266 } |
| 244 return this; | 267 return this; |
| 245 } | 268 } |
| 246 | 269 |
| 247 /** @domName contains */ | 270 /** @domName contains */ |
| 248 bool contains(Node otherNode) { | 271 bool contains(Node otherNode) { |
| 249 // TODO: Feature detect and use built in. | 272 // TODO: Feature detect and use built in. |
| 250 while (otherNode != null && otherNode != this) { | 273 while (otherNode != null && otherNode != this) { |
| 251 otherNode = otherNode.parent; | 274 otherNode = otherNode.parent; |
| 252 } | 275 } |
| 253 return otherNode == this; | 276 return otherNode == this; |
| 254 } | 277 } |
| 255 | 278 |
| 256 // TODO(jacobr): remove when/if List supports a method similar to | 279 // TODO(jacobr): remove when/if List supports a method similar to |
| 257 // insertBefore or we switch NodeList to implement LinkedList rather than | 280 // insertBefore or we switch NodeList to implement LinkedList rather than |
| 258 // array. | 281 // array. |
| 259 Node insertBefore(Node newChild, Node refChild) { | 282 Node insertBefore(Node newChild, Node refChild) { |
| 283 assert(!_inMeasurementFrame || !_inDocument); |
| 260 return LevelDom.wrapNode(_ptr.insertBefore( | 284 return LevelDom.wrapNode(_ptr.insertBefore( |
| 261 LevelDom.unwrap(newChild), LevelDom.unwrap(refChild))); | 285 LevelDom.unwrap(newChild), LevelDom.unwrap(refChild))); |
| 262 } | 286 } |
| 263 | 287 |
| 264 Node clone(bool deep) { | 288 Node clone(bool deep) { |
| 265 return LevelDom.wrapNode(_ptr.cloneNode(deep)); | 289 return LevelDom.wrapNode(_ptr.cloneNode(deep)); |
| 266 } | 290 } |
| 291 |
| 292 bool get _inDocument() => document.contains(this); |
| 267 } | 293 } |
| OLD | NEW |