buildHTML method

  1. @override
String buildHTML({
  1. bool withIndent = false,
  2. String parentIndent = '',
  3. String indent = ' ',
  4. bool disableIndent = false,
  5. bool xhtml = false,
  6. DSXResolution dsxResolution = DSXResolution.skipDSX,
  7. bool buildTemplates = false,
  8. DOMNode? parentNode,
  9. DOMNode? previousNode,
  10. DOMContext<Object>? domContext,
})
override

Generates a HTML from this node tree.

withIndent If true will generate a indented HTML.

Implementation

@override
String buildHTML({
  bool withIndent = false,
  String parentIndent = '',
  String indent = '  ',
  bool disableIndent = false,
  bool xhtml = false,
  DSXResolution dsxResolution = DSXResolution.skipDSX,
  bool buildTemplates = false,
  DOMNode? parentNode,
  DOMNode? previousNode,
  DOMContext? domContext,
}) {
  // Commented nodes are ignored, like in `DOMNode.buildHTML` and
  // `DOMGenerator.build`:
  if (isCommented) return '';

  if (buildTemplates && hasUnresolvedTemplate) {
    var htmlUnresolvedTemplate = buildHTML(
      withIndent: true,
      buildTemplates: false,
      dsxResolution: DSXResolution.skipDSX,
    );

    var template = DOMTemplate.tryParse(htmlUnresolvedTemplate);

    if (template != null) {
      var templateNode = TemplateNode(template);

      var html = templateNode.buildHTML(
        withIndent: withIndent,
        parentIndent: parentIndent,
        indent: indent,
        disableIndent: disableIndent,
        xhtml: xhtml,
        dsxResolution: dsxResolution,
        buildTemplates: true,
        parentNode: parentNode,
        previousNode: previousNode,
        domContext: domContext,
      );

      return html;
    }
  }

  if (!disableIndent && !_tagAllowsInnerIndent(tag)) {
    disableIndent = true;
  }

  var allowIndent =
      withIndent &&
      isNotEmptyContent &&
      hasOnlyElementNodes &&
      !disableIndent;

  var innerIndent = allowIndent ? parentIndent + indent : '';
  var innerBreakLine = allowIndent ? '\n' : '';

  if (parentIndent.isNotEmpty &&
      previousNode != null &&
      previousNode.isStringElement) {
    parentIndent = '';
  }

  final content = _content;
  var emptyContent = content == null || content.isEmpty;

  if (_selfClosingTags.contains(tag) ||
      (emptyContent && (xhtml || _selfClosingTagsOptional.contains(tag)))) {
    var html =
        parentIndent +
        buildOpenTagHTML(
          openCloseTag: xhtml,
          dsxResolution: dsxResolution,
          domContext: domContext,
        );
    return html;
  }

  var html = StringBuffer();

  html.write(parentIndent);
  html.write(
    buildOpenTagHTML(dsxResolution: dsxResolution, domContext: domContext),
  );
  html.write(innerBreakLine);

  if (!emptyContent) {
    buildHTMLContent(
      output: html,
      withIndent: withIndent,
      innerIndent: innerIndent,
      innerBreakLine: innerBreakLine,
      indent: indent,
      disableIndent: disableIndent,
      xhtml: xhtml,
      dsxResolution: dsxResolution,
      buildTemplates: buildTemplates,
      domContext: domContext,
    );
  }

  if (allowIndent) {
    html.write(parentIndent);
  }

  html.write(buildCloseTagHTML());

  return html.toString();
}