diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 8fb37ffc3e..975f75893e 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -46,3 +46,4 @@ FairEmail uses: * [IPAddress](https://github.com/seancfoley/IPAddress). Copyright 2016-2018 Sean C Foley. [Apache License 2.0](https://github.com/seancfoley/IPAddress/blob/master/LICENSE). * [MaterialDings](https://github.com/Accusoft/MaterialDings). Copyright (c) 2018 Accusoft Corporation. [MIT License](https://github.com/Accusoft/MaterialDings/blob/master/LICENSE.md). * [Send](https://github.com/timvisee/send). [Mozilla Public License 2.0](https://github.com/timvisee/send/blob/master/LICENSE). +* [DetectHtml](https://github.com/dbennett455/DetectHtml). [The MIT License](https://github.com/dbennett455/DetectHtml/blob/master/LICENSE). diff --git a/app/src/main/assets/ATTRIBUTION.md b/app/src/main/assets/ATTRIBUTION.md index 8fb37ffc3e..975f75893e 100644 --- a/app/src/main/assets/ATTRIBUTION.md +++ b/app/src/main/assets/ATTRIBUTION.md @@ -46,3 +46,4 @@ FairEmail uses: * [IPAddress](https://github.com/seancfoley/IPAddress). Copyright 2016-2018 Sean C Foley. [Apache License 2.0](https://github.com/seancfoley/IPAddress/blob/master/LICENSE). * [MaterialDings](https://github.com/Accusoft/MaterialDings). Copyright (c) 2018 Accusoft Corporation. [MIT License](https://github.com/Accusoft/MaterialDings/blob/master/LICENSE.md). * [Send](https://github.com/timvisee/send). [Mozilla Public License 2.0](https://github.com/timvisee/send/blob/master/LICENSE). +* [DetectHtml](https://github.com/dbennett455/DetectHtml). [The MIT License](https://github.com/dbennett455/DetectHtml/blob/master/LICENSE). diff --git a/app/src/main/java/eu/faircode/email/EditTextCompose.java b/app/src/main/java/eu/faircode/email/EditTextCompose.java index da70320c84..3f62daeb3c 100644 --- a/app/src/main/java/eu/faircode/email/EditTextCompose.java +++ b/app/src/main/java/eu/faircode/email/EditTextCompose.java @@ -55,6 +55,7 @@ import androidx.core.view.inputmethod.InputConnectionCompat; import androidx.core.view.inputmethod.InputContentInfoCompat; import androidx.preference.PreferenceManager; +import org.github.DetectHtml; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -487,7 +488,7 @@ public class EditTextCompose extends FixedEditText { String h = null; if (raw) { CharSequence text = item.getText(); - if (text != null && HtmlHelper.isHtml(text.toString())) + if (text != null && DetectHtml.isHtml(text.toString())) h = text.toString(); } if (h == null) diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index 8c3dd8c269..5459eb8c0a 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -3829,12 +3829,6 @@ public class HtmlHelper { .remove("x-keep-line"); } - static boolean isHtml(String text) { - Pattern p = Pattern.compile(".*\\<[^>]+>.*", Pattern.DOTALL); - boolean isHtml = p.matcher(text).matches(); - return isHtml; - } - static Spanned fromHtml(@NonNull String html, Context context) { Document document = JsoupEx.parse(html); return fromDocument(context, document, null, null); diff --git a/app/src/main/java/org/github/DetectHtml.java b/app/src/main/java/org/github/DetectHtml.java new file mode 100644 index 0000000000..3fd7087a16 --- /dev/null +++ b/app/src/main/java/org/github/DetectHtml.java @@ -0,0 +1,43 @@ +package org.github; + +/** + * Detect HTML markup in a string + * This will detect tags or entities + * + * @author dbennett455@gmail.com - David H. Bennett + * + */ + +import java.util.regex.Pattern; + +public class DetectHtml +{ + // adapted from post by Phil Haack and modified to match better + public final static String tagStart= + "\\<\\w+((\\s+\\w+(\\s*\\=\\s*(?:\".*?\"|'.*?'|[^'\"\\>\\s]+))?)+\\s*|\\s*)\\>"; + public final static String tagEnd= + "\\"; + public final static String tagSelfClosing= + "\\<\\w+((\\s+\\w+(\\s*\\=\\s*(?:\".*?\"|'.*?'|[^'\"\\>\\s]+))?)+\\s*|\\s*)/\\>"; + public final static String htmlEntity= + "&[a-zA-Z][a-zA-Z0-9]+;"; + public final static Pattern htmlPattern=Pattern.compile( + "("+tagStart+".*"+tagEnd+")|("+tagSelfClosing+")|("+htmlEntity+")", + Pattern.DOTALL + ); + + /** + * Will return true if s contains HTML markup tags or entities. + * + * @param s String to test + * @return true if string contains HTML + */ + public static boolean isHtml(String s) { + boolean ret=false; + if (s != null) { + ret=htmlPattern.matcher(s).find(); + } + return ret; + } + +}