insertBefore method
void
insertBefore(
- Node node,
- Node? before
)
inherited
Implementation
@override
void insertBefore(Node node, Node? before) {
// Can't add document
if (node is Document) {
throw ArgumentError.value(node, 'node');
}
// Can't add node into itself
if (identical(node, this)) {
throw ArgumentError.value(node, 'node');
}
// If 'before' is non-null, it must be a child
if (before != null && !identical(before._parent, this)) {
throw ArgumentError.value(before, 'before');
}
// Mark as dirty
_markDirty();
// Remove from old parent
if (node.parentNode != null) {
node.remove();
}
// Validate state
if (node.parentNode != null ||
node.previousNode != null && node.nextNode != null) {
throw StateError('Node is not detached.');
}
node._parent = this;
if (before == null) {
final oldLastChild = _lastChild;
if (oldLastChild == null) {
_firstChild = node;
} else {
oldLastChild._nextNode = node;
}
node._previousNode = oldLastChild;
_lastChild = node;
} else {
final previous = before._previousNode;
if (previous == null) {
// Set first sibling
_firstChild = node;
} else {
previous._nextNode = node;
node._previousNode = previous;
}
node._nextNode = before;
before._previousNode = node;
}
// Validate state
if (_firstChild == null || _lastChild == null || node.parentNode == null) {
throw StateError('Node is not attached.');
}
}