blob: e544887e2d3e846d07caeaa8feec0087be756b1a [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 $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 extends ListBase<Node> {
final Node _this;
_ChildNodeListLazy(this._this);
$if DART2JS
Node get first {
Node result = JS('Node|Null', '#.firstChild', _this);
if (result == null) throw new StateError("No elements");
return result;
}
Node get last {
Node result = JS('Node|Null', '#.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|Null', '#.firstChild', _this);
}
$else
Node get first {
Node result = _this.firstChild;
if (result == null) throw new StateError("No elements");
return result;
}
Node get last {
Node result = _this.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.firstChild;
}
$endif
void add(Node value) {
_this.append(value);
}
void addAll(Iterable<Node> iterable) {
if (iterable is _ChildNodeListLazy) {
_ChildNodeListLazy otherList = iterable;
if (!identical(otherList._this, _this)) {
// Optimized route for copying between nodes.
for (var i = 0, len = otherList.length; i < len; ++i) {
_this.append(otherList._this.firstChild);
}
}
return;
}
for (Node node in iterable) {
_this.append(node);
}
}
void insert(int index, Node node) {
if (index < 0 || index > length) {
throw new RangeError.range(index, 0, length);
}
if (index == length) {
_this.append(node);
} else {
_this.insertBefore(node, this[index]);
}
}
void insertAll(int index, Iterable<Node> iterable) {
var item = this[index];
_this.insertAllBefore(iterable, item);
}
void setAll(int index, Iterable<Node> iterable) {
throw new UnsupportedError("Cannot setAll on Node list");
}
Node removeLast() {
final result = last;
if (result != null) {
_this._removeChild(result);
}
return result;
}
Node removeAt(int index) {
var result = this[index];
if (result != null) {
_this._removeChild(result);
}
return result;
}
bool remove(Object object) {
if (object is! Node) return false;
Node node = object;
if (!identical(_this, node.parentNode)) return false;
_this._removeChild(node);
return true;
}
void _filter(bool test(Node node), bool removeMatching) {
// This implementation of removeWhere/retainWhere is more efficient
// than the default in ListBase. Child nodes can be removed in constant
// time.
Node child = _this.firstChild;
while (child != null) {
Node nextChild = child.nextNode;
if (test(child) == removeMatching) {
_this._removeChild(child);
}
child = nextChild;
}
}
void removeWhere(bool test(Node node)) {
_filter(test, true);
}
void retainWhere(bool test(Node node)) {
_filter(test, false);
}
void clear() {
_this.text = '';
}
void operator []=(int index, Node value) {
_this._replaceChild(value, this[index]);
}
Iterator<Node> get iterator => _this._childNodes.iterator;
// 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]) {
throw new UnsupportedError("Cannot sort Node list");
}
// FIXME: implement these.
void setRange(int start, int end, Iterable<Node> iterable,
[int skipCount = 0]) {
throw new UnsupportedError("Cannot setRange on Node list");
}
void fillRange(int start, int end, [Node fill]) {
throw new UnsupportedError("Cannot fillRange on Node list");
}
// -- end List<Node> mixins.
// TODO(jacobr): benchmark whether this is more efficient or whether caching
// a local copy of _childNodes is more efficient.
int get length => _this._childNodes.length;
void set length(int value) {
throw new UnsupportedError(
"Cannot set length on immutable List.");
}
Node operator[](int index) => _this._childNodes[index];
}
/** Information about the instantiated template. */
class TemplateInstance {
// TODO(rafaelw): firstNode & lastNode should be read-synchronous
// in cases where script has modified the template instance boundary.
/** The first node of this template instantiation. */
final Node firstNode;
/**
* The last node of this template instantiation.
* This could be identical to [firstNode] if the template only expanded to a
* single node.
*/
final Node lastNode;
/** The model used to instantiate the template. */
final model;
TemplateInstance(this.firstNode, this.lastNode, this.model);
}
$(ANNOTATIONS)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC {
List<Node> get nodes {
return new _ChildNodeListLazy(this);
}
void set nodes(Iterable<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) {
append(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._removeChild(this);
}
}
/**
* Replaces this node with another node.
*/
@DomName('Node.replaceChild')
Node replaceWith(Node otherNode) {
try {
final Node parent = this.parentNode;
parent._replaceChild(otherNode, this);
} catch (e) {
};
return this;
}
/**
* Inserts all of the nodes into this node directly before refChild.
*
* See also:
*
* * [insertBefore]
*/
Node insertAllBefore(Iterable<Node> newNodes, Node refChild) {
if (newNodes is _ChildNodeListLazy) {
_ChildNodeListLazy otherList = newNodes;
if (identical(otherList._this, this)) {
throw new ArgumentError(newNodes);
}
// Optimized route for copying between nodes.
for (var i = 0, len = otherList.length; i < len; ++i) {
this.insertBefore(otherList._this.firstChild, refChild);
}
} else {
for (var node in newNodes) {
this.insertBefore(node, refChild);
}
}
}
/**
* Print out a String representation of this Node.
*/
String toString() => nodeValue == null ? super.toString() : nodeValue;
/**
* Creates a binding to the attribute [name] to the [path] of the [model].
*
* This can be overridden by custom elements to provide the binding used in
* [Node.bind]. This will only create the binding; it will not add it to
* [bindings].
*
* You should not need to call this directly except from [Node.bind].
*/
@Experimental()
createBinding(String name, model, String path) =>
TemplateElement.mdvPackage(this).createBinding(name, model, path);
/**
* Binds the attribute [name] to the [path] of the [model].
* Path is a String of accessors such as `foo.bar.baz`.
* Returns the `NodeBinding` instance.
*/
@Experimental()
bind(String name, model, String path) =>
TemplateElement.mdvPackage(this).bind(name, model, path);
/** Unbinds the attribute [name]. */
@Experimental()
void unbind(String name) {
TemplateElement.mdvPackage(this).unbind(name);
}
/** Unbinds all bound attributes. */
@Experimental()
void unbindAll() {
TemplateElement.mdvPackage(this).unbindAll();
}
/** Gets the data bindings that are associated with this node. */
@Experimental()
Map<String, dynamic> get bindings =>
TemplateElement.mdvPackage(this).bindings;
/** Gets the template instance that instantiated this node, if any. */
@Experimental()
TemplateInstance get templateInstance =>
TemplateElement.mdvPackage(this).templateInstance;
$!MEMBERS
}