Prevent Jsoup from throwing up

This commit is contained in:
M66B 2019-10-03 18:19:22 +02:00
parent 4afb121fbe
commit ea4c7fa8f2
7 changed files with 55 additions and 26 deletions

View File

@ -124,7 +124,6 @@ import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;
import com.google.android.material.snackbar.Snackbar;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
@ -2672,7 +2671,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
return null;
String body = Helper.readText(file);
Document document = Jsoup.parse(body);
Document document = JsoupEx.parse(body);
// Check for inline encryption
int begin = body.indexOf(Helper.PGP_BEGIN_MESSAGE);
@ -2705,7 +2704,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
// Cleanup message
String html = HtmlHelper.sanitize(context, body, text_color, show_images);
if (debug) {
Document format = Jsoup.parse(html);
Document format = JsoupEx.parse(html);
format.outputSettings().prettyPrint(true).outline(true).indentAmount(1);
String[] lines = format.html().split("\\r?\\n");
for (int i = 0; i < lines.length; i++)
@ -4225,7 +4224,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
String html = Helper.readText(file);
Document doc = Jsoup.parse(html);
Document doc = JsoupEx.parse(html);
HtmlHelper.removeViewportLimitations(doc);
HtmlHelper.embedImages(context, id, doc);

View File

@ -57,7 +57,6 @@ import com.sun.mail.util.MessageRemovedIOException;
import org.json.JSONArray;
import org.json.JSONException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import java.io.BufferedInputStream;
@ -2525,7 +2524,7 @@ class Core {
private static void fixAttachments(Context context, long id, String body) {
DB db = DB.getInstance(context);
for (Element element : Jsoup.parse(body).select("img")) {
for (Element element : JsoupEx.parse(body).select("img")) {
String src = element.attr("src");
if (src.startsWith("cid:")) {
EntityAttachment attachment = db.attachment().getAttachment(id, "<" + src.substring(4) + ">");

View File

@ -103,7 +103,6 @@ import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;
import com.google.android.material.snackbar.Snackbar;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
@ -1799,7 +1798,7 @@ public class FragmentCompose extends FragmentBase {
}
private boolean isEmpty() {
if (!TextUtils.isEmpty(Jsoup.parse(HtmlHelper.toHtml(etBody.getText())).text().trim()))
if (!TextUtils.isEmpty(JsoupEx.parse(HtmlHelper.toHtml(etBody.getText())).text().trim()))
return false;
if (rvAttachment.getAdapter().getItemCount() > 0)
return false;
@ -2330,7 +2329,7 @@ public class FragmentCompose extends FragmentBase {
boolean usenet = prefs.getBoolean("usenet_signature", false);
if (usenet) {
Document rdoc = Jsoup.parse(refText);
Document rdoc = JsoupEx.parse(refText);
List<Node> tbd = new ArrayList<>();
@ -2832,7 +2831,7 @@ public class FragmentCompose extends FragmentBase {
if (rfile.exists())
sb.append(Helper.readText(rfile));
List<String> cids = new ArrayList<>();
for (Element element : Jsoup.parse(sb.toString()).select("img")) {
for (Element element : JsoupEx.parse(sb.toString()).select("img")) {
String src = element.attr("src");
if (src.startsWith("cid:"))
cids.add("<" + src.substring(4) + ">");

View File

@ -107,7 +107,6 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.sun.mail.util.FolderClosedIOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.openintents.openpgp.OpenPgpError;
@ -3870,8 +3869,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
db.beginTransaction();
// Write decrypted body
Helper.writeText(message.getFile(context),
decrypted.toString().replace("\0", ""));
Helper.writeText(message.getFile(context), decrypted.toString());
db.message().setMessageStored(id, new Date().getTime());
@ -3894,8 +3892,6 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
// Write decrypted body
String html = parts.getHtml(context);
if (html != null)
html = html.replace("\0", "");
Helper.writeText(message.getFile(context), html);
// Remove previously decrypted attachments
@ -4336,7 +4332,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
return null;
String html = Helper.readText(file);
Document document = Jsoup.parse(html);
Document document = JsoupEx.parse(html);
HtmlHelper.embedImages(context, id, document);
Element body = document.body();

View File

@ -49,7 +49,6 @@ import androidx.core.text.HtmlCompat;
import androidx.core.util.PatternsCompat;
import androidx.preference.PreferenceManager;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
@ -98,7 +97,7 @@ public class HtmlHelper {
Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
static String sanitize(Context context, String html, boolean text_color, boolean show_images) {
Document parsed = Jsoup.parse(html);
Document parsed = JsoupEx.parse(html);
// <html xmlns:v="urn:schemas-microsoft-com:vml"
// xmlns:o="urn:schemas-microsoft-com:office:office"
@ -815,7 +814,7 @@ public class HtmlHelper {
if (body == null)
return null;
String text = Jsoup.parse(body).text();
String text = JsoupEx.parse(body).text();
String preview = text.substring(0, Math.min(text.length(), PREVIEW_SIZE));
if (preview.length() < text.length())
@ -897,7 +896,7 @@ public class HtmlHelper {
sb.append(' ');
nl = true;
}
}, Jsoup.parse(html));
}, JsoupEx.parse(html));
sb.append("\n");
@ -999,7 +998,7 @@ public class HtmlHelper {
String html = HtmlCompat.toHtml(spanned, TO_HTML_PARAGRAPH_LINES_CONSECUTIVE);
// @Google: why convert size to and from in a different way?
Document doc = Jsoup.parse(html);
Document doc = JsoupEx.parse(html);
for (Element element : doc.select("span")) {
String style = element.attr("style");
if (style.startsWith("font-size:")) {

View File

@ -0,0 +1,40 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class JsoupEx {
static Document parse(String html) {
/*
org.jsoup.UncheckedIOException: java.io.IOException: Input is binary and unsupported
at org.jsoup.parser.CharacterReader.<init>(SourceFile:38)
at org.jsoup.parser.CharacterReader.<init>(SourceFile:43)
at org.jsoup.parser.TreeBuilder.initialiseParse(SourceFile:38)
at org.jsoup.parser.HtmlTreeBuilder.initialiseParse(SourceFile:65)
at org.jsoup.parser.TreeBuilder.parse(SourceFile:46)
at org.jsoup.parser.Parser.parse(SourceFile:107)
at org.jsoup.Jsoup.parse(SourceFile:58)
*/
return Jsoup.parse(html.replace("\0", ""));
}
}

View File

@ -32,7 +32,6 @@ import androidx.preference.PreferenceManager;
import com.sun.mail.util.FolderClosedIOException;
import com.sun.mail.util.MessageRemovedIOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.BufferedReader;
@ -310,14 +309,14 @@ public class MessageHelper {
StringBuilder body = new StringBuilder();
body.append("<html><body>");
Document mdoc = Jsoup.parse(Helper.readText(message.getFile(context)));
Document mdoc = JsoupEx.parse(Helper.readText(message.getFile(context)));
if (mdoc.body() != null)
body.append(mdoc.body().html());
// When sending message
if (identity != null) {
if (!TextUtils.isEmpty(identity.signature) && message.signature) {
Document sdoc = Jsoup.parse(identity.signature);
Document sdoc = JsoupEx.parse(identity.signature);
if (sdoc.body() != null) {
if (usenet) // https://www.ietf.org/rfc/rfc3676.txt
body.append("<span>-- <br></span>");
@ -1022,8 +1021,6 @@ public class MessageHelper {
}
// Prevent Jsoup throwing an exception
result = result.replace("\0", "");
if (part == plain) {
StringBuilder sb = new StringBuilder();
sb.append("<span>");