Browse files

[BUGFIX] Move prefixed attribute getter logic into C extension

  • Loading branch information...
1 parent f5ffb4f commit 28f920b5dfec7a267b73ad83edcb547e2f90973c @benlangfeld benlangfeld committed Jul 10, 2012
Showing with 22 additions and 13 deletions.
  1. +21 −1 ext/nokogiri/xml_node.c
  2. +1 −12 lib/nokogiri/xml/node.rb
View
22 ext/nokogiri/xml_node.c
@@ -703,12 +703,32 @@ static VALUE get(VALUE self, VALUE attribute)
{
xmlNodePtr node;
xmlChar* propstr ;
+ xmlNsPtr ns;
VALUE rval ;
+ VALUE match;
+ VALUE prefix;
+ VALUE attribute_name;
+
Data_Get_Struct(self, xmlNode, node);
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;
View
13 lib/nokogiri/xml/node.rb
@@ -251,18 +251,7 @@ def at_css *rules
###
# Get the attribute value for the attribute +name+
def [] name
- attr_name = 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
+ get name.to_s
end
###

0 comments on commit 28f920b

Please sign in to comment.