visitElementBefore method

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

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

Returns false to skip its children.

Implementation

@override
bool visitElementBefore(md.Element element) {
  final String tag = element.tag;
  _currentBlockTag ??= tag;
  _lastVisitedTag = tag;

  if (builders.containsKey(tag)) {
    builders[tag]!.visitElementBefore(element);
  }

  if (paddingBuilders.containsKey(tag)) {
    paddingBuilders[tag]!.visitElementBefore(element);
  }

  int? start;
  if (_isBlockTag(tag)) {
    _addAnonymousBlockIfNeeded();
    if (_isListTag(tag)) {
      _listIndents.add(tag);
      if (element.attributes['start'] != null) {
        start = int.parse(element.attributes['start']!) - 1;
      }
    } else if (tag == 'blockquote') {
      _isInBlockquote = true;
    } else if (tag == 'table') {
      _tables.add(_TableElement());
    } else if (tag == 'tr') {
      final int length = _tables.single.rows.length;
      BoxDecoration? decoration =
          styleSheet.tableCellsDecoration as BoxDecoration?;
      if (length == 0 || length.isOdd) {
        decoration = null;
      }
      _tables.single.rows.add(TableRow(
        decoration: decoration,
        // TODO(stuartmorgan): This should be fixed, not suppressed; enabling
        // this lint warning exposed that the builder is modifying the
        // children of TableRows, even though they are @immutable.
        // ignore: prefer_const_literals_to_create_immutables
        children: <Widget>[],
      ));
    }
    final _BlockElement bElement = _BlockElement(tag);
    if (start != null) {
      bElement.nextListIndex = start;
    }
    _blocks.add(bElement);
  } else {
    if (tag == 'a') {
      final String? text = extractTextFromElement(element);
      // Don't add empty links
      if (text == null) {
        return false;
      }
      final String? destination = element.attributes['href'];
      final String title = element.attributes['title'] ?? '';

      _linkHandlers.add(
        delegate.createLink(text, destination, title),
      );
    }

    _addParentInlineIfNeeded(_blocks.last.tag);

    // The Markdown parser passes empty table data tags for blank
    // table cells. Insert a text node with an empty string in this
    // case for the table cell to get properly created.
    if (element.tag == 'td' &&
        element.children != null &&
        element.children!.isEmpty) {
      element.children!.add(md.Text(''));
    }

    final TextStyle parentStyle = _inlines.last.style!;
    _inlines.add(_InlineElement(
      tag,
      style: parentStyle.merge(styleSheet.styles[tag]),
    ));
  }

  return true;
}