Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser): adds CDATA nodes support #13

Merged
merged 6 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions addons/godot_xml/xml.gd
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ static func _make_node(queue: Array, parser: XMLParser):
return
_attach_node_data(queue.back(), parser)
return
XMLParser.NODE_CDATA:
if queue.is_empty():
return
_attach_node_cdata(queue.back(), parser)
return


static func _make_node_element(parser: XMLParser):
Expand Down Expand Up @@ -194,6 +199,8 @@ static func _attach_node_data(node: XMLNode, parser: XMLParser) -> void:
# we therefore strip "blankets", resulting in only actual content slipping into .content
node.content = parser.get_node_data().strip_edges().lstrip(" ").rstrip(" ")

static func _attach_node_cdata(node: XMLNode, parser: XMLParser) -> void:
node.cdata.append(parser.get_node_name().strip_edges().lstrip(" ").rstrip(" "))

static func _get_attributes(parser: XMLParser) -> Dictionary:
var attrs: Dictionary = {}
Expand Down
73 changes: 44 additions & 29 deletions addons/godot_xml/xml_node.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ var attributes: Dictionary = {}
## XML node content.
var content: String = ""

## XML node CDATA.
var cdata: Array[String] = []

## Whether the XML node is an empty node (AKA standalone node).
var standalone: bool = false

Expand All @@ -23,13 +26,15 @@ var _node_props = null # Array[String]
## Converts this node (and all of it's children) into a [Dictionary].
## Name is set as [code]__name__: name[/code].
## Content is set as [code]__content__: content[/code].
## CDATA is set as [code]__cdata__: [cdata, ...][/code].
## Attributes are set as [code]attrs: {attr_name: attr_value}[/code].
## Children are set as [code]children: {child_name: child_dict}[/code].
func to_dict() -> Dictionary:
var output = {}

output["__name__"] = name
output["__content__"] = content
output["__cdata__"] = cdata
output["attrs"] = attributes

var children_dict = {}
Expand All @@ -42,10 +47,11 @@ func to_dict() -> Dictionary:


func _to_string():
return "<XMLNode name=%s attributes=%s content=%s standalone=%s children=%s>" % [
return "<XMLNode name=%s attributes=%s content=%s cdata=%s standalone=%s children=%s>" % [
name,
"{...}" if len(attributes) > 0 else "{}",
'"..."' if len(content) > 0 else '""',
"[...]" if len(cdata) > 0 else "[]",
str(standalone),
"[...]" if len(children) > 0 else "[]"
]
Expand All @@ -56,7 +62,7 @@ func _get(property: StringName):
if _node_props == null:
_initialize_node_properties()

if property not in ["name", "attributes", "content", "standalone", "children"] and property in _node_props:
if property not in ["name", "attributes", "content", "cdata", "standalone", "children"] and property in _node_props:
for child in children:
if child.name == property:
return child
Expand Down Expand Up @@ -98,12 +104,13 @@ func _initialize_node_properties():

func _dump() -> String:
var template = (
"<{node_name}{attributes}>{content}{children}</{node_name}>"
"<{node_name}{attributes}>{content}{cdata}{children}</{node_name}>"
if not standalone else
"<{node_name}{attributes}/>"
)
var attribute_string = ""
var children_string = ""
var cdata_string = ""

if not attributes.is_empty():
attribute_string += " "
Expand All @@ -115,41 +122,49 @@ func _dump() -> String:
for child in children:
children_string += child._dump()

for cdata_content in cdata:
cdata_string += "<![CDATA[%s]]>" % cdata_content

return template.format({
"node_name": name,
"attributes": attribute_string,
"content": content,
"cdata": cdata_string,
"children": children_string,
})


func _dump_pretty(indent_level: int, indent_length: int) -> String:
var template = (
"{indent}<{node_name}{attributes}>{content}{children}\n{indent}</{node_name}>"
if not standalone else
"{indent}<{node_name}{attributes}/>"
)
var indent_string = " ".repeat(indent_level * indent_length)
var indent_next_string = indent_string + " ".repeat(indent_length)
var attribute_string = ""
var content_string = "\n" + indent_next_string + content if not content.is_empty() else ""
var children_string = ""

if not attributes.is_empty():
attribute_string += " "

for attribute_key in attributes:
var attribute_value = attributes.get(attribute_key)
attribute_string += '{key}="{value}"'.format({"key": attribute_key, "value": attribute_value})
var template = (
"{indent}<{node_name}{attributes}>{content}{cdata}{children}\n{indent}</{node_name}>"
if not standalone else
"{indent}<{node_name}{attributes}/>"
)
var indent_string = " ".repeat(indent_level * indent_length)
var indent_next_string = indent_string + " ".repeat(indent_length)
var attribute_string = ""
var content_string = "\n" + indent_next_string + content if not content.is_empty() else ""
var children_string = ""
var cdata_string = ""

if not attributes.is_empty():
attribute_string += " "

for attribute_key in attributes:
var attribute_value = attributes.get(attribute_key)
attribute_string += '{key}="{value}"'.format({"key": attribute_key, "value": attribute_value})

for child in children:
children_string += "\n" + child._dump_pretty(indent_level + 1, indent_length)
for child in children:
children_string += "\n" + child._dump_pretty(indent_level + 1, indent_length)

return template.format({
"node_name": name,
"attributes": attribute_string,
"content": content_string,
"children": children_string,
for cdata_content in cdata:
cdata_string += "\n" + indent_next_string + ("<![CDATA[%s]]>" % cdata_content)

"indent": indent_string,
})
return template.format({
"node_name": name,
"attributes": attribute_string,
"content": content_string,
"cdata": cdata_string,
"children": children_string,
"indent": indent_string,
})