[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 // SPDX-License-Identifier: GPL-2.0-only
#include <assert.h>
#include <glib.h> #include <glib.h>
#include <stdbool.h> #include <stdbool.h>
#include <strings.h> #include <strings.h>
@ -10,10 +11,6 @@
static xmlNode* static xmlNode*
create_attribute_tree(const xmlAttr *attr) create_attribute_tree(const xmlAttr *attr)
{ {
if (!strchr((char *)attr->name, '.')) {
return NULL;
}
gchar **parts = g_strsplit((char *)attr->name, ".", -1); gchar **parts = g_strsplit((char *)attr->name, ".", -1);
int length = g_strv_length(parts); int length = g_strv_length(parts);
xmlNode *root_node = NULL; xmlNode *root_node = NULL;
@ -35,6 +32,12 @@ create_attribute_tree(const xmlAttr *attr)
parent_node = current_node; 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); xmlChar *content = xmlNodeGetContent(attr->children);
xmlNodeSetContent(current_node, content); xmlNodeSetContent(current_node, content);
xmlFree(content); xmlFree(content);

16
t/xml.c
View file

@ -24,6 +24,13 @@ struct test_case {
"</position>" "</position>"
"</action>" "</action>"
"</keybind>" "</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'/>", "<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 aaa.bbb='111' bbb='222' ccc.bbb='333'/>",
"<AAA bbb=\"222\">" "<AAA><bbb>"
"<bbb><aaa>111</aaa></bbb>" "<aaa>111</aaa>"
"<bbb><ccc>333</ccc></bbb>" "222"
"</AAA>", "<ccc>333</ccc>"
"</bbb></AAA>",
}, { }, {
"<AAA>" "<AAA>"
"<BBB aaa.bbb='111'/>" "<BBB aaa.bbb='111'/>"