prototypeOf function

XmlElement? prototypeOf(
  1. XmlElement? node
)

Implementation

XmlElement? prototypeOf(XmlElement? node) {
  if (node == null) return null;

  // get the id
  var id = Xml.attribute(node: node, tag: "id");

  // if missing, assign it a unique key
  if (id == null) {
    id = newId();
    Xml.setAttribute(node, "id", id);
  }

  // process data bindings
  var xml = node.toString();
  var bindings = Binding.getBindings(xml);
  List<String?> processed = [];

  if (bindings != null) {
    bool doReplace = false;

    // process each binding
    for (var binding in bindings) {
      // special case
      if ((binding.source == 'data') &&
          !processed.contains(binding.signature)) {
        doReplace = true;

        processed.add(binding.signature);

        // set the signature
        var signature =
            "{$id.${binding.source}.${binding.property}${(binding.dotnotation?.signature != null ? ".${binding.dotnotation!.signature}" : "")}}";
        xml = xml.replaceAll(binding.signature, signature);
      }
    }

    // parse the new xml
    var newNode = Xml.tryParse(xml)?.rootElement;

    // if valid node, we need to replace this node in the tree so
    // ancestor prototypes don't translate data incorrectly
    if (newNode != null) {
      if (doReplace) {
        var parent = node.parent;
        var index = node.parent?.children.indexOf(node) ?? -1;
        newNode = newNode.copy();
        if (index >= 0 && parent != null) {
          parent.children.removeAt(index);
          parent.children.insert(index, newNode);
        }
      }
      node = newNode;
    }
  }

  return node;
}