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

clone & singleton classes #316

Closed
marcandre opened this issue Jul 26, 2010 · 6 comments · Fixed by #3117
Closed

clone & singleton classes #316

marcandre opened this issue Jul 26, 2010 · 6 comments · Fixed by #3117
Milestone

Comments

@marcandre
Copy link

If a specific Nokogiri::HTML::Document extends a module M, a clone of that document won't.

I expected otherwise, since in Ruby all builtin classes, as well as user created classes will copy the singleton_class when cloning.

Thakns!

require 'nokogiri'
require 'open-uri'

module M; def foo; end end

doc = Nokogiri::HTML(open('http://www.google.com/search?q=tenderlove'))
doc.extend M
doc.clone.respond_to? :foo # => false, should be true

@flavorjones
Copy link
Member

Fair enough, will fix.

@flavorjones
Copy link
Member

Well, here's a question for you: Nokogiri documents already follow "nontraditional" clone/dup semantics by copying the underlying document tree.

So: can you explain your use case for extending an object and then cloning it? I'm just trying to wrap my head around how this idiom is being used with Nokogiri objects.

@marcandre
Copy link
Author

I coded up some parsing methods that apply for any web site; the methods are in a module which I include in Nokogiri document. Later on I needed some custom parsing for some specific domains. The easy way out was to extend the document with the module specific for the domain. Different parsing need to be done and tend to be destructive so I cloned the document before calling the different parsing methods.

There are other solutions; I can subclass Document, have the methods be module functions and pass the document, create a sort of delegate class and sub class that, extend all clones, etc...

But since cloning should copy the singleton class, I thought you might want to fix it :-)
It's clearly not a big priority!

@flavorjones
Copy link
Member

Thanks for going into detail. I'm always interested in what people are doing with nokogiri (well, almost always ;)).

LIke I said, this is a fair enough request, I don't have a problem with it. We'll schedule work, and probably target 1.5.

@flavorjones
Copy link
Member

Picked up this work yesterday, see progress in #3117

"Better late than never" as they say 😅

flavorjones added a commit that referenced this issue Jan 31, 2024
flavorjones added a commit that referenced this issue Feb 1, 2024
flavorjones added a commit that referenced this issue Feb 1, 2024
flavorjones added a commit that referenced this issue Feb 1, 2024
flavorjones added a commit that referenced this issue Feb 1, 2024
flavorjones added a commit that referenced this issue Feb 1, 2024
flavorjones added a commit that referenced this issue Feb 1, 2024
flavorjones added a commit that referenced this issue Feb 1, 2024
flavorjones added a commit that referenced this issue Feb 1, 2024
flavorjones added a commit that referenced this issue Feb 1, 2024
flavorjones added a commit that referenced this issue Feb 1, 2024
flavorjones added a commit that referenced this issue Feb 1, 2024
flavorjones added a commit that referenced this issue Feb 1, 2024
flavorjones added a commit that referenced this issue Feb 1, 2024
flavorjones added a commit that referenced this issue Feb 2, 2024
**What problem is this PR intended to solve?**

Fixes #316

Classes this PR updates:

- [x] `XML::Node`
- [x] `XML::Document`
- [x] `XML::NodeSet`

**Have you included adequate test coverage?**

Yes.


**Does this change affect the behavior of either the C or the Java
implementations?**

The fix applies to both implementations.
@flavorjones flavorjones modified the milestones: v2.0.0, v1.17.0 Jun 24, 2024
flavorjones added a commit that referenced this issue Dec 12, 2024
Back in b92660e (#1834 fixing #1063) I omitted support in JRuby for
the "new_parent_document" argument to `Node#dup` because there was no
performance reason to implement it. So the test was skipped.

However, in 1e7d38a and other commits in #3117 (fixing #316), I
introduced a call to `initialize_copy_with_args` that passes the new
parent document as an argument on both CRuby and JRuby
implementations. Because the test was skipped, I didn't catch that
this broke on JRuby.

In particular this was a problem for Loofah which relies on
decorators, and even more particularly this broke the
`Loofah::TextBehavior` formatting concern for
`Loofah::*::DocumentFragment` objects.

Maybe we should be running downstream tests with JRuby, too? But that
feels like a big investment right now so I'll avoid scarring on the
first cut, and wait to see if it happens again.
flavorjones added a commit that referenced this issue Dec 12, 2024
Back in b92660e (#1834 fixing #1063) I omitted support in JRuby for
the "new_parent_document" argument to `Node#dup` because there was no
performance reason to implement it. So the test was skipped.

However, in 1e7d38a and other commits in #3117 (fixing #316), I
introduced a call to `initialize_copy_with_args` that passes the new
parent document as an argument on both CRuby and JRuby
implementations. Because the test was skipped, I didn't catch that
this broke on JRuby.

In particular this was a problem for Loofah which relies on
decorators, and even more particularly this broke the
`Loofah::TextBehavior` formatting concern for
`Loofah::*::DocumentFragment` objects.

Maybe we should be running downstream tests with JRuby, too? But that
feels like a big investment right now so I'll avoid scarring on the
first cut, and wait to see if it happens again.

(cherry picked from commit dda0be2)
flavorjones added a commit that referenced this issue Dec 12, 2024
**What problem is this PR intended to solve?**

Back in b92660e (#1834 fixing #1063) I omitted support in JRuby for the
"new_parent_document" argument to `Node#dup` because there was no
performance reason to implement it. So the test was skipped.

However, in 1e7d38a and other commits in #3117 (fixing #316), I
introduced a call to `initialize_copy_with_args` that passes the new
parent document as an argument on both CRuby and JRuby implementations.
Because the test was skipped, I didn't catch that this broke on JRuby.

In particular this was a problem for Loofah which relies on decorators,
and even more particularly this broke the `Loofah::TextBehavior`
formatting concern for `Loofah::*::DocumentFragment` objects.


**Have you included adequate test coverage?**

The skipped test is no longer skipped!

Maybe we should be running downstream tests with JRuby, too? But that
feels like a big investment right now so I'll avoid scarring on the
first cut, and wait to see if it happens again.


**Does this change affect the behavior of either the C or the Java
implementations?**

Brings the Java impl into agreement with the C impl.
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

Successfully merging a pull request may close this issue.

2 participants