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

Nokogiri::XML::Node#namespace= cannot set a namespace without a prefix on JRuby #648

Closed
benlangfeld opened this issue Apr 1, 2012 · 10 comments

Comments

@benlangfeld
Copy link
Contributor

require 'nokogiri'

describe Nokogiri::XML::Node do
  let(:node) { Nokogiri::XML.parse('<foo xmlns="http://bar.com"/>').root }

  subject { Nokogiri::XML::Node.new 'foo', node.document }

  describe '#namespace=' do
    it 'should set a namespace which does not have a prefix' do
      subject.namespace = node.namespace
      ns = subject.namespace
      ns.should be_a Nokogiri::XML::Namespace
      ns.prefix.should be == nil
      ns.href.should be == "http://bar.com"
    end
  end
end

1.9.3:

{11:31}[ruby-1.9.3]~/Desktop ben% be rspec spec.rb
.

Finished in 0.00421 seconds
1 example, 0 failures

JRuby 1.6.7:

{11:31}[jruby-1.6.7]~/Desktop ben% be rspec spec.rb
F

Failures:

  1) Nokogiri::XML::Node#set_namespace should set a namespace which does not have a prefix
     Failure/Error: subject.namespace = node.namespace
     TypeError:
       can't convert nil into String
     # nokogiri/XmlNode.java:1163:in `set_namespace'
     # /Users/ben/code/ruby/nokogiri/lib/nokogiri/xml/node.rb:700:in `namespace='
     # ./spec.rb:10:in `(root)'

Finished in 0.209 seconds
1 example, 1 failure

Failed examples:

rspec ./spec.rb:9 # Nokogiri::XML::Node#set_namespace should set a namespace which does not have a prefix
bundle exec rspec spec.rb  26.79s user 1.18s system 243% cpu 11.499 total
@benlangfeld
Copy link
Contributor Author

Changing the line referenced in XmlNode.java:1163 from

String prefix = rubyStringToString(ns.prefix(context));

to

String prefix = ns.prefix(context).isNil() ? "" : rubyStringToString(ns.prefix(context));

gets this further, but results in this failure instead:

  1) Nokogiri::XML::Node#namespace= should set a namespace which does not have a prefix
     Failure/Error: Unable to find matching line from backtrace
     Java::OrgW3cDom::DOMException:
       NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
     # org.apache.xerces.dom.CoreDocumentImpl.checkNamespaceWF(Unknown Source)
     # org.apache.xerces.dom.ElementNSImpl.setName(Unknown Source)
     # org.apache.xerces.dom.ElementNSImpl.rename(Unknown Source)
     # org.apache.xerces.dom.CoreDocumentImpl.renameNode(Unknown Source)
     # nokogiri.XmlNode.set_namespace(XmlNode.java:1168)

@yokolet
Copy link
Member

yokolet commented Apr 1, 2012

Hello again!

As you found, the bug was exactly there.
I changed to return null when prefix is nil. Returning null will be treated better than empty string in later processing.

I pushed the change, rev. 599a2af .
Probably, the commit fixes the problem.

@benlangfeld
Copy link
Contributor Author

That's fixed the first layer of the issue in the same way that my last comment suggested, but it reveals another more complex problem, also indicated in my last comment. I'm not sure what the correct approach is to solve this 2nd layer.

@yokolet
Copy link
Member

yokolet commented Apr 1, 2012

Let me clarify. You are talking this error?

Java::OrgW3cDom::DOMException:
       NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

Or, do you have other error(s)?

After the change in rev. 599a2af , the spec above passed. I didn't get the error. If you have more specs that don't pass, would you share those?

@benlangfeld
Copy link
Contributor Author

That spec raises the NAMESPACE_ERR. Could it be an issue with my version of Xerces?

@benlangfeld
Copy link
Contributor Author

I appear to have Xerces-J 2.6.2, bundled with the Apple JVM. Is this too old? How would I link Nokogiri against a newer version?

@benlangfeld
Copy link
Contributor Author

Ok, it turns out Xerces wasn't to blame, but the fact that I missed the vital issue here:

{23:47}[ruby-1.9.3]~/code/ruby/nokogiri@master✗✗✗ ben% be rake compile
install -c tmp/x86_64-darwin10.8.0/nokogiri/1.9.3/nokogiri.bundle lib/nokogiri/nokogiri.bundle

I consider myself appropriately shamed. Closing :)

@yokolet
Copy link
Member

yokolet commented Apr 1, 2012

Good to know.

Yes, we need "rake compile" after the change.
I'm relived my change fixed the issue.

@benlangfeld
Copy link
Contributor Author

I had done a 'rake compile', however I did it on 1.9.3, and didn't notice the lack of an updated nokogiri.jar. Thanks for the fix as always.

@yokolet
Copy link
Member

yokolet commented Apr 1, 2012

Ah, I see. That happens.

I always use two terminals, one for pure Java (ocean color), the other for libxml (white). This is easy to recognize what I've done for two Nokogiris. Also, it is easy to check what has been done for each Nokogiri from command history.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants