Skip to content

Commit c75f521

Browse files
author
Kjell Ahlstedt
committed
Element::remove_attribute(): Delete the C++ wrapper
* libxml++/nodes/element.cc: Call Node::free_wrappers() before the call to xmlUnsetProp() or xmlUnsetNsProp(). Bug #768404. Based on a patch by Harald Schmalzl <h.schmalzl@gekko.at>
1 parent b004859 commit c75f521

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

libxml++/nodes/element.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,35 @@ Attribute* Element::set_attribute(const Glib::ustring& name, const Glib::ustring
133133

134134
void Element::remove_attribute(const Glib::ustring& name, const Glib::ustring& ns_prefix)
135135
{
136+
// xmlHasProp() seaches for an attribute with a specified name in any namespace.
137+
// Not useful here.
138+
// xmlHasNsProp() seaches both for an attribute node in the element node
139+
// and for an attribute declaration in the DTD.
140+
// xmlUnsetProp() or xmlUnsetNsProp() won't delete an attribute declaration.
141+
auto attr = xmlHasNsProp(cobj(), (const xmlChar*)name.c_str(),
142+
ns_prefix.empty() ? nullptr : (const xmlChar*)ns_prefix.c_str());
143+
if (!attr || attr->type == XML_ATTRIBUTE_DECL)
144+
return;
145+
136146
if (ns_prefix.empty())
147+
{
148+
// *this has an attribute with the specified name and no namespace.
149+
// xmlUnsetProp() will delete the existing attribute.
150+
// Delete the C++ wrapper before the call to xmlUnsetProp().
151+
Node::free_wrappers(reinterpret_cast<xmlNode*>(attr));
137152
xmlUnsetProp(cobj(), (const xmlChar*)name.c_str());
153+
}
138154
else
139155
{
140156
auto ns = xmlSearchNs(cobj()->doc, cobj(), (const xmlChar*)ns_prefix.c_str());
141157
if (ns)
158+
{
159+
// *this has an attribute with the specified name and namespace.
160+
// xmlUnsetNsProp() will delete the existing attribute.
161+
// Delete the C++ wrapper before the call to xmlUnsetNsProp().
162+
Node::free_wrappers(reinterpret_cast<xmlNode*>(attr));
142163
xmlUnsetNsProp(cobj(), ns, (const xmlChar*)name.c_str());
164+
}
143165
}
144166
}
145167

0 commit comments

Comments
 (0)