createRawHtml<T extends Element> function

T createRawHtml<T extends Element>(
  1. String html, {
  2. bool textContent = false,
})

Parses html into a detached element via a <template>.

By default, the HTML is parsed — it is not sanitized; pass only trusted markup. Untrusted input is an XSS vector.

If textContent is true, returns a <span> whose textContent is html (treats html as plain text).

Implementation

T createRawHtml<T extends Element>(String html,
    {bool textContent = false}) {
  if (textContent)
    return (HTMLSpanElement()..textContent = html) as T;

  final template = HTMLTemplateElement();
  template.innerHTML = html.toJS;

  final elem = template.content.firstElementChild;
  if (elem == null)
    throw UnsupportedError('No element from html: $html');

  return elem as T;
}