d31e5df37d
Review URL: https://chromiumcodereview.appspot.com//10825393 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@10853 260f80e4-7a28-3924-810f-c04153c831b5
147 lines
4.2 KiB
Plaintext
147 lines
4.2 KiB
Plaintext
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
/**
|
|
* Lazy implementation of the child nodes of an element that does not request
|
|
* the actual child nodes of an element until strictly necessary greatly
|
|
* improving performance for the typical cases where it is not required.
|
|
*/
|
|
class _ChildNodeListLazy implements NodeList {
|
|
final _NodeImpl _this;
|
|
|
|
_ChildNodeListLazy(this._this);
|
|
|
|
|
|
$if DART2JS
|
|
_NodeImpl get first() => JS('_NodeImpl', '#.firstChild', _this);
|
|
_NodeImpl last() => JS('_NodeImpl', '#.lastChild', _this);
|
|
$else
|
|
_NodeImpl get first() => _this.$dom_firstChild;
|
|
_NodeImpl last() => _this.$dom_lastChild;
|
|
$endif
|
|
|
|
void add(_NodeImpl value) {
|
|
_this.$dom_appendChild(value);
|
|
}
|
|
|
|
void addLast(_NodeImpl value) {
|
|
_this.$dom_appendChild(value);
|
|
}
|
|
|
|
|
|
void addAll(Collection<_NodeImpl> collection) {
|
|
for (_NodeImpl node in collection) {
|
|
_this.$dom_appendChild(node);
|
|
}
|
|
}
|
|
|
|
_NodeImpl removeLast() {
|
|
final result = last();
|
|
if (result != null) {
|
|
_this.$dom_removeChild(result);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void clear() {
|
|
_this.text = '';
|
|
}
|
|
|
|
void operator []=(int index, _NodeImpl value) {
|
|
_this.$dom_replaceChild(value, this[index]);
|
|
}
|
|
|
|
Iterator<Node> iterator() => _this.$dom_childNodes.iterator();
|
|
|
|
// TODO(jacobr): We can implement these methods much more efficiently by
|
|
// looking up the nodeList only once instead of once per iteration.
|
|
void forEach(void f(Node element)) => _Collections.forEach(this, f);
|
|
|
|
Collection map(f(Node element)) => _Collections.map(this, [], f);
|
|
|
|
Collection<Node> filter(bool f(Node element)) =>
|
|
new _NodeListWrapper(_Collections.filter(this, <Node>[], f));
|
|
|
|
bool every(bool f(Node element)) => _Collections.every(this, f);
|
|
|
|
bool some(bool f(Node element)) => _Collections.some(this, f);
|
|
|
|
bool isEmpty() => this.length == 0;
|
|
|
|
// From List<Node>:
|
|
|
|
// TODO(jacobr): this could be implemented for child node lists.
|
|
// The exception we throw here is misleading.
|
|
void sort(int compare(Node a, Node b)) {
|
|
throw new UnsupportedOperationException("Cannot sort immutable List.");
|
|
}
|
|
|
|
int indexOf(Node element, [int start = 0]) =>
|
|
_Lists.indexOf(this, element, start, this.length);
|
|
|
|
int lastIndexOf(Node element, [int start = 0]) =>
|
|
_Lists.lastIndexOf(this, element, start);
|
|
|
|
// FIXME: implement these.
|
|
void setRange(int start, int rangeLength, List<Node> from, [int startFrom]) {
|
|
throw new UnsupportedOperationException(
|
|
"Cannot setRange on immutable List.");
|
|
}
|
|
void removeRange(int start, int rangeLength) {
|
|
throw new UnsupportedOperationException(
|
|
"Cannot removeRange on immutable List.");
|
|
}
|
|
void insertRange(int start, int rangeLength, [Node initialValue]) {
|
|
throw new UnsupportedOperationException(
|
|
"Cannot insertRange on immutable List.");
|
|
}
|
|
NodeList getRange(int start, int rangeLength) =>
|
|
new _NodeListWrapper(_Lists.getRange(this, start, rangeLength, <Node>[]));
|
|
|
|
// -- end List<Node> mixins.
|
|
|
|
// TODO(jacobr): benchmark whether this is more efficient or whether caching
|
|
// a local copy of $dom_childNodes is more efficient.
|
|
int get length() => _this.$dom_childNodes.length;
|
|
|
|
_NodeImpl operator[](int index) => _this.$dom_childNodes[index];
|
|
}
|
|
|
|
class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC {
|
|
_ChildNodeListLazy get nodes() {
|
|
return new _ChildNodeListLazy(this);
|
|
}
|
|
|
|
void set nodes(Collection<Node> value) {
|
|
// Copy list first since we don't want liveness during iteration.
|
|
// TODO(jacobr): there is a better way to do this.
|
|
List copy = new List.from(value);
|
|
text = '';
|
|
for (Node node in copy) {
|
|
$dom_appendChild(node);
|
|
}
|
|
}
|
|
|
|
// TODO(jacobr): should we throw an exception if parent is already null?
|
|
_NodeImpl remove() {
|
|
if (this.parent != null) {
|
|
final _NodeImpl parent = this.parent;
|
|
parent.$dom_removeChild(this);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
_NodeImpl replaceWith(Node otherNode) {
|
|
try {
|
|
final _NodeImpl parent = this.parent;
|
|
parent.$dom_replaceChild(otherNode, this);
|
|
} catch(var e) {
|
|
|
|
};
|
|
return this;
|
|
}
|
|
|
|
$!MEMBERS
|
|
}
|