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