fromElement static method

XmlElement fromElement(
  1. XmlElement element
)

Converts all values in the element into a clone of the element with the necessary converted attributes.

To find all the necessary attributes we traverse all of element's parents to find any g element that contains styling information.

If any of the attributes is a transform, this wraps the cloned element into a g element.

Implementation

static XmlElement fromElement(XmlElement element) {
  final temporaryElement = XmlElement(
    XmlName('svg-to-avd:temporary-container'),
    element.attributes.map((child) => child.copy()),
  );

  final groupParents =
      element.ancestorElements.where((e) => e.name.local == ElementName.g);

  for (final parent in groupParents) {
    for (final attribute in parent.attributes) {
      if (temporaryElement.getAttribute(attribute.name.local) == null) {
        temporaryElement.setAttribute(attribute.name.local, attribute.value);
      }
    }
  }

  final newAttributes = fromAttributes(temporaryElement.attributes);

  final clone = XmlElement(
    element.name.copy(),
    newAttributes,
    element.children.map((child) => child.copy()),
    element.isSelfClosing,
  );

  final transform = element.getAttributeNode(AttributeName.transform);
  if (transform != null) {
    return XmlElement(
      XmlName(ElementName.g),
      [transform.copy()],
      [clone],
    );
  }

  return clone;
}