Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #843 #1327

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions ext/java/nokogiri/XmlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1074,12 +1074,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
16 changes: 10 additions & 6 deletions ext/nokogiri/xml_namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,24 @@ static VALUE href(VALUE self)
VALUE Nokogiri_wrap_xml_namespace(xmlDocPtr doc, xmlNsPtr node)
{
VALUE ns, document, node_cache;
nokogiriTuplePtr node_has_a_document;

assert(doc->_private);
if (doc->type == XML_DOCUMENT_FRAG_NODE) doc = doc->doc;
node_has_a_document = DOC_RUBY_OBJECT_TEST(doc);

if(node->_private)
if(node->_private && node_has_a_document)
return (VALUE)node->_private;

ns = Data_Wrap_Struct(cNokogiriXmlNamespace, 0, 0, node);

document = DOC_RUBY_OBJECT(doc);
if (doc->_private) {
document = DOC_RUBY_OBJECT(doc);

node_cache = rb_iv_get(document, "@node_cache");
rb_ary_push(node_cache, ns);
node_cache = rb_iv_get(document, "@node_cache");
rb_ary_push(node_cache, ns);

rb_iv_set(ns, "@document", DOC_RUBY_OBJECT(doc));
rb_iv_set(ns, "@document", DOC_RUBY_OBJECT(doc));
}

node->_private = (void *)ns;

Expand Down
19 changes: 19 additions & 0 deletions test/test_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,25 @@ def test_ns_uri
reader.map { |n| n.namespace_uri })
end

def test_namespaced_attributes
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
<x xmlns:edi='http://ecommerce.example.org/schema' xmlns:commons="http://rets.org/xsd/RETSCommons">
<edi:foo commons:street-number="43">hello</edi:foo>
<y edi:name="francis" bacon="87"/>
</x>
eoxml
attr_ns = []
while reader.read
if reader.node_type == Nokogiri::XML::Node::ELEMENT_NODE
reader.attribute_nodes.each {|attr| attr_ns << (attr.namespace.nil? ? nil : attr.namespace.prefix) }
end
end
assert_equal(['commons',
'edi',
nil],
attr_ns)
end

def test_local_name
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
<x xmlns:edi='http://ecommerce.example.org/schema'>
Expand Down