X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=c%2B%2B11%2Fxml-template.cpp;h=4f0c9fef6e903aa4a9772de64e176d612dd3c291;hb=1725768e2eaf68a1445f1aef9d95da0bf461e88b;hp=317a561779fab53a7177d8376e1bbd37de585909;hpb=9673065348fb768fd794926406236263e3ed875c;p=xml-template diff --git a/c++11/xml-template.cpp b/c++11/xml-template.cpp index 317a561..4f0c9fe 100644 --- a/c++11/xml-template.cpp +++ b/c++11/xml-template.cpp @@ -14,12 +14,11 @@ void clean_node(xmlNode *node) } if (node->ns != NULL && strcmp(reinterpret_cast(node->ns->href), "http://template.sesse.net/") == 0) { - xmlNode *frag = xmlNewDocFragment(node->doc); - xmlReplaceNode(node, frag); - frag->children = node->children; - frag->last = node->last; + while (node->children != NULL) { + xmlAddPrevSibling(node, node->children); + } - node->children = node->last = NULL; + xmlUnlinkNode(node); xmlFreeNode(node); } } @@ -168,8 +167,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 +189,8 @@ void Substitute::process(xmlNode *node, bool clean) } // Check all substitutions to see if we found anything appropriate. + string tag = reinterpret_cast(child->name); for (auto it : substitution_map) { - string tag = reinterpret_cast(child->name); - // Attribute substitution. if (begins_with(it.first, tag + "/")) { const xmlChar *attr_key = reinterpret_cast( @@ -202,28 +200,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( - it.first.c_str() + tag.size() + 2); + it.first.c_str() + id.size() + 2); const xmlChar *attr_value = reinterpret_cast( 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);