blob: 85671be8f333ec1dfaded12069f440148a379149 [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;
}
Node removeAt(int index) {
var result = this[index];
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);
dynamic reduce(dynamic initialValue,
dynamic combine(dynamic previousValue, Node element)) {
return Collections.reduce(this, initialValue, combine);
}
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([int compare(Node a, Node b)]) {
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;
void set length(int value) {
throw new UnsupportedError(
"Cannot set length on immutable List.");
}
Node operator[](int index) => _this.$dom_childNodes[index];
}
/// @domName $DOMNAME
class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC {
List<Node> 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.parentNode != null) {
final Node parent = this.parentNode;
parentNode.$dom_removeChild(this);
}
}
/**
* Replaces this node with another node.
* @domName Node.replaceChild
*/
Node replaceWith(Node otherNode) {
try {
final Node parent = this.parentNode;
parent.$dom_replaceChild(otherNode, this);
} catch (e) {
};
return this;
}
$!MEMBERS
}