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