| OLD | NEW |
| 1 // Copyright (c) 2012, 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 // 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 | |
| 14 class _ChildrenNodeList implements NodeList { | 5 class _ChildrenNodeList implements NodeList { |
| 15 // Raw node. | 6 // Raw node. |
| 16 final _node; | 7 final _node; |
| 17 final _childNodes; | 8 final _childNodes; |
| 18 | 9 |
| 19 _ChildrenNodeList._wrap(var node) | 10 _ChildrenNodeList._wrap(var node) |
| 20 : _childNodes = node.childNodes, | 11 : _childNodes = node.childNodes, |
| 21 _node = node; | 12 _node = node; |
| 22 | 13 |
| 23 List<Node> _toList() { | 14 List<Node> _toList() { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 void operator []=(int index, Node value) { | 63 void operator []=(int index, Node value) { |
| 73 _node.replaceChild(LevelDom.unwrap(value), _childNodes[index]); | 64 _node.replaceChild(LevelDom.unwrap(value), _childNodes[index]); |
| 74 } | 65 } |
| 75 | 66 |
| 76 void set length(int newLength) { | 67 void set length(int newLength) { |
| 77 throw new UnsupportedOperationException(''); | 68 throw new UnsupportedOperationException(''); |
| 78 } | 69 } |
| 79 | 70 |
| 80 /** @domName Node.appendChild */ | 71 /** @domName Node.appendChild */ |
| 81 Node add(Node value) { | 72 Node add(Node value) { |
| 82 assert(!_inMeasurementFrame | |
| 83 || (!_nodeInDocument(_node) && !value._inDocument)); | |
| 84 _node.appendChild(LevelDom.unwrap(value)); | 73 _node.appendChild(LevelDom.unwrap(value)); |
| 85 return value; | 74 return value; |
| 86 } | 75 } |
| 87 | 76 |
| 88 Node addLast(Node value) { | 77 Node addLast(Node value) { |
| 89 assert(!_inMeasurementFrame | 78 _node.appendChild(LevelDom.unwrap(value)); |
| 90 || (!_nodeInDocument(_node) && !value._inDocument)); | |
| 91 _node.appendChild(LevelDom.unwrap(value)); | |
| 92 return value; | 79 return value; |
| 93 } | 80 } |
| 94 | 81 |
| 95 Iterator<Node> iterator() { | 82 Iterator<Node> iterator() { |
| 96 return _toList().iterator(); | 83 return _toList().iterator(); |
| 97 } | 84 } |
| 98 | 85 |
| 99 void addAll(Collection<Node> collection) { | 86 void addAll(Collection<Node> collection) { |
| 100 assert(!_inMeasurementFrame || !_nodeInDocument(_node)); | |
| 101 for (Node node in collection) { | 87 for (Node node in collection) { |
| 102 assert(!_inMeasurementFrame || !node._inDocument); | |
| 103 _node.appendChild(LevelDom.unwrap(node)); | 88 _node.appendChild(LevelDom.unwrap(node)); |
| 104 } | 89 } |
| 105 } | 90 } |
| 106 | 91 |
| 107 void sort(int compare(Node a, Node b)) { | 92 void sort(int compare(Node a, Node b)) { |
| 108 throw const UnsupportedOperationException('TODO(jacobr): should we impl?'); | 93 throw const UnsupportedOperationException('TODO(jacobr): should we impl?'); |
| 109 } | 94 } |
| 110 | 95 |
| 111 void copyFrom(List<Object> src, int srcStart, int dstStart, int count) { | 96 void copyFrom(List<Object> src, int srcStart, int dstStart, int count) { |
| 112 throw 'Not impl yet. todo(jacobr)'; | 97 throw 'Not impl yet. todo(jacobr)'; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 127 int indexOf(Node element, [int start = 0]) { | 112 int indexOf(Node element, [int start = 0]) { |
| 128 return Lists.indexOf(this, element, start, this.length); | 113 return Lists.indexOf(this, element, start, this.length); |
| 129 } | 114 } |
| 130 | 115 |
| 131 int lastIndexOf(Node element, [int start = null]) { | 116 int lastIndexOf(Node element, [int start = null]) { |
| 132 if (start === null) start = length - 1; | 117 if (start === null) start = length - 1; |
| 133 return Lists.lastIndexOf(this, element, start); | 118 return Lists.lastIndexOf(this, element, start); |
| 134 } | 119 } |
| 135 | 120 |
| 136 void clear() { | 121 void clear() { |
| 137 assert(!_inMeasurementFrame || !_nodeInDocument(_node)); | |
| 138 _node.textContent = ''; | 122 _node.textContent = ''; |
| 139 } | 123 } |
| 140 | 124 |
| 141 Node removeLast() { | 125 Node removeLast() { |
| 142 assert(!_inMeasurementFrame || !_nodeInDocument(_node)); | |
| 143 final last = this.last(); | 126 final last = this.last(); |
| 144 if (last != null) { | 127 if (last != null) { |
| 145 _node.removeChild(LevelDom.unwrap(last)); | 128 _node.removeChild(LevelDom.unwrap(last)); |
| 146 } | 129 } |
| 147 return last; | 130 return last; |
| 148 } | 131 } |
| 149 | 132 |
| 150 Node last() { | 133 Node last() { |
| 151 return LevelDom.wrapNode(_node.lastChild); | 134 return LevelDom.wrapNode(_node.lastChild); |
| 152 } | 135 } |
| 153 } | 136 } |
| 154 | 137 |
| 155 class NodeWrappingImplementation extends EventTargetWrappingImplementation imple
ments Node { | 138 class NodeWrappingImplementation extends EventTargetWrappingImplementation imple
ments Node { |
| 156 NodeList _nodes; | 139 NodeList _nodes; |
| 157 | 140 |
| 158 NodeWrappingImplementation._wrap(ptr) : super._wrap(ptr); | 141 NodeWrappingImplementation._wrap(ptr) : super._wrap(ptr); |
| 159 | 142 |
| 160 void set nodes(Collection<Node> value) { | 143 void set nodes(Collection<Node> value) { |
| 161 assert(!_inMeasurementFrame || !_inDocument); | |
| 162 // Copy list first since we don't want liveness during iteration. | 144 // Copy list first since we don't want liveness during iteration. |
| 163 List copy = new List.from(value); | 145 List copy = new List.from(value); |
| 164 nodes.clear(); | 146 nodes.clear(); |
| 165 nodes.addAll(copy); | 147 nodes.addAll(copy); |
| 166 } | 148 } |
| 167 | 149 |
| 168 NodeList get nodes() { | 150 NodeList get nodes() { |
| 169 if (_nodes === null) { | 151 if (_nodes === null) { |
| 170 _nodes = new _ChildrenNodeList._wrap(_ptr); | 152 _nodes = new _ChildrenNodeList._wrap(_ptr); |
| 171 } | 153 } |
| 172 return _nodes; | 154 return _nodes; |
| 173 } | 155 } |
| 174 | 156 |
| 175 Node get nextNode() => LevelDom.wrapNode(_ptr.nextSibling); | 157 Node get nextNode() => LevelDom.wrapNode(_ptr.nextSibling); |
| 176 | 158 |
| 177 Document get document() => LevelDom.wrapDocument(_ptr.ownerDocument); | 159 Document get document() => LevelDom.wrapDocument(_ptr.ownerDocument); |
| 178 | 160 |
| 179 Node get parent() => LevelDom.wrapNode(_ptr.parentNode); | 161 Node get parent() => LevelDom.wrapNode(_ptr.parentNode); |
| 180 | 162 |
| 181 Node get previousNode() => LevelDom.wrapNode(_ptr.previousSibling); | 163 Node get previousNode() => LevelDom.wrapNode(_ptr.previousSibling); |
| 182 | 164 |
| 183 String get text() => _ptr.textContent; | 165 String get text() => _ptr.textContent; |
| 184 | 166 |
| 185 void set text(String value) { | 167 void set text(String value) { _ptr.textContent = value; } |
| 186 assert(!_inMeasurementFrame || !_inDocument); | |
| 187 _ptr.textContent = value; | |
| 188 } | |
| 189 | 168 |
| 190 // New methods implemented. | 169 // New methods implemented. |
| 191 Node replaceWith(Node otherNode) { | 170 Node replaceWith(Node otherNode) { |
| 192 assert(!_inMeasurementFrame || !_inDocument); | |
| 193 try { | 171 try { |
| 194 _ptr.parentNode.replaceChild(LevelDom.unwrap(otherNode), _ptr); | 172 _ptr.parentNode.replaceChild(LevelDom.unwrap(otherNode), _ptr); |
| 195 } catch(var e) { | 173 } catch(var e) { |
| 196 // TODO(jacobr): what should we return on failure? | 174 // TODO(jacobr): what should we return on failure? |
| 197 } | 175 } |
| 198 return this; | 176 return this; |
| 199 } | 177 } |
| 200 | 178 |
| 201 Node remove() { | 179 Node remove() { |
| 202 assert(!_inMeasurementFrame || !_inDocument); | |
| 203 // TODO(jacobr): should we throw an exception if parent is already null? | 180 // TODO(jacobr): should we throw an exception if parent is already null? |
| 204 if (_ptr.parentNode !== null) { | 181 if (_ptr.parentNode !== null) { |
| 205 _ptr.parentNode.removeChild(_ptr); | 182 _ptr.parentNode.removeChild(_ptr); |
| 206 } | 183 } |
| 207 return this; | 184 return this; |
| 208 } | 185 } |
| 209 | 186 |
| 210 /** @domName contains */ | 187 /** @domName contains */ |
| 211 bool contains(Node otherNode) { | 188 bool contains(Node otherNode) { |
| 212 // TODO: Feature detect and use built in. | 189 // TODO: Feature detect and use built in. |
| 213 while (otherNode != null && otherNode != this) { | 190 while (otherNode != null && otherNode != this) { |
| 214 otherNode = otherNode.parent; | 191 otherNode = otherNode.parent; |
| 215 } | 192 } |
| 216 return otherNode == this; | 193 return otherNode == this; |
| 217 } | 194 } |
| 218 | 195 |
| 219 // TODO(jacobr): remove when/if List supports a method similar to | 196 // TODO(jacobr): remove when/if List supports a method similar to |
| 220 // insertBefore or we switch NodeList to implement LinkedList rather than | 197 // insertBefore or we switch NodeList to implement LinkedList rather than |
| 221 // array. | 198 // array. |
| 222 Node insertBefore(Node newChild, Node refChild) { | 199 Node insertBefore(Node newChild, Node refChild) { |
| 223 assert(!_inMeasurementFrame || !_inDocument); | |
| 224 return LevelDom.wrapNode(_ptr.insertBefore( | 200 return LevelDom.wrapNode(_ptr.insertBefore( |
| 225 LevelDom.unwrap(newChild), LevelDom.unwrap(refChild))); | 201 LevelDom.unwrap(newChild), LevelDom.unwrap(refChild))); |
| 226 } | 202 } |
| 227 | 203 |
| 228 Node clone(bool deep) { | 204 Node clone(bool deep) { |
| 229 return LevelDom.wrapNode(_ptr.cloneNode(deep)); | 205 return LevelDom.wrapNode(_ptr.cloneNode(deep)); |
| 230 } | 206 } |
| 231 | |
| 232 bool get _inDocument() => document.contains(this); | |
| 233 } | 207 } |
| OLD | NEW |