visitElementBefore method

  1. @override
bool visitElementBefore(
  1. Element element
)
override

Called when an Element has been reached, before its children have been visited.

Returns false to skip its children.

Implementation

@override
bool visitElementBefore(Element element) {
  // Hackish. Separate block-level elements with newlines.
  if (buffer.isNotEmpty && _blockTags.contains(element.tag)) {
    buffer.writeln();
  }

  buffer.write('<${element.tag}');

  for (final entry in element.attributes.entries) {
    buffer.write(' ${entry.key}="${entry.value}"');
  }

  final generatedId = element.generatedId;

  // attach header anchor ids generated from text
  if (generatedId != null) {
    buffer.write(' id="${uniquifyId(generatedId)}"');
  }

  _lastVisitedTag = element.tag;

  if (element.isEmpty) {
    // Empty element like <hr/>.
    buffer.write(' />');

    if (element.tag == 'br') {
      buffer.write('\n');
    }

    return false;
  } else {
    _elementStack.add(element);
    buffer.write('>');
    return true;
  }
}