]> git.sesse.net Git - xml-template/blobdiff - c++11/xml-template.cpp
In the C++11 version, we would set the wrong key on id+attribute matches. Fixed.
[xml-template] / c++11 / xml-template.cpp
index 317a561779fab53a7177d8376e1bbd37de585909..503e5e329d4f7269a8115c1c1df20114705b4819 100644 (file)
@@ -168,8 +168,8 @@ void Substitute::process(xmlNode *node, bool clean)
 {
        xmlNode *next_child;
        for (xmlNode *child = node->children; child != NULL; child = next_child) {
-               next_child = child->next;       
-               bool processed = false;
+               next_child = child->next;
+               Directive *next_processor = this;
 
                if (child->type == XML_ELEMENT_NODE) {
                        // Find the ID, if any.
@@ -190,9 +190,8 @@ void Substitute::process(xmlNode *node, bool clean)
                        }
 
                        // Check all substitutions to see if we found anything appropriate.
+                       string tag = reinterpret_cast<const char *>(child->name);
                        for (auto it : substitution_map) {
-                               string tag = reinterpret_cast<const char *>(child->name);
-
                                // Attribute substitution.
                                if (begins_with(it.first, tag + "/")) {
                                        const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
@@ -202,28 +201,22 @@ void Substitute::process(xmlNode *node, bool clean)
                                        xmlSetProp(child, attr_key, attr_value);
                                } else if ((!id.empty() && begins_with(it.first, "#" + id + "/"))) {
                                        const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
-                                               it.first.c_str() + tag.size() + 2);
+                                               it.first.c_str() + id.size() + 2);
                                        const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
                                                it.second->get_contents().c_str());
                                        xmlSetProp(child, attr_key, attr_value);
                                }
 
-                               if (processed) {
-                                       continue;
-                               }
-
-                               // Regular substitution.
+                               // Regular substitution. (Don't call process() immediately, because
+                               // that might delete the element, which would cause problems.)
                                if (it.first == tag ||
                                    (!id.empty() && it.first == ("#" + id))) {
-                                       it.second->process(child, clean);
-                                       processed = true;
+                                       next_processor = it.second;
                                }
                        }
                }
 
-               if (!processed) {
-                       process(child, clean);
-               }
+               next_processor->process(child, clean);
        }
        if (clean) {
                clean_node(node);