insertAfter method

  1. @override
void insertAfter(
  1. Bs4Element element, [
  2. Bs4Element? ref
])

Inserts an element immediately following the element in the parse tree.

Without ref argument it acts just like the append method.

ref specifies an position of an element, where should the insertion apply.

If the ref is not in the parse tree, throws RangeError.

Implementation

@override
void insertAfter(Bs4Element element, [Bs4Element? ref]) {
  if (ref == null) {
    _element.nodes.add(element._element);
  } else {
    final nodes = _element.nodes;
    final nl = nodes.length;
    final indexOf = nodes.indexOf(ref._element);
    if (indexOf < 0) {
      final msg = 'Referenced element does not exist in the parse tree.';
      throw (RangeError(msg));
    } else if ((nl - 1 == indexOf) ||
        (nl - 2 == indexOf && nodes[nl - 1].nodeType == 3)) {
      append(element);
    } else {
      _element.nodes.insert(indexOf + 1, element._element);
    }
  }
}