jsoup icon indicating copy to clipboard operation
jsoup copied to clipboard

parse issue: self closing tag not parsed correctly

Open xhite opened this issue 11 years ago • 8 comments

after parsing: toString() should return or

but returns with no closing tag

xhite avatar Dec 24 '14 15:12 xhite

For a quick workaround you can use the XML-Parser (if possible):

String html = "<img src=\"image.jpg\"/>";

Document doc = Jsoup.parse(html, "", Parser.xmlParser());
System.out.println(doc);

In Html the img-tag is not required to have an end tag (on XHtml it is):

In HTML the tag has no end tag. In XHTML the tag must be properly closed.

http://www.w3schools.com/tags/tag_img.asp

offa avatar Dec 27 '14 17:12 offa

Same things happens with <link> tag Example: <link type='text/css' href='static/css/main.css' rel='stylesheet' /> is parsed to: <link type='text/css' href='static/css/main.css' rel='stylesheet'>

It might be the case that the HTML file doesn't have the doctype tag informing that it should be parsed as XHTML or HTML5 file.

For others having this issue it was working in 1.7.3 version. I couldn't find the link to jar but it is still on their server :) http://jsoup.org/packages/jsoup-1.7.3.jar

nowNick avatar Dec 31 '14 09:12 nowNick

I can confirm that this bug doesn`t occur with 1.7.3 This version is still available in Maven Central.

martinmatthias avatar Mar 19 '15 08:03 martinmatthias

I don't really understand this bug. There's no parsing issue. And the serialization is correct. HTML does not use self closing tags, or have empty tags needing a closing tag. Those are hang-overs from the bad old XHTML days. If you don't want HTML output serialization, use the XML serializater.

jhy avatar Mar 30 '15 19:03 jhy

All you want to get is just well-formed xhtml (like it does in 1.7.3) with latest Jsoup 1.8.2, then parse two times.

// first parse and clean up raw html
Document saneHtml = Jsoup.parse(htmlTxt);

// optionally perform whitelist
Whitelist whitelist = Whitelist.relaxed();
whitelist.addTags("style");
whitelist.addAttributes(":all", "style");
whitelist.addAttributes(":all", "class");
whitelist.addProtocols("img", "src", "data"); //for base64 encoded images
String saneHtmlTxt = Jsoup.clean(saneHtml.toString(), whitelist);

// then make well-formed xhtml and closing tags with xmlParser
saneHtml = Jsoup.parse(saneHtmlTxt, "", Parser.xmlParser()); 
saneHtml.outputSettings().escapeMode(Entities.EscapeMode.xhtml);

System.out.println(saneHtml.toString());

victorskl avatar Apr 22 '15 00:04 victorskl

jsoup is HTML 5. HTML 5 does not do self closing tags. There's no need in HTML for that.

If you want old-school syntax, that is what Document.OutputSettings.xml is for.

http://jsoup.org/apidocs/org/jsoup/nodes/Document.OutputSettings.html#syntax(org.jsoup.nodes.Document.OutputSettings.Syntax)

public Document.OutputSettings syntax(Document.OutputSettings.Syntax syntax) Set the document's output syntax. Either html, with empty tags and boolean attributes (etc), or xml, with self-closing tags.

jhy avatar Apr 22 '15 03:04 jhy

Thanks jhy. Yes, setting output syntax to xml works.

Document saneHtml = Jsoup.parse(htmlTxt);
saneHtml.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
saneHtml.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
System.out.println(saneHtml.toString());

victorskl avatar Apr 22 '15 04:04 victorskl

Cool, thanks. I thought at one point I'd updated try.jsoup.org to have that syntax option, to make it more visible. Guess I just dreamt that :) I should add in most of the output settings.

jhy avatar Apr 22 '15 05:04 jhy