Added detect HTML

This commit is contained in:
M66B 2023-01-19 08:53:23 +01:00
parent cc44993d1c
commit e29be73126
5 changed files with 47 additions and 7 deletions

View File

@ -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).

View File

@ -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).

View File

@ -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)

View File

@ -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);

View File

@ -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=
"\\</\\w+\\>";
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;
}
}