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. bool resolveDSX = false,
  7. bool buildTemplates = false,
  8. DOMNode? parentNode,
  9. DOMNode? previousNode,
  10. DOMContext? 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,
    bool resolveDSX = false,
    bool buildTemplates = false,
    DOMNode? parentNode,
    DOMNode? previousNode,
    DOMContext? domContext}) {
  if (buildTemplates && hasUnresolvedTemplate) {
    var htmlUnresolvedTemplate =
        buildHTML(withIndent: true, buildTemplates: false, resolveDSX: false);

    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,
          resolveDSX: resolveDSX,
          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 && _selfClosingTagsOptional.contains(tag))) {
    var html = parentIndent +
        buildOpenTagHTML(
            openCloseTag: xhtml,
            resolveDSX: resolveDSX,
            domContext: domContext);
    return html;
  }

  var html = parentIndent +
      buildOpenTagHTML(resolveDSX: resolveDSX, domContext: domContext) +
      innerBreakLine;

  if (!emptyContent) {
    DOMNode? prev;
    for (var node in content) {
      var subElement = node.buildHTML(
          withIndent: withIndent,
          parentIndent: innerIndent,
          indent: indent,
          disableIndent: disableIndent,
          xhtml: xhtml,
          resolveDSX: resolveDSX,
          buildTemplates: buildTemplates,
          parentNode: this,
          previousNode: prev,
          domContext: domContext);
      html += subElement + innerBreakLine;
      prev = node;
    }
  }

  html += (allowIndent ? parentIndent : '') + buildCloseTagHTML();

  return html;
}