Search code examples
javajsoup

set outputsettings for cloned elements in Jsoup


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

package test;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

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
    }
}

The issue is that, for inline elements, this makes a difference. for instance, if I get

<body>
 <div style="display:inline;">
  test
 </div>
 <div style="display:inline;">
  test
 </div>
</body>

instead of

<div style="display:inline-block;">test</div><div style="display:inline-block;">test</div>

I get extra spaces, which is wrong.


Solution

  • I think that I got the trick:

        Element elt = Jsoup.parse(elementToBeCloned).body().child(0);
        elt.ownedDocument().outputSettings(elementToBeCloned.ownedDocument().outputSettings().clone());
    

    The idea is to make a new parsing to generate a new Document, and then clone the OutputSettings. No idea why I didn't think about it.