Skip to content

Add xml_node::ensure_attribute and xml_node::ensure_child - #730

Merged
zeux merged 3 commits into
masterfrom
ensure
Jun 15, 2026
Merged

Add xml_node::ensure_attribute and xml_node::ensure_child#730
zeux merged 3 commits into
masterfrom
ensure

Conversation

@zeux

@zeux zeux commented Jun 15, 2026

Copy link
Copy Markdown
Owner

These are equivalent to a named lookup (attribute/child) followed by
appending the attribute/child if an existing one is not found. The
implementation is trivial but this is fairly commonplace in code that
modifies an existing DOM.

For example, updating an existing configuration node can now be done as:

pugi::xml_node font = config.ensure_child("font");
font.ensure_attribute("size").set_value(14);

... whereas previously this would require creating new nodes/attributes explicitly:

pugi::xml_node font = config.child("font");
if (!font) font = config.append_child("font");

pugi::xml_attribute size = font.attribute("size");
if (!size) size = font.append_attribute("size");

size.set_value(14);

Fixes #409.

zeux added 3 commits June 14, 2026 22:16
These are equivalent to a named lookup (attribute/child) followed by
appending the attribute/child if an existing one is not found. The
implementation is trivial but this is fairly commonplace in code that
modifies an existing DOM.
These include both raw string and string view variants.
Also add these to the changelog for 1.16 as it's right around the
corner and the changelog has already been written.
@zeux
zeux merged commit 77c1bb6 into master Jun 15, 2026
28 checks passed
@zeux
zeux deleted the ensure branch June 15, 2026 17:16
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.

child_auto and attribute_auto

1 participant