2018-08-02 13:33:06 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
2018-08-14 05:53:24 +00:00
|
|
|
This file is part of FairEmail.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-14 05:53:24 +00:00
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
2018-08-02 13:33:06 +00:00
|
|
|
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.
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-08-02 13:33:06 +00:00
|
|
|
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
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2020-01-05 17:32:53 +00:00
|
|
|
Copyright 2018-2020 by Marcel Bokhorst (M66B)
|
2018-08-02 13:33:06 +00:00
|
|
|
*/
|
|
|
|
|
2018-12-14 09:11:45 +00:00
|
|
|
import android.content.Context;
|
2019-03-10 18:03:05 +00:00
|
|
|
import android.content.SharedPreferences;
|
2019-10-04 21:04:03 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Canvas;
|
2019-09-24 12:46:12 +00:00
|
|
|
import android.graphics.Color;
|
2019-10-04 21:04:03 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
2019-09-13 11:46:52 +00:00
|
|
|
import android.net.Uri;
|
2019-09-07 18:13:58 +00:00
|
|
|
import android.os.Build;
|
2019-02-10 12:01:21 +00:00
|
|
|
import android.text.Html;
|
2019-08-29 19:57:04 +00:00
|
|
|
import android.text.SpannableStringBuilder;
|
2019-02-10 12:01:21 +00:00
|
|
|
import android.text.Spanned;
|
2018-12-14 09:11:45 +00:00
|
|
|
import android.text.TextUtils;
|
2019-08-29 19:57:04 +00:00
|
|
|
import android.text.style.ForegroundColorSpan;
|
2018-12-14 09:11:45 +00:00
|
|
|
import android.util.Base64;
|
|
|
|
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2019-10-04 21:04:03 +00:00
|
|
|
import androidx.core.content.ContextCompat;
|
2020-01-26 12:41:12 +00:00
|
|
|
import androidx.core.graphics.ColorUtils;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.core.text.HtmlCompat;
|
|
|
|
import androidx.core.util.PatternsCompat;
|
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
2019-03-10 19:39:17 +00:00
|
|
|
import org.jsoup.nodes.Attribute;
|
2018-08-02 13:33:06 +00:00
|
|
|
import org.jsoup.nodes.Document;
|
2018-08-28 09:10:31 +00:00
|
|
|
import org.jsoup.nodes.Element;
|
2018-08-02 13:33:06 +00:00
|
|
|
import org.jsoup.nodes.Node;
|
|
|
|
import org.jsoup.nodes.TextNode;
|
2019-03-12 09:04:16 +00:00
|
|
|
import org.jsoup.safety.Cleaner;
|
2018-08-28 12:52:33 +00:00
|
|
|
import org.jsoup.safety.Whitelist;
|
2018-08-02 13:33:06 +00:00
|
|
|
import org.jsoup.select.NodeTraversor;
|
|
|
|
import org.jsoup.select.NodeVisitor;
|
|
|
|
|
2019-10-04 21:04:03 +00:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2018-12-14 09:11:45 +00:00
|
|
|
import java.io.File;
|
2019-04-06 09:05:33 +00:00
|
|
|
import java.io.FileInputStream;
|
2018-12-14 09:11:45 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2019-09-13 11:46:52 +00:00
|
|
|
import java.util.ArrayList;
|
2019-01-05 11:17:33 +00:00
|
|
|
import java.util.Arrays;
|
2019-03-14 19:33:01 +00:00
|
|
|
import java.util.Collections;
|
2020-01-22 11:28:39 +00:00
|
|
|
import java.util.HashMap;
|
2019-01-05 11:17:33 +00:00
|
|
|
import java.util.List;
|
2019-09-23 20:07:22 +00:00
|
|
|
import java.util.Locale;
|
2018-08-05 08:53:43 +00:00
|
|
|
import java.util.regex.Matcher;
|
2019-06-05 08:23:41 +00:00
|
|
|
import java.util.regex.Pattern;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2019-03-06 10:39:10 +00:00
|
|
|
import static androidx.core.text.HtmlCompat.FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM;
|
2019-02-10 12:01:21 +00:00
|
|
|
import static androidx.core.text.HtmlCompat.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE;
|
|
|
|
|
2018-09-12 11:54:16 +00:00
|
|
|
public class HtmlHelper {
|
2019-10-20 17:35:31 +00:00
|
|
|
private static final int PREVIEW_SIZE = 500; // characters
|
2019-02-19 16:21:25 +00:00
|
|
|
|
2019-09-25 07:41:10 +00:00
|
|
|
private static final float MIN_LUMINANCE = 0.5f;
|
2019-11-14 08:15:18 +00:00
|
|
|
private static final int TAB_SIZE = 2;
|
2019-09-01 07:44:03 +00:00
|
|
|
private static final int MAX_AUTO_LINK = 250;
|
2019-08-20 18:05:11 +00:00
|
|
|
private static final int TRACKING_PIXEL_SURFACE = 25; // pixels
|
2019-05-13 09:03:15 +00:00
|
|
|
|
2019-03-14 19:33:01 +00:00
|
|
|
private static final List<String> heads = Collections.unmodifiableList(Arrays.asList(
|
|
|
|
"h1", "h2", "h3", "h4", "h5", "h6", "p", "ol", "ul", "table", "br", "hr"));
|
|
|
|
private static final List<String> tails = Collections.unmodifiableList(Arrays.asList(
|
|
|
|
"h1", "h2", "h3", "h4", "h5", "h6", "p", "ol", "ul", "li"));
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2020-01-22 11:28:39 +00:00
|
|
|
private static final HashMap<String, Integer> x11ColorMap = new HashMap<>();
|
|
|
|
|
|
|
|
static {
|
|
|
|
// https://www.w3.org/TR/css-color-3/
|
|
|
|
x11ColorMap.put("aliceblue", 0xF0F8FF);
|
|
|
|
x11ColorMap.put("antiquewhite", 0xFAEBD7);
|
|
|
|
x11ColorMap.put("aqua", 0x00FFFF);
|
|
|
|
x11ColorMap.put("aquamarine", 0x7FFFD4);
|
|
|
|
x11ColorMap.put("azure", 0xF0FFFF);
|
|
|
|
x11ColorMap.put("beige", 0xF5F5DC);
|
|
|
|
x11ColorMap.put("bisque", 0xFFE4C4);
|
|
|
|
x11ColorMap.put("black", 0x000000);
|
|
|
|
x11ColorMap.put("blanchedalmond", 0xFFEBCD);
|
|
|
|
x11ColorMap.put("blue", 0x0000FF);
|
|
|
|
x11ColorMap.put("blueviolet", 0x8A2BE2);
|
|
|
|
x11ColorMap.put("brown", 0xA52A2A);
|
|
|
|
x11ColorMap.put("burlywood", 0xDEB887);
|
|
|
|
x11ColorMap.put("cadetblue", 0x5F9EA0);
|
|
|
|
x11ColorMap.put("chartreuse", 0x7FFF00);
|
|
|
|
x11ColorMap.put("chocolate", 0xD2691E);
|
|
|
|
x11ColorMap.put("coral", 0xFF7F50);
|
|
|
|
x11ColorMap.put("cornflowerblue", 0x6495ED);
|
|
|
|
x11ColorMap.put("cornsilk", 0xFFF8DC);
|
|
|
|
x11ColorMap.put("crimson", 0xDC143C);
|
|
|
|
x11ColorMap.put("cyan", 0x00FFFF);
|
|
|
|
x11ColorMap.put("darkblue", 0x00008B);
|
|
|
|
x11ColorMap.put("darkcyan", 0x008B8B);
|
|
|
|
x11ColorMap.put("darkgoldenrod", 0xB8860B);
|
|
|
|
x11ColorMap.put("darkgray", 0xA9A9A9);
|
|
|
|
x11ColorMap.put("darkgreen", 0x006400);
|
|
|
|
x11ColorMap.put("darkgrey", 0xA9A9A9);
|
|
|
|
x11ColorMap.put("darkkhaki", 0xBDB76B);
|
|
|
|
x11ColorMap.put("darkmagenta", 0x8B008B);
|
|
|
|
x11ColorMap.put("darkolivegreen", 0x556B2F);
|
|
|
|
x11ColorMap.put("darkorange", 0xFF8C00);
|
|
|
|
x11ColorMap.put("darkorchid", 0x9932CC);
|
|
|
|
x11ColorMap.put("darkred", 0x8B0000);
|
|
|
|
x11ColorMap.put("darksalmon", 0xE9967A);
|
|
|
|
x11ColorMap.put("darkseagreen", 0x8FBC8F);
|
|
|
|
x11ColorMap.put("darkslateblue", 0x483D8B);
|
|
|
|
x11ColorMap.put("darkslategray", 0x2F4F4F);
|
|
|
|
x11ColorMap.put("darkslategrey", 0x2F4F4F);
|
|
|
|
x11ColorMap.put("darkturquoise", 0x00CED1);
|
|
|
|
x11ColorMap.put("darkviolet", 0x9400D3);
|
|
|
|
x11ColorMap.put("deeppink", 0xFF1493);
|
|
|
|
x11ColorMap.put("deepskyblue", 0x00BFFF);
|
|
|
|
x11ColorMap.put("dimgray", 0x696969);
|
|
|
|
x11ColorMap.put("dimgrey", 0x696969);
|
|
|
|
x11ColorMap.put("dodgerblue", 0x1E90FF);
|
|
|
|
x11ColorMap.put("firebrick", 0xB22222);
|
|
|
|
x11ColorMap.put("floralwhite", 0xFFFAF0);
|
|
|
|
x11ColorMap.put("forestgreen", 0x228B22);
|
|
|
|
x11ColorMap.put("fuchsia", 0xFF00FF);
|
|
|
|
x11ColorMap.put("gainsboro", 0xDCDCDC);
|
|
|
|
x11ColorMap.put("ghostwhite", 0xF8F8FF);
|
|
|
|
x11ColorMap.put("gold", 0xFFD700);
|
|
|
|
x11ColorMap.put("goldenrod", 0xDAA520);
|
|
|
|
x11ColorMap.put("gray", 0x808080);
|
|
|
|
x11ColorMap.put("green", 0x008000);
|
|
|
|
x11ColorMap.put("greenyellow", 0xADFF2F);
|
|
|
|
x11ColorMap.put("grey", 0x808080);
|
|
|
|
x11ColorMap.put("honeydew", 0xF0FFF0);
|
|
|
|
x11ColorMap.put("hotpink", 0xFF69B4);
|
|
|
|
x11ColorMap.put("indianred", 0xCD5C5C);
|
|
|
|
x11ColorMap.put("indigo", 0x4B0082);
|
|
|
|
x11ColorMap.put("ivory", 0xFFFFF0);
|
|
|
|
x11ColorMap.put("khaki", 0xF0E68C);
|
|
|
|
x11ColorMap.put("lavender", 0xE6E6FA);
|
|
|
|
x11ColorMap.put("lavenderblush", 0xFFF0F5);
|
|
|
|
x11ColorMap.put("lawngreen", 0x7CFC00);
|
|
|
|
x11ColorMap.put("lemonchiffon", 0xFFFACD);
|
|
|
|
x11ColorMap.put("lightblue", 0xADD8E6);
|
|
|
|
x11ColorMap.put("lightcoral", 0xF08080);
|
|
|
|
x11ColorMap.put("lightcyan", 0xE0FFFF);
|
|
|
|
x11ColorMap.put("lightgoldenrodyellow", 0xFAFAD2);
|
|
|
|
x11ColorMap.put("lightgray", 0xD3D3D3);
|
|
|
|
x11ColorMap.put("lightgreen", 0x90EE90);
|
|
|
|
x11ColorMap.put("lightgrey", 0xD3D3D3);
|
|
|
|
x11ColorMap.put("lightpink", 0xFFB6C1);
|
|
|
|
x11ColorMap.put("lightsalmon", 0xFFA07A);
|
|
|
|
x11ColorMap.put("lightseagreen", 0x20B2AA);
|
|
|
|
x11ColorMap.put("lightskyblue", 0x87CEFA);
|
|
|
|
x11ColorMap.put("lightslategray", 0x778899);
|
|
|
|
x11ColorMap.put("lightslategrey", 0x778899);
|
|
|
|
x11ColorMap.put("lightsteelblue", 0xB0C4DE);
|
|
|
|
x11ColorMap.put("lightyellow", 0xFFFFE0);
|
|
|
|
x11ColorMap.put("lime", 0x00FF00);
|
|
|
|
x11ColorMap.put("limegreen", 0x32CD32);
|
|
|
|
x11ColorMap.put("linen", 0xFAF0E6);
|
|
|
|
x11ColorMap.put("magenta", 0xFF00FF);
|
|
|
|
x11ColorMap.put("maroon", 0x800000);
|
|
|
|
x11ColorMap.put("mediumaquamarine", 0x66CDAA);
|
|
|
|
x11ColorMap.put("mediumblue", 0x0000CD);
|
|
|
|
x11ColorMap.put("mediumorchid", 0xBA55D3);
|
|
|
|
x11ColorMap.put("mediumpurple", 0x9370DB);
|
|
|
|
x11ColorMap.put("mediumseagreen", 0x3CB371);
|
|
|
|
x11ColorMap.put("mediumslateblue", 0x7B68EE);
|
|
|
|
x11ColorMap.put("mediumspringgreen", 0x00FA9A);
|
|
|
|
x11ColorMap.put("mediumturquoise", 0x48D1CC);
|
|
|
|
x11ColorMap.put("mediumvioletred", 0xC71585);
|
|
|
|
x11ColorMap.put("midnightblue", 0x191970);
|
|
|
|
x11ColorMap.put("mintcream", 0xF5FFFA);
|
|
|
|
x11ColorMap.put("mistyrose", 0xFFE4E1);
|
|
|
|
x11ColorMap.put("moccasin", 0xFFE4B5);
|
|
|
|
x11ColorMap.put("navajowhite", 0xFFDEAD);
|
|
|
|
x11ColorMap.put("navy", 0x000080);
|
|
|
|
x11ColorMap.put("oldlace", 0xFDF5E6);
|
|
|
|
x11ColorMap.put("olive", 0x808000);
|
|
|
|
x11ColorMap.put("olivedrab", 0x6B8E23);
|
|
|
|
x11ColorMap.put("orange", 0xFFA500);
|
|
|
|
x11ColorMap.put("orangered", 0xFF4500);
|
|
|
|
x11ColorMap.put("orchid", 0xDA70D6);
|
|
|
|
x11ColorMap.put("palegoldenrod", 0xEEE8AA);
|
|
|
|
x11ColorMap.put("palegreen", 0x98FB98);
|
|
|
|
x11ColorMap.put("paleturquoise", 0xAFEEEE);
|
|
|
|
x11ColorMap.put("palevioletred", 0xDB7093);
|
|
|
|
x11ColorMap.put("papayawhip", 0xFFEFD5);
|
|
|
|
x11ColorMap.put("peachpuff", 0xFFDAB9);
|
|
|
|
x11ColorMap.put("peru", 0xCD853F);
|
|
|
|
x11ColorMap.put("pink", 0xFFC0CB);
|
|
|
|
x11ColorMap.put("plum", 0xDDA0DD);
|
|
|
|
x11ColorMap.put("powderblue", 0xB0E0E6);
|
|
|
|
x11ColorMap.put("purple", 0x800080);
|
|
|
|
x11ColorMap.put("red", 0xFF0000);
|
|
|
|
x11ColorMap.put("rosybrown", 0xBC8F8F);
|
|
|
|
x11ColorMap.put("royalblue", 0x4169E1);
|
|
|
|
x11ColorMap.put("saddlebrown", 0x8B4513);
|
|
|
|
x11ColorMap.put("salmon", 0xFA8072);
|
|
|
|
x11ColorMap.put("sandybrown", 0xF4A460);
|
|
|
|
x11ColorMap.put("seagreen", 0x2E8B57);
|
|
|
|
x11ColorMap.put("seashell", 0xFFF5EE);
|
|
|
|
x11ColorMap.put("sienna", 0xA0522D);
|
|
|
|
x11ColorMap.put("silver", 0xC0C0C0);
|
|
|
|
x11ColorMap.put("skyblue", 0x87CEEB);
|
|
|
|
x11ColorMap.put("slateblue", 0x6A5ACD);
|
|
|
|
x11ColorMap.put("slategray", 0x708090);
|
|
|
|
x11ColorMap.put("slategrey", 0x708090);
|
|
|
|
x11ColorMap.put("snow", 0xFFFAFA);
|
|
|
|
x11ColorMap.put("springgreen", 0x00FF7F);
|
|
|
|
x11ColorMap.put("steelblue", 0x4682B4);
|
|
|
|
x11ColorMap.put("tan", 0xD2B48C);
|
|
|
|
x11ColorMap.put("teal", 0x008080);
|
|
|
|
x11ColorMap.put("thistle", 0xD8BFD8);
|
|
|
|
x11ColorMap.put("tomato", 0xFF6347);
|
|
|
|
x11ColorMap.put("turquoise", 0x40E0D0);
|
|
|
|
x11ColorMap.put("violet", 0xEE82EE);
|
|
|
|
x11ColorMap.put("wheat", 0xF5DEB3);
|
|
|
|
x11ColorMap.put("white", 0xFFFFFF);
|
|
|
|
x11ColorMap.put("whitesmoke", 0xF5F5F5);
|
|
|
|
x11ColorMap.put("yellow", 0xFFFF00);
|
|
|
|
x11ColorMap.put("yellowgreen", 0x9ACD32);
|
|
|
|
}
|
|
|
|
|
2019-11-19 20:53:12 +00:00
|
|
|
static Document sanitize(Context context, String html, boolean show_images, boolean autolink) {
|
2019-10-20 12:15:20 +00:00
|
|
|
try {
|
2019-11-15 07:43:35 +00:00
|
|
|
return _sanitize(context, html, show_images, autolink);
|
2019-10-20 12:15:20 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
// OutOfMemoryError
|
|
|
|
Log.e(ex);
|
2019-11-22 18:16:02 +00:00
|
|
|
Document document = Document.createShell("");
|
2019-11-19 20:53:12 +00:00
|
|
|
Element strong = document.createElement("strong");
|
2019-12-06 07:50:46 +00:00
|
|
|
strong.text(Log.formatThrowable(ex));
|
2019-11-22 18:16:02 +00:00
|
|
|
document.body().appendChild(strong);
|
2019-11-19 20:53:12 +00:00
|
|
|
return document;
|
2019-10-20 12:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 20:53:12 +00:00
|
|
|
private static Document _sanitize(Context context, String html, boolean show_images, boolean autolink) {
|
2019-10-04 13:25:04 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean text_color = prefs.getBoolean("text_color", true);
|
2019-10-16 09:05:38 +00:00
|
|
|
boolean display_hidden = prefs.getBoolean("display_hidden", false);
|
2019-10-04 13:25:04 +00:00
|
|
|
boolean disable_tracking = prefs.getBoolean("disable_tracking", true);
|
|
|
|
|
2019-10-03 16:19:22 +00:00
|
|
|
Document parsed = JsoupEx.parse(html);
|
2019-05-13 09:03:15 +00:00
|
|
|
|
|
|
|
// <html xmlns:v="urn:schemas-microsoft-com:vml"
|
|
|
|
// xmlns:o="urn:schemas-microsoft-com:office:office"
|
|
|
|
// xmlns:w="urn:schemas-microsoft-com:office:word"
|
|
|
|
// xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
|
|
|
|
// xmlns="http://www.w3.org/TR/REC-html40">
|
|
|
|
|
|
|
|
// <o:p> </o:p></span>
|
|
|
|
|
|
|
|
// Default XHTML namespace: http://www.w3.org/1999/xhtml
|
|
|
|
|
|
|
|
String ns = null;
|
|
|
|
for (Element h : parsed.select("html"))
|
|
|
|
for (Attribute a : h.attributes()) {
|
2019-05-15 09:18:14 +00:00
|
|
|
String key = a.getKey();
|
|
|
|
String value = a.getValue();
|
|
|
|
if (value != null &&
|
|
|
|
key.startsWith("xmlns:") &&
|
|
|
|
value.startsWith("http://www.w3.org/")) {
|
|
|
|
ns = key.split(":")[1];
|
2019-05-13 09:03:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 09:18:14 +00:00
|
|
|
for (Element e : parsed.select("*")) {
|
|
|
|
String tag = e.tagName();
|
|
|
|
if (tag.contains(":")) {
|
|
|
|
if (ns != null && tag.startsWith(ns)) {
|
2019-05-13 09:03:15 +00:00
|
|
|
e.tagName(tag.split(":")[1]);
|
|
|
|
Log.i("Updated tag=" + tag + " to=" + e.tagName());
|
|
|
|
} else {
|
|
|
|
e.remove();
|
|
|
|
Log.i("Removed tag=" + tag);
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 09:18:14 +00:00
|
|
|
}
|
2019-05-13 09:03:15 +00:00
|
|
|
|
2019-03-12 09:04:16 +00:00
|
|
|
Whitelist whitelist = Whitelist.relaxed()
|
2019-09-25 10:48:02 +00:00
|
|
|
.addTags("hr", "abbr", "big", "font")
|
2019-03-10 15:21:46 +00:00
|
|
|
.removeTags("col", "colgroup", "thead", "tbody")
|
2019-03-12 09:04:16 +00:00
|
|
|
.removeAttributes("table", "width")
|
|
|
|
.removeAttributes("td", "colspan", "rowspan", "width")
|
|
|
|
.removeAttributes("th", "colspan", "rowspan", "width")
|
2018-12-14 09:05:48 +00:00
|
|
|
.addProtocols("img", "src", "cid")
|
2019-03-12 09:04:16 +00:00
|
|
|
.addProtocols("img", "src", "data");
|
2019-09-23 18:13:38 +00:00
|
|
|
if (text_color)
|
2019-09-25 10:48:02 +00:00
|
|
|
whitelist
|
|
|
|
.addAttributes(":all", "style")
|
|
|
|
.addAttributes("font", "color");
|
2019-09-23 18:13:38 +00:00
|
|
|
|
2019-03-12 09:04:16 +00:00
|
|
|
final Document document = new Cleaner(whitelist).clean(parsed);
|
2018-11-24 11:27:44 +00:00
|
|
|
|
2019-09-24 12:46:12 +00:00
|
|
|
boolean dark = Helper.isDarkTheme(context);
|
|
|
|
|
2019-09-25 10:48:02 +00:00
|
|
|
// Font
|
|
|
|
for (Element font : document.select("font")) {
|
|
|
|
String color = font.attr("color");
|
2019-09-25 13:20:27 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
|
|
|
font.removeAttr("color");
|
|
|
|
font.removeAttr("face");
|
2019-09-25 10:48:02 +00:00
|
|
|
font.attr("style", "color:" + color + ";");
|
|
|
|
font.tagName("span");
|
|
|
|
}
|
|
|
|
|
2019-10-16 09:05:38 +00:00
|
|
|
// Sanitize styles
|
|
|
|
for (Element element : document.select("*")) {
|
|
|
|
String style = element.attr("style");
|
2019-09-23 17:51:17 +00:00
|
|
|
if (!TextUtils.isEmpty(style)) {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
String[] params = style.split(";");
|
|
|
|
for (String param : params) {
|
|
|
|
String[] kv = param.split(":");
|
2019-10-16 09:05:38 +00:00
|
|
|
if (kv.length == 2) {
|
|
|
|
String key = kv[0].trim().toLowerCase(Locale.ROOT);
|
|
|
|
String value = kv[1].toLowerCase(Locale.ROOT)
|
2020-01-06 17:46:27 +00:00
|
|
|
.replaceAll("\\s", "");
|
2019-10-16 09:05:38 +00:00
|
|
|
switch (key) {
|
2019-09-23 17:51:17 +00:00
|
|
|
case "color":
|
2020-01-26 12:41:12 +00:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
|
2019-10-16 09:05:38 +00:00
|
|
|
String c = value
|
2020-01-21 10:06:26 +00:00
|
|
|
.replace("none", "")
|
2020-01-21 15:36:55 +00:00
|
|
|
.replace("unset", "")
|
2019-09-24 14:51:51 +00:00
|
|
|
.replace("inherit", "")
|
2019-09-25 08:05:30 +00:00
|
|
|
.replace("initial", "")
|
|
|
|
.replace("windowtext", "")
|
2019-09-24 15:48:10 +00:00
|
|
|
.replace("transparent", "")
|
2020-01-24 14:22:50 +00:00
|
|
|
.replace("!important", "")
|
2020-01-26 12:41:12 +00:00
|
|
|
.replaceAll("[^a-z0-9(),.%]", "");
|
2019-09-24 13:02:33 +00:00
|
|
|
|
2019-09-24 12:46:12 +00:00
|
|
|
Integer color = null;
|
2019-09-24 13:02:33 +00:00
|
|
|
try {
|
2019-09-24 18:00:48 +00:00
|
|
|
if (TextUtils.isEmpty(c))
|
|
|
|
; // Do nothing
|
|
|
|
else if (c.startsWith("#"))
|
2019-09-24 12:46:12 +00:00
|
|
|
color = Integer.decode(c) | 0xFF000000;
|
2020-01-26 12:41:12 +00:00
|
|
|
else if (c.startsWith("rgb") || c.startsWith("hsl")) {
|
2019-09-24 14:17:14 +00:00
|
|
|
int s = c.indexOf("(");
|
|
|
|
int e = c.indexOf(")");
|
|
|
|
if (s > 0 && e > s) {
|
2020-01-26 12:41:12 +00:00
|
|
|
String[] component = c.substring(s + 1, e).split(",");
|
|
|
|
|
|
|
|
for (int i = 0; i < component.length; i++)
|
|
|
|
if (component[i].endsWith("%"))
|
|
|
|
if (c.startsWith("rgb")) {
|
|
|
|
int percent = Integer.parseInt(component[i].replace("%", ""));
|
|
|
|
component[i] = Integer.toString(Math.round(255 * (percent / 100f)));
|
|
|
|
} else
|
|
|
|
component[i] = component[i].replace("%", "");
|
|
|
|
|
|
|
|
if (c.startsWith("rgb") && component.length >= 3)
|
2019-09-24 14:17:14 +00:00
|
|
|
color = Color.rgb(
|
2020-01-26 12:41:12 +00:00
|
|
|
Integer.parseInt(component[0]),
|
|
|
|
Integer.parseInt(component[1]),
|
|
|
|
Integer.parseInt(component[2]));
|
|
|
|
else if (c.startsWith("hsl") && component.length >= 3)
|
|
|
|
color = ColorUtils.HSLToColor(new float[]{
|
|
|
|
Float.parseFloat(component[0]),
|
|
|
|
Integer.parseInt(component[1]) / 100f,
|
|
|
|
Integer.parseInt(component[2]) / 100f});
|
2019-09-24 14:17:14 +00:00
|
|
|
}
|
2020-01-22 11:28:39 +00:00
|
|
|
} else if (x11ColorMap.containsKey(c))
|
|
|
|
color = x11ColorMap.get(c);
|
2019-09-25 12:46:38 +00:00
|
|
|
else
|
2020-01-21 10:06:26 +00:00
|
|
|
try {
|
|
|
|
color = Color.parseColor(c);
|
|
|
|
} catch (IllegalArgumentException ex) {
|
2020-01-23 08:03:04 +00:00
|
|
|
color = Integer.decode("#" + c) | 0xFF000000;
|
2020-01-21 10:06:26 +00:00
|
|
|
}
|
2020-01-26 12:41:12 +00:00
|
|
|
|
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
Log.i("Color " + c + "=" + (color == null ? null : Long.toHexString(color)));
|
|
|
|
|
2019-09-24 13:02:33 +00:00
|
|
|
} catch (Throwable ex) {
|
2020-01-23 08:03:04 +00:00
|
|
|
Log.e("Color=" + c + ": " + ex);
|
2019-09-24 13:02:33 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 07:44:46 +00:00
|
|
|
if (color != null && !(dark && color == Color.BLACK)) {
|
2019-10-01 08:01:44 +00:00
|
|
|
color = Helper.adjustLuminance(color, dark, MIN_LUMINANCE);
|
2019-09-25 07:41:10 +00:00
|
|
|
c = String.format("#%06x", color & 0xFFFFFF);
|
2019-09-24 12:46:12 +00:00
|
|
|
sb.append("color:").append(c).append(";");
|
2019-09-25 13:20:27 +00:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
|
2019-10-16 09:05:38 +00:00
|
|
|
element.attr("color", c);
|
2019-09-24 12:46:12 +00:00
|
|
|
}
|
2019-09-23 17:51:17 +00:00
|
|
|
break;
|
2019-09-23 20:07:22 +00:00
|
|
|
|
|
|
|
case "background":
|
|
|
|
case "background-color":
|
|
|
|
break;
|
2019-09-24 12:46:12 +00:00
|
|
|
|
|
|
|
case "line-through":
|
|
|
|
sb.append(param).append(";");
|
|
|
|
break;
|
2019-10-16 09:05:38 +00:00
|
|
|
|
|
|
|
case "display":
|
2019-11-26 08:15:33 +00:00
|
|
|
if (!display_hidden && "none".equals(value)) {
|
2019-10-16 09:05:38 +00:00
|
|
|
Log.i("Removing element " + element.tagName());
|
2019-10-18 14:01:55 +00:00
|
|
|
element.empty();
|
2019-10-16 09:05:38 +00:00
|
|
|
}
|
2019-11-11 09:47:06 +00:00
|
|
|
|
|
|
|
if ("inline".equals(value) || "inline-block".equals(value))
|
|
|
|
element.attr("inline", "true");
|
|
|
|
|
2019-10-16 09:05:38 +00:00
|
|
|
break;
|
2019-11-26 08:15:33 +00:00
|
|
|
|
|
|
|
case "height":
|
|
|
|
case "width":
|
|
|
|
//case "font-size":
|
|
|
|
//case "line-height":
|
|
|
|
if (!display_hidden &&
|
|
|
|
("0".equals(value) || "0px".equals(value))) {
|
|
|
|
Log.i("Removing element " + element.tagName());
|
|
|
|
element.empty();
|
|
|
|
}
|
|
|
|
break;
|
2019-09-23 17:51:17 +00:00
|
|
|
}
|
2019-10-16 09:05:38 +00:00
|
|
|
}
|
2019-09-23 17:51:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 16:05:13 +00:00
|
|
|
if (sb.length() == 0)
|
2019-10-16 09:05:38 +00:00
|
|
|
element.removeAttr("style");
|
2019-09-25 16:05:13 +00:00
|
|
|
else
|
2019-10-16 09:05:38 +00:00
|
|
|
element.attr("style", sb.toString());
|
2019-09-23 17:51:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 08:45:54 +00:00
|
|
|
// Remove new lines without surrounding content
|
2019-11-11 10:32:47 +00:00
|
|
|
//for (Element br : document.select("br"))
|
|
|
|
// if (br.parent() != null && !hasVisibleContent(br.parent().childNodes()))
|
|
|
|
// br.tagName("span");
|
2019-09-12 08:45:54 +00:00
|
|
|
|
2020-01-20 17:02:40 +00:00
|
|
|
for (Element div : document.select("div"))
|
|
|
|
if (div.children().select("div").size() == 0 &&
|
|
|
|
hasVisibleContent(div.childNodes())) {
|
|
|
|
Node last = div.childNode(div.childNodeSize() - 1);
|
|
|
|
if (last != null && "br".equals(last.nodeName()))
|
|
|
|
last.remove();
|
|
|
|
}
|
|
|
|
|
2019-09-12 08:45:54 +00:00
|
|
|
// Paragraphs
|
2019-11-11 10:32:47 +00:00
|
|
|
for (Element p : document.select("p")) {
|
|
|
|
p.appendElement("br");
|
2019-09-12 08:45:54 +00:00
|
|
|
p.tagName("div");
|
2019-11-11 10:32:47 +00:00
|
|
|
}
|
2019-09-12 08:45:54 +00:00
|
|
|
|
2019-03-10 15:21:46 +00:00
|
|
|
// Short quotes
|
|
|
|
for (Element q : document.select("q")) {
|
|
|
|
q.prependText("\"");
|
|
|
|
q.appendText("\"");
|
|
|
|
q.tagName("em");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pre formatted text
|
2019-09-01 09:10:00 +00:00
|
|
|
for (Element pre : document.select("pre")) {
|
2019-11-19 08:57:55 +00:00
|
|
|
pre.html(formatPre(pre.wholeText()));
|
|
|
|
pre.tagName("tt");
|
2019-03-10 15:21:46 +00:00
|
|
|
}
|
2019-03-10 11:14:39 +00:00
|
|
|
|
2019-03-10 15:21:46 +00:00
|
|
|
// Code
|
2019-09-21 16:10:41 +00:00
|
|
|
document.select("code").tagName("strong");
|
2019-03-10 15:21:46 +00:00
|
|
|
|
|
|
|
// Lines
|
|
|
|
for (Element hr : document.select("hr")) {
|
|
|
|
hr.tagName("div");
|
2019-03-18 18:23:43 +00:00
|
|
|
hr.text("----------------------------------------");
|
2019-02-10 12:01:21 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 15:21:46 +00:00
|
|
|
// Descriptions
|
|
|
|
document.select("dl").tagName("div");
|
2019-03-11 08:27:56 +00:00
|
|
|
for (Element dt : document.select("dt")) {
|
|
|
|
dt.tagName("strong");
|
2019-03-10 15:21:46 +00:00
|
|
|
dt.appendElement("br");
|
2019-03-11 08:27:56 +00:00
|
|
|
}
|
|
|
|
for (Element dd : document.select("dd")) {
|
|
|
|
dd.tagName("em");
|
|
|
|
dd.appendElement("br").appendElement("br");
|
|
|
|
}
|
2018-11-24 11:27:44 +00:00
|
|
|
|
2019-03-12 12:13:53 +00:00
|
|
|
// Abbreviations
|
|
|
|
document.select("abbr").tagName("u");
|
|
|
|
|
2019-06-30 07:41:18 +00:00
|
|
|
// Subscript/Superscript
|
|
|
|
for (Element subp : document.select("sub,sup")) {
|
|
|
|
Element small = document.createElement("small");
|
|
|
|
small.html(subp.html());
|
|
|
|
subp.html(small.outerHtml());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lists
|
|
|
|
for (Element li : document.select("li")) {
|
|
|
|
li.tagName("span");
|
2020-01-30 10:12:26 +00:00
|
|
|
Element parent = li.parent();
|
|
|
|
if (parent == null || "ul".equals(parent.tagName()))
|
|
|
|
li.prependText("• ");
|
|
|
|
else
|
|
|
|
li.prependText((li.elementSiblingIndex() + 1) + ". ");
|
2019-06-30 07:41:18 +00:00
|
|
|
li.appendElement("br"); // line break after list item
|
|
|
|
}
|
|
|
|
document.select("ol").tagName("div");
|
|
|
|
document.select("ul").tagName("div");
|
2018-12-21 14:19:07 +00:00
|
|
|
|
2019-03-30 16:48:04 +00:00
|
|
|
// Tables
|
|
|
|
for (Element col : document.select("th,td")) {
|
2019-09-02 17:01:17 +00:00
|
|
|
// separate columns
|
2019-09-12 16:53:57 +00:00
|
|
|
if (hasVisibleContent(col.childNodes()))
|
2019-09-12 08:45:54 +00:00
|
|
|
if (col.nextElementSibling() != null)
|
2019-09-02 17:01:17 +00:00
|
|
|
col.appendText(" ");
|
2019-03-30 16:48:04 +00:00
|
|
|
|
|
|
|
if ("th".equals(col.tagName()))
|
|
|
|
col.tagName("strong");
|
|
|
|
else
|
|
|
|
col.tagName("span");
|
|
|
|
}
|
|
|
|
|
2019-09-12 08:45:54 +00:00
|
|
|
for (Element row : document.select("tr")) {
|
2019-03-30 16:48:04 +00:00
|
|
|
row.tagName("span");
|
2019-09-12 16:53:57 +00:00
|
|
|
if (hasVisibleContent(row.childNodes())) {
|
2019-09-12 13:28:21 +00:00
|
|
|
Element next = row.nextElementSibling();
|
|
|
|
if (next != null && "tr".equals(next.tagName()))
|
|
|
|
row.appendElement("br");
|
|
|
|
}
|
2019-09-12 08:45:54 +00:00
|
|
|
}
|
2019-03-30 16:48:04 +00:00
|
|
|
|
2019-09-11 20:52:15 +00:00
|
|
|
document.select("caption").tagName("div");
|
2019-06-18 19:56:34 +00:00
|
|
|
|
|
|
|
for (Element table : document.select("table"))
|
|
|
|
if (table.parent() != null && "a".equals(table.parent().tagName()))
|
2019-06-30 07:51:43 +00:00
|
|
|
table.tagName("span"); // Links cannot contain tables
|
2019-06-18 19:56:34 +00:00
|
|
|
else
|
|
|
|
table.tagName("div");
|
2019-03-30 16:48:04 +00:00
|
|
|
|
2019-10-04 13:25:04 +00:00
|
|
|
// Remove tracking pixels
|
2019-09-13 11:46:52 +00:00
|
|
|
if (disable_tracking)
|
2019-10-04 13:25:04 +00:00
|
|
|
removeTrackingPixels(context, document);
|
2019-09-13 11:46:52 +00:00
|
|
|
|
|
|
|
// Images
|
2019-06-30 07:59:03 +00:00
|
|
|
for (Element img : document.select("img")) {
|
2019-10-14 06:50:27 +00:00
|
|
|
String alt = img.attr("alt");
|
|
|
|
String src = img.attr("src");
|
|
|
|
String tracking = img.attr("tracking");
|
|
|
|
|
2019-10-19 08:19:05 +00:00
|
|
|
if (!show_images && !TextUtils.isEmpty(alt))
|
|
|
|
if (TextUtils.isEmpty(tracking))
|
|
|
|
img.appendText(" " + alt + " ");
|
|
|
|
else {
|
2019-10-05 07:38:36 +00:00
|
|
|
img.append(" ");
|
2019-10-19 08:19:05 +00:00
|
|
|
Element a = document.createElement("a");
|
|
|
|
a.attr("href", tracking);
|
|
|
|
a.text(alt);
|
|
|
|
img.appendChild(a);
|
|
|
|
img.appendText(" ");
|
2019-06-30 07:41:18 +00:00
|
|
|
}
|
2019-07-26 18:45:53 +00:00
|
|
|
|
|
|
|
// Annotate source with width and height
|
2019-10-14 06:50:27 +00:00
|
|
|
if (!TextUtils.isEmpty(src)) {
|
2019-07-26 19:33:32 +00:00
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
2019-07-26 18:45:53 +00:00
|
|
|
|
2019-07-27 05:32:26 +00:00
|
|
|
String awidth = img.attr("width");
|
|
|
|
for (int i = 0; i < awidth.length(); i++)
|
|
|
|
if (Character.isDigit(awidth.charAt(i)))
|
|
|
|
width = width * 10 + (byte) awidth.charAt(i) - (byte) '0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
String aheight = img.attr("height");
|
|
|
|
for (int i = 0; i < aheight.length(); i++)
|
|
|
|
if (Character.isDigit(aheight.charAt(i)))
|
|
|
|
height = height * 10 + (byte) aheight.charAt(i) - (byte) '0';
|
|
|
|
else
|
|
|
|
break;
|
2019-07-26 19:33:32 +00:00
|
|
|
|
|
|
|
if (width != 0 || height != 0) {
|
2019-10-05 09:57:54 +00:00
|
|
|
ImageHelper.AnnotatedSource a = new ImageHelper.AnnotatedSource(
|
2019-10-14 06:50:27 +00:00
|
|
|
src, width, height, !TextUtils.isEmpty(tracking));
|
2019-07-26 19:33:32 +00:00
|
|
|
img.attr("src", a.getAnnotated());
|
|
|
|
}
|
2019-07-26 18:45:53 +00:00
|
|
|
}
|
2019-06-30 07:59:03 +00:00
|
|
|
}
|
2019-03-30 16:48:04 +00:00
|
|
|
|
2019-06-30 07:41:18 +00:00
|
|
|
// Autolink
|
2019-11-15 07:43:35 +00:00
|
|
|
if (autolink) {
|
|
|
|
final Pattern pattern = Pattern.compile(
|
|
|
|
PatternsCompat.AUTOLINK_EMAIL_ADDRESS.pattern() + "|" +
|
|
|
|
PatternsCompat.AUTOLINK_WEB_URL.pattern());
|
|
|
|
|
|
|
|
NodeTraversor.traverse(new NodeVisitor() {
|
|
|
|
private int links = 0;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void head(Node node, int depth) {
|
|
|
|
if (links < MAX_AUTO_LINK && node instanceof TextNode) {
|
|
|
|
TextNode tnode = (TextNode) node;
|
|
|
|
String text = tnode.getWholeText();
|
|
|
|
|
|
|
|
Matcher matcher = pattern.matcher(text);
|
|
|
|
if (matcher.find()) {
|
|
|
|
Element span = document.createElement("span");
|
|
|
|
|
|
|
|
int pos = 0;
|
|
|
|
do {
|
|
|
|
boolean linked = false;
|
|
|
|
Node parent = tnode.parent();
|
|
|
|
while (parent != null) {
|
|
|
|
if ("a".equals(parent.nodeName())) {
|
|
|
|
linked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
parent = parent.parent();
|
2019-03-12 09:04:16 +00:00
|
|
|
}
|
2019-02-19 07:29:03 +00:00
|
|
|
|
2019-11-15 07:43:35 +00:00
|
|
|
boolean email = matcher.group().contains("@") && !matcher.group().contains(":");
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Web url=" + matcher.group() +
|
|
|
|
" " + matcher.start() + "..." + matcher.end() + "/" + text.length() +
|
|
|
|
" linked=" + linked + " email=" + email + " count=" + links);
|
2019-02-19 07:29:03 +00:00
|
|
|
|
2019-11-15 07:43:35 +00:00
|
|
|
if (linked)
|
|
|
|
span.appendText(text.substring(pos, matcher.end()));
|
|
|
|
else {
|
|
|
|
span.appendText(text.substring(pos, matcher.start()));
|
2019-02-19 07:29:03 +00:00
|
|
|
|
2019-11-15 07:43:35 +00:00
|
|
|
Element a = document.createElement("a");
|
|
|
|
a.attr("href", (email ? "mailto:" : "") + matcher.group());
|
|
|
|
a.text(matcher.group());
|
|
|
|
span.appendChild(a);
|
2019-08-20 18:05:11 +00:00
|
|
|
|
2019-11-15 07:43:35 +00:00
|
|
|
links++;
|
|
|
|
}
|
2019-02-19 07:29:03 +00:00
|
|
|
|
2019-11-15 07:43:35 +00:00
|
|
|
pos = matcher.end();
|
|
|
|
} while (links < MAX_AUTO_LINK && matcher.find());
|
2019-06-13 08:20:45 +00:00
|
|
|
|
2019-11-15 07:43:35 +00:00
|
|
|
span.appendText(text.substring(pos));
|
2019-02-16 21:55:44 +00:00
|
|
|
|
2019-11-15 07:43:35 +00:00
|
|
|
tnode.before(span);
|
|
|
|
tnode.text("");
|
|
|
|
}
|
2018-09-02 11:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 07:43:35 +00:00
|
|
|
@Override
|
|
|
|
public void tail(Node node, int depth) {
|
|
|
|
}
|
|
|
|
}, document);
|
|
|
|
}
|
2019-03-12 09:04:16 +00:00
|
|
|
|
2019-09-12 08:45:54 +00:00
|
|
|
// Selective new lines
|
|
|
|
for (Element div : document.select("div"))
|
2019-11-11 09:47:06 +00:00
|
|
|
if (!Boolean.parseBoolean(div.attr("inline")) &&
|
|
|
|
div.children().select("div").size() == 0 &&
|
2019-09-12 16:53:57 +00:00
|
|
|
hasVisibleContent(div.childNodes())) {
|
2019-09-11 17:52:04 +00:00
|
|
|
div.appendElement("br");
|
2019-09-11 20:20:00 +00:00
|
|
|
}
|
2019-09-12 08:45:54 +00:00
|
|
|
|
|
|
|
for (Element div : document.select("div"))
|
|
|
|
div.tagName("span");
|
2019-07-08 18:14:33 +00:00
|
|
|
|
2019-09-25 13:20:27 +00:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
|
|
|
|
for (Element span : document.select("span"))
|
|
|
|
if (!TextUtils.isEmpty(span.attr("color")))
|
|
|
|
span.tagName("font");
|
|
|
|
|
2019-11-22 18:16:02 +00:00
|
|
|
if (document.body() == null) {
|
|
|
|
Log.e("Sanitize without body");
|
|
|
|
document.normalise();
|
|
|
|
}
|
|
|
|
|
2019-11-19 20:53:12 +00:00
|
|
|
return document;
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-12-14 09:11:45 +00:00
|
|
|
|
2019-09-12 16:53:57 +00:00
|
|
|
private static boolean hasVisibleContent(List<Node> nodes) {
|
2019-09-12 08:45:54 +00:00
|
|
|
for (Node node : nodes)
|
|
|
|
if (node instanceof TextNode && !((TextNode) node).isBlank())
|
|
|
|
return true;
|
|
|
|
else if (node instanceof Element) {
|
|
|
|
Element element = (Element) node;
|
|
|
|
if (!element.isBlock() &&
|
|
|
|
(element.hasText() || element.selectFirst("img") != null))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-09-02 17:01:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 08:57:55 +00:00
|
|
|
static String formatPre(String text) {
|
|
|
|
int level = 0;
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
String[] lines = text.split("\\r?\\n");
|
|
|
|
for (String line : lines) {
|
|
|
|
// Opening quotes
|
|
|
|
int tlevel = 0;
|
|
|
|
while (line.startsWith(">")) {
|
|
|
|
tlevel++;
|
|
|
|
if (tlevel > level)
|
|
|
|
sb.append("<blockquote>");
|
|
|
|
|
|
|
|
line = line.substring(1); // >
|
|
|
|
|
|
|
|
if (line.startsWith(" "))
|
|
|
|
line = line.substring(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Closing quotes
|
|
|
|
for (int i = 0; i < level - tlevel; i++)
|
|
|
|
sb.append("</blockquote>");
|
|
|
|
level = tlevel;
|
|
|
|
|
|
|
|
// Tabs characters
|
|
|
|
StringBuilder l = new StringBuilder();
|
|
|
|
for (int j = 0; j < line.length(); j++) {
|
|
|
|
char kar = line.charAt(j);
|
|
|
|
if (kar == '\t') {
|
|
|
|
l.append(' ');
|
|
|
|
while (l.length() % TAB_SIZE != 0)
|
|
|
|
l.append(' ');
|
|
|
|
} else
|
|
|
|
l.append(kar);
|
|
|
|
}
|
|
|
|
line = l.toString();
|
|
|
|
|
|
|
|
// Html characters
|
|
|
|
line = Html.escapeHtml(line);
|
|
|
|
|
|
|
|
// Space characters
|
|
|
|
int len = line.length();
|
|
|
|
for (int j = 0; j < len; j++) {
|
|
|
|
char kar = line.charAt(j);
|
|
|
|
if (kar == ' ') {
|
|
|
|
// Prevent trimming start
|
|
|
|
// Keep one space for word wrapping
|
|
|
|
if (j == 0 || (j + 1 < len && line.charAt(j + 1) == ' '))
|
|
|
|
sb.append(" ");
|
|
|
|
else
|
|
|
|
sb.append(' ');
|
|
|
|
} else
|
|
|
|
sb.append(kar);
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.append("<br>");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Closing quotes
|
|
|
|
for (int i = 0; i < level; i++)
|
|
|
|
sb.append("</blockquote>");
|
|
|
|
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
2019-10-04 13:25:04 +00:00
|
|
|
static void removeTrackingPixels(Context context, Document document) {
|
2019-10-04 21:04:03 +00:00
|
|
|
Drawable d = ContextCompat.getDrawable(context, R.drawable.baseline_my_location_24);
|
|
|
|
d.setTint(Helper.resolveColor(context, R.attr.colorWarning));
|
2019-10-05 07:38:36 +00:00
|
|
|
|
2019-10-04 21:04:03 +00:00
|
|
|
Bitmap bm = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
|
|
|
|
Canvas c = new Canvas(bm);
|
|
|
|
d.setBounds(0, 0, c.getWidth(), c.getHeight());
|
|
|
|
d.draw(c);
|
|
|
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2019-10-05 07:38:36 +00:00
|
|
|
sb.append("data:image/png;base64,");
|
2019-10-05 09:57:54 +00:00
|
|
|
sb.append(Base64.encodeToString(bos.toByteArray(), Base64.NO_WRAP));
|
2019-10-04 21:04:03 +00:00
|
|
|
|
2019-10-04 13:25:04 +00:00
|
|
|
// Build list of allowed hosts
|
|
|
|
List<String> hosts = new ArrayList<>();
|
|
|
|
for (Element img : document.select("img")) {
|
|
|
|
String src = img.attr("src");
|
|
|
|
if (!TextUtils.isEmpty(src) && !isTrackingPixel(img)) {
|
|
|
|
Uri uri = Uri.parse(img.attr("src"));
|
|
|
|
String host = uri.getHost();
|
|
|
|
if (host != null && !hosts.contains(host))
|
|
|
|
hosts.add(host);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Images
|
|
|
|
for (Element img : document.select("img")) {
|
2019-10-05 09:57:54 +00:00
|
|
|
img.removeAttr("tracking");
|
2019-10-04 13:25:04 +00:00
|
|
|
String src = img.attr("src");
|
|
|
|
if (!TextUtils.isEmpty(src) && isTrackingPixel(img)) {
|
2019-10-14 06:50:27 +00:00
|
|
|
Uri uri = Uri.parse(src);
|
2019-10-04 13:25:04 +00:00
|
|
|
String host = uri.getHost();
|
|
|
|
if (host == null || !hosts.contains(host)) {
|
2019-10-04 21:04:03 +00:00
|
|
|
img.attr("src", sb.toString());
|
|
|
|
img.attr("alt", context.getString(R.string.title_legend_tracking_pixel));
|
|
|
|
img.attr("height", "24");
|
|
|
|
img.attr("width", "24");
|
2019-10-05 09:57:54 +00:00
|
|
|
img.attr("style", "display:block !important; width:24px !important; height:24px !important;");
|
2019-10-14 06:50:27 +00:00
|
|
|
img.attr("tracking", src);
|
2019-10-04 13:25:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 19:25:52 +00:00
|
|
|
private static boolean isTrackingPixel(Element img) {
|
|
|
|
String width = img.attr("width").trim();
|
|
|
|
String height = img.attr("height").trim();
|
2019-03-30 16:55:00 +00:00
|
|
|
|
2019-10-04 19:25:52 +00:00
|
|
|
if (TextUtils.isEmpty(width) || TextUtils.isEmpty(height))
|
|
|
|
return false;
|
2018-12-14 09:11:45 +00:00
|
|
|
|
2019-10-03 06:55:37 +00:00
|
|
|
try {
|
2019-10-04 19:25:52 +00:00
|
|
|
return (Integer.parseInt(width) * Integer.parseInt(height) <= TRACKING_PIXEL_SURFACE);
|
|
|
|
} catch (NumberFormatException ignored) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-10-03 06:55:37 +00:00
|
|
|
|
2019-10-05 09:57:54 +00:00
|
|
|
static void embedInlineImages(Context context, long id, Document document) throws IOException {
|
2019-10-04 19:25:52 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
for (Element img : document.select("img")) {
|
|
|
|
String src = img.attr("src");
|
|
|
|
if (src.startsWith("cid:")) {
|
|
|
|
String cid = '<' + src.substring(4) + '>';
|
2019-10-03 06:55:37 +00:00
|
|
|
EntityAttachment attachment = db.attachment().getAttachment(id, cid);
|
2019-10-04 19:25:52 +00:00
|
|
|
if (attachment != null && attachment.available) {
|
|
|
|
File file = attachment.getFile(context);
|
|
|
|
try (InputStream is = new FileInputStream(file)) {
|
|
|
|
byte[] bytes = new byte[(int) file.length()];
|
|
|
|
if (is.read(bytes) != bytes.length)
|
|
|
|
throw new IOException("length");
|
2019-01-29 13:29:39 +00:00
|
|
|
|
2019-10-04 19:25:52 +00:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.append("data:");
|
|
|
|
sb.append(attachment.type);
|
|
|
|
sb.append(";base64,");
|
2019-10-05 09:57:54 +00:00
|
|
|
sb.append(Base64.encodeToString(bytes, Base64.NO_WRAP));
|
2019-01-29 13:29:39 +00:00
|
|
|
|
2019-10-04 19:25:52 +00:00
|
|
|
img.attr("src", sb.toString());
|
2019-04-30 08:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-04 19:25:52 +00:00
|
|
|
}
|
2019-07-15 10:17:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 17:54:52 +00:00
|
|
|
static void setViewport(Document document) {
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag
|
|
|
|
document.head().select("meta").select("[name=viewport]").remove();
|
|
|
|
|
|
|
|
document.head().prependChild(document.createElement("meta")
|
|
|
|
.attr("name", "viewport")
|
|
|
|
.attr("content", "width=device-width, initial-scale=1.0"));
|
|
|
|
|
|
|
|
Log.i(document.head().html());
|
2019-05-04 11:06:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 20:09:47 +00:00
|
|
|
static String getPreview(String body) {
|
2019-11-11 19:23:34 +00:00
|
|
|
try {
|
|
|
|
return _getPreview(body);
|
|
|
|
} catch (OutOfMemoryError ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String _getPreview(String body) {
|
2019-10-03 16:07:56 +00:00
|
|
|
if (body == null)
|
|
|
|
return null;
|
|
|
|
|
2019-10-03 16:19:22 +00:00
|
|
|
String text = JsoupEx.parse(body).text();
|
2019-10-03 16:07:56 +00:00
|
|
|
|
|
|
|
String preview = text.substring(0, Math.min(text.length(), PREVIEW_SIZE));
|
|
|
|
if (preview.length() < text.length())
|
|
|
|
preview += "…";
|
|
|
|
|
|
|
|
return preview;
|
2019-01-05 11:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static String getText(String html) {
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
|
|
|
|
2019-08-22 18:05:45 +00:00
|
|
|
html = html.replace("<br> ", "<br>");
|
|
|
|
|
2019-01-05 11:17:33 +00:00
|
|
|
NodeTraversor.traverse(new NodeVisitor() {
|
2019-02-10 12:01:21 +00:00
|
|
|
private int qlevel = 0;
|
2019-02-11 15:36:42 +00:00
|
|
|
private int tlevel = 0;
|
2019-09-07 06:22:46 +00:00
|
|
|
private int plevel = 0;
|
2019-11-22 19:42:45 +00:00
|
|
|
private int lindex = 0;
|
2019-02-10 12:01:21 +00:00
|
|
|
|
2019-01-05 11:17:33 +00:00
|
|
|
public void head(Node node, int depth) {
|
2019-08-23 06:29:58 +00:00
|
|
|
if (node instanceof TextNode)
|
2019-09-07 06:22:46 +00:00
|
|
|
if (plevel > 0) {
|
|
|
|
String[] lines = ((TextNode) node).getWholeText().split("\\r?\\n");
|
|
|
|
for (String line : lines) {
|
2019-11-22 19:42:45 +00:00
|
|
|
append(line, true);
|
2019-09-07 06:22:46 +00:00
|
|
|
newline();
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
append(((TextNode) node).text());
|
2019-08-23 06:29:58 +00:00
|
|
|
else {
|
2019-01-05 11:17:33 +00:00
|
|
|
String name = node.nodeName();
|
2019-02-10 12:01:21 +00:00
|
|
|
if ("li".equals(name))
|
2019-08-23 06:29:58 +00:00
|
|
|
append("*");
|
2019-02-10 12:01:21 +00:00
|
|
|
else if ("blockquote".equals(name))
|
|
|
|
qlevel++;
|
2019-09-07 06:22:46 +00:00
|
|
|
else if ("pre".equals(name))
|
|
|
|
plevel++;
|
2019-02-10 12:01:21 +00:00
|
|
|
|
|
|
|
if (heads.contains(name))
|
|
|
|
newline();
|
2019-01-05 11:17:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void tail(Node node, int depth) {
|
|
|
|
String name = node.nodeName();
|
2019-08-23 06:29:58 +00:00
|
|
|
if ("a".equals(name))
|
2019-11-07 12:30:13 +00:00
|
|
|
append("[" + node.attr("href") + "]");
|
2019-08-23 06:29:58 +00:00
|
|
|
else if ("img".equals(name))
|
2019-11-07 12:30:13 +00:00
|
|
|
append("[" + node.attr("src") + "]");
|
2019-08-23 06:29:58 +00:00
|
|
|
else if ("th".equals(name) || "td".equals(name)) {
|
2019-02-10 12:01:21 +00:00
|
|
|
Node next = node.nextSibling();
|
|
|
|
if (next == null || !("th".equals(next.nodeName()) || "td".equals(next.nodeName())))
|
|
|
|
newline();
|
2019-11-07 12:30:13 +00:00
|
|
|
else
|
|
|
|
append(" ");
|
2019-02-10 12:01:21 +00:00
|
|
|
} else if ("blockquote".equals(name))
|
|
|
|
qlevel--;
|
2019-09-07 06:22:46 +00:00
|
|
|
else if ("pre".equals(name))
|
|
|
|
plevel--;
|
2019-02-10 12:01:21 +00:00
|
|
|
|
2019-01-05 11:17:33 +00:00
|
|
|
if (tails.contains(name))
|
2019-02-10 12:01:21 +00:00
|
|
|
newline();
|
|
|
|
}
|
|
|
|
|
2019-02-11 15:36:42 +00:00
|
|
|
private void append(String text) {
|
2019-11-22 19:42:45 +00:00
|
|
|
append(text, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void append(String text, boolean raw) {
|
2019-02-11 15:36:42 +00:00
|
|
|
if (tlevel != qlevel) {
|
|
|
|
newline();
|
|
|
|
tlevel = qlevel;
|
|
|
|
}
|
2019-11-22 19:42:45 +00:00
|
|
|
|
|
|
|
if (!raw && !"-- ".equals(text)) {
|
|
|
|
text = text.trim();
|
|
|
|
if (lindex > 0)
|
|
|
|
text = " " + text;
|
|
|
|
}
|
|
|
|
|
2019-02-11 15:36:42 +00:00
|
|
|
sb.append(text);
|
2019-11-22 19:42:45 +00:00
|
|
|
lindex += text.length();
|
2019-02-11 15:36:42 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 12:01:21 +00:00
|
|
|
private void newline() {
|
2019-11-22 19:42:45 +00:00
|
|
|
lindex = 0;
|
2019-02-10 12:01:21 +00:00
|
|
|
sb.append("\n");
|
2019-11-22 19:42:45 +00:00
|
|
|
|
2019-02-10 12:01:21 +00:00
|
|
|
for (int i = 0; i < qlevel; i++)
|
2019-11-22 19:42:45 +00:00
|
|
|
sb.append("> ");
|
2019-01-05 11:17:33 +00:00
|
|
|
}
|
2019-10-03 16:19:22 +00:00
|
|
|
}, JsoupEx.parse(html));
|
2019-01-05 11:17:33 +00:00
|
|
|
|
2019-02-10 12:01:21 +00:00
|
|
|
sb.append("\n");
|
|
|
|
|
2019-01-05 11:17:33 +00:00
|
|
|
return sb.toString();
|
2018-12-24 20:09:47 +00:00
|
|
|
}
|
2019-02-10 12:01:21 +00:00
|
|
|
|
2019-08-29 19:57:04 +00:00
|
|
|
static Spanned highlightHeaders(Context context, String headers) {
|
|
|
|
int colorAccent = Helper.resolveColor(context, R.attr.colorAccent);
|
|
|
|
SpannableStringBuilder ssb = new SpannableStringBuilder(headers);
|
|
|
|
int index = 0;
|
|
|
|
for (String line : headers.split("\n")) {
|
|
|
|
if (line.length() > 0 && !Character.isWhitespace(line.charAt(0))) {
|
|
|
|
int colon = line.indexOf(':');
|
|
|
|
if (colon > 0)
|
|
|
|
ssb.setSpan(new ForegroundColorSpan(colorAccent), index, index + colon, 0);
|
|
|
|
}
|
|
|
|
index += line.length() + 1;
|
|
|
|
}
|
|
|
|
return ssb;
|
|
|
|
}
|
|
|
|
|
2019-02-10 12:01:21 +00:00
|
|
|
static Spanned fromHtml(@NonNull String html) {
|
|
|
|
return fromHtml(html, null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Spanned fromHtml(@NonNull String html, @Nullable Html.ImageGetter imageGetter, @Nullable Html.TagHandler tagHandler) {
|
2019-03-26 13:37:04 +00:00
|
|
|
Spanned spanned = HtmlCompat.fromHtml(html, FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM, imageGetter, tagHandler);
|
|
|
|
|
|
|
|
int i = spanned.length();
|
|
|
|
while (i > 1 && spanned.charAt(i - 2) == '\n' && spanned.charAt(i - 1) == '\n')
|
|
|
|
i--;
|
|
|
|
if (i != spanned.length())
|
|
|
|
spanned = (Spanned) spanned.subSequence(0, i);
|
|
|
|
|
|
|
|
return spanned;
|
2019-02-10 12:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static String toHtml(Spanned spanned) {
|
2019-05-01 13:58:41 +00:00
|
|
|
String html = HtmlCompat.toHtml(spanned, TO_HTML_PARAGRAPH_LINES_CONSECUTIVE);
|
|
|
|
|
|
|
|
// @Google: why convert size to and from in a different way?
|
2019-10-03 16:19:22 +00:00
|
|
|
Document doc = JsoupEx.parse(html);
|
2019-05-01 13:58:41 +00:00
|
|
|
for (Element element : doc.select("span")) {
|
|
|
|
String style = element.attr("style");
|
|
|
|
if (style.startsWith("font-size:")) {
|
|
|
|
int colon = style.indexOf(':');
|
|
|
|
int semi = style.indexOf("em;", colon);
|
|
|
|
if (semi > colon)
|
|
|
|
try {
|
|
|
|
String hsize = style.substring(colon + 1, semi).replace(',', '.');
|
|
|
|
float size = Float.parseFloat(hsize);
|
|
|
|
element.tagName(size < 1.0f ? "small" : "big");
|
|
|
|
element.attributes().remove("style");
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:18:21 +00:00
|
|
|
return doc.html();
|
2019-02-10 12:01:21 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|