Skip to content

feat: Implement XML node abstractions with validation and serialization - #17

Merged
XenoSnowFox merged 13 commits into
mainfrom
feature/xml-representation
Aug 28, 2026
Merged

feat: Implement XML node abstractions with validation and serialization#17
XenoSnowFox merged 13 commits into
mainfrom
feature/xml-representation

Conversation

@XenoSnowFox

Copy link
Copy Markdown
Contributor

No description provided.

This commit introduces the core XML node abstractions for tags, declarations, attributes, and text content. It adds reusable interfaces and concrete node classes with serialization, escaping, and child-node support for building basic XML documents.
Fix XML attribute serialization by escaping '<' as '&lt;' when writing attribute values. This keeps attribute contents valid XML and prevents malformed output when attribute text contains comparison operators or angle brackets.
This change adds a shared XML name validator and enforces it when creating tag names and setting attributes. Invalid names now throw std::invalid_argument instead of being accepted silently, preventing malformed XML content at the API boundary.
Add XmlCharacter.hpp (isValidXmlCharacterData) to enforce XML 1.0 character rules (reject control chars < 0x20 except tab/newline/carriage return). Use it in IAttributes to validate attribute values and in XmlTextContent (ctor, setter, append) to validate text content. Also add necessary includes and throw std::invalid_argument on invalid input.
The XML attribute accessor was returning std::optional<std::string_view>, which can dangle when the underlying stored value is not guaranteed to outlive the call. This change returns a copied std::string instead, making the API safe and consistent with callers that need an owned value.
Add cycle detection to XmlTagNode::append: attempting to append a child that would create a cycle now throws std::invalid_argument. Implement containsNode (iterative DFS with a visited set) to detect reachability. Include necessary headers (<stdexcept>, <unordered_set>).
This change enforces XML declaration correctness by requiring the tag name to be `xml`, validating required `version` values (`1.0` or `1.1`), and rejecting unsupported attributes, invalid encoding names, and invalid standalone values during serialization. It also serializes declarations in the standard `<?xml ...?>` form with optional `encoding` and `standalone` attributes.
These headers were using std::string_view and related standard library types without including the required headers. This patch adds the missing includes to the XML node interfaces and content classes so they compile cleanly and remain self-contained.
Serialize XML attributes in a deterministic key order instead of the underlying hash-map order so output is stable and predictable. The change also keeps attribute escaping intact while writing values. Minor formatting/include-order cleanups were applied to the XML declaration and tag headers.
Replace the unordered attribute container with std::map and remove the extra sort step during XML serialization. This keeps attribute output deterministic and in key order while simplifying the implementation and reducing unnecessary work.
Remove handling that converted '"' and '\'' to &quot; and &apos; in XmlTextContent output. Quotes are valid in XML character data and should only be escaped in attribute values; this avoids unnecessary escaping or double-encoding in text nodes. Change made in include/xtrpg/xml/node/XmlTextContent.hpp.
The stream extraction overload for XmlTextContent was appending a newline after each read line. This patch removes that delimiter so a single line is appended exactly as read, preserving the original text content without introducing extra line breaks.
This change updates XML attribute escaping to encode tab, newline, and carriage return characters as XML numeric references (`&#x9;`, `&#xA;`, `&#xD;`). This ensures attribute values remain valid and safe when serialized into XML.
@XenoSnowFox
XenoSnowFox merged commit 11eaf22 into main Aug 28, 2026
4 checks passed
@XenoSnowFox
XenoSnowFox deleted the feature/xml-representation branch August 28, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant