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

Impossible to specify output settings for cloned elements #763

Closed
Sharcoux opened this issue Sep 24, 2016 · 3 comments
Closed

Impossible to specify output settings for cloned elements #763

Sharcoux opened this issue Sep 24, 2016 · 3 comments
Milestone

Comments

@Sharcoux
Copy link

Sharcoux commented Sep 24, 2016

Cloned elements don't have an owner Document, which makes me unable to set output settings for these elements, and that leads to some issues in my app. Try this:

public class Main {
    public static void main(String[] args) {
        Document doc = Jsoup.parse("<div></div>");
        doc.outputSettings(new Document.OutputSettings().prettyPrint(false));
        Element e = doc.body();
        Element copy = e.clone();
        System.out.println(e.outerHtml());//returns html not pretty printed
        System.out.println(copy.outerHtml());//returns html pretty printed
        System.out.println(copy.ownerDocument());//returns null
    }
}

Of course, here it doesn't seem important, but what if you have 2 div with "display:inline-block" side by side ? The pretty printed html would render with an extra space between the 2 divs which is not the expected behaviour.

Shouldn't the outputSettings being copied to the cloned element ? Or the owner document being cloned as well as the cloned element ? Or at least, shouldn't we be provided a function to set outputSetting directly to an Element ? The owner document could be generated at that moment if needed ?

@mct10
Copy link

mct10 commented May 16, 2021

Hi, I want to have a try on solving this.

@jhy
Copy link
Owner

jhy commented Jul 9, 2021

My suggestion would be to clone the parent Owner Document and attach that to the clone (instead of adding a Document OutputSetting to every element)

@jhy jhy added the improvement label Jul 9, 2021
@jhy jhy closed this as completed in 2b6041f Dec 27, 2021
@jhy jhy added this to the 1.15.1 milestone Dec 27, 2021
@jhy
Copy link
Owner

jhy commented Dec 27, 2021

Thanks for the suggestion - fixed! Only took a few years to get to ;)

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

3 participants