serialize method

String serialize({
  1. required BuildContext context,
  2. required Node node,
  3. Color? backgroundColor,
})

Serializes any DOM node.

If the node is a document node or <html> node, no changes are made.

Otherwise the node is wrapped inside a document that is styled by calling cssFromBuildContext.

Implementation

String serialize({
  required BuildContext context,
  required html.Node node,
  Color? backgroundColor,
}) {
  // A document node?
  if (node is html.Document) {
    return node.documentElement!.outerHtml!;
  }

  // <html>...</html> element?
  if (node is html.HtmlHtmlElement) {
    return node.outerHtml!;
  }

  // Wrap the node inside <body>:
  //     <html>
  //       <body>
  //         NODE
  //       </body>
  //     </html>
  final bodyElement = html.BodyElement();
  bodyElement.append(node);

  final htmlElement = html.HtmlHtmlElement();
  htmlElement.append(bodyElement);

  // Get CSS style
  final cssStyle = bodyElement.style;

  // Set background color
  if (backgroundColor != null) {
    cssStyle.backgroundColor = cssColor(backgroundColor);
  }

  // Transfer style
  cssFromBuildContext(cssStyle, context);

  return htmlElement.outerHtml!;
}