htmlAsSvgContent function

String? htmlAsSvgContent(
  1. String html, {
  2. int? width,
  3. int? height,
  4. String? rootClass,
  5. String? style,
})

Implementation

String? htmlAsSvgContent(String html,
    {int? width, int? height, String? rootClass, String? style}) {
  print(style);
  var htmlRoot = $htmlRoot(html);
  if (htmlRoot == null) return null;

  if (isNotEmptyObject(rootClass)) {
    htmlRoot.addClass('ui-render');
  }

  var titleNode =
      htmlRoot.selectAllWhere((e) => e is DOMElement && e.tag == 'title');

  var titleText = titleNode.firstOrNull ?? 'HTML as SVG';

  htmlRoot
      .selectAllWhere((n) => true)
      .whereType<DOMElement>()
      .forEach((e) => e['xmlns'] = 'http://www.w3.org/1999/xhtml');

  html = htmlRoot.buildHTML(xhtml: true);

  var svg =
      '''<svg viewBox="0 0 $width $height" width="${width}px" height="${height}px" xmlns="http://www.w3.org/2000/svg">
<foreignObject x="0" y="0" width="$width" height="$height">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head xmlns="http://www.w3.org/1999/xhtml">
<title>$titleText</title>
<style xmlns="http://www.w3.org/1999/xhtml">
$style
</style>
</head>
<body xmlns="http://www.w3.org/1999/xhtml">
$html
</body>
</html>
</foreignObject>
</svg>''';

  return svg;
}