Skip to content

Commit

Permalink
[BUGFIX] Move prefixed attribute getter logic into C extension
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld committed Jul 10, 2012
1 parent f5ffb4f commit 28f920b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
22 changes: 21 additions & 1 deletion ext/nokogiri/xml_node.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -703,12 +703,32 @@ static VALUE get(VALUE self, VALUE attribute)
{ {
xmlNodePtr node; xmlNodePtr node;
xmlChar* propstr ; xmlChar* propstr ;
xmlNsPtr ns;
VALUE rval ; VALUE rval ;
VALUE match;
VALUE prefix;
VALUE attribute_name;

Data_Get_Struct(self, xmlNode, node); Data_Get_Struct(self, xmlNode, node);


if(NIL_P(attribute)) return Qnil; if(NIL_P(attribute)) return Qnil;


propstr = xmlGetNoNsProp(node, (xmlChar *)StringValuePtr(attribute)); match = rb_funcall(attribute, rb_intern("match"), 1,
rb_reg_new_str(rb_str_new2("([\\w]*):([\\w]*)$"), 0)
);

if(RTEST(match)) {
prefix = rb_funcall(match, rb_intern("[]"), 1, INT2NUM(1));
attribute_name = rb_funcall(match, rb_intern("[]"), 1, INT2NUM(2));

ns = xmlSearchNs(node->doc, node, (const xmlChar *)(StringValuePtr(prefix)));
if(!ns) return Qnil;

propstr = xmlGetNsProp(node, (xmlChar *)StringValuePtr(attribute_name), ns->href);
} else {
if(!key_eh(self, attribute)) return Qnil;
propstr = xmlGetNoNsProp(node, (xmlChar *)StringValuePtr(attribute));
}


if(!propstr) return Qnil; if(!propstr) return Qnil;


Expand Down
13 changes: 1 addition & 12 deletions lib/nokogiri/xml/node.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -251,18 +251,7 @@ def at_css *rules
### ###
# Get the attribute value for the attribute +name+ # Get the attribute value for the attribute +name+
def [] name def [] name
attr_name = name.to_s get name.to_s

if attr_name.include?(':')
match = attr_name.match(/([\w\d:]*):([\w\d]*)$/)
prefix, attribute = match[1], match[2]
namespace = namespace_with_prefix prefix

attribute_with_ns(attribute, namespace.href).value
else
return unless key?(attr_name)
get attr_name
end
end end


### ###
Expand Down

0 comments on commit 28f920b

Please sign in to comment.