summaryrefslogtreecommitdiff
path: root/addons/gdUnit4/src/reporters/xml/XmlElement.gd
diff options
context:
space:
mode:
authorradiohotline <radoihotline@disroot.rg>2026-08-25 16:33:56 +0300
committerradiohotline <radoihotline@disroot.rg>2026-08-25 16:33:56 +0300
commita89e744ed0e8c759caf041a580112365bbfe4303 (patch)
treedd61e320904176995727e86a857d00c90d4933ba /addons/gdUnit4/src/reporters/xml/XmlElement.gd
parent6d53d8cac78250dc888aa49d5f9d4175c6652feb (diff)
escoria updateHEADmain
Diffstat (limited to 'addons/gdUnit4/src/reporters/xml/XmlElement.gd')
-rw-r--r--addons/gdUnit4/src/reporters/xml/XmlElement.gd69
1 files changed, 69 insertions, 0 deletions
diff --git a/addons/gdUnit4/src/reporters/xml/XmlElement.gd b/addons/gdUnit4/src/reporters/xml/XmlElement.gd
new file mode 100644
index 0000000..86c7421
--- /dev/null
+++ b/addons/gdUnit4/src/reporters/xml/XmlElement.gd
@@ -0,0 +1,69 @@
+class_name XmlElement
+extends RefCounted
+
+var _name :String
+# Dictionary[String, String]
+var _attributes :Dictionary = {}
+var _childs :Array[XmlElement] = []
+var _parent :XmlElement = null
+var _text :String = ""
+
+
+func _init(name :String) -> void:
+ _name = name
+
+
+func dispose() -> void:
+ for child in _childs:
+ child.dispose()
+ _childs.clear()
+ _attributes.clear()
+ _parent = null
+
+
+func attribute(name :String, value :Variant) -> XmlElement:
+ _attributes[name] = str(value)
+ return self
+
+
+func text(p_text :String) -> XmlElement:
+ _text = p_text if p_text.ends_with("\n") else p_text + "\n"
+ return self
+
+
+func add_child(child :XmlElement) -> XmlElement:
+ _childs.append(child)
+ child._parent = self
+ return self
+
+
+func add_childs(childs :Array[XmlElement]) -> XmlElement:
+ for child in childs:
+ @warning_ignore("return_value_discarded")
+ add_child(child)
+ return self
+
+
+func indentation() -> String:
+ return "" if _parent == null else _parent.indentation() + " "
+
+
+func to_xml() -> String:
+ var attributes := ""
+ for key in _attributes.keys() as Array[String]:
+ attributes += ' {attr}="{value}"'.format({"attr": key, "value": _attributes.get(key)})
+
+ var childs := ""
+ for child in _childs:
+ childs += child.to_xml()
+
+ return "{_indentation}<{name}{attributes}>\n{childs}{text}{_indentation}</{name}>\n"\
+ .format({"name": _name,
+ "attributes": attributes,
+ "childs": childs,
+ "_indentation": indentation(),
+ "text": cdata(_text)})
+
+
+func cdata(p_text :String) -> String:
+ return "" if p_text.is_empty() else "<![CDATA[\n{text}]]>\n".format({"text" : p_text})