parse method

  1. @override
Node? parse(
  1. BlockParser parser
)
override

Parses a table into its three parts:

  • a head row of head cells (<th> cells)
  • a divider of hyphens and pipes (not rendered)
  • many body rows of body cells (<td> cells)

Implementation

@override
Node? parse(BlockParser parser) {
  final alignments = _parseAlignments(parser.next!.content);
  final columnCount = alignments.length;
  final headRow = _parseRow(parser, alignments, 'th');
  if (headRow.children!.length != columnCount) {
    parser.retreat();
    return null;
  }
  final head = Element('thead', [headRow]);

  // Advance past the divider of hyphens.
  parser.advance();

  final rows = <Element>[];
  while (!parser.isDone && !BlockSyntax.isAtBlockEnd(parser)) {
    final row = _parseRow(parser, alignments, 'td');
    final children = row.children;
    if (children != null) {
      while (children.length < columnCount) {
        // Insert synthetic empty cells.
        children.add(Element('td', []));
      }
      while (children.length > columnCount) {
        children.removeLast();
      }
    }
    while (row.children!.length > columnCount) {
      row.children!.removeLast();
    }
    rows.add(row);
  }
  if (rows.isEmpty) {
    return Element('table', [head]);
  } else {
    final body = Element('tbody', rows);

    return Element('table', [head, body]);
  }
}