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!);
  final columnCount = alignments.length;
  final headRow = parseRow(parser, alignments, 'th');
  if (headRow.children!.length != columnCount) {
    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');
    while (row.children!.length < columnCount) {
      // Insert synthetic empty cells.
      row.children!.add(Element.empty('td'));
    }
    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]);
  }
}