Disable autolink for composer

pull/168/head
M66B 5 years ago
parent 12f0b169a6
commit 44dd58d0d1

@ -139,7 +139,7 @@ public class ActivityEML extends ActivityBase {
String html = result.parts.getHtml(context); String html = result.parts.getHtml(context);
if (html != null) if (html != null)
result.body = HtmlHelper.fromHtml(HtmlHelper.sanitize(context, html, false)); result.body = HtmlHelper.fromHtml(HtmlHelper.sanitize(context, html, false, false));
return result; return result;
} }

@ -1449,7 +1449,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
return document.html(); return document.html();
} else { } else {
// Cleanup message // Cleanup message
String html = HtmlHelper.sanitize(context, body, show_images); String html = HtmlHelper.sanitize(context, body, show_images, true);
// Collapse quotes // Collapse quotes
if (!show_quotes) { if (!show_quotes) {

@ -68,7 +68,7 @@ public class EditTextCompose extends AppCompatEditText {
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0); ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
String html = item.coerceToHtmlText(context); String html = item.coerceToHtmlText(context);
html = HtmlHelper.sanitize(context, html, false); html = HtmlHelper.sanitize(context, html, false, false);
Spanned paste = HtmlHelper.fromHtml(html); Spanned paste = HtmlHelper.fromHtml(html);
int start = getSelectionStart(); int start = getSelectionStart();

@ -781,7 +781,7 @@ public class FragmentCompose extends FragmentBase {
String text = HtmlHelper.getText(ref); String text = HtmlHelper.getText(ref);
html = "<p>" + text.replaceAll("\\r?\\n", "<br>") + "</p>"; html = "<p>" + text.replaceAll("\\r?\\n", "<br>") + "</p>";
} else } else
html = HtmlHelper.sanitize(context, ref, true); html = HtmlHelper.sanitize(context, ref, true, false);
refFile.delete(); refFile.delete();
return body + html; return body + html;
@ -2235,7 +2235,7 @@ public class FragmentCompose extends FragmentBase {
data.draft.subject = args.getString("subject", ""); data.draft.subject = args.getString("subject", "");
body = args.getString("body", ""); body = args.getString("body", "");
if (!TextUtils.isEmpty(body)) if (!TextUtils.isEmpty(body))
body = HtmlHelper.sanitize(context, body, false); body = HtmlHelper.sanitize(context, body, false, false);
if (answer > 0) { if (answer > 0) {
EntityAnswer a = db.answer().getAnswer(answer); EntityAnswer a = db.answer().getAnswer(answer);
@ -2309,7 +2309,7 @@ public class FragmentCompose extends FragmentBase {
data.draft.subject = ref.subject; data.draft.subject = ref.subject;
if (ref.content) { if (ref.content) {
String html = Helper.readText(ref.getFile(context)); String html = Helper.readText(ref.getFile(context));
body = HtmlHelper.sanitize(context, html, true); body = HtmlHelper.sanitize(context, html, true, false);
} }
} else if ("list".equals(action)) { } else if ("list".equals(action)) {
data.draft.subject = ref.subject; data.draft.subject = ref.subject;
@ -2596,7 +2596,7 @@ public class FragmentCompose extends FragmentBase {
if (data.draft.content) { if (data.draft.content) {
File file = data.draft.getFile(context); File file = data.draft.getFile(context);
String html = Helper.readText(file); String html = Helper.readText(file);
html = HtmlHelper.sanitize(context, html, true); html = HtmlHelper.sanitize(context, html, true, false);
Helper.writeText(file, html); Helper.writeText(file, html);
} else { } else {
if (data.draft.uid == null) if (data.draft.uid == null)
@ -3316,7 +3316,7 @@ public class FragmentCompose extends FragmentBase {
Spanned spannedRef = null; Spanned spannedRef = null;
File refFile = draft.getRefFile(context); File refFile = draft.getRefFile(context);
if (refFile.exists()) { if (refFile.exists()) {
String quote = HtmlHelper.sanitize(context, Helper.readText(refFile), show_images); String quote = HtmlHelper.sanitize(context, Helper.readText(refFile), show_images, false);
Spanned spannedQuote = HtmlHelper.fromHtml(quote, Spanned spannedQuote = HtmlHelper.fromHtml(quote,
new Html.ImageGetter() { new Html.ImageGetter() {
@Override @Override

@ -80,9 +80,9 @@ public class HtmlHelper {
private static final List<String> tails = Collections.unmodifiableList(Arrays.asList( private static final List<String> tails = Collections.unmodifiableList(Arrays.asList(
"h1", "h2", "h3", "h4", "h5", "h6", "p", "ol", "ul", "li")); "h1", "h2", "h3", "h4", "h5", "h6", "p", "ol", "ul", "li"));
static String sanitize(Context context, String html, boolean show_images) { static String sanitize(Context context, String html, boolean show_images, boolean autolink) {
try { try {
return _sanitize(context, html, show_images); return _sanitize(context, html, show_images, autolink);
} catch (Throwable ex) { } catch (Throwable ex) {
// OutOfMemoryError // OutOfMemoryError
Log.e(ex); Log.e(ex);
@ -90,7 +90,7 @@ public class HtmlHelper {
} }
} }
private static String _sanitize(Context context, String html, boolean show_images) { private static String _sanitize(Context context, String html, boolean show_images, boolean autolink) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean text_color = prefs.getBoolean("text_color", true); boolean text_color = prefs.getBoolean("text_color", true);
boolean display_hidden = prefs.getBoolean("display_hidden", false); boolean display_hidden = prefs.getBoolean("display_hidden", false);
@ -450,69 +450,71 @@ public class HtmlHelper {
} }
// Autolink // Autolink
final Pattern pattern = Pattern.compile( if (autolink) {
PatternsCompat.AUTOLINK_EMAIL_ADDRESS.pattern() + "|" + final Pattern pattern = Pattern.compile(
PatternsCompat.AUTOLINK_WEB_URL.pattern()); PatternsCompat.AUTOLINK_EMAIL_ADDRESS.pattern() + "|" +
PatternsCompat.AUTOLINK_WEB_URL.pattern());
NodeTraversor.traverse(new NodeVisitor() {
private int links = 0; NodeTraversor.traverse(new NodeVisitor() {
private int links = 0;
@Override
public void head(Node node, int depth) { @Override
if (links < MAX_AUTO_LINK && node instanceof TextNode) { public void head(Node node, int depth) {
TextNode tnode = (TextNode) node; if (links < MAX_AUTO_LINK && node instanceof TextNode) {
String text = tnode.getWholeText(); TextNode tnode = (TextNode) node;
String text = tnode.getWholeText();
Matcher matcher = pattern.matcher(text);
if (matcher.find()) { Matcher matcher = pattern.matcher(text);
Element span = document.createElement("span"); if (matcher.find()) {
Element span = document.createElement("span");
int pos = 0;
do { int pos = 0;
boolean linked = false; do {
Node parent = tnode.parent(); boolean linked = false;
while (parent != null) { Node parent = tnode.parent();
if ("a".equals(parent.nodeName())) { while (parent != null) {
linked = true; if ("a".equals(parent.nodeName())) {
break; linked = true;
break;
}
parent = parent.parent();
} }
parent = parent.parent();
}
boolean email = matcher.group().contains("@") && !matcher.group().contains(":"); boolean email = matcher.group().contains("@") && !matcher.group().contains(":");
if (BuildConfig.DEBUG) if (BuildConfig.DEBUG)
Log.i("Web url=" + matcher.group() + Log.i("Web url=" + matcher.group() +
" " + matcher.start() + "..." + matcher.end() + "/" + text.length() + " " + matcher.start() + "..." + matcher.end() + "/" + text.length() +
" linked=" + linked + " email=" + email + " count=" + links); " linked=" + linked + " email=" + email + " count=" + links);
if (linked) if (linked)
span.appendText(text.substring(pos, matcher.end())); span.appendText(text.substring(pos, matcher.end()));
else { else {
span.appendText(text.substring(pos, matcher.start())); span.appendText(text.substring(pos, matcher.start()));
Element a = document.createElement("a"); Element a = document.createElement("a");
a.attr("href", (email ? "mailto:" : "") + matcher.group()); a.attr("href", (email ? "mailto:" : "") + matcher.group());
a.text(matcher.group()); a.text(matcher.group());
span.appendChild(a); span.appendChild(a);
links++; links++;
} }
pos = matcher.end(); pos = matcher.end();
} while (links < MAX_AUTO_LINK && matcher.find()); } while (links < MAX_AUTO_LINK && matcher.find());
span.appendText(text.substring(pos)); span.appendText(text.substring(pos));
tnode.before(span); tnode.before(span);
tnode.text(""); tnode.text("");
}
} }
} }
}
@Override @Override
public void tail(Node node, int depth) { public void tail(Node node, int depth) {
} }
}, document); }, document);
}
// Selective new lines // Selective new lines
for (Element div : document.select("div")) for (Element div : document.select("div"))

Loading…
Cancel
Save