Permalink
Browse files

do not throw an error if the namespace uri cannot be found. related t…

…o issue #814.

Currently there's some inconsistency between MRI and JRuby. See
test_use_namespace_defined_in_parent for explanation of that difference.
  • Loading branch information...
1 parent 7d3506c commit 45e3a00c5fbb5c8a21296ae3dac02db5ef59d415 @jvshahid jvshahid committed Jan 18, 2013
Showing with 25 additions and 3 deletions.
  1. +8 −3 ext/java/nokogiri/XmlNode.java
  2. +17 −0 test/xml/test_builder.rb
@@ -1192,7 +1192,10 @@ public IRubyObject set(ThreadContext context, IRubyObject rbkey, IRubyObject rbv
} else {
uri = findNamespaceHref(context, prefix);
}
- element.setAttributeNS(uri, key, val);
+ if (uri != null)
+ element.setAttributeNS(uri, key, val);
+ else
+ element.setAttribute(key, val);
} else {
element.setAttribute(key, val);
}
@@ -1206,14 +1209,16 @@ private String findNamespaceHref(ThreadContext context, String prefix) {
XmlNode currentNode = this;
while(currentNode != document(context)) {
RubyArray namespaces = (RubyArray) currentNode.namespace_scopes(context);
- Iterator iterator = namespaces.iterator();
+ Iterator<?> iterator = namespaces.iterator();
while(iterator.hasNext()) {
XmlNamespace namespace = (XmlNamespace) iterator.next();
if (namespace.getPrefix().equals(prefix)) {
return namespace.getHref();
}
}
- currentNode = (XmlNode) currentNode.parent(context);
+ IRubyObject parent = currentNode.parent(context);
+ if (parent.isNil()) return null;
+ currentNode = (XmlNode) parent;
}
return null;
}
View
@@ -111,6 +111,23 @@ def test_specify_namespace
assert_equal 'bar', doc.at('foo|baz', 'foo' => 'bar').namespace.href
end
+ # issue 814
+ # Java throws an exception when a namespace is specified and
+ # is used in a nested tag
+ def test_use_namespace_defined_in_parent
+ b = Nokogiri::XML::Builder.new { |xml|
+ xml.root('xmlns:a' => 'a') {
+ xml.element('a:b' => 'a:c')
+ }
+ }
+ doc = b.doc
+ # C Nokogiri will barf on the following line, the
+ # commented test will succeed in MRI but not in JRuby.
+ # The confusing part is that the attribute has no namespace ?!
+ # refute_nil doc.xpath('//element').first.attributes['a:b']
+ refute_nil doc.xpath('//element').first.attributes['b']
+ end
+
def test_dtd_in_builder_output
builder = Nokogiri::XML::Builder.new do |xml|
xml.doc.create_internal_subset(

0 comments on commit 45e3a00

Please sign in to comment.