Skip to content

Commit

Permalink
[MRI] Silence dupe-id error on setting attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Mar 16, 2015
1 parent 4be0541 commit f49e439
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions ext/nokogiri/xml_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -778,34 +778,44 @@ static VALUE namespaced_key_eh(VALUE self, VALUE attribute, VALUE namespace)
*
* Set the +property+ to +value+
*/
static VALUE set(VALUE self, VALUE property, VALUE value)
static VALUE set(VALUE node_rb, VALUE property_name_rb, VALUE property_value_rb)
{
xmlNodePtr node, cur;
xmlAttrPtr prop;
Data_Get_Struct(self, xmlNode, node);
xmlChar* property_name ;
xmlAttrPtr property;

Data_Get_Struct(node_rb, xmlNode, node);

if (node->type != XML_ELEMENT_NODE) {
return(Qnil); // TODO: would raising an exception be more appropriate?
}

property_name = (xmlChar *)StringValuePtr(property_name_rb);

/* If a matching attribute node already exists, then xmlSetProp will destroy
* the existing node's children. However, if Nokogiri has a node object
* pointing to one of those children, we are left with a broken reference.
*
* We can avoid this by unlinking these nodes first.
*/
if (node->type != XML_ELEMENT_NODE)
return(Qnil);
prop = xmlHasProp(node, (xmlChar *)StringValuePtr(property));
if (prop && prop->children) {
for (cur = prop->children; cur; cur = cur->next) {
property = xmlHasProp(node, property_name);
if (property && property->children) {
for (cur = property->children; cur; cur = cur->next) {
if (cur->_private) {
nokogiri_root_node(cur);
xmlUnlinkNode(cur);
}
}
}

xmlSetProp(node, (xmlChar *)StringValuePtr(property),
(xmlChar *)StringValuePtr(value));
xmlResetLastError();
xmlSetStructuredErrorFunc(NULL, Nokogiri_error_silencer);

xmlSetProp(node, property_name, (xmlChar *)StringValuePtr(property_value_rb));

xmlSetStructuredErrorFunc(NULL, NULL);

return value;
return property_value_rb;
}

/*
Expand Down

0 comments on commit f49e439

Please sign in to comment.