visitElementAfter method

  1. @override
void visitElementAfter(
  1. Element element
)

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

Will not be called if visitElementBefore returns false.

Implementation

@override
void visitElementAfter(Element element) {
  _elementStack.removeLast();

  switch (element.tag) {
    case 'h1':
    case 'h2':
    case 'h3':
    case 'h4':
    case 'h5':
    case 'h6':
      _endHeading();
      _buffer.write('\n');
      _lastWasBlock = true;
      break;

    case 'p':
      _endTextStyle();
      // Apply text wrapping if width is set
      if (_inParagraph && options.width != null) {
        final content = _paragraphBuffer.toString();
        _paragraphBuffer.clear();
        _inParagraph = false;

        if (content.isNotEmpty) {
          // Calculate effective width (account for blockquote prefix)
          var effectiveWidth = options.width!;
          if (_inBlockquote) {
            // Each blockquote level takes 2 chars: "│ "
            effectiveWidth -= _blockquoteDepth * 2;
          }

          if (effectiveWidth > 0) {
            final wrapped = _wrapText(content, effectiveWidth);
            _buffer.write(wrapped);
          } else {
            _buffer.write(content);
          }
        }
      }
      _buffer.write('\n');
      _lastWasBlock = true;
      break;

    case 'blockquote':
      _blockquoteDepth--;
      if (_blockquoteDepth == 0) {
        _inBlockquote = false;
      }
      _lastWasBlock = true;
      break;

    case 'pre':
      _endCodeBlock();
      _lastWasBlock = true;
      break;

    case 'ul':
      _listDepth--;
      if (_listDepth == 0) {
        _lastWasBlock = true;
      }
      break;

    case 'ol':
      _listDepth--;
      _listCounters.removeLast();
      if (_listDepth == 0) {
        _lastWasBlock = true;
      }
      break;

    case 'li':
      _endTextStyle();
      // Only add newline if the li didn't end with a nested list
      // (nested lists already handle their own newlines)
      if (!_hasNestedList(element)) {
        _buffer.write('\n');
      }
      break;

    case 'em':
      _endInlineStyle();
      break;

    case 'strong':
      _endInlineStyle();
      break;

    case 'code':
      if (!_isInsidePreBlock()) {
        _endInlineStyle();
      }
      break;

    case 'a':
      _endLink();
      _pendingLinkUrl = null;
      break;

    case 'del':
      _endInlineStyle();
      break;

    case 'table':
      _renderTable();
      _lastWasBlock = true;
      break;

    case 'thead':
      _inTableHeader = false;
      break;

    case 'tbody':
      // Nothing to do
      break;

    case 'tr':
      // Add completed row to headers or rows
      if (_inTableHeader) {
        _tableHeaders.addAll(_currentTableRow);
      } else {
        _tableRows.add(List<String>.from(_currentTableRow));
      }
      _currentTableRow.clear();
      break;

    case 'th':
    case 'td':
      // Save cell content to current row
      _currentTableRow.add(_currentCellBuffer.toString().trim());
      _currentCellBuffer.clear();
      _inTableCell = false;
      break;
  }
}