mutate<T extends TagXml> static method
Mutate a XmlElement into a TagXml of type T.
- If the
XmlElementisnull, it returnsnull. - If the
XmlElementis already an instance ofT, it returns theXmlElementasT. - If the
XmlElementis not an instance ofT, it creates a new instance ofTand copies the attributes and children of theXmlElementto the new instance, insert the new instance in the parent of theXmlElementand remove theXmlElement.
Implementation
static T? mutate<T extends TagXml>(XmlNode? element, T Function() constructor, [bool force = false]) {
if (element == null) {
if (force) return constructor();
return null;
}
if (element is T && force == false) return element;
var newTag = constructor();
while (element.attributes.isNotEmpty) {
var a = element.attributes.first;
element.removeAttribute(a.name.qualified);
newTag.setAttribute(a.name.qualified, a.value);
}
while (element.children.isNotEmpty) {
var c = element.children.first;
element.children.remove(c);
newTag.children.add(c);
}
if (element.hasParent && element.parent != null) {
var parent = element.parent!;
var index = parent.children.indexOf(element);
parent.children.remove(element);
parent.children.insert(index, newTag);
}
return newTag;
}