ElementList<T extends Element> abstract class
An immutable list containing HTML elements. This list contains some additional methods for ease of CSS manipulation on a group of elements.
abstract class ElementList<T extends Element> extends ListBase<T> {
/**
* The union of all CSS classes applied to the elements in this list.
*
* This set makes it easy to add, remove or toggle (add if not present, remove
* if present) the classes applied to a collection of elements.
*
* htmlList.classes.add('selected');
* htmlList.classes.toggle('isOnline');
* htmlList.classes.remove('selected');
*/
CssClassSet get classes;
/** Replace the classes with `value` for every element in this list. */
set classes(Iterable<String> value);
}
Extends
ListBase<T> > ElementList<T>
Properties
abstract CssClassSet get classes #
The union of all CSS classes applied to the elements in this list.
This set makes it easy to add, remove or toggle (add if not present, remove if present) the classes applied to a collection of elements.
htmlList.classes.add('selected');
htmlList.classes.toggle('isOnline');
htmlList.classes.remove('selected');
abstract dynamic set classes(Iterable<String> value) #
Replace the classes with value for every element in this list.
abstract int get length #
Returns the number of elements in the list.
The valid indices for a list are 0 through length - 1.
abstract void set length(int newLength) #
Changes the length of the list. If
newLength is greater than
the current length, entries are initialized to null.
Throws an UnsupportedError if the list is not extendable.
Operators
Methods
void addAll(Iterable<E> iterable) #
Appends all elements of the iterable to the end of this list.
Extends the length of the list by the number of elements in
iterable.
Throws an UnsupportedError if this list is not extensible.
void addAll(Iterable<E> iterable) {
for (E element in iterable) {
this[this.length++] = element;
}
}
bool any(bool test(E element)) #
Returns true if one element of this collection satisfies the predicate test. Returns false otherwise.
bool any(bool test(E element)) {
int length = this.length;
for (int i = 0; i < length; i++) {
if (test(this[i])) return true;
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
return false;
}
bool contains(E element) #
bool contains(E element) {
int length = this.length;
for (int i = 0; i < length; i++) {
if (this[i] == element) return true;
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
return false;
}
E elementAt(int index) #
Returns the indexth element.
If this has fewer than
index elements throws a RangeError.
Note: if this does not have a deterministic iteration order then the
function may simply return any element without any iteration if there are
at least
index elements in this.
E elementAt(int index) => this[index];
bool every(bool test(E element)) #
Returns true if every elements of this collection satisify the
predicate
test. Returns false otherwise.
bool every(bool test(E element)) {
int length = this.length;
for (int i = 0; i < length; i++) {
if (!test(this[i])) return false;
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
return true;
}
Iterable expand(Iterable f(E element)) #
Expand each element of this Iterable into zero or more elements.
The resulting Iterable will run through the elements returned by f for each element of this, in order.
The returned Iterable is lazy, and will call
f for each element
of this every time it's iterated.
Iterable expand(Iterable f(E element)) => new ExpandIterable<E, dynamic>(this, f);
void fillRange(int start, int end, [E fill]) #
Sets the elements in the range
start to
end exclusive to the given
fillValue.
It is an error if
start..
end is not a valid range pointing into the
this.
void fillRange(int start, int end, [E fill]) {
_rangeCheck(start, end);
for (int i = start; i < end; i++) {
this[i] = fill;
}
}
E firstWhere(bool test(E element), {E orElse()}) #
Returns the first element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null, a StateError is
thrown.
E firstWhere(bool test(E element), { E orElse() }) {
int length = this.length;
for (int i = 0; i < length; i++) {
E element = this[i];
if (test(element)) return element;
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
if (orElse != null) return orElse();
throw new StateError("No matching element");
}
dynamic fold(initialValue, combine(previousValue, E element)) #
Reduces a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function.
Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.
Example of calculating the sum of an iterable:
iterable.fold(0, (prev, element) => prev + element);
fold(var initialValue, combine(var previousValue, E element)) {
var value = initialValue;
int length = this.length;
for (int i = 0; i < length; i++) {
value = combine(value, this[i]);
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
return value;
}
Iterable<E> getRange(int start, int end) #
Returns an Iterable that iterators over the elements in the range
start to
end exclusive. The result of this function
is backed by this.
It is an error if end is before start.
It is an error if the
start and
end are not valid ranges at the time
of the call to this method. The returned Iterable behaves similar to
skip(start).take(end - start). That is, it will not throw exceptions
if this changes size.
Example:
var list = [1, 2, 3, 4, 5];
var range = list.getRange(1, 4);
print(range.join(', ')); // => 2, 3, 4
list.length = 3;
print(range.join(', ')); // => 2, 3
Iterable<E> getRange(int start, int end) {
_rangeCheck(start, end);
return new SubListIterable(this, start, end);
}
int indexOf(E element, [int startIndex = 0]) #
Returns the first index of element in the list.
Searches the list from index start to the length of the list.
The first time an element e is encountered so that e == element,
the index of e is returned.
Returns -1 if
element is not found.
int indexOf(E element, [int startIndex = 0]) {
if (startIndex >= this.length) {
return -1;
}
if (startIndex < 0) {
startIndex = 0;
}
for (int i = startIndex; i < this.length; i++) {
if (this[i] == element) {
return i;
}
}
return -1;
}
void insert(int index, E element) #
Inserts the element at position index in the list.
This increases the length of the list by one and shifts all elements at or after the index towards the end of the list.
It is an error if the index does not point inside the list or at the position after the last element.
void insert(int index, E element) {
if (index < 0 || index > length) {
throw new RangeError.range(index, 0, length);
}
if (index == this.length) {
add(element);
return;
}
// We are modifying the length just below the is-check. Without the check
// Array.copy could throw an exception, leaving the list in a bad state
// (with a length that has been increased, but without a new element).
if (index is! int) throw new ArgumentError(index);
this.length++;
setRange(index + 1, this.length, this, index);
this[index] = element;
}
void insertAll(int index, Iterable<E> iterable) #
Inserts all elements of iterable at position index in the list.
This increases the length of the list by the length of iterable and shifts all later elements towards the end of the list.
It is an error if the index does not point inside the list or at the position after the last element.
void insertAll(int index, Iterable<E> iterable) {
if (index < 0 || index > length) {
throw new RangeError.range(index, 0, length);
}
// TODO(floitsch): we can probably detect more cases.
if (iterable is! List && iterable is! Set && iterable is! SubListIterable) {
iterable = iterable.toList();
}
int insertionLength = iterable.length;
// There might be errors after the length change, in which case the list
// will end up being modified but the operation not complete. Unless we
// always go through a "toList" we can't really avoid that.
this.length += insertionLength;
setRange(index + insertionLength, this.length, this, index);
setAll(index, iterable);
}
String join([String separator = ""]) #
Converts each element to a String and concatenates the strings.
Converts each element to a String by calling Object.toString on it.
Then concatenates the strings, optionally separated by the
separator
string.
String join([String separator = ""]) {
int length = this.length;
if (!separator.isEmpty) {
if (length == 0) return "";
String first = "${this[0]}";
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
StringBuffer buffer = new StringBuffer(first);
for (int i = 1; i < length; i++) {
buffer.write(separator);
buffer.write(this[i]);
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
return buffer.toString();
} else {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
buffer.write(this[i]);
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
return buffer.toString();
}
}
int lastIndexOf(E element, [int startIndex]) #
Returns the last index in the list a of the given
element, starting
the search at index
startIndex to 0.
Returns -1 if
element is not found.
int lastIndexOf(E element, [int startIndex]) {
if (startIndex == null) {
startIndex = this.length - 1;
} else {
if (startIndex < 0) {
return -1;
}
if (startIndex >= this.length) {
startIndex = this.length - 1;
}
}
for (int i = startIndex; i >= 0; i--) {
if (this[i] == element) {
return i;
}
}
return -1;
}
E lastWhere(bool test(E element), {E orElse()}) #
Returns the last element that satisfies the given predicate test.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null, a StateError is
thrown.
E lastWhere(bool test(E element), { E orElse() }) {
int length = this.length;
for (int i = length - 1; i >= 0; i--) {
E element = this[i];
if (test(element)) return element;
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
if (orElse != null) return orElse();
throw new StateError("No matching element");
}
Iterable map(f(E element)) #
Returns a lazy Iterable where each element e of this is replaced
by the result of f(e).
This method returns a view of the mapped elements. As long as the
returned Iterable is not iterated over, the supplied function
f will
not be invoked. The transformed elements will not be cached. Iterating
multiple times over the the returned Iterable will invoke the supplied
function
f multiple times on the same element.
Iterable map(f(E element)) => new MappedListIterable(this, f);
E reduce(E combine(E previousValue, E element)) #
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
Example of calculating the sum of an iterable:
iterable.reduce((value, element) => value + element);
E reduce(E combine(E previousValue, E element)) {
if (length == 0) throw new StateError("No elements");
E value = this[0];
for (int i = 1; i < length; i++) {
value = combine(value, this[i]);
}
return value;
}
bool remove(Object element) #
Removes value from the list. Returns true if value was
in the list. Returns false otherwise. The method has no effect
if value value was not in the list.
bool remove(Object element) {
for (int i = 0; i < this.length; i++) {
if (this[i] == element) {
this.setRange(i, this.length - 1, this, i + 1);
this.length -= 1;
return true;
}
}
return false;
}
E removeAt(int index) #
Removes the element at position index from the list.
This reduces the length of this by one and moves all later elements
down by one position.
Returns the removed element.
Throws an ArgumentError if
index is not an int.
Throws an RangeError if the
index does not point inside
the list.
Throws an UnsupportedError, and doesn't remove the element,
if the length of this cannot be changed.
E removeAt(int index) {
E result = this[index];
setRange(index, this.length - 1, this, index + 1);
length--;
return result;
}
void removeRange(int start, int end) #
Removes the elements in the range start to end exclusive.
It is an error if
start..
end is not a valid range pointing into the
this.
void removeRange(int start, int end) {
_rangeCheck(start, end);
int length = end - start;
setRange(start, this.length - length, this, end);
this.length -= length;
}
void replaceRange(int start, int end, Iterable<E> newContents) #
Removes the elements in the range
start to
end exclusive and replaces
them with the contents of the iterable.
It is an error if
start..
end is not a valid range pointing into the
this.
Example:
var list = [1, 2, 3, 4, 5];
list.replaceRange(1, 3, [6, 7, 8, 9]);
print(list); // [1, 6, 7, 8, 9, 4, 5]
void replaceRange(int start, int end, Iterable<E> newContents) {
// TODO(floitsch): Optimize this.
removeRange(start, end);
insertAll(start, newContents);
}
void setAll(int index, Iterable<E> iterable) #
Overwrites elements of this with the elemenst of
iterable starting
at position
index in the list.
This operation does not increase the length of this.
It is an error if the index does not point inside the list or at the position after the last element.
It is an error if the
iterable is longer than length -
index.
void setAll(int index, Iterable<E> iterable) {
if (iterable is List) {
setRange(index, index + iterable.length, iterable);
} else {
for (E element in iterable) {
this[index++] = element;
}
}
}
void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) #
Copies the elements of
iterable, skipping the
skipCount first elements,
into the range
start to
end exclusive of this.
If start equals end and start.. end represents a legal range, this method has no effect.
It is an error if
start..
end is not a valid range pointing into the
this.
It is an error if the iterable does not have enough elements after skipping skipCount elements.
Example:
var list = [1, 2, 3, 4];
var list2 = [5, 6, 7, 8, 9];
list.setRange(1, 3, list2, 3);
print(list); // => [1, 8, 9, 4]
void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) {
_rangeCheck(start, end);
int length = end - start;
if (length == 0) return;
if (skipCount < 0) throw new ArgumentError(skipCount);
List otherList;
int otherStart;
// TODO(floitsch): Make this accept more.
if (iterable is List) {
otherList = iterable;
otherStart = skipCount;
} else {
otherList = iterable.skip(skipCount).toList(growable: false);
otherStart = 0;
}
if (otherStart + length > otherList.length) {
throw new StateError("Not enough elements");
}
if (otherStart < start) {
// Copy backwards to ensure correct copy if [from] is this.
for (int i = length - 1; i >= 0; i--) {
this[start + i] = otherList[otherStart + i];
}
} else {
for (int i = 0; i < length; i++) {
this[start + i] = otherList[otherStart + i];
}
}
}
E singleWhere(bool test(E element)) #
Returns the single element that satisfies
test. If no or more than one
element match then a StateError is thrown.
E singleWhere(bool test(E element)) {
int length = this.length;
E match = null;
bool matchFound = false;
for (int i = 0; i < length; i++) {
E element = this[i];
if (test(element)) {
if (matchFound) {
throw new StateError("More than one matching element");
}
matchFound = true;
match = element;
}
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
if (matchFound) return match;
throw new StateError("No matching element");
}
Iterable<E> skipWhile(bool test(E element)) #
Returns an Iterable that skips elements while
test is satisfied.
The filtering happens lazily. Every new Iterator of the returned
Iterable will iterate over all elements of this.
As long as the iterator's elements do not satisfy
test they are
discarded. Once an element satisfies the
test the iterator stops testing
and uses every element unconditionally. That is, the elements of the
returned Iterable are the elements of this starting from the first
element that doesn't satisfy
test.
Iterable<E> skipWhile(bool test(E element)) {
return new SkipWhileIterable<E>(this, test);
}
void sort([int compare(E a, E b)]) #
Sorts the list according to the order specified by the compare function.
The
compare function must act as a Comparator.
The default List implementations use Comparable.compare if
compare is omitted.
void sort([int compare(E a, E b)]) {
if (compare == null) {
var defaultCompare = Comparable.compare;
compare = defaultCompare;
}
Sort.sort(this, compare);
}
List<E> sublist(int start, [int end]) #
Returns a new list containing the elements from start to end.
If
end is omitted, the length of this is used.
It is an error if
start or
end are not indices into this,
or if
end is before
start.
List<E> sublist(int start, [int end]) {
if (end == null) end = length;
_rangeCheck(start, end);
int length = end - start;
List<E> result = new List<E>()..length = length;
for (int i = 0; i < length; i++) {
result[i] = this[start + i];
}
return result;
}
Iterable<E> takeWhile(bool test(E element)) #
Returns an Iterable that stops once
test is not satisfied anymore.
The filtering happens lazily. Every new Iterator of the returned
Iterable will start iterating over the elements of this.
When the iterator encounters an element e that does not satisfy
test,
it discards e and moves into the finished state. That is, it will not
ask or provide any more elements.
Iterable<E> takeWhile(bool test(E element)) {
return new TakeWhileIterable<E>(this, test);
}
List<E> toList({bool growable: true}) #
Creates a List containing the elements of this Iterable.
The elements will be in iteration order. The list is fixed-length if growable is false.
List<E> toList({ bool growable: true }) {
List<E> result;
if (growable) {
result = new List<E>()..length = length;
} else {
result = new List<E>(length);
}
for (int i = 0; i < length; i++) {
result[i] = this[i];
}
return result;
}
String toString() #
Returns a string representation of this object.
String toString() => ToString.iterableToString(this);
Iterable<E> where(bool test(E element)) #
Returns a lazy Iterable with all elements that satisfy the
predicate
test.
This method returns a view of the mapped elements. As long as the
returned Iterable is not iterated over, the supplied function
test will
not be invoked. Iterating will not cache results, and thus iterating
multiple times over the the returned Iterable will invoke the supplied
function
test multiple times on the same element.
Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test);