Skip to content

Commit

Permalink
return the namespace even if it's not in the cache
Browse files Browse the repository at this point in the history
This fixes Nokogiri::XML::Attr#namespace not working when used with XmlReader,
because namespaces never get cached in that scenario.

We can't add it to the cache at this point, because the node that defined the
namespace is not available.
  • Loading branch information
codekitchen authored and jvshahid committed Dec 9, 2015
1 parent 24fc533 commit b5c179a
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions ext/java/nokogiri/XmlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1082,12 +1082,23 @@ public IRubyObject key_p(ThreadContext context, IRubyObject rbkey) {

@JRubyMethod
public IRubyObject namespace(ThreadContext context) {
if (doc instanceof HtmlDocument) return context.getRuntime().getNil();
Ruby runtime = context.getRuntime();
if (doc instanceof HtmlDocument) return runtime.getNil();
NokogiriNamespaceCache nsCache = NokogiriHelpers.getNamespaceCacheFormNode(node);
String prefix = node.getPrefix();
XmlNamespace namespace = nsCache.get(prefix == null ? "" : prefix, node.getNamespaceURI());
String namespaceURI = node.getNamespaceURI();
if (namespaceURI == null || namespaceURI == "") {
return runtime.getNil();
}

XmlNamespace namespace = nsCache.get(prefix == null ? "" : prefix, namespaceURI);
if (namespace == null || namespace.isEmpty()) {
return context.getRuntime().getNil();
// if it's not in the cache, create an unowned, uncached namespace and
// return that. XmlReader can't insert namespaces into the cache, so
// this is necessary for XmlReader to work correctly.
namespace =
(XmlNamespace) NokogiriService.XML_NAMESPACE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Namespace"));
namespace.init(null, RubyString.newString(runtime, prefix), RubyString.newString(runtime, namespaceURI), doc);
}

return namespace;
Expand Down

0 comments on commit b5c179a

Please sign in to comment.