blob: cf46de298a9c03b81ab2dcc38389caf696cb6064 [file] [log] [blame]
// 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.
part of html;
/**
* 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 List {
final Node _this;
_ChildNodeListLazy(this._this);
$if DART2JS
Node get first => JS('Node', '#.firstChild', _this);
Node get last => JS('Node', '#.lastChild', _this);
$else
Node get first => _this.$dom_firstChild;
Node get last => _this.$dom_lastChild;
$endif
void add(Node value) {
_this.$dom_appendChild(value);
}
void addLast(Node value) {
_this.$dom_appendChild(value);
}
void addAll(Collection<Node> collection) {
for (Node node in collection) {
_this.$dom_appendChild(node);
}
}
Node removeLast() {
final result = last;
if (result != null) {
_this.$dom_removeChild(result);
}
return result;
}
void clear() {
_this.text = '';
}
void operator []=(int index, Node 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.
bool contains(Node element) => _Collections.contains(this, element);
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)) =>
_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 get 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([Comparator<Node> compare = Comparable.compare]) {
throw new UnsupportedError("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 UnsupportedError(
"Cannot setRange on immutable List.");
}
void removeRange(int start, int rangeLength) {
throw new UnsupportedError(
"Cannot removeRange on immutable List.");
}
void insertRange(int start, int rangeLength, [Node initialValue]) {
throw new UnsupportedError(
"Cannot insertRange on immutable List.");
}
List<Node> getRange(int start, int rangeLength) =>
_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;
Node 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);
}
}
/**
* Removes this node from the DOM.
* @domName Node.removeChild
*/
void remove() {
// TODO(jacobr): should we throw an exception if parent is already null?
// TODO(vsm): Use the native remove when available.
if (this.parent != null) {
final Node parent = this.parent;
parent.$dom_removeChild(this);
}
}
/**
* Replaces this node with another node.
* @domName Node.replaceChild
*/
Node replaceWith(Node otherNode) {
try {
final Node parent = this.parent;
parent.$dom_replaceChild(otherNode, this);
} catch (e) {
};
return this;
}
$!MEMBERS
}