Skip to content

Commit

Permalink
Merge branch 'pr-1327'
Browse files Browse the repository at this point in the history
Close #1327 and Fixes #843
  • Loading branch information
jvshahid committed Dec 9, 2015
2 parents 7b32758 + d81ce3b commit ed0587d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
18 changes: 15 additions & 3 deletions ext/java/nokogiri/XmlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1082,12 +1082,24 @@ 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 namespaceURI = node.getNamespaceURI();
if (namespaceURI == null || namespaceURI == "") {
return runtime.getNil();
}

String prefix = node.getPrefix();
XmlNamespace namespace = nsCache.get(prefix == null ? "" : prefix, node.getNamespaceURI());
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 = new XmlNamespace(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Namespace"));
IRubyObject rubyPrefix = NokogiriHelpers.stringOrNil(runtime, prefix);
IRubyObject rubyUri = NokogiriHelpers.stringOrNil(runtime, namespaceURI);
namespace.init(null, rubyPrefix, rubyUri, doc);
}

return namespace;
Expand Down
15 changes: 8 additions & 7 deletions ext/nokogiri/xml_namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,20 @@ VALUE Nokogiri_wrap_xml_namespace(xmlDocPtr doc, xmlNsPtr node)
{
VALUE ns, document, node_cache;

assert(doc->_private);
if (node->_private) return (VALUE)node->_private;

if(node->_private)
return (VALUE)node->_private;
if (doc->type == XML_DOCUMENT_FRAG_NODE) doc = doc->doc;

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

0 comments on commit ed0587d

Please sign in to comment.