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(md.Element element) {
  final String tag = element.tag;
  if (_currentBlockTag == null) _currentBlockTag = tag;

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

  var 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 length = _tables.single.rows.length;
      BoxDecoration decoration = styleSheet.tableCellsDecoration;
      if (length == 0 || length % 2 == 1) decoration = null;
      _tables.single.rows.add(TableRow(
        decoration: decoration,
        children: <Widget>[],
      ));
    }
    var bElement = _BlockElement(tag);
    if (start != null) bElement.nextListIndex = start;
    _blocks.add(bElement);
  } else {
    if (tag == 'a') {
      String text = extractTextFromElement(element);
      // Don't add empty links
      if (text == null) {
        return false;
      }
      String destination = element.attributes['href'];
      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(''));
    }

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

  return true;
}