Skip to content

Commit

Permalink
Fix the failing test that was added by PR #726 related to issue #712.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvshahid committed Oct 21, 2012
1 parent 0b07e6e commit 7e9b209
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
38 changes: 34 additions & 4 deletions ext/java/nokogiri/XmlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import nokogiri.internals.HtmlDomParserContext;
Expand Down Expand Up @@ -1002,7 +1003,7 @@ public IRubyObject namespace(ThreadContext context) {
NokogiriNamespaceCache nsCache = xmlDocument.getNamespaceCache();
String prefix = node.getPrefix();
XmlNamespace namespace = nsCache.get(prefix == null ? "" : prefix, node.getNamespaceURI());
if (namespace == null || ((XmlNamespace) namespace).isEmpty()) {
if (namespace == null || namespace.isEmpty()) {
return context.getRuntime().getNil();
}

Expand All @@ -1025,10 +1026,10 @@ public IRubyObject namespace_definitions(ThreadContext context) {
if (doc instanceof HtmlDocument) return namespace_definitions;
List<XmlNamespace> namespaces = ((XmlDocument)doc).getNamespaceCache().get(node);
for (XmlNamespace namespace : namespaces) {
((RubyArray)namespace_definitions).append(namespace);
namespace_definitions.append(namespace);
}

return (RubyArray) namespace_definitions;
return namespace_definitions;
}

/**
Expand Down Expand Up @@ -1162,13 +1163,42 @@ public IRubyObject set(ThreadContext context, IRubyObject rbkey, IRubyObject rbv
String key = rubyStringToString(rbkey);
String val = rubyStringToString(rbval);
Element element = (Element) node;
element.setAttribute(key, val);

int colonIndex = key.indexOf(":");
if (colonIndex > 0) {
String prefix = key.substring(0, colonIndex);
String uri = null;
if (prefix.equals("xml")) {
uri = "http://www.w3.org/XML/1998/namespace";
} else {
uri = findNamespaceHref(context, prefix);
}
element.setAttributeNS(uri, key, val);
} else {
element.setAttribute(key, val);
}
return this;
} else {
return rbval;
}
}

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();
while(iterator.hasNext()) {
XmlNamespace namespace = (XmlNamespace) iterator.next();
if (namespace.getPrefix().equals(prefix)) {
return namespace.getHref();
}
}
currentNode = (XmlNode) currentNode.parent(context);
}
return null;
}

@JRubyMethod
public IRubyObject parent(ThreadContext context) {
/*
Expand Down
8 changes: 7 additions & 1 deletion test/xml/test_node_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,22 @@ def test_prefixed_attributes
end

def test_set_prefixed_attributes
doc = Nokogiri::XML "<root />"
doc = Nokogiri::XML %Q{<root xmlns:foo="x"/>}

node = doc.root

node['xml:lang'] = 'en-GB'
node['foo:bar'] = 'bazz'

assert_equal 'en-GB', node['xml:lang']
assert_equal 'en-GB', node.attributes['lang'].value
assert_equal nil, node['lang']
assert_equal 'http://www.w3.org/XML/1998/namespace', node.attributes['lang'].namespace.href

assert_equal 'bazz', node['foo:bar']
assert_equal 'bazz', node.attributes['bar'].value
assert_equal nil, node['bar']
assert_equal 'x', node.attributes['bar'].namespace.href
end

def test_namespace_key?
Expand Down

0 comments on commit 7e9b209

Please sign in to comment.