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

Try harder to get charset from meta elements #835

Merged
merged 2 commits into from
Jun 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/org/jsoup/helper/DataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.jsoup.nodes.Element;
import org.jsoup.nodes.XmlDeclaration;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;

import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down Expand Up @@ -101,16 +102,20 @@ static Document parseByteData(ByteBuffer byteData, String charsetName, String ba
// look for <meta http-equiv="Content-Type" content="text/html;charset=gb2312"> or HTML5 <meta charset="gb2312">
docData = Charset.forName(defaultCharset).decode(byteData).toString();
doc = parser.parseInput(docData, baseUri);
Element meta = doc.select("meta[http-equiv=content-type], meta[charset]").first();
Elements metaElements = doc.select("meta[http-equiv=content-type], meta[charset]");
String foundCharset = null; // if not found, will keep utf-8 as best attempt
if (meta != null) {
for (Element meta : metaElements) {
if (meta.hasAttr("http-equiv")) {
foundCharset = getCharsetFromContentType(meta.attr("content"));
}
if (foundCharset == null && meta.hasAttr("charset")) {
foundCharset = meta.attr("charset");
}
if (foundCharset != null) {
break;
}
}

// look for <?xml encoding='ISO-8859-1'?>
if (foundCharset == null && doc.childNodeSize() > 0 && doc.childNode(0) instanceof XmlDeclaration) {
XmlDeclaration prolog = (XmlDeclaration) doc.childNode(0);
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/jsoup/helper/DataUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ public void wrongMetaCharsetFallback() {
}
}

@Test
public void secondMetaElementWithContentTypeContainsCharsetParameter() throws Exception {
ByteBuffer inBuffer = ByteBuffer.wrap(("<html><head>" +
"<meta http-equiv=\"Content-Type\" content=\"text/html\">" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=euc-kr\">" +
"</head><body>한국어</body></html>").getBytes("euc-kr"));

Document doc = DataUtil.parseByteData(inBuffer, null, "http://example.com", Parser.htmlParser());

assertEquals("한국어", doc.body().text());
}

@Test
public void firstMetaElementWithCharsetShouldBeUsedForDecoding() throws Exception {
ByteBuffer inBuffer = ByteBuffer.wrap(("<html><head>" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=koi8-u\">" +
"</head><body>Übergrößenträger</body></html>").getBytes("iso-8859-1"));

Document doc = DataUtil.parseByteData(inBuffer, null, "http://example.com", Parser.htmlParser());

assertEquals("Übergrößenträger", doc.body().text());
}

@Test
public void supportsBOMinFiles() throws IOException {
// test files from http://www.i18nl10n.com/korean/utftest/
Expand Down