Navigation Menu

Skip to content

Commit

Permalink
do not throw an error if the namespace uri cannot be found. related t…
Browse files Browse the repository at this point in the history
…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
jvshahid committed Jan 20, 2013
1 parent 7d3506c commit 45e3a00
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
11 changes: 8 additions & 3 deletions ext/java/nokogiri/XmlNode.java
Expand Up @@ -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);
}
Expand All @@ -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;
}
Expand Down
17 changes: 17 additions & 0 deletions test/xml/test_builder.rb
Expand Up @@ -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(
Expand Down

0 comments on commit 45e3a00

Please sign in to comment.