createHTML function

Element createHTML([
  1. String? html,
  2. NodeValidator? validator
])

Creates a HTML Element. Returns 1st node form parsed HTML.

Implementation

Element createHTML([String? html, NodeValidator? validator]) {
  if (html == null || html.isEmpty) return SpanElement();

  var dependentTagMatch = _regexpDependentTag.firstMatch(html);

  if (dependentTagMatch != null) {
    var dependentTagName = dependentTagMatch.group(1)!.toLowerCase();

    DivElement div;
    if (dependentTagName == 'td' || dependentTagName == 'th') {
      div = createDiv(true, '<table><tbody><tr>\n$html\n</tr></tbody></table>');
    } else if (dependentTagName == 'tr') {
      div = createDiv(true, '<table><tbody>\n$html\n</tbody></table>');
    } else if (dependentTagName == 'tbody' ||
        dependentTagName == 'thead' ||
        dependentTagName == 'tfoot') {
      div = createDiv(true, '<table>\n$html\n</table>');
    } else {
      throw StateError("Can't handle dependent tag: $dependentTagName");
    }

    var childNode = div.querySelector(dependentTagName);
    return childNode!;
  } else {
    var div = createDiv(true, html, validator);
    if (div.nodes.isEmpty) return div;

    var childNode = div.nodes.firstWhereOrNull((e) => e is Element);

    if (childNode is Element) {
      return childNode;
    }

    var span = SpanElement();
    span.nodes.addAll(div.nodes);
    return span;
  }
}