Files
sdk/lib/dom/templates/html/impl/impl_NodeList.darttemplate
T
floitsch@google.com 9d3ea53f2b Fix some warnings.
Partially fixes Issue 2142

Review URL: https://chromiumcodereview.appspot.com//9963029

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@6119 260f80e4-7a28-3924-810f-c04153c831b5
2012-04-03 09:42:27 +00:00

174 lines
4.9 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.
// TODO(nweiz): when all implementations we target have the same name for the
// coreimpl implementation of List<E>, extend that rather than wrapping.
class _ListWrapper<E> implements List<E> {
List _list;
_ListWrapper(List this._list);
Iterator<E> iterator() => _list.iterator();
void forEach(void f(E element)) => _list.forEach(f);
Collection map(f(E element)) => _list.map(f);
List<E> filter(bool f(E element)) => _list.filter(f);
bool every(bool f(E element)) => _list.every(f);
bool some(bool f(E element)) => _list.some(f);
bool isEmpty() => _list.isEmpty();
int get length() => _list.length;
E operator [](int index) => _list[index];
void operator []=(int index, E value) { _list[index] = value; }
void set length(int newLength) { _list.length = newLength; }
void add(E value) => _list.add(value);
void addLast(E value) => _list.addLast(value);
void addAll(Collection<E> collection) => _list.addAll(collection);
void sort(int compare(E a, E b)) => _list.sort(compare);
int indexOf(E element, [int start = 0]) => _list.indexOf(element, start);
int lastIndexOf(E element, [int start = 0]) =>
_list.lastIndexOf(element, start);
void clear() => _list.clear();
E removeLast() => _list.removeLast();
E last() => _list.last();
List<E> getRange(int start, int rangeLength) =>
_list.getRange(start, rangeLength);
void setRange(int start, int rangeLength, List<E> from, [int startFrom = 0])
=> _list.setRange(start, rangeLength, from, startFrom);
void removeRange(int start, int rangeLength) =>
_list.removeRange(start, rangeLength);
void insertRange(int start, int rangeLength, [E initialValue = null]) =>
_list.insertRange(start, rangeLength, initialValue);
E get first() => _list[0];
}
/**
* This class is used to insure the results of list operations are NodeLists
* instead of lists.
*/
class _NodeListWrapper extends _ListWrapper<Node> implements NodeList {
_NodeListWrapper(List list) : super(list);
NodeList filter(bool f(Node element)) =>
new _NodeListWrapper(_list.filter(f));
NodeList getRange(int start, int rangeLength) =>
new _NodeListWrapper(_list.getRange(start, rangeLength));
}
class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC {
_NodeImpl _parent;
// -- start List<Node> mixins.
// Node is the element type.
// From Iterable<Node>:
Iterator<Node> iterator() {
// Note: NodeLists are not fixed size. And most probably length shouldn't
// be cached in both iterator _and_ forEach method. For now caching it
// for consistency.
return new _FixedSizeListIterator<Node>(this);
}
// From Collection<Node>:
void add(_NodeImpl value) {
_parent.$dom_appendChild(value);
}
void addLast(_NodeImpl value) {
_parent.$dom_appendChild(value);
}
void addAll(Collection<_NodeImpl> collection) {
for (_NodeImpl node in collection) {
_parent.$dom_appendChild(node);
}
}
_NodeImpl removeLast() {
final result = this.last();
if (result != null) {
_parent.$dom_removeChild(result);
}
return result;
}
void clear() {
_parent.text = '';
}
void operator []=(int index, _NodeImpl value) {
_parent.$dom_replaceChild(value, this[index]);
}
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>:
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);
Node last() => this[length - 1];
Node get first() => this[0];
// FIXME: implement thesee.
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.
$!MEMBERS
}