createTableCells function

List<TABLENode> createTableCells(
  1. Object? rowCells, [
  2. bool header = false
])

Implementation

List<TABLENode> createTableCells(Object? rowCells, [bool header = false]) {
  if (rowCells is Iterable) {
    rowCells = rowCells.asList;
  }

  if (rowCells is List) {
    if (listMatchesAll(
      rowCells,
      (dynamic e) => (!header && e is TDElement) || (header && e is THElement),
    )) {
      return rowCells.whereType<TABLENode>().toList();
    } else if (listMatchesAll(
      rowCells,
      (dynamic e) => _domHTML.isHtmlNode(e),
    )) {
      var tdList = rowCells.where((e) {
        var tag = _domHTML.getNodeTag(e);
        return (tag == 'td' || tag == 'th');
      });
      var list = tdList
          .map((e) => DOMNode.from(e))
          .nonNulls
          .whereType<TABLENode>()
          .toList();
      return list;
    }
  }

  if (rowCells != null && rowCells is! Iterable) {
    rowCells = [rowCells];
  }

  final cells = rowCells as Iterable?;
  if (cells == null) return <TABLENode>[];

  // Existing cells are never nested into another cell: a `td` in a header
  // row is converted to a `th`, and a `th` in a body row is kept (a row
  // header):
  return cells.map<TABLENode>((e) {
    if (e is TDElement) return header ? e.asTHElement() : e;
    if (e is THElement) return e;
    return $tag(header ? 'th' : 'td', content: e) as TABLENode;
  }).toList();
}