createHTML function

HTMLElement createHTML({
  1. String? html,
  2. @Deprecated("`NodeValidator` not implemented on package `web`") Object? validator,
  3. bool unsafe = false,
})

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

Implementation

HTMLElement createHTML(
    {String? html,
    @Deprecated("`NodeValidator` not implemented on package `web`")
    Object? validator,
    bool unsafe = false}) {
  if (html == null || html.isEmpty) return HTMLSpanElement();

  var dependentTagMatch = _regexpDependentTag.firstMatch(html);

  if (dependentTagMatch != null) {
    return _createDependentTagElement(dependentTagMatch, html, unsafe);
  } else {
    var div = createDiv(inline: true, html: html, unsafe: unsafe);

    var childNodes = div.childNodes;
    if (childNodes.isEmpty) return div;

    var childNode = childNodes.whereHTMLElement().firstOrNull;

    var htmlElement = childNode.asHTMLElementChecked;
    if (htmlElement != null) {
      return htmlElement;
    }

    var span = HTMLSpanElement();
    span.appendNodes(div.childNodes.toList());
    return span;
  }
}