0672317bec
Committed: https://code.google.com/p/dart/source/detail?r=18575 Reverted: http://code.google.com/p/dart/source/detail?r=18576 Review URL: https://codereview.chromium.org//12212213 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@18579 260f80e4-7a28-3924-810f-c04153c831b5
289 lines
7.7 KiB
Plaintext
289 lines
7.7 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.
|
|
|
|
part of $LIBRARYNAME;
|
|
|
|
/**
|
|
* 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 {
|
|
Node result = JS('Node', '#.firstChild', _this);
|
|
if (result == null) throw new StateError("No elements");
|
|
return result;
|
|
}
|
|
Node get last {
|
|
Node result = JS('Node', '#.lastChild', _this);
|
|
if (result == null) throw new StateError("No elements");
|
|
return result;
|
|
}
|
|
Node get single {
|
|
int l = this.length;
|
|
if (l == 0) throw new StateError("No elements");
|
|
if (l > 1) throw new StateError("More than one element");
|
|
return JS('Node', '#.firstChild', _this);
|
|
}
|
|
$else
|
|
Node get first {
|
|
Node result = _this.$dom_firstChild;
|
|
if (result == null) throw new StateError("No elements");
|
|
return result;
|
|
}
|
|
Node get last {
|
|
Node result = _this.$dom_lastChild;
|
|
if (result == null) throw new StateError("No elements");
|
|
return result;
|
|
}
|
|
Node get single {
|
|
int l = this.length;
|
|
if (l == 0) throw new StateError("No elements");
|
|
if (l > 1) throw new StateError("More than one element");
|
|
return _this.$dom_firstChild;
|
|
}
|
|
$endif
|
|
|
|
Node min([int compare(Node a, Node b)]) {
|
|
return IterableMixinWorkaround.min(this, compare);
|
|
}
|
|
|
|
Node max([int compare(Node a, Node b)]) {
|
|
return IterableMixinWorkaround.max(this, compare);
|
|
}
|
|
|
|
void add(Node value) {
|
|
_this.$dom_appendChild(value);
|
|
}
|
|
|
|
void addLast(Node value) {
|
|
_this.$dom_appendChild(value);
|
|
}
|
|
|
|
|
|
void addAll(Iterable<Node> iterable) {
|
|
if (iterable is _ChildNodeListLazy) {
|
|
iterable = new List.from(iterable);
|
|
}
|
|
for (Node node in iterable) {
|
|
_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 remove(Object object) {
|
|
if (object is! Node) return;
|
|
Node node = object;
|
|
if (!identical(_this, node.parentNode)) return;
|
|
_this.$dom_removeChild(node);
|
|
}
|
|
|
|
void removeAll(Iterable elements) {
|
|
IterableMixinWorkaround.removeAll(this, elements);
|
|
}
|
|
|
|
void retainAll(Iterable elements) {
|
|
IterableMixinWorkaround.retainAll(this, elements);
|
|
}
|
|
|
|
void removeMatching(bool test(Node node)) {
|
|
IterableMixinWorkaround.removeMatching(this, test);
|
|
}
|
|
|
|
void retainMatching(bool test(Node node)) {
|
|
IterableMixinWorkaround.retainMatching(this, test);
|
|
}
|
|
|
|
void clear() {
|
|
_this.text = '';
|
|
}
|
|
|
|
void operator []=(int index, Node value) {
|
|
_this.$dom_replaceChild(value, this[index]);
|
|
}
|
|
|
|
Iterator<Node> get 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) => IterableMixinWorkaround.contains(this, element);
|
|
|
|
void forEach(void f(Node element)) => IterableMixinWorkaround.forEach(this, f);
|
|
|
|
dynamic reduce(dynamic initialValue,
|
|
dynamic combine(dynamic previousValue, Node element)) {
|
|
return IterableMixinWorkaround.reduce(this, initialValue, combine);
|
|
}
|
|
|
|
String join([String separator]) {
|
|
return IterableMixinWorkaround.joinList(this, separator);
|
|
}
|
|
|
|
Iterable map(f(Node element)) {
|
|
return IterableMixinWorkaround.mapList(this, f);
|
|
}
|
|
|
|
Iterable<Node> where(bool f(Node element)) {
|
|
return IterableMixinWorkaround.where(this, f);
|
|
}
|
|
|
|
Iterable expand(Iterable f(Node element)) {
|
|
return IterableMixinWorkaround.expand(this, f);
|
|
}
|
|
|
|
bool every(bool f(Node element)) => IterableMixinWorkaround.every(this, f);
|
|
|
|
bool any(bool f(Node element)) => IterableMixinWorkaround.any(this, f);
|
|
|
|
List<Node> toList() => new List<Node>.from(this);
|
|
Set<Node> toSet() => new Set<Node>.from(this);
|
|
|
|
bool get isEmpty => this.length == 0;
|
|
|
|
// From List<Node>:
|
|
|
|
Iterable<Node> take(int n) {
|
|
return IterableMixinWorkaround.takeList(this, n);
|
|
}
|
|
|
|
Iterable<Node> takeWhile(bool test(Node value)) {
|
|
return IterableMixinWorkaround.takeWhile(this, test);
|
|
}
|
|
|
|
Iterable<Node> skip(int n) {
|
|
return IterableMixinWorkaround.skipList(this, n);
|
|
}
|
|
|
|
Iterable<Node> skipWhile(bool test(Node value)) {
|
|
return IterableMixinWorkaround.skipWhile(this, test);
|
|
}
|
|
|
|
Node firstMatching(bool test(Node value), {Node orElse()}) {
|
|
return IterableMixinWorkaround.firstMatching(this, test, orElse);
|
|
}
|
|
|
|
Node lastMatching(bool test(Node value), {Node orElse()}) {
|
|
return IterableMixinWorkaround.lastMatchingInList(this, test, orElse);
|
|
}
|
|
|
|
Node singleMatching(bool test(Node value)) {
|
|
return IterableMixinWorkaround.singleMatching(this, test);
|
|
}
|
|
|
|
Node elementAt(int index) {
|
|
return this[index];
|
|
}
|
|
|
|
Iterable<Node> get reversed {
|
|
return IterableMixinWorkaround.reversedList(this);
|
|
}
|
|
|
|
// 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];
|
|
}
|
|
|
|
$(ANNOTATIONS)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
|
|
}
|