From f49e439146b951e35d169110fefe4d5882399d62 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Mon, 16 Mar 2015 07:50:31 -0400 Subject: [PATCH] [MRI] Silence dupe-id error on setting attribute. Fixes #1262. --- ext/nokogiri/xml_node.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/ext/nokogiri/xml_node.c b/ext/nokogiri/xml_node.c index 7043f5db22..41c195f746 100644 --- a/ext/nokogiri/xml_node.c +++ b/ext/nokogiri/xml_node.c @@ -778,11 +778,19 @@ 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 @@ -790,11 +798,9 @@ static VALUE set(VALUE self, VALUE property, VALUE value) * * 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); @@ -802,10 +808,14 @@ static VALUE set(VALUE self, VALUE property, VALUE value) } } - 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; } /*