insertBefore method

  1. @override
void insertBefore(
  1. Node node,
  2. Node? before
)
override

Implementation

@override
void insertBefore(Node node, Node? before) {
  if (node is Element) {
    if (_firstElementChild != null) {
      throw DomException._failedToExecute(
        DomException.HIERARCHY_REQUEST,
        'Node',
        'appendChild',
        'Only one element on document allowed.',
      );
    }
  } else if (node is InternalDocumentType) {
    // OK
  } else if (node is Comment) {
    // OK
  } else if (node is Text) {
    // Check that the text is whitespace
    final value = node.text!.replaceAll('\n', '').trim();
    if (value.isNotEmpty) {
      throw DomException._mayNotBeInsertedInside(
        'Document',
        'insertBefore',
        node,
        this,
      );
    }
    // Ignore the text
    return;
  } else {
    throw DomException._mayNotBeInsertedInside(
      'Document',
      'insertBefore',
      node,
      this,
    );
  }

  // Insert
  super.insertBefore(node, before);
}