Conditionally upgrade HTML to UTF8

pull/199/head
M66B 3 years ago
parent 62a501298b
commit 697bda090b

@ -1876,7 +1876,6 @@ public class MessageHelper {
if ((TextUtils.isEmpty(charset) || charset.equalsIgnoreCase(StandardCharsets.US_ASCII.name()))) if ((TextUtils.isEmpty(charset) || charset.equalsIgnoreCase(StandardCharsets.US_ASCII.name())))
charset = null; charset = null;
if (h.isPlainText()) {
Charset cs = null; Charset cs = null;
try { try {
if (charset != null) if (charset != null)
@ -1884,6 +1883,7 @@ public class MessageHelper {
} catch (UnsupportedCharsetException ignored) { } catch (UnsupportedCharsetException ignored) {
} }
if (h.isPlainText()) {
if (charset == null || StandardCharsets.ISO_8859_1.equals(cs)) { if (charset == null || StandardCharsets.ISO_8859_1.equals(cs)) {
Charset detected = CharsetHelper.detect(result); Charset detected = CharsetHelper.detect(result);
if (StandardCharsets.ISO_8859_1.equals(cs) && if (StandardCharsets.ISO_8859_1.equals(cs) &&
@ -1907,27 +1907,28 @@ public class MessageHelper {
result = HtmlHelper.flow(result); result = HtmlHelper.flow(result);
result = "<div x-plain=\"true\">" + HtmlHelper.formatPre(result) + "</div>"; result = "<div x-plain=\"true\">" + HtmlHelper.formatPre(result) + "</div>";
} else if (h.isHtml()) { } else if (h.isHtml()) {
if (charset == null) { // Conditionally upgrade to UTF8
if (CharsetHelper.isUTF8(result)) if ((cs == null ||
StandardCharsets.US_ASCII.equals(cs) ||
StandardCharsets.ISO_8859_1.equals(cs)) &&
CharsetHelper.isUTF8(result))
result = new String(result.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); result = new String(result.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
} else {
// Fix incorrect UTF16 // Fix incorrect UTF16
try { try {
Charset c = Charset.forName(charset); if (CHARSET16.contains(cs)) {
if (CHARSET16.contains(c)) {
Charset detected = CharsetHelper.detect(result); Charset detected = CharsetHelper.detect(result);
if (!CHARSET16.contains(detected)) if (!CHARSET16.contains(detected))
Log.e(new Throwable("Charset=" + c + " detected=" + detected)); Log.e(new Throwable("Charset=" + cs + " detected=" + detected));
if (StandardCharsets.US_ASCII.equals(detected) || if (StandardCharsets.US_ASCII.equals(detected) ||
StandardCharsets.UTF_8.equals(detected)) { StandardCharsets.UTF_8.equals(detected)) {
charset = null; charset = null;
result = new String(result.getBytes(c), detected); result = new String(result.getBytes(cs), detected);
} }
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
}
if (charset == null) { if (charset == null) {
// <meta charset="utf-8" /> // <meta charset="utf-8" />

Loading…
Cancel
Save