FairEmail/app/src/main/java/eu/faircode/email/HtmlHelper.java

2392 lines
100 KiB
Java
Raw Normal View History

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;
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;
2020-09-12 12:04:32 +00:00
import android.graphics.DashPathEffect;
2020-04-24 21:13:32 +00:00
import android.graphics.Paint;
2020-09-12 12:04:32 +00:00
import android.graphics.PathEffect;
import android.graphics.Typeface;
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;
2020-04-24 21:13:32 +00:00
import android.text.Layout;
2020-04-23 17:45:33 +00:00
import android.text.Spannable;
2019-08-29 19:57:04 +00:00
import android.text.SpannableStringBuilder;
2019-02-10 12:01:21 +00:00
import android.text.Spanned;
import android.text.TextPaint;
2018-12-14 09:11:45 +00:00
import android.text.TextUtils;
2020-05-03 17:29:02 +00:00
import android.text.style.AlignmentSpan;
2020-04-24 21:13:32 +00:00
import android.text.style.BulletSpan;
2019-08-29 19:57:04 +00:00
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
2020-04-24 21:13:32 +00:00
import android.text.style.LeadingMarginSpan;
import android.text.style.QuoteSpan;
import android.text.style.RelativeSizeSpan;
2020-05-04 13:21:34 +00:00
import android.text.style.ReplacementSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
2020-04-25 16:00:46 +00:00
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
2020-04-26 08:44:31 +00:00
import android.text.style.TypefaceSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
2018-12-14 09:11:45 +00:00
import android.util.Base64;
import android.util.Patterns;
2020-05-03 17:29:02 +00:00
import android.view.View;
2020-03-26 14:25:44 +00:00
import android.view.textclassifier.TextClassificationManager;
import android.view.textclassifier.TextLanguage;
2018-12-14 09:11:45 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
2019-10-04 21:04:03 +00:00
import androidx.core.content.ContextCompat;
2020-02-14 08:34:28 +00:00
import androidx.core.content.FileProvider;
2020-06-29 07:42:11 +00:00
import androidx.core.content.res.ResourcesCompat;
2020-01-26 12:41:12 +00:00
import androidx.core.graphics.ColorUtils;
import androidx.core.util.PatternsCompat;
import androidx.preference.PreferenceManager;
2020-04-21 07:04:55 +00:00
import com.steadystate.css.dom.CSSMediaRuleImpl;
2020-04-20 13:01:05 +00:00
import com.steadystate.css.dom.CSSStyleRuleImpl;
2020-04-21 07:04:55 +00:00
import com.steadystate.css.dom.MediaListImpl;
2020-04-20 13:01:05 +00:00
import com.steadystate.css.parser.CSSOMParser;
import com.steadystate.css.parser.SACParserCSS3;
import com.steadystate.css.parser.selectors.ClassConditionImpl;
import com.steadystate.css.parser.selectors.ConditionalSelectorImpl;
2020-04-22 14:32:35 +00:00
import com.steadystate.css.parser.selectors.ElementSelectorImpl;
2020-04-20 13:01:05 +00:00
2019-03-10 19:39:17 +00:00
import org.jsoup.nodes.Attribute;
2020-02-02 12:03:06 +00:00
import org.jsoup.nodes.Comment;
2018-08-02 13:33:06 +00:00
import org.jsoup.nodes.Document;
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;
2020-07-16 20:06:52 +00:00
import org.jsoup.select.Elements;
2020-02-02 12:03:06 +00:00
import org.jsoup.select.NodeFilter;
2018-08-02 13:33:06 +00:00
import org.jsoup.select.NodeTraversor;
import org.jsoup.select.NodeVisitor;
2020-04-20 13:01:05 +00:00
import org.w3c.css.sac.CSSException;
import org.w3c.css.sac.CSSParseException;
import org.w3c.css.sac.ErrorHandler;
import org.w3c.css.sac.InputSource;
import org.w3c.css.sac.Selector;
import org.w3c.dom.css.CSSRule;
import org.w3c.dom.css.CSSRuleList;
import org.w3c.dom.css.CSSStyleSheet;
2020-04-21 07:04:55 +00:00
import org.w3c.dom.stylesheets.MediaList;
2018-08-02 13:33:06 +00:00
2019-10-04 21:04:03 +00:00
import java.io.ByteArrayOutputStream;
2018-12-14 09:11:45 +00:00
import java.io.File;
2020-02-28 09:01:18 +00:00
import java.io.FileInputStream;
2018-12-14 09:11:45 +00:00
import java.io.IOException;
2020-02-28 09:01:18 +00:00
import java.io.InputStream;
2020-04-20 13:01:05 +00:00
import java.io.StringReader;
2019-09-13 11:46:52 +00:00
import java.util.ArrayList;
2019-01-05 11:17:33 +00:00
import java.util.Arrays;
2020-04-22 09:10:14 +00:00
import java.util.Date;
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;
import java.util.Map;
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
2020-08-11 16:46:26 +00:00
import static androidx.core.text.HtmlCompat.TO_HTML_PARAGRAPH_LINES_INDIVIDUAL;
2020-04-20 13:01:05 +00:00
import static org.w3c.css.sac.Condition.SAC_CLASS_CONDITION;
2019-02-10 12:01:21 +00:00
public class HtmlHelper {
private static final int PREVIEW_SIZE = 500; // characters
2020-02-20 10:37:40 +00:00
private static final int DEFAULT_FONT_SIZE = 16; // pixels
2020-04-28 16:32:10 +00:00
private static final int DEFAULT_FONT_SIZE_PT = 12; // points
2020-04-20 07:57:42 +00:00
private static final float FONT_SMALL = 0.8f;
private static final float FONT_LARGE = 1.25f;
private static final int GRAY_THRESHOLD = Math.round(255 * 0.2f);
2020-05-07 06:05:09 +00:00
private static final float MIN_LUMINANCE = 0.7f;
2019-11-14 08:15:18 +00:00
private static final int TAB_SIZE = 2;
2020-05-04 18:19:05 +00:00
private static final int MAX_ALT = 250;
2019-09-01 07:44:03 +00:00
private static final int MAX_AUTO_LINK = 250;
private static final int MAX_FORMAT_TEXT_SIZE = 200 * 1024; // characters
2020-02-14 10:06:48 +00:00
private static final int MAX_FULL_TEXT_SIZE = 1024 * 1024; // characters
2020-08-06 17:00:11 +00:00
private static final int SMALL_IMAGE_SIZE = 5; // pixels
2019-08-20 18:05:11 +00:00
private static final int TRACKING_PIXEL_SURFACE = 25; // pixels
private static final float[] HEADING_SIZES = {1.5f, 1.4f, 1.3f, 1.2f, 1.1f, 1f};
2020-05-04 13:21:34 +00:00
private static final String LINE = "----------------------------------------";
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);
}
2020-03-25 19:25:06 +00:00
static Document sanitizeCompose(Context context, String html, boolean show_images) {
try {
2020-08-11 11:21:28 +00:00
Document parsed = JsoupEx.parse(html);
2020-03-25 19:25:06 +00:00
return sanitize(context, parsed, false, show_images);
} catch (Throwable ex) {
// OutOfMemoryError
Log.e(ex);
Document document = Document.createShell("");
Element strong = document.createElement("strong");
strong.text(Log.formatThrowable(ex));
document.body().appendChild(strong);
return document;
}
2020-02-14 08:41:28 +00:00
}
2020-03-25 19:25:06 +00:00
static Document sanitizeView(Context context, Document parsed, boolean show_images) {
2019-10-20 12:15:20 +00:00
try {
2020-03-25 19:25:06 +00:00
return sanitize(context, parsed, true, show_images);
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
}
}
2020-03-25 19:25:06 +00:00
private static Document sanitize(Context context, Document parsed, boolean view, boolean show_images) {
2019-10-04 13:25:04 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String theme = prefs.getString("theme", "light");
boolean text_color = (!view || (prefs.getBoolean("text_color", true) && !"black_and_white".equals(theme)));
boolean text_size = (!view || prefs.getBoolean("text_size", true));
boolean text_font = (!view || prefs.getBoolean("text_font", true));
2020-05-03 17:29:02 +00:00
boolean text_align = prefs.getBoolean("text_align", 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);
2020-04-22 10:10:30 +00:00
boolean parse_classes = prefs.getBoolean("parse_classes", false);
2020-09-11 14:52:11 +00:00
boolean inline_images = prefs.getBoolean("inline_images", false);
2020-09-13 14:23:11 +00:00
boolean text_separators = prefs.getBoolean("text_separators", false);
2019-10-04 13:25:04 +00:00
int textColorPrimary = Helper.resolveColor(context, android.R.attr.textColorPrimary);
2020-02-09 14:04:35 +00:00
// https://chromium.googlesource.com/chromium/blink/+/master/Source/core/css/html.css
2019-05-13 09:03:15 +00:00
2020-02-02 12:03:06 +00:00
// <!--[if ...]><!--> ... <!--<![endif]-->
2020-02-04 09:10:36 +00:00
// https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/hh801214(v=vs.85)
if (!display_hidden && false)
2020-02-02 12:03:06 +00:00
parsed.filter(new NodeFilter() {
private boolean remove = false;
@Override
public FilterResult head(Node node, int depth) {
if (node instanceof Comment) {
String data = ((Comment) node).getData().trim();
if (data.startsWith("[if") && !data.endsWith("endif]")) {
remove = true;
return FilterResult.REMOVE;
} else if (remove && data.endsWith("endif]")) {
remove = false;
return FilterResult.REMOVE;
}
}
return (remove ? FilterResult.REMOVE : FilterResult.CONTINUE);
}
@Override
public FilterResult tail(Node node, int depth) {
return FilterResult.CONTINUE;
}
});
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>&nbsp;</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
2020-02-14 08:04:16 +00:00
// Limit length
2020-03-25 19:25:06 +00:00
if (view && truncate(parsed, true)) {
2020-02-14 08:04:16 +00:00
parsed.body()
2020-02-16 09:56:27 +00:00
.appendElement("br")
2020-02-14 08:04:16 +00:00
.appendElement("p")
.appendElement("em")
.text(context.getString(R.string.title_too_large));
2020-03-25 19:25:06 +00:00
parsed.body()
.appendElement("p")
.appendElement("big")
.appendElement("a")
.attr("href", "full:")
.text(context.getString(R.string.title_show_full));
2020-02-14 08:04:16 +00:00
}
2020-04-20 13:01:05 +00:00
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style
2020-04-21 07:04:55 +00:00
List<CSSStyleSheet> sheets = new ArrayList<>();
2020-04-22 10:10:30 +00:00
if (parse_classes)
2020-07-16 20:06:52 +00:00
sheets = parseStyles(parsed.head().select("style"));
2020-04-20 13:01:05 +00:00
2019-03-12 09:04:16 +00:00
Whitelist whitelist = Whitelist.relaxed()
.addTags("hr", "abbr", "big", "font", "dfn", "del", "s", "tt")
2020-04-20 13:01:05 +00:00
.addAttributes(":all", "class")
.addAttributes(":all", "style")
2020-05-05 16:43:37 +00:00
.addAttributes("div", "x-plain")
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")
2020-02-14 08:04:16 +00:00
.addProtocols("img", "src", "data")
2020-06-22 09:08:16 +00:00
.removeProtocols("a", "href", "ftp")
2020-06-21 19:22:10 +00:00
.addProtocols("a", "href", "full", "xmpp", "geo", "tel");
2019-09-23 18:13:38 +00:00
if (text_color)
2020-04-27 07:40:43 +00:00
whitelist.addAttributes("font", "color");
2020-06-28 13:59:02 +00:00
if (text_size)
whitelist.addAttributes("font", "size");
if (text_font)
whitelist.addAttributes("font", "face");
2020-05-03 17:29:02 +00:00
if (text_align)
whitelist.addTags("center").addAttributes(":all", "align");
2020-04-27 07:40:43 +00:00
if (!view)
whitelist.addProtocols("img", "src", "content");
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);
2020-09-12 07:09:07 +00:00
// Remove tracking pixels
if (disable_tracking)
removeTrackingPixels(context, document, false);
2019-09-25 10:48:02 +00:00
// Font
for (Element font : document.select("font")) {
2020-04-20 07:57:42 +00:00
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/font
2020-04-26 16:43:20 +00:00
String style = font.attr("style");
2019-09-25 10:48:02 +00:00
String color = font.attr("color");
2020-04-20 07:57:42 +00:00
String size = font.attr("size");
2020-06-28 13:59:02 +00:00
String face = font.attr("face");
2020-04-20 07:57:42 +00:00
2020-04-26 16:43:20 +00:00
style = style.trim();
if (!TextUtils.isEmpty(style) && !style.endsWith(";"))
style += ";";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
font.removeAttr("color");
2020-04-20 07:57:42 +00:00
font.removeAttr("size");
2020-06-28 13:59:02 +00:00
font.removeAttr("face");
2020-04-20 07:57:42 +00:00
2020-04-26 16:43:20 +00:00
StringBuilder sb = new StringBuilder(style);
2020-04-20 07:57:42 +00:00
if (!TextUtils.isEmpty(color))
sb.append("color:").append(color).append(";");
if (!TextUtils.isEmpty(size)) {
try {
int s = Integer.parseInt(size);
if (s < 3)
size = "small";
else if (s > 3)
size = "large";
else
size = "medium";
sb.append("font-size:").append(size).append(";");
} catch (NumberFormatException ex) {
2020-05-28 06:48:28 +00:00
Log.i(ex);
2020-04-20 07:57:42 +00:00
}
}
2020-06-28 13:59:02 +00:00
if (!TextUtils.isEmpty(face))
sb.append("font-family:").append(face).append(";");
2020-04-20 07:57:42 +00:00
font.attr("style", sb.toString());
2019-09-25 10:48:02 +00:00
font.tagName("span");
}
2019-10-16 09:05:38 +00:00
// Sanitize styles
for (Element element : document.select("*")) {
2020-04-22 14:32:35 +00:00
// Class style
2020-07-16 20:25:16 +00:00
String tag = element.tagName();
String clazz = element.attr("class");
String style = processStyles(tag, clazz, null, sheets);
2020-04-20 13:01:05 +00:00
2020-04-22 14:32:35 +00:00
// Element style
style = mergeStyles(style, element.attr("style"));
2020-04-20 13:01:05 +00:00
2020-05-03 17:29:02 +00:00
if (text_align) {
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/center
if ("center".equals(element.tagName())) {
style = mergeStyles(style, "text-align:center");
element.tagName("div");
2020-05-05 11:30:48 +00:00
} else if ("table".equals(element.tagName()))
style = mergeStyles(style, "text-align:left");
else {
// https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
String align = element.attr("align");
if (!TextUtils.isEmpty(align))
style = mergeStyles(style, "text-align:" + align);
2020-05-03 17:29:02 +00:00
}
}
2020-04-20 13:01:05 +00:00
// Process 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) {
2020-04-20 13:01:05 +00:00
int colon = param.indexOf(':');
if (colon > 0) {
String key = param.substring(0, colon).trim().toLowerCase(Locale.ROOT);
String value = param.substring(colon + 1).toLowerCase(Locale.ROOT)
2020-02-02 10:11:44 +00:00
.replace("!important", "")
2020-02-02 12:01:46 +00:00
.trim()
2020-02-02 10:11:44 +00:00
.replaceAll("\\s+", " ");
2019-10-16 09:05:38 +00:00
switch (key) {
2019-09-23 17:51:17 +00:00
case "color":
// https://developer.mozilla.org/en-US/docs/Web/CSS/color
if (!text_color)
continue;
2020-06-29 12:21:19 +00:00
Integer color = parseColor(value);
2020-05-03 19:36:48 +00:00
if (color == null)
element.removeAttr("color");
else {
2020-06-29 12:21:19 +00:00
if (view)
color = adjustColor(dark, textColorPrimary, color);
2020-02-08 17:45:28 +00:00
// fromHtml does not support transparency
2020-02-15 18:22:28 +00:00
String c = String.format("#%06x", color);
2019-09-24 12:46:12 +00:00
sb.append("color:").append(c).append(";");
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 "font-size":
// https://developer.mozilla.org/en-US/docs/Web/CSS/font-size
if (!text_size)
continue;
float current = 1.0f;
2020-02-20 10:28:55 +00:00
Element parent = element.parent();
2020-04-28 07:02:07 +00:00
while (parent != null) {
String xFontSize = parent.attr("x-font-size");
if (!TextUtils.isEmpty(xFontSize)) {
current = Float.parseFloat(xFontSize);
break;
2020-02-10 08:29:47 +00:00
}
2020-04-28 07:02:07 +00:00
parent = parent.parent();
}
2020-04-28 07:02:07 +00:00
Float fsize = getFontSize(value, current);
if (fsize != null && fsize != 0) {
2020-04-28 07:02:07 +00:00
element.attr("x-font-size", Float.toString(fsize));
element.attr("x-font-size-rel", Float.toString(fsize / current));
}
break;
2020-02-08 19:39:51 +00:00
case "font-weight":
2020-02-10 08:29:47 +00:00
if (element.parent() != null) {
Integer fweight = getFontWeight(value);
if (fweight != null && fweight >= 600) {
Element strong = new Element("strong");
element.replaceWith(strong);
strong.appendChild(element);
}
2020-02-08 19:39:51 +00:00
}
2019-09-23 20:07:22 +00:00
break;
2019-09-24 12:46:12 +00:00
2020-06-28 13:59:02 +00:00
case "font-family":
if (!text_font)
continue;
// https://developer.mozilla.org/en-US/docs/Web/CSS/font-family
sb.append(key).append(":").append(value).append(";");
break;
case "text-decoration":
// https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration
if (value.contains("line-through"))
sb.append("text-decoration:line-through;");
2019-09-24 12:46:12 +00:00
break;
2019-10-16 09:05:38 +00:00
case "display":
// https://developer.mozilla.org/en-US/docs/Web/CSS/display
2020-02-10 18:55:52 +00:00
if (element.parent() != null &&
!display_hidden && "none".equals(value)) {
Log.i("Removing display none " + element.tagName());
element.remove();
2019-10-16 09:05:38 +00:00
}
2020-02-05 09:31:20 +00:00
if ("inline".equals(value) || "inline-block".equals(value)) {
if (element.nextSibling() != null)
2020-05-05 16:47:06 +00:00
element.attr("x-inline", "true");
2020-02-05 09:31:20 +00:00
}
2019-10-16 09:05:38 +00:00
break;
case "height":
case "width":
//case "font-size":
//case "line-height":
2020-02-10 18:55:52 +00:00
if (element.parent() != null && !display_hidden) {
Float s = getFontSize(value, 1.0f);
if (s != null && s == 0) {
2020-02-10 18:55:52 +00:00
Log.i("Removing no height/width " + element.tagName());
element.remove();
}
}
break;
2020-04-13 17:31:02 +00:00
2020-04-21 10:27:32 +00:00
case "margin":
case "padding":
case "margin-top":
case "margin-bottom":
2020-04-13 17:31:02 +00:00
case "padding-top":
case "padding-bottom":
2020-04-21 10:27:32 +00:00
// https://developer.mozilla.org/en-US/docs/Web/CSS/margin
// https://developer.mozilla.org/en-US/docs/Web/CSS/padding
2020-04-14 08:26:39 +00:00
if (element.isBlock() && hasVisibleContent(element.childNodes())) {
2020-04-21 10:27:32 +00:00
Float[] p = new Float[4];
String[] v = value.split(" ");
2020-08-16 09:50:52 +00:00
for (int i = 0; i < v.length && i < p.length; i++)
p[i] = getFontSize(v[i], 1.0f);
2020-04-21 10:27:32 +00:00
if (v.length == 1) {
p[1] = p[0];
p[2] = p[0];
p[3] = p[0];
} else if (v.length == 2) {
p[2] = p[0];
p[3] = p[1];
}
if (key.endsWith("top"))
p[2] = null;
else if (key.endsWith("bottom"))
p[0] = null;
if (p[0] != null)
if (p[0] == 0)
element.attr("x-line-before", "false");
else if (p[0] > 0.5)
element.attr("x-line-before", "true");
if (p[2] != null)
if (p[2] == 0)
element.attr("x-line-after", "false");
else if (p[2] > 0.5)
element.attr("x-line-after", "true");
2020-04-13 17:31:02 +00:00
}
break;
2020-05-03 17:29:02 +00:00
case "text-align":
// https://developer.mozilla.org/en-US/docs/Web/CSS/text-align
if (text_align)
sb.append(key).append(':').append(value).append(';');
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");
2020-01-31 12:52:57 +00:00
else {
2019-10-16 09:05:38 +00:00
element.attr("style", sb.toString());
2020-01-31 12:52:57 +00:00
if (BuildConfig.DEBUG)
Log.i("Style=" + sb);
}
2019-09-23 17:51:17 +00:00
}
}
2020-02-24 19:34:16 +00:00
// Remove trailing br from div
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();
}
2020-02-24 19:34:16 +00:00
// Replace headings
if (!text_size)
for (Element h : document.select("h1,h2,h3,h4,h5,h6")) {
h.appendElement("br");
h.appendElement("br");
h.tagName("strong");
}
2019-09-12 08:45:54 +00:00
// Paragraphs
2019-11-11 10:32:47 +00:00
for (Element p : document.select("p")) {
if (!"false".equals(p.attr("x-line-after")))
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
// Short inline quotes
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/q
2019-03-10 15:21:46 +00:00
for (Element q : document.select("q")) {
q.tagName("a");
q.attr("href", q.attr("cite"));
q.removeAttr("cite");
2019-03-10 15:21:46 +00:00
}
// Citation
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/cite
for (Element cite : document.select("cite")) {
cite.prependText("\"");
cite.appendText("\"");
cite.tagName("em");
}
// Definition
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dfn
for (Element dfn : document.select("dfn"))
dfn.tagName("em");
2019-03-10 15:21:46 +00:00
// Pre formatted text
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre
2020-05-02 15:51:15 +00:00
for (Element pre : document.select("pre")) {
pre.html(formatPre(pre.wholeText()));
pre.tagName("div");
2020-05-05 16:43:37 +00:00
pre.attr("x-plain", "true");
2020-05-02 15:51:15 +00:00
}
2019-03-10 11:14:39 +00:00
2019-03-10 15:21:46 +00:00
// Code
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/code
2019-09-21 16:10:41 +00:00
document.select("code").tagName("strong");
2019-03-10 15:21:46 +00:00
// Lines
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/hr
2020-05-04 13:21:34 +00:00
if (!view)
for (Element hr : document.select("hr")) {
hr.tagName("div");
hr.text(LINE);
}
2019-02-10 12:01:21 +00:00
2019-03-10 15:21:46 +00:00
// Descriptions
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dl
2019-03-10 15:21:46 +00:00
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
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/abbr
2019-03-12 12:13:53 +00:00
document.select("abbr").tagName("u");
2019-06-30 07:41:18 +00:00
// Subscript/Superscript
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sub
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sup
2020-04-27 15:50:58 +00:00
if (!view)
2020-04-25 16:00:46 +00:00
for (Element subp : document.select("sub,sup"))
subp.tagName("small");
2019-06-30 07:41:18 +00:00
2019-03-30 16:48:04 +00:00
// Tables
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table
2019-03-30 16:48:04 +00:00
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)
2020-05-18 14:56:50 +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()))
2020-09-13 06:34:48 +00:00
if (text_separators)
2020-09-12 12:04:32 +00:00
row.appendElement("hr")
.attr("x-dashed", "true");
else
row.appendElement("br");
2019-09-12 13:28:21 +00:00
}
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");
2020-04-28 05:52:28 +00:00
for (Element hf : document.select("thead,tfoot"))
hf.tagName("span");
2019-03-30 16:48:04 +00:00
2019-09-13 11:46:52 +00:00
// Images
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img
for (Element img : document.select("img")) {
2019-10-14 06:50:27 +00:00
String alt = img.attr("alt");
String src = img.attr("src");
2020-05-05 16:49:55 +00:00
String tracking = img.attr("x-tracking");
2019-10-14 06:50:27 +00:00
2020-05-04 18:19:05 +00:00
if (alt.length() > MAX_ALT)
alt = alt.substring(0, MAX_ALT) + "";
2020-09-11 14:52:11 +00:00
if (!show_images && !(inline_images && src.startsWith("cid:")) && !TextUtils.isEmpty(alt))
2019-10-19 08:19:05 +00:00
if (TextUtils.isEmpty(tracking))
2020-02-10 07:41:55 +00:00
img.appendText("[" + alt + "]");
2019-10-19 08:19:05 +00:00
else {
Element a = document.createElement("a");
a.attr("href", tracking);
2020-05-01 11:52:13 +00:00
a.text("[" + alt + "]");
2019-10-19 08:19:05 +00:00
img.appendChild(a);
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-03-30 16:48:04 +00:00
2019-06-30 07:41:18 +00:00
// Autolink
2020-03-25 19:25:06 +00:00
if (view) {
// https://en.wikipedia.org/wiki/List_of_URI_schemes
2020-06-21 14:05:09 +00:00
// xmpp:[<user>]@<host>[:<port>]/[<resource>][?<query>]
// geo:<lat>,<lon>[,<alt>][;u=<uncertainty>]
// tel:<phonenumber>
2019-11-15 07:43:35 +00:00
final Pattern pattern = Pattern.compile(
2020-06-21 15:21:17 +00:00
"(((?i:mailto):)?" + PatternsCompat.AUTOLINK_EMAIL_ADDRESS.pattern() + ")|" +
PatternsCompat.AUTOLINK_WEB_URL.pattern()
.replace("(?i:http|https|rtsp)://",
2020-06-21 19:22:10 +00:00
"(((?i:http|https)://)|((?i:xmpp):))") + "|" +
"(?i:geo:\\d+,\\d+(,\\d+)?(;u=\\d+)?)|" +
"(?i:tel:" + Patterns.PHONE.pattern() + ")");
2019-11-15 07:43:35 +00:00
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
String group = matcher.group();
int start = matcher.start();
int end = matcher.end();
// Workaround for links between parenthesis
if (group.endsWith(")") &&
start > 0 && text.charAt(start - 1) == '(') {
group = group.substring(0, group.length() - 1);
end--;
}
boolean email = group.contains("@") && !group.contains(":");
Log.d("Web url=" + group + " " + start + "..." + end + "/" + text.length() +
2019-12-07 16:02:42 +00:00
" 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, end));
2019-11-15 07:43:35 +00:00
else {
span.appendText(text.substring(pos, 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:" : "") + group);
a.text(group);
2019-11-15 07:43:35 +00:00
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
pos = end;
2019-11-15 07:43:35 +00:00
} 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
2020-02-06 13:24:15 +00:00
for (Element div : document.select("div")) {
2020-05-05 16:47:06 +00:00
boolean inline = Boolean.parseBoolean(div.attr("x-inline"));
2020-02-06 13:24:15 +00:00
if (inline)
div.tagName("span");
}
2019-09-12 08:45:54 +00:00
// Selective new lines
2020-02-06 09:35:35 +00:00
for (Element div : document.select("div")) {
Node prev = div.previousSibling();
if (prev != null && hasVisibleContent(Arrays.asList(prev)))
div.prependElement("br");
if (hasVisibleContent(div.childNodes()))
2019-09-11 17:52:04 +00:00
div.appendElement("br");
2020-02-06 09:35:35 +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
2020-05-05 16:48:37 +00:00
for (Element e : document.select("*[x-line-before],*[x-line-after]")) {
if ("true".equals(e.attr("x-line-before"))) {
2020-04-21 17:44:35 +00:00
Element prev = e.previousElementSibling();
if (prev == null || !"br".equals(prev.tagName()))
e.prependElement("br");
}
if ("true".equals(e.attr("x-line-after"))) {
2020-04-21 17:44:35 +00:00
Element next = e.nextElementSibling();
if (next == null || !"br".equals(next.tagName()))
e.appendElement("br");
}
2020-08-11 17:23:08 +00:00
e.removeAttr("x-line-before");
e.removeAttr("x-line-after");
2020-04-21 10:27:32 +00:00
}
2020-04-13 17:31:02 +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
2020-07-16 20:06:52 +00:00
static List<CSSStyleSheet> parseStyles(Elements styles) {
List<CSSStyleSheet> sheets = new ArrayList<>();
for (Element style : styles) {
if (BuildConfig.DEBUG)
Log.i("Style=" + style.data());
try {
InputSource source = new InputSource(new StringReader(style.data()));
String media = style.attr("media");
if (!TextUtils.isEmpty(media))
source.setMedia(media);
CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
parser.setErrorHandler(new ErrorHandler() {
@Override
public void warning(CSSParseException ex) throws CSSException {
Log.i("CSS warning=" + ex.getMessage());
}
@Override
public void error(CSSParseException ex) throws CSSException {
Log.i("CSS error=" + ex.getMessage());
}
@Override
public void fatalError(CSSParseException ex) throws CSSException {
Log.w(ex);
}
});
long start = new Date().getTime();
sheets.add(parser.parseStyleSheet(source, null, null));
long elapsed = new Date().getTime() - start;
Log.i("Style parse=" + elapsed + " ms");
} catch (Throwable ex) {
Log.w(ex);
}
}
return sheets;
}
2020-07-16 20:25:16 +00:00
static String processStyles(String tag, String clazz, String style, List<CSSStyleSheet> sheets) {
2020-04-21 07:04:55 +00:00
for (CSSStyleSheet sheet : sheets)
2020-04-22 14:32:35 +00:00
if (isScreenMedia(sheet.getMedia())) {
style = processStyles(null, clazz, style, sheet.getCssRules(), Selector.SAC_ELEMENT_NODE_SELECTOR);
style = processStyles(tag, clazz, style, sheet.getCssRules(), Selector.SAC_ELEMENT_NODE_SELECTOR);
style = processStyles(tag, clazz, style, sheet.getCssRules(), Selector.SAC_CONDITIONAL_SELECTOR);
}
2020-04-21 07:04:55 +00:00
return style;
}
2020-04-22 14:32:35 +00:00
private static String processStyles(String tag, String clazz, String style, CSSRuleList rules, int stype) {
2020-04-21 07:04:55 +00:00
for (int i = 0; rules != null && i < rules.getLength(); i++) {
CSSRule rule = rules.item(i);
switch (rule.getType()) {
case CSSRule.STYLE_RULE:
CSSStyleRuleImpl srule = (CSSStyleRuleImpl) rule;
for (int j = 0; j < srule.getSelectors().getLength(); j++) {
Selector selector = srule.getSelectors().item(j);
2020-04-22 14:32:35 +00:00
if (selector.getSelectorType() != stype)
continue;
2020-04-21 07:04:55 +00:00
switch (selector.getSelectorType()) {
2020-04-22 14:32:35 +00:00
case Selector.SAC_ELEMENT_NODE_SELECTOR:
ElementSelectorImpl eselector = (ElementSelectorImpl) selector;
2020-04-22 14:54:50 +00:00
if (tag == null
? eselector.getLocalName() == null
2020-04-22 14:32:35 +00:00
: tag.equals(eselector.getLocalName()))
style = mergeStyles(style, srule.getStyle().getCssText());
break;
2020-04-21 07:04:55 +00:00
case Selector.SAC_CONDITIONAL_SELECTOR:
ConditionalSelectorImpl cselector = (ConditionalSelectorImpl) selector;
if (cselector.getCondition().getConditionType() == SAC_CLASS_CONDITION) {
ClassConditionImpl ccondition = (ClassConditionImpl) cselector.getCondition();
if (clazz.equals(ccondition.getValue()))
2020-04-22 14:32:35 +00:00
style = mergeStyles(style, srule.getStyle().getCssText());
2020-04-21 07:04:55 +00:00
}
break;
}
}
break;
case CSSRule.MEDIA_RULE:
CSSMediaRuleImpl mrule = (CSSMediaRuleImpl) rule;
if (isScreenMedia(mrule.getMedia()))
2020-04-22 14:32:35 +00:00
style = processStyles(tag, clazz, style, mrule.getCssRules(), stype);
2020-04-21 07:04:55 +00:00
break;
}
}
return style;
}
private static boolean isScreenMedia(MediaList media) {
// https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
2020-04-21 09:41:52 +00:00
// https://developers.google.com/gmail/design/reference/supported_css#supported_types
2020-04-21 07:04:55 +00:00
if (media instanceof MediaListImpl) {
MediaListImpl _media = (MediaListImpl) media;
for (int i = 0; i < _media.getLength(); i++) {
String query = _media.mediaQuery(i).getCssText(null);
if ("all".equals(query) ||
"screen".equals(query) || "only screen".equals(query))
return true;
}
} else
Log.e("Media class=" + media.getClass().getName());
return false;
}
2020-07-16 20:25:16 +00:00
static String mergeStyles(String base, String style) {
2020-04-21 18:29:58 +00:00
return mergeStyles(base, style, null);
}
private static String mergeStyles(String base, String style, String selector) {
2020-04-20 13:01:05 +00:00
Map<String, String> result = new HashMap<>();
List<String> params = new ArrayList<>();
if (!TextUtils.isEmpty(base))
params.addAll(Arrays.asList(base.split(";")));
if (!TextUtils.isEmpty(style))
params.addAll(Arrays.asList(style.split(";")));
for (String param : params) {
int colon = param.indexOf(':');
if (colon > 0) {
String key = param.substring(0, colon).trim().toLowerCase(Locale.ROOT);
2020-04-21 18:29:58 +00:00
if (selector == null || selector.equals(key))
result.put(key, param);
2020-04-20 13:01:05 +00:00
} else
Log.w("Invalid style param=" + param);
}
return TextUtils.join(";", result.values());
}
private static Integer getFontWeight(String value) {
2020-04-30 07:28:24 +00:00
// https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight
if (TextUtils.isEmpty(value))
return null;
value = value.toLowerCase(Locale.ROOT).trim();
switch (value) {
2020-04-30 07:28:24 +00:00
case "thin":
return 100;
2020-04-26 14:02:21 +00:00
case "light":
case "lighter":
return 300;
case "normal":
2020-04-28 05:53:22 +00:00
case "regular":
return 400;
case "bolder":
2020-04-28 05:53:22 +00:00
case "strong":
return 600;
case "bold":
return 700;
2020-04-30 07:28:24 +00:00
case "heavy":
return 900;
2020-04-28 18:13:49 +00:00
case "none":
2020-04-30 07:28:24 +00:00
case "auto":
2020-05-04 16:27:46 +00:00
case "unset":
2020-04-30 07:28:24 +00:00
case "initial":
2020-04-27 14:23:32 +00:00
case "inherit":
return null;
}
try {
return Integer.parseInt(value);
2020-04-25 18:35:45 +00:00
} catch (NumberFormatException ex) {
2020-05-28 06:48:28 +00:00
Log.i(ex);
}
return null;
}
private static Float getFontSize(String value, float current) {
2020-04-22 08:50:58 +00:00
// https://developer.mozilla.org/en-US/docs/Web/CSS/font-size
if (TextUtils.isEmpty(value))
return null;
2020-04-22 08:50:58 +00:00
if (value.contains("calc") ||
2020-04-30 07:28:24 +00:00
"none".equals(value) ||
2020-04-22 08:50:58 +00:00
"auto".equals(value) ||
2020-05-04 16:27:46 +00:00
"unset".equals(value) ||
2020-04-22 12:31:12 +00:00
"initial".equals(value) ||
2020-04-22 08:50:58 +00:00
"inherit".equals(value))
return null;
2020-04-20 07:57:42 +00:00
// Absolute
switch (value) {
case "xx-small":
case "x-small":
case "small":
return FONT_SMALL;
case "medium":
return 1.0f;
case "large":
case "x-large":
case "xx-large":
case "xxx-large":
return FONT_LARGE;
}
// Relative
switch (value) {
case "smaller":
2020-04-28 07:02:07 +00:00
return FONT_SMALL * current;
2020-04-20 07:57:42 +00:00
case "larger":
2020-04-28 07:02:07 +00:00
return FONT_LARGE * current;
2020-04-20 07:57:42 +00:00
}
try {
2020-04-20 07:57:42 +00:00
if (value.endsWith("%"))
2020-04-28 07:02:07 +00:00
return Float.parseFloat(value.substring(0, value.length() - 1).trim()) / 100 * current;
if (value.endsWith("em"))
2020-04-28 07:02:07 +00:00
return Float.parseFloat(value.substring(0, value.length() - 2).trim()) * current;
2020-02-20 10:28:55 +00:00
if (value.endsWith("rem"))
return Float.parseFloat(value.substring(0, value.length() - 3).trim());
2020-04-28 16:32:10 +00:00
if (value.endsWith("pt"))
return Float.parseFloat(value.substring(0, value.length() - 2).trim()) / DEFAULT_FONT_SIZE_PT;
if (value.endsWith("px"))
2020-04-22 12:31:12 +00:00
return Float.parseFloat(value.substring(0, value.length() - 2).trim()) / DEFAULT_FONT_SIZE;
2020-06-16 18:39:00 +00:00
// https://www.w3.org/Style/Examples/007/units.en.html
if (value.endsWith("pc")) // 6 pc = 72 pt
return Float.parseFloat(value.substring(0, value.length() - 2).trim()) / 12 / DEFAULT_FONT_SIZE_PT;
if (value.endsWith("cm")) // 1 inch = 2.54 cm
return Float.parseFloat(value.substring(0, value.length() - 2).trim()) / 2.54f / 72 / DEFAULT_FONT_SIZE_PT;
if (value.endsWith("in")) // 1 inch = 72pt
return Float.parseFloat(value.substring(0, value.length() - 2).trim()) / 72 / DEFAULT_FONT_SIZE_PT;
2020-04-22 12:31:12 +00:00
return Float.parseFloat(value.trim()) / DEFAULT_FONT_SIZE;
2020-04-20 07:57:42 +00:00
} catch (NumberFormatException ex) {
2020-04-24 10:07:29 +00:00
Log.i(ex);
2020-04-22 12:31:12 +00:00
return null;
}
}
2020-06-29 12:21:19 +00:00
private static Integer parseColor(@NonNull String value) {
2020-01-31 12:52:57 +00:00
// https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
String c = value
2020-02-04 09:19:17 +00:00
.replace("null", "")
2020-01-31 12:52:57 +00:00
.replace("none", "")
.replace("unset", "")
2020-02-10 08:34:15 +00:00
.replace("auto", "")
2020-01-31 12:52:57 +00:00
.replace("inherit", "")
.replace("initial", "")
.replace("windowtext", "")
2020-02-07 08:58:22 +00:00
.replace("currentcolor", "")
2020-01-31 12:52:57 +00:00
.replace("transparent", "")
2020-02-04 09:19:17 +00:00
.replaceAll("[^a-z0-9(),.%#]", "")
.replaceAll("#+", "#");
2020-01-31 12:52:57 +00:00
Integer color = null;
try {
if (TextUtils.isEmpty(c))
return null;
2020-02-04 09:00:07 +00:00
else if (c.startsWith("#")) {
2020-02-04 09:19:17 +00:00
if (c.length() > 1) {
String code = c.substring(1);
if (x11ColorMap.containsKey(code)) // workaround
2020-02-08 17:45:28 +00:00
color = x11ColorMap.get(code);
2020-02-04 09:19:17 +00:00
else
2020-02-15 19:05:12 +00:00
color = Long.decode(c).intValue();
2020-02-04 09:19:17 +00:00
}
2020-02-04 09:00:07 +00:00
} else if (c.startsWith("rgb") || c.startsWith("hsl")) {
2020-01-31 12:52:57 +00:00
int s = c.indexOf("(");
int e = c.indexOf(")");
if (s > 0 && e > s) {
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)
color = Color.rgb(
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});
}
} else if (x11ColorMap.containsKey(c))
2020-02-08 17:45:28 +00:00
color = x11ColorMap.get(c);
2020-01-31 12:52:57 +00:00
else
try {
color = Color.parseColor(c);
} catch (IllegalArgumentException ex) {
2020-02-04 09:00:07 +00:00
// Workaround
2020-02-15 19:05:12 +00:00
color = Long.decode("#" + c).intValue();
2020-01-31 12:52:57 +00:00
}
if (BuildConfig.DEBUG)
Log.i("Color " + c + "=" + (color == null ? null : Long.toHexString(color)));
} catch (Throwable ex) {
2020-05-28 06:48:28 +00:00
Log.i("Color=" + c + ": " + ex);
2020-01-31 12:52:57 +00:00
}
2020-06-29 12:21:19 +00:00
return color;
}
2020-01-31 13:45:51 +00:00
2020-06-29 12:21:19 +00:00
private static Integer adjustColor(boolean dark, int textColorPrimary, Integer color) {
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
if (r == g && r == b && (dark ? 255 - r : r) < GRAY_THRESHOLD)
color = textColorPrimary;
else
color = Helper.adjustLuminance(color, dark, MIN_LUMINANCE);
2020-01-31 12:52:57 +00:00
2020-06-29 12:21:19 +00:00
return (color & 0xFFFFFF);
2020-01-31 12:52:57 +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;
2020-09-12 10:19:34 +00:00
if (element.isBlock())
return false;
if (element.hasText())
2019-09-12 08:45:54 +00:00
return true;
2020-09-12 10:19:34 +00:00
if (element.selectFirst("img[src~=.+]") != null)
return true;
for (Element a : element.select("a[href~=.+]"))
if (a.childNodes().size() > 0)
return true;
2019-09-12 08:45:54 +00:00
}
return false;
2019-09-02 17:01:17 +00:00
}
2020-04-07 17:43:02 +00:00
// https://tools.ietf.org/html/rfc3676
static String flow(String text) {
boolean continuation = false;
StringBuilder flowed = new StringBuilder();
for (String line : text.split("\\r?\\n")) {
if (continuation)
while (line.startsWith(">")) {
line = line.substring(1);
if (line.startsWith(" "))
line = line.substring(1);
}
continuation = (line.endsWith(" ") && !"-- ".equals(line));
flowed.append(line);
if (!continuation)
flowed.append("\r\n");
}
return flowed.toString();
}
2019-11-19 08:57:55 +00:00
static String formatPre(String text) {
2020-05-22 18:07:58 +00:00
return formatPre(text, true);
}
static String formatPre(String text, boolean quote) {
2019-11-19 08:57:55 +00:00
int level = 0;
StringBuilder sb = new StringBuilder();
String[] lines = text.split("\\r?\\n");
for (String line : lines) {
// Opening quotes
2020-05-20 08:56:05 +00:00
// https://tools.ietf.org/html/rfc3676#section-4.5
2020-05-22 18:07:58 +00:00
if (quote) {
int tlevel = 0;
while (line.startsWith(">")) {
tlevel++;
if (tlevel > level)
sb.append("<blockquote>");
2019-11-19 08:57:55 +00:00
2020-05-22 18:07:58 +00:00
line = line.substring(1); // >
2020-05-20 13:28:19 +00:00
2020-05-22 18:07:58 +00:00
if (line.startsWith(" >"))
line = line.substring(1);
}
if (tlevel > 0)
if (line.length() > 0 && line.charAt(0) == ' ')
line = line.substring(1);
2019-11-19 08:57:55 +00:00
2020-05-22 18:07:58 +00:00
// Closing quotes
for (int i = 0; i < level - tlevel; i++)
sb.append("</blockquote>");
level = tlevel;
}
2019-11-19 08:57:55 +00:00
// Tabs characters
StringBuilder l = new StringBuilder();
for (int j = 0; j < line.length(); j++) {
char kar = line.charAt(j);
if (kar == '\t') {
2020-05-05 14:15:49 +00:00
l.append(' ');
2019-11-19 08:57:55 +00:00
while (l.length() % TAB_SIZE != 0)
2020-05-05 14:15:49 +00:00
l.append(' ');
2019-11-19 08:57:55 +00:00
} else
l.append(kar);
}
line = l.toString();
// Html characters
2020-05-02 09:14:17 +00:00
// This will handle spaces / word wrapping as well
2019-11-19 08:57:55 +00:00
line = Html.escapeHtml(line);
2020-05-02 09:14:17 +00:00
sb.append(line);
2019-11-19 08:57:55 +00:00
sb.append("<br>");
}
// Closing quotes
for (int i = 0; i < level; i++)
sb.append("</blockquote>");
return sb.toString();
}
static void removeTrackingPixels(Context context, Document document, boolean full) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean disconnect_images = prefs.getBoolean("disconnect_images", false);
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
// 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);
}
}
2019-10-04 13:25:04 +00:00
// Images
for (Element img : document.select("img")) {
2020-05-05 16:49:55 +00:00
img.removeAttr("x-tracking");
2019-10-04 13:25:04 +00:00
String src = img.attr("src");
if (TextUtils.isEmpty(src)) {
if (!full)
img.remove();
continue;
}
Uri uri = Uri.parse(src);
String host = uri.getHost();
if (host == null || hosts.contains(host)) {
2020-08-06 17:00:11 +00:00
if (!full) {
// Remove spacer, etc
Integer width = Helper.parseInt(img.attr("width").trim());
Integer height = Helper.parseInt(img.attr("height").trim());
if ((width != null && width <= SMALL_IMAGE_SIZE) ||
(height != null && height <= SMALL_IMAGE_SIZE))
img.remove();
}
continue;
}
if (isTrackingPixel(img) ||
(disconnect_images && DisconnectBlacklist.isTracking(host))) {
img.attr("src", sb.toString());
img.attr("alt", context.getString(R.string.title_legend_tracking_pixel));
img.attr("height", "24");
img.attr("width", "24");
img.attr("style", "display:block !important; width:24px !important; height:24px !important;");
img.attr("x-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
2020-02-28 09:01:18 +00:00
static void embedInlineImages(Context context, long id, Document document, boolean local) 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);
2020-02-28 09:01:18 +00:00
if (local) {
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file);
img.attr("src", uri.toString());
Log.i("Inline image uri=" + uri);
} else {
try (InputStream is = new FileInputStream(file)) {
byte[] bytes = new byte[(int) file.length()];
if (is.read(bytes) != bytes.length)
throw new IOException("length");
StringBuilder sb = new StringBuilder();
sb.append("data:");
sb.append(attachment.type);
sb.append(";base64,");
sb.append(Base64.encodeToString(bytes, Base64.NO_WRAP));
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
}
}
static void setViewport(Document document, boolean overview) {
2019-10-08 17:54:52 +00:00
// https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag
Elements meta = document.head().select("meta").select("[name=viewport]");
2020-08-28 15:03:03 +00:00
if (overview) // fit width
meta.remove();
2020-08-28 15:03:03 +00:00
else {
if (meta.size() == 1) {
String content = meta.attr("content")
.toLowerCase()
.replace(" ", "")
.replace("user-scalable=no", "user-scalable=yes");
meta.attr("content", content);
} else {
meta.remove();
document.head().prependElement("meta")
.attr("name", "viewport")
.attr("content", "width=device-width, initial-scale=1.0");
2020-08-28 15:03:03 +00:00
}
}
2019-10-08 17:54:52 +00:00
Log.d(document.head().html());
}
2020-03-26 14:25:44 +00:00
static String getLanguage(Context context, String body) {
2020-02-20 09:35:01 +00:00
try {
2020-03-26 14:25:44 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2020-03-26 19:28:17 +00:00
boolean language_detection = prefs.getBoolean("language_detection", false);
if (!language_detection)
2020-03-26 14:25:44 +00:00
return null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
TextClassificationManager tcm =
(TextClassificationManager) context.getSystemService(Context.TEXT_CLASSIFICATION_SERVICE);
if (tcm == null)
return null;
String text = getPreview(body);
2020-06-23 18:33:15 +00:00
if (text == null)
2020-03-26 14:25:44 +00:00
return null;
TextLanguage.Request trequest = new TextLanguage.Request.Builder(text).build();
TextLanguage tlanguage = tcm.getTextClassifier().detectLanguage(trequest);
if (tlanguage.getLocaleHypothesisCount() > 0)
return tlanguage.getLocale(0).toLocale().getLanguage();
}
return null;
} catch (Throwable ex) {
2020-02-20 09:35:01 +00:00
Log.e(ex);
return null;
}
}
2018-12-24 20:09:47 +00:00
static String getPreview(String body) {
2019-11-11 19:23:34 +00:00
try {
2020-02-20 09:35:01 +00:00
if (body == null)
return null;
Document d = JsoupEx.parse(body);
return _getText(d, false);
2019-11-11 19:23:34 +00:00
} catch (OutOfMemoryError ex) {
Log.e(ex);
return null;
}
}
2020-02-20 09:35:01 +00:00
@Deprecated
2020-02-11 19:07:00 +00:00
static String getFullText(String body) {
try {
2020-02-20 09:35:01 +00:00
if (body == null)
return null;
Document d = JsoupEx.parse(body);
return _getText(d, true);
2020-02-11 19:07:00 +00:00
} catch (OutOfMemoryError ex) {
Log.e(ex);
return null;
}
}
2020-02-20 09:35:01 +00:00
static String getFullText(File file) throws IOException {
try {
Document d = JsoupEx.parse(file);
return _getText(d, true);
} catch (OutOfMemoryError ex) {
Log.e(ex);
2019-10-03 16:07:56 +00:00
return null;
2020-02-20 09:35:01 +00:00
}
}
2019-10-03 16:07:56 +00:00
2020-02-20 09:35:01 +00:00
private static String _getText(Document d, boolean full) {
2020-02-14 10:06:48 +00:00
truncate(d, !full);
for (Element bq : d.select("blockquote")) {
bq.prependChild(new TextNode("["));
bq.appendChild(new TextNode("]"));
}
2020-02-14 10:06:48 +00:00
String text = d.text();
2020-02-11 19:07:00 +00:00
if (full)
return text;
2019-10-03 16:07:56 +00:00
2020-07-14 06:57:46 +00:00
return truncate(text, PREVIEW_SIZE);
}
static String truncate(String text, int at) {
if (text.length() < at)
return text;
2019-10-03 16:07:56 +00:00
2020-07-14 06:57:46 +00:00
String preview = text.substring(0, at);
int space = preview.lastIndexOf(' ');
if (space > 0)
preview = preview.substring(0, space + 1);
return preview + "";
2019-01-05 11:17:33 +00:00
}
2020-09-10 09:14:51 +00:00
@NonNull
2020-05-02 12:23:11 +00:00
static String getText(Context context, String html) {
2019-01-05 11:17:33 +00:00
final StringBuilder sb = new StringBuilder();
2020-02-14 10:06:48 +00:00
Document d = JsoupEx.parse(html);
truncate(d, true);
2020-06-28 19:23:33 +00:00
SpannableStringBuilder ssb = fromDocument(context, d, false, true, null, null);
2019-01-05 11:17:33 +00:00
2020-05-02 12:23:11 +00:00
for (URLSpan span : ssb.getSpans(0, ssb.length(), URLSpan.class)) {
String url = span.getURL();
if (TextUtils.isEmpty(url))
continue;
if (url.toLowerCase(Locale.ROOT).startsWith("mailto:"))
url = url.substring("mailto:".length());
int start = ssb.getSpanStart(span);
int end = ssb.getSpanEnd(span);
String text = ssb.subSequence(start, end).toString();
if (!text.contains(url))
ssb.insert(end, "[" + url + "]");
}
2019-11-22 19:42:45 +00:00
2020-05-02 12:23:11 +00:00
for (ImageSpan span : ssb.getSpans(0, ssb.length(), ImageSpan.class)) {
String source = span.getSource();
if (TextUtils.isEmpty(source))
continue;
int start = ssb.getSpanStart(span);
int end = ssb.getSpanEnd(span);
for (int i = start; i < end; i++)
if (ssb.charAt(i) == '\uFFFC')
ssb.replace(i, i + 1, " ");
ssb.insert(end, "[" + source + "]");
}
2019-02-11 15:36:42 +00:00
2020-05-20 08:56:05 +00:00
// https://tools.ietf.org/html/rfc3676#section-4.5
2020-05-02 12:23:11 +00:00
for (QuoteSpan span : ssb.getSpans(0, ssb.length(), QuoteSpan.class)) {
int start = ssb.getSpanStart(span);
int end = ssb.getSpanEnd(span);
2020-05-20 08:56:05 +00:00
2020-05-26 20:15:55 +00:00
for (int i = end - 2; i >= start; i--)
2020-05-02 12:23:11 +00:00
if (ssb.charAt(i) == '\n')
2020-05-20 13:28:19 +00:00
if (i + 1 < ssb.length() && ssb.charAt(i + 1) == '>')
ssb.insert(i + 1, ">");
else
ssb.insert(i + 1, "> ");
2020-05-20 08:56:05 +00:00
2020-05-25 16:57:59 +00:00
if (start < ssb.length())
ssb.insert(start, ssb.charAt(start) == '>' ? ">" : "> ");
2020-05-02 12:23:11 +00:00
}
2019-11-22 19:42:45 +00:00
2020-05-02 12:23:11 +00:00
for (BulletSpan span : ssb.getSpans(0, ssb.length(), BulletSpan.class)) {
int start = ssb.getSpanStart(span);
ssb.insert(start, "* ");
}
2019-01-05 11:17:33 +00:00
2020-05-02 12:23:11 +00:00
for (NumberSpan span : ssb.getSpans(0, ssb.length(), NumberSpan.class)) {
int start = ssb.getSpanStart(span);
ssb.insert(start, "- ");
}
2019-02-10 12:01:21 +00:00
2020-05-02 12:23:11 +00:00
return ssb.toString();
2018-12-24 20:09:47 +00:00
}
2020-03-08 15:05:43 +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;
}
2020-02-14 12:31:34 +00:00
static void cleanup(Document d) {
2020-02-14 15:57:09 +00:00
// https://www.chromestatus.com/feature/5756335865987072
// Some messages contain 100 thousands of Apple spaces
2020-02-14 14:40:47 +00:00
for (Element aspace : d.select(".Apple-converted-space")) {
Node next = aspace.nextSibling();
if (next instanceof TextNode) {
TextNode tnode = (TextNode) next;
2020-02-14 15:57:09 +00:00
tnode.text(" " + tnode.text());
2020-02-14 12:31:34 +00:00
aspace.remove();
} else
aspace.replaceWith(new TextNode(" "));
2020-02-14 14:40:47 +00:00
}
2020-02-14 12:31:34 +00:00
}
2020-02-14 10:06:48 +00:00
static boolean truncate(Document d, boolean reformat) {
2020-02-16 09:08:46 +00:00
int max = (reformat ? MAX_FORMAT_TEXT_SIZE : MAX_FULL_TEXT_SIZE);
2020-02-14 10:06:48 +00:00
int length = 0;
int images = 0;
2020-02-14 10:06:48 +00:00
for (Element elm : d.select("*")) {
if ("img".equals(elm.tagName()))
images++;
2020-02-16 09:08:46 +00:00
boolean skip = false;
2020-02-16 09:08:46 +00:00
for (Node child : elm.childNodes()) {
if (child instanceof TextNode) {
TextNode tnode = ((TextNode) child);
String text = tnode.getWholeText();
if (length < max) {
if (length + text.length() >= max) {
text = text.substring(0, max - length) + " ...";
tnode.text(text);
skip = true;
}
} else {
if (skip)
2020-02-16 09:56:27 +00:00
tnode.text("");
2020-02-16 09:08:46 +00:00
}
length += text.length();
}
}
if (length >= max && !skip)
2020-02-14 10:06:48 +00:00
elm.remove();
}
2020-02-16 09:08:46 +00:00
Log.i("Message size=" + length + " images=" + images);
2020-02-16 09:08:46 +00:00
return (length >= max);
2020-02-14 10:06:48 +00:00
}
static boolean contains(Document d, String[] texts) {
Map<String, Boolean> condition = new HashMap<>();
for (String t : texts)
condition.put(t, false);
for (Element elm : d.select("*"))
for (Node child : elm.childNodes()) {
if (child instanceof TextNode) {
TextNode tnode = ((TextNode) child);
String text = tnode.getWholeText();
for (String t : texts)
if (!condition.get(t) && text.contains(t)) {
condition.put(t, true);
boolean found = true;
for (String c : texts)
if (!condition.get(c)) {
found = false;
break;
}
if (found)
return true;
}
}
}
return false;
}
2020-06-28 19:23:33 +00:00
static SpannableStringBuilder fromDocument(
Context context, @NonNull Document document, boolean compress,
@Nullable Html.ImageGetter imageGetter, @Nullable Html.TagHandler tagHandler) {
return fromDocument(context, document, true, compress, imageGetter, tagHandler);
2020-04-24 12:00:53 +00:00
}
2020-06-28 19:23:33 +00:00
private static SpannableStringBuilder fromDocument(
Context context, @NonNull Document document,
final boolean warn, final boolean compress,
@Nullable Html.ImageGetter imageGetter, @Nullable Html.TagHandler tagHandler) {
2020-04-24 14:12:07 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2020-04-25 08:11:01 +00:00
boolean debug = prefs.getBoolean("debug", false);
2020-09-13 14:23:11 +00:00
boolean text_separators = prefs.getBoolean("text_separators", false);
final int colorPrimary = Helper.resolveColor(context, R.attr.colorPrimary);
final int colorAccent = Helper.resolveColor(context, R.attr.colorAccent);
2020-05-04 18:29:18 +00:00
final int colorSeparator = Helper.resolveColor(context, R.attr.colorSeparator);
final int dp3 = Helper.dp2pixels(context, 3);
final int dp6 = Helper.dp2pixels(context, 6);
final int dp24 = Helper.dp2pixels(context, 24);
2020-05-03 17:29:02 +00:00
final boolean ltr = (TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) == View.LAYOUT_DIRECTION_LTR);
2020-04-24 14:12:07 +00:00
2020-08-14 18:15:52 +00:00
int message_zoom = prefs.getInt("message_zoom", 100);
float textSize = Helper.getTextSize(context, 0) * message_zoom / 100f;
2020-04-27 15:50:58 +00:00
// https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements
NodeTraversor.traverse(new NodeVisitor() {
private Element element;
2020-05-05 14:15:49 +00:00
private int plain = 0;
2020-04-27 15:50:58 +00:00
private List<TextNode> block = new ArrayList<>();
2020-05-05 14:15:49 +00:00
private String WHITESPACE = " \t\f\u00A0";
2020-04-27 15:50:58 +00:00
private String WHITESPACE_NL = WHITESPACE + "\r\n";
private Pattern TRIM_WHITESPACE_NL =
Pattern.compile("[" + WHITESPACE + "]*\\r?\\n[" + WHITESPACE + "]*");
2020-05-04 17:08:34 +00:00
// https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements
2020-04-27 15:50:58 +00:00
@Override
public void head(Node node, int depth) {
2020-05-05 14:15:49 +00:00
if (node instanceof TextNode) {
if (plain == 0)
block.add((TextNode) node);
} else if (node instanceof Element) {
2020-04-27 15:50:58 +00:00
element = (Element) node;
2020-05-05 16:43:37 +00:00
if ("true".equals(element.attr("x-plain")))
2020-05-05 14:15:49 +00:00
plain++;
2020-05-22 10:27:37 +00:00
if (element.isBlock()) {
2020-04-27 15:50:58 +00:00
normalizeText(block);
block.clear();
2020-04-25 07:58:52 +00:00
}
}
2020-04-27 15:50:58 +00:00
}
2020-04-24 18:35:39 +00:00
2020-04-27 15:50:58 +00:00
@Override
public void tail(Node node, int depth) {
if (node instanceof Element) {
element = (Element) node;
2020-05-05 16:43:37 +00:00
if ("true".equals(element.attr("x-plain")))
2020-05-05 14:15:49 +00:00
plain--;
2020-05-22 10:27:37 +00:00
if (element.isBlock() || "br".equals(element.tagName())) {
2020-04-27 15:50:58 +00:00
normalizeText(block);
block.clear();
2020-04-25 07:58:52 +00:00
}
}
2020-04-27 15:50:58 +00:00
}
2020-04-24 18:35:39 +00:00
2020-04-27 15:50:58 +00:00
private void normalizeText(List<TextNode> block) {
// https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Whitespace
TextNode tnode;
String text;
2020-05-16 14:45:04 +00:00
int index;
2020-04-27 15:50:58 +00:00
for (int i = 0; i < block.size(); ) {
tnode = block.get(i);
text = tnode.getWholeText();
2020-04-24 18:35:39 +00:00
2020-05-26 19:01:31 +00:00
// Remove whitespace before/after newlines
text = TRIM_WHITESPACE_NL.matcher(text).replaceAll(" ");
2020-05-02 12:23:11 +00:00
if ("-- ".equals(text)) {
2020-05-26 19:01:31 +00:00
tnode.text(text);
2020-05-02 12:23:11 +00:00
i++;
continue;
}
// Remove leading whitespace
2020-05-16 14:45:04 +00:00
if (i == 0 || endsWithWhitespace(block.get(i - 1).text())) {
index = 0;
while (isWhiteSpace(text, index))
index++;
if (index > 0)
text = text.substring(index);
}
2020-04-24 18:35:39 +00:00
2020-05-16 15:44:25 +00:00
// Remove multiple trailing whitespace
index = text.length() - 1;
while (isWhiteSpace(text, index) &&
2020-05-18 14:56:50 +00:00
(isWhiteSpace(text, index - 1) || i == block.size() - 1))
2020-05-16 15:44:25 +00:00
index--;
text = text.substring(0, index + 1);
2020-04-27 15:50:58 +00:00
tnode.text(text);
2020-04-26 18:22:48 +00:00
2020-04-27 15:50:58 +00:00
if (TextUtils.isEmpty(text))
block.remove(i);
else
i++;
}
2020-04-25 08:11:01 +00:00
2020-05-16 15:44:25 +00:00
// Remove last trailing whitespace
if (block.size() > 0) {
tnode = block.get(block.size() - 1);
text = tnode.getWholeText();
2020-05-22 09:45:10 +00:00
if (!"-- ".equals(text) && endsWithWhitespace(text)) {
2020-05-16 15:44:25 +00:00
text = text.substring(0, text.length() - 1);
tnode.text(text);
2020-05-16 15:44:25 +00:00
}
}
2020-04-27 15:50:58 +00:00
if (debug) {
if (block.size() > 0) {
2020-05-03 07:33:10 +00:00
TextNode first = block.get(0);
TextNode last = block.get(block.size() - 1);
first.text("(" + first.getWholeText());
last.text(last.getWholeText() + ")");
2020-04-25 07:58:52 +00:00
}
}
2020-04-27 15:50:58 +00:00
}
2020-05-16 14:45:04 +00:00
boolean isWhiteSpace(String text, int index) {
if (index < 0 || index >= text.length())
2020-04-27 15:50:58 +00:00
return false;
2020-05-16 14:45:04 +00:00
char kar = text.charAt(index);
return (WHITESPACE_NL.indexOf(kar) >= 0);
2020-04-27 15:50:58 +00:00
}
2020-04-27 15:50:58 +00:00
boolean endsWithWhitespace(String text) {
2020-05-16 14:45:04 +00:00
return isWhiteSpace(text, text.length() - 1);
2020-04-27 15:50:58 +00:00
}
}, document.body());
2020-04-25 07:58:52 +00:00
2020-04-27 15:50:58 +00:00
// https://developer.android.com/guide/topics/text/spans
SpannableStringBuilder ssb = new SpannableStringBuilder();
2020-04-25 07:58:52 +00:00
2020-04-27 15:50:58 +00:00
NodeTraversor.traverse(new NodeVisitor() {
private Element element;
private TextNode tnode;
2020-04-25 07:58:52 +00:00
2020-04-27 15:50:58 +00:00
@Override
public void head(Node node, int depth) {
if (node instanceof Element) {
element = (Element) node;
element.attr("start-index", Integer.toString(ssb.length()));
if (debug)
2020-05-05 08:11:03 +00:00
ssb.append("[" + element.tagName() + ":" + element.attr("style") + "]");
2020-04-27 15:50:58 +00:00
} else if (node instanceof TextNode) {
tnode = (TextNode) node;
2020-05-02 09:15:08 +00:00
ssb.append(tnode.getWholeText());
}
2020-04-27 15:50:58 +00:00
}
2020-04-27 15:50:58 +00:00
@Override
public void tail(Node node, int depth) {
if (node instanceof Element) {
element = (Element) node;
int start = Integer.parseInt(element.attr("start-index"));
if (debug)
ssb.append("[/" + element.tagName() + "]");
2020-04-28 07:14:22 +00:00
// Apply style
String style = element.attr("style");
if (!TextUtils.isEmpty(style)) {
String[] params = style.split(";");
for (String param : params) {
int semi = param.indexOf(":");
if (semi < 0)
continue;
String key = param.substring(0, semi);
String value = param.substring(semi + 1);
switch (key) {
case "color":
2020-05-16 17:35:46 +00:00
if (!TextUtils.isEmpty(value))
try {
int color = Integer.parseInt(value.substring(1), 16) | 0xFF000000;
ssb.setSpan(new ForegroundColorSpan(color), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (NumberFormatException ex) {
2020-05-28 06:48:28 +00:00
Log.i(ex);
2020-05-16 17:35:46 +00:00
}
2020-04-28 07:14:22 +00:00
break;
2020-06-28 13:59:02 +00:00
case "font-family":
2020-06-28 19:58:35 +00:00
String face = value.toLowerCase(Locale.ROOT);
2020-06-29 07:42:11 +00:00
if (BuildConfig.DEBUG && "fantasy".equals(face)) {
Typeface typeface = ResourcesCompat.getFont(context, R.font.fantasy);
ssb.setSpan(new CustomTypefaceSpan(face, typeface), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else
ssb.setSpan(new TypefaceSpan(face), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2020-06-28 13:59:02 +00:00
break;
2020-04-28 07:14:22 +00:00
case "text-decoration":
if ("line-through".equals(value))
ssb.setSpan(new StrikethroughSpan(), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
2020-05-03 17:29:02 +00:00
case "text-align":
2020-05-05 11:30:48 +00:00
boolean table = false;
Element e = element;
while (e != null) {
if ("table".equals(e.tagName())) {
table = true;
2020-05-03 17:29:02 +00:00
break;
2020-05-05 11:30:48 +00:00
}
e = e.parent();
}
if (!table) {
Layout.Alignment alignment = null;
switch (value) {
case "left":
case "justify":
alignment = (ltr ? Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_OPPOSITE);
break;
case "center":
alignment = Layout.Alignment.ALIGN_CENTER;
break;
case "right":
alignment = (ltr ? Layout.Alignment.ALIGN_OPPOSITE : Layout.Alignment.ALIGN_NORMAL);
break;
}
if (alignment != null)
ssb.setSpan(new AlignmentSpan.Standard(alignment), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2020-05-03 17:29:02 +00:00
}
break;
2020-04-28 07:14:22 +00:00
}
}
}
// Apply calculated font size
String xFontSize = element.attr("x-font-size-rel");
2020-04-28 07:14:22 +00:00
if (!TextUtils.isEmpty(xFontSize)) {
Float fsize = Float.parseFloat(xFontSize);
ssb.setSpan(new RelativeSizeSpan(fsize), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2020-04-28 07:14:22 +00:00
}
// Apply element
2020-08-12 20:21:55 +00:00
try {
String tag = element.tagName();
int semi = tag.indexOf(':');
if (semi >= 0)
tag = tag.substring(semi + 1);
switch (tag) {
case "a":
String href = element.attr("href");
if (!TextUtils.isEmpty(href))
ssb.setSpan(new URLSpan(href), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case "body":
// Do nothing
break;
case "big":
ssb.setSpan(new RelativeSizeSpan(FONT_LARGE), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case "blockquote":
if (start == 0 || ssb.charAt(start - 1) != '\n')
ssb.insert(start++, "\n");
2020-09-06 14:47:49 +00:00
if (start == ssb.length())
ssb.append(' ');
2020-08-12 20:21:55 +00:00
if (ssb.length() == 0 || ssb.charAt(ssb.length() - 1) != '\n')
ssb.append("\n");
2020-04-27 15:50:58 +00:00
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P)
2020-08-12 20:21:55 +00:00
ssb.setSpan(new QuoteSpan(colorPrimary), start, ssb.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
2020-04-27 15:50:58 +00:00
else
2020-08-12 20:21:55 +00:00
ssb.setSpan(new QuoteSpan(colorPrimary, dp3, dp6), start, ssb.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
break;
case "br":
newline(ssb.length());
break;
case "div": // compose
case "p": // compose
newline(ssb.length());
newline(ssb.length());
break;
case "i":
case "em":
ssb.setSpan(new StyleSpan(Typeface.ITALIC), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case "font":
// Do nothing
break;
case "h1":
case "h2":
case "h3":
case "h4":
case "h5":
case "h6":
int level = element.tagName().charAt(1) - '1';
ssb.setSpan(new RelativeSizeSpan(HEADING_SIZES[level]), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(new StyleSpan(Typeface.BOLD), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
newline(start);
newline(ssb.length());
break;
case "hr":
2020-09-13 06:34:48 +00:00
if (text_separators) {
int lhr = 0;
for (LineSpan ls : ssb.getSpans(0, ssb.length(), LineSpan.class)) {
int end = ssb.getSpanEnd(ls);
if (end > lhr)
lhr = end;
}
2020-09-12 10:25:10 +00:00
boolean nls = true;
for (int i = lhr; i < ssb.length(); i++)
if (ssb.charAt(i) != '\n') {
nls = false;
break;
}
if (nls)
2020-09-12 10:25:10 +00:00
break;
while (ssb.length() > 1 &&
ssb.charAt(ssb.length() - 2) == '\n' &&
ssb.charAt(ssb.length() - 1) == '\n')
ssb.delete(ssb.length() - 1, ssb.length());
}
2020-09-12 10:25:10 +00:00
2020-08-12 20:21:55 +00:00
ssb.append("\n" + LINE + "\n");
float stroke = context.getResources().getDisplayMetrics().density;
2020-09-12 12:04:32 +00:00
float dash = ("true".equals(element.attr("x-dashed")) ? dp3 : 0f);
ssb.setSpan(new LineSpan(colorSeparator, stroke, dash),
2020-08-12 20:21:55 +00:00
ssb.length() - 1 - LINE.length(), ssb.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case "img":
String src = element.attr("src");
Drawable d = (imageGetter == null
? context.getDrawable(R.drawable.baseline_broken_image_24)
: imageGetter.getDrawable(src));
ssb.insert(start, "\uFFFC"); // Object replacement character
ssb.setSpan(new ImageSpan(d, src), start, start + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case "li":
if (start == 0 || ssb.charAt(start - 1) != '\n')
ssb.insert(start++, "\n");
if (ssb.length() == 0 || ssb.charAt(ssb.length() - 1) != '\n')
ssb.append("\n");
Element parent = element.parent();
if (parent == null || "ul".equals(parent.tagName()))
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P)
ssb.setSpan(new BulletSpan(dp6, colorAccent), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
else
ssb.setSpan(new BulletSpan(dp6, colorAccent, dp3), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
else {
2020-09-02 07:13:35 +00:00
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol
2020-08-12 20:21:55 +00:00
int index = 0;
2020-09-02 07:13:35 +00:00
String s = parent.attr("start");
if (!TextUtils.isEmpty(s) && TextUtils.isDigitsOnly(s))
index = Integer.parseInt(s) - 1;
2020-08-12 20:21:55 +00:00
for (Node child : parent.childNodes()) {
if (child instanceof Element &&
child.nodeName().equals(element.tagName())) {
index++;
if (child == element)
break;
}
2020-04-24 21:13:32 +00:00
}
2020-08-12 20:21:55 +00:00
ssb.setSpan(new NumberSpan(dp6, colorAccent, textSize, index), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
break;
case "ol":
case "ul":
int llevel = 0;
Element lparent = element.parent();
while (lparent != null) {
if (lparent.tagName().equals(element.tagName()))
llevel++;
lparent = lparent.parent();
}
if (llevel > 0)
ssb.setSpan(new LeadingMarginSpan.Standard(llevel * dp24), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case "small":
ssb.setSpan(new RelativeSizeSpan(FONT_SMALL), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case "span":
// Do nothing
break;
case "sub":
ssb.setSpan(new SubscriptSpan(), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(new RelativeSizeSpan(FONT_SMALL), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case "sup":
ssb.setSpan(new SuperscriptSpan(), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(new RelativeSizeSpan(FONT_SMALL), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case "b":
case "strong":
ssb.setSpan(new StyleSpan(Typeface.BOLD), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case "s":
case "del":
case "strike":
ssb.setSpan(new StrikethroughSpan(), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case "tt":
ssb.setSpan(new TypefaceSpan("monospace"), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
case "u":
ssb.setSpan(new UnderlineSpan(), start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
default:
if (warn)
Log.e("Unknown tag=" + element.tagName());
}
} catch (Throwable ex) {
Log.e(ex);
2020-04-27 15:50:58 +00:00
}
}
2020-04-27 15:50:58 +00:00
}
2020-04-25 09:04:12 +00:00
2020-04-27 15:50:58 +00:00
private void newline(int index) {
2020-05-03 07:33:31 +00:00
int count = 0;
2020-06-28 19:23:33 +00:00
if (compress) {
int i = Math.min(index, ssb.length() - 1);
while (i >= 0) {
char kar = ssb.charAt(i);
if (kar == '\n')
count++;
else if (kar != ' ' && kar != '\u00A0')
break;
i--;
}
2020-05-03 07:33:31 +00:00
}
if (count < 2)
ssb.insert(index, "\n");
2020-04-27 15:50:58 +00:00
}
}, document.body());
2020-04-24 16:05:50 +00:00
2020-04-27 15:50:58 +00:00
if (debug)
for (int i = ssb.length() - 1; i >= 0; i--)
if (ssb.charAt(i) == '\n')
ssb.insert(i, "|");
2020-05-03 07:33:10 +00:00
else if (ssb.charAt(i) == ' ')
ssb.replace(i, i + 1, "_");
else if (ssb.charAt(i) == '\u00A0')
ssb.replace(i, i + 1, "");
2020-04-25 09:04:12 +00:00
2020-05-02 12:23:11 +00:00
Object[] spans = ssb.getSpans(0, ssb.length(), Object.class);
Map<Object, Integer> start = new HashMap<>();
Map<Object, Integer> end = new HashMap<>();
Map<Object, Integer> flags = new HashMap<>();
for (Object span : spans) {
start.put(span, ssb.getSpanStart(span));
end.put(span, ssb.getSpanEnd(span));
flags.put(span, ssb.getSpanFlags(span));
ssb.removeSpan(span);
}
2020-08-12 06:41:14 +00:00
for (int i = spans.length - 1; i >= 0; i--) {
int s = start.get(spans[i]);
int e = end.get(spans[i]);
int f = flags.get(spans[i]);
2020-08-12 08:25:08 +00:00
if (spans[i] instanceof BulletSpan || spans[i] instanceof NumberSpan)
2020-08-12 06:41:14 +00:00
if (s > 1 && ssb.charAt(s - 1) == '\n' &&
e > 1 && ssb.charAt(e - 1) == '\n')
f |= Spanned.SPAN_PARAGRAPH;
ssb.setSpan(spans[i], s, e, f);
}
2020-05-02 12:23:11 +00:00
return ssb;
2020-04-24 12:00:53 +00:00
}
2020-06-28 19:23:33 +00:00
static Spanned fromHtml(@NonNull String html, boolean compress, Context context) {
return fromHtml(html, compress, null, null, context);
2019-02-10 12:01:21 +00:00
}
2020-06-28 19:23:33 +00:00
static Spanned fromHtml(@NonNull String html, boolean compress, @Nullable Html.ImageGetter imageGetter, @Nullable Html.TagHandler tagHandler, Context context) {
2020-06-28 18:43:20 +00:00
Document document = JsoupEx.parse(html);
2020-06-28 20:31:51 +00:00
return fromDocument(context, document, false, compress, imageGetter, tagHandler);
2019-02-10 12:01:21 +00:00
}
2020-06-28 18:43:20 +00:00
static String toHtml(Spanned spanned, Context context) {
HtmlEx converter = new HtmlEx(context);
2020-08-11 16:46:26 +00:00
String html = converter.toHtml(spanned, TO_HTML_PARAGRAPH_LINES_INDIVIDUAL);
2019-05-01 13:58:41 +00:00
// @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
}
2020-04-23 17:45:33 +00:00
2020-04-24 21:13:32 +00:00
private static Spanned reverseSpans(Spanned spanned) {
2020-04-23 17:45:33 +00:00
Object[] spans = spanned.getSpans(0, spanned.length(), Object.class);
Spannable reverse = Spannable.Factory.getInstance().newSpannable(spanned.toString());
if (spans != null && spans.length > 0)
2020-05-02 20:01:41 +00:00
for (int i = spans.length - 1; i >= 0; i--)
2020-04-23 17:45:33 +00:00
reverse.setSpan(
spans[i],
spanned.getSpanStart(spans[i]),
spanned.getSpanEnd(spans[i]),
spanned.getSpanFlags(spans[i]));
return reverse;
}
2020-04-24 21:13:32 +00:00
2020-05-04 13:21:34 +00:00
public static class LineSpan extends ReplacementSpan {
2020-05-04 18:29:18 +00:00
private int lineColor;
2020-05-04 13:21:34 +00:00
private float strokeWidth;
2020-09-12 12:04:32 +00:00
private float dashLength;
2020-05-04 13:21:34 +00:00
2020-09-12 12:04:32 +00:00
LineSpan(int lineColor, float strokeWidth, float dashLength) {
2020-05-04 18:29:18 +00:00
this.lineColor = lineColor;
2020-05-04 13:21:34 +00:00
this.strokeWidth = strokeWidth;
2020-09-12 12:04:32 +00:00
this.dashLength = dashLength;
2020-05-04 13:21:34 +00:00
}
@Override
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) {
return 0;
}
@Override
public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
2020-05-04 18:29:18 +00:00
int ypos = (top + bottom) / 2;
int c = paint.getColor();
2020-05-04 13:21:34 +00:00
float s = paint.getStrokeWidth();
2020-09-12 12:04:32 +00:00
PathEffect p = paint.getPathEffect();
2020-05-04 18:29:18 +00:00
paint.setColor(lineColor);
2020-05-04 13:21:34 +00:00
paint.setStrokeWidth(strokeWidth);
2020-09-12 12:04:32 +00:00
if (dashLength != 0)
paint.setPathEffect(new DashPathEffect(new float[]{dashLength, dashLength}, 0));
2020-05-04 18:29:18 +00:00
canvas.drawLine(0, ypos, canvas.getWidth(), ypos, paint);
paint.setColor(c);
2020-05-04 13:21:34 +00:00
paint.setStrokeWidth(s);
2020-09-12 12:04:32 +00:00
paint.setPathEffect(p);
2020-05-04 13:21:34 +00:00
}
}
2020-06-29 07:42:11 +00:00
public static class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface newType;
public CustomTypefaceSpan(String family, Typeface type) {
super(family);
newType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf) {
Typeface old = paint.getTypeface();
int oldStyle = (old == null ? 0 : old.getStyle());
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0)
paint.setFakeBoldText(true);
if ((fake & Typeface.ITALIC) != 0)
paint.setTextSkewX(-0.25f);
paint.setTypeface(tf);
}
}
2018-08-02 13:33:06 +00:00
}