Element.html constructor

Element.html(
  1. String? html, {
  2. NodeValidator? validator,
  3. NodeTreeSanitizer? treeSanitizer,
})

Creates an HTML element from a valid fragment of HTML.

var element = new Element.html('<div class="foo">content</div>');

The HTML fragment should contain only one single root element, any leading or trailing text nodes will be removed.

The HTML fragment is parsed as if it occurred within the context of a <body> tag, this means that special elements such as <caption> which must be parsed within the scope of a <table> element will be dropped. Use createFragment to parse contextual HTML fragments.

Unless a validator is provided this will perform the default validation and remove all scriptable elements and attributes.

See also:

Implementation

factory Element.html(String? html,
    {NodeValidator? validator, NodeTreeSanitizer? treeSanitizer}) {
  var fragment = document.body!.createFragment(html,
      validator: validator, treeSanitizer: treeSanitizer);

  return fragment.nodes.where((e) => e is Element).single as Element;
}