Permalink
Browse files

Fix the failing test that was added by PR #726 related to issue #712.

  • Loading branch information...
1 parent 0b07e6e commit 7e9b209d83c845c1c9e51246a1d70517298737b7 @jvshahid jvshahid committed Oct 21, 2012
Showing with 41 additions and 5 deletions.
  1. +34 −4 ext/java/nokogiri/XmlNode.java
  2. +7 −1 test/xml/test_node_attributes.rb
@@ -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;
@@ -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();
}
@@ -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;
}
/**
@@ -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) {
/*
@@ -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?

0 comments on commit 7e9b209

Please sign in to comment.