buildTemplate method

T? buildTemplate(
  1. DOMElement? domParent,
  2. T? parent,
  3. TemplateNode domNode,
  4. DOMTreeMap<T> treeMap,
  5. DOMContext<T>? context,
)

Implementation

T? buildTemplate(DOMElement? domParent, T? parent, TemplateNode domNode,
    DOMTreeMap<T> treeMap, DOMContext<T>? context) {
  context ??= domContext;

  if (domParent != null) {
    domNode.parent = domParent;
  }

  var variables = context?.variables ?? <String, dynamic>{};
  var templateResolved = domNode.template
      .build(variables, intlMessageResolver: context?.intlMessageResolver);

  if (templateResolved is List) {
    if (templateResolved.isEmpty) {
      templateResolved = null;
    } else if (templateResolved.length == 1) {
      templateResolved = templateResolved.first;
    } else if (templateResolved.where((e) => e is! DOMNode).isEmpty) {
      templateResolved = $span(content: templateResolved);
    } else if (templateResolved.whereType<DOMNode>().isNotEmpty) {
      var nodes =
          templateResolved.expand((e) => DOMNode.parseNodes(e)).toList();
      templateResolved = $span(content: nodes);
    }
  }

  String str;
  if (templateResolved is DOMNode) {
    var node = templateResolved;
    if (node is! TextNode) {
      if (domParent != null) {
        node.parent = domParent;
      }
      return build(domParent, parent, node, treeMap, context);
    } else {
      str = templateResolved.text;
    }
  } else {
    str = DOMTemplate.objectToString(templateResolved);
  }

  if (possiblyWithHTML(str)) {
    var nodes = parseHTML(str);
    if (nodes != null && nodes.isNotEmpty) {
      DOMNode node;
      if (nodes.length == 1) {
        node = nodes[0];
      } else {
        node = $tag('span', content: nodes);
      }

      if (node is TextNode) {
        T? textNode;
        if (parent != null) {
          textNode = appendElementTextNode(parent, node);
        } else {
          textNode = createTextNode(node);
        }

        if (textNode != null) {
          treeMap.map(domNode, textNode);
        }

        return textNode;
      } else {
        if (domParent != null) {
          node.parent = domParent;
        }
        return build(domParent, parent, node, treeMap, context);
      }
    }
  }

  T? textNode;
  if (parent != null) {
    textNode = appendElementText(parent, str);
  } else {
    textNode = createTextNode(str);
  }

  if (textNode != null) {
    treeMap.map(domNode, textNode);
  }

  return textNode;
}