[fixup]also convert non-dotted attributes

This commit is contained in:
tokyo4j 2025-04-11 17:53:47 +09:00
parent e0993db702
commit 9ba06c160e
2 changed files with 19 additions and 8 deletions

View file

@ -1,4 +1,5 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <assert.h>
#include <glib.h>
#include <stdbool.h>
#include <strings.h>
@ -10,10 +11,6 @@
static xmlNode*
create_attribute_tree(const xmlAttr *attr)
{
if (!strchr((char *)attr->name, '.')) {
return NULL;
}
gchar **parts = g_strsplit((char *)attr->name, ".", -1);
int length = g_strv_length(parts);
xmlNode *root_node = NULL;
@ -35,6 +32,12 @@ create_attribute_tree(const xmlAttr *attr)
parent_node = current_node;
}
/*
* Note: empty attributes or attributes with only dots are forbidden
* and root_node becomes never NULL here.
*/
assert(root_node);
xmlChar *content = xmlNodeGetContent(attr->children);
xmlNodeSetContent(current_node, content);
xmlFree(content);

16
t/xml.c
View file

@ -24,6 +24,13 @@ struct test_case {
"</position>"
"</action>"
"</keybind>"
}, {
"<AAA aaa='111' bbb='222'/>",
"<AAA>"
"<aaa>111</aaa>"
"<bbb>222</bbb>"
"</AAA>"
}, {
"<AAA aaa.bbb.ccc='111' ddd.ccc='222' eee.bbb.ccc='333'/>",
@ -50,10 +57,11 @@ struct test_case {
}, {
"<AAA aaa.bbb='111' bbb='222' ccc.bbb='333'/>",
"<AAA bbb=\"222\">"
"<bbb><aaa>111</aaa></bbb>"
"<bbb><ccc>333</ccc></bbb>"
"</AAA>",
"<AAA><bbb>"
"<aaa>111</aaa>"
"222"
"<ccc>333</ccc>"
"</bbb></AAA>",
}, {
"<AAA>"
"<BBB aaa.bbb='111'/>"