]> git.mdlowis.com Git - proto/labwc.git/commitdiff
common/xml: let LAB_XML_FOR_EACH() skip first child text nodes
authortokyo4j <hrak1529@gmail.com>
Sat, 2 Aug 2025 15:02:02 +0000 (00:02 +0900)
committerJohan Malm <johanmalm@users.noreply.github.com>
Sun, 3 Aug 2025 14:05:53 +0000 (15:05 +0100)
Before this patch, first text nodes like the spaces between <a> and <b>
below were also travered by LAB_XML_FOR_EACH():

  <a>  <b>foo</b></a>

include/common/xml.h

index 3cfa5539f3d46852185a44fbf5a32eb59e8013f1..8b49d0e409fb11dfa13ab23ab5d30be47e57debc 100644 (file)
@@ -35,16 +35,13 @@ bool lab_xml_get_string(xmlNode *node, const char *key, char *s, size_t len);
 bool lab_xml_get_int(xmlNode *node, const char *key, int *i);
 bool lab_xml_get_bool(xmlNode *node, const char *key, bool *b);
 
+/* also skips other unusual nodes like comments */
 static inline xmlNode *
-lab_xml_get_next_child(xmlNode *child)
+lab_xml_skip_text(xmlNode *child)
 {
-       if (!child) {
-               return NULL;
-       }
-       do {
+       while (child && child->type != XML_ELEMENT_NODE) {
                child = child->next;
-       } while (child && child->type != XML_ELEMENT_NODE);
-
+       }
        return child;
 }
 
@@ -58,11 +55,13 @@ lab_xml_get_key_and_content(xmlNode *node, char **name, char **content)
 }
 
 #define LAB_XML_FOR_EACH(parent, child, key, content) \
-       for ((child) = (parent)->children, \
+       for ((child) = lab_xml_skip_text((parent)->children), \
                lab_xml_get_key_and_content((child), &(key), &(content)); \
+               \
                (child); \
+               \
                xmlFree((xmlChar *)(content)), \
-               (child) = lab_xml_get_next_child(child), \
+               (child) = lab_xml_skip_text((child)->next), \
                lab_xml_get_key_and_content((child), &(key), &(content)))
 
 #endif /* LABWC_XML_H */