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

Replacing a Node from another document will crash on exit #162

Closed
ration opened this issue Nov 12, 2009 · 5 comments
Closed

Replacing a Node from another document will crash on exit #162

ration opened this issue Nov 12, 2009 · 5 comments

Comments

@ration
Copy link

ration commented Nov 12, 2009

If you replace a node from another document, there is a crash on exit. A Node is probably referenced from both documents and tried to delete twice.

Example:

!/usr/bin/env ruby

require 'nokogiri'

xml1 = "

Original caption " xml2 = " Replacement caption " doc1 = Nokogiri::XML(xml1) doc2 = Nokogiri::XML(xml2) caption1 = doc1.xpath("//caption")[0] caption2 = doc2.xpath("//caption")[0] caption1.replace(caption2)
@flavorjones
Copy link
Member

I believe what ration meant to say was:

#!/usr/bin/env ruby

require 'nokogiri'

xml1 = "<test>
  <caption>Original caption</caption>
</test>"
xml2 = "<test>
  <caption>Replacement caption</caption>
</test>"
doc1 = Nokogiri::XML(xml1)
doc2 = Nokogiri::XML(xml2)
caption1 = doc1.xpath("//caption")[0]
caption2 = doc2.xpath("//caption")[0]
caption1.replace(caption2)

@flavorjones
Copy link
Member

Node#replace reimplemented using reparent_node_with. Fixed by ebd4483

@ration
Copy link
Author

ration commented Nov 17, 2009

Ah yes, I didn't notice that the markdown broke the tags..

With this fix, doc2 will not contain the caption element. Is this the preferred behavior?
Personally I would assume the node is copied there, and something like "move" would actually move it.

@flavorjones
Copy link
Member

yes, this is the preferred behavior. if you want to copy the node, copy it:
caption1.replace(caption2.dup)

@ration
Copy link
Author

ration commented Nov 18, 2009

Ok, thanks for the fix!

Moving was actually what I wanted, so this is perfect. I might not be the only one who doesn't automatically see this behavior, so maybe the documentation for replace deserves a comment about this side effect for the originating doc.

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

No branches or pull requests

2 participants