From 96224c0fc69460d60ac215ecd33e4cd5cf435fda Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 4 May 2018 10:11:24 +0200 Subject: [PATCH] filter sequences of more than 3 ltr-rtl --- .../siacs/conversations/entities/Message.java | 4 ++-- .../conversations/utils/MessageUtils.java | 18 ++++++++++++++---- .../eu/siacs/conversations/utils/UIHelper.java | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index 99d13de17..0957b5fb7 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -606,7 +606,7 @@ public class Message extends AbstractEntity { } public SpannableStringBuilder getMergedBody() { - SpannableStringBuilder body = new SpannableStringBuilder(this.body.trim()); + SpannableStringBuilder body = new SpannableStringBuilder(MessageUtils.filterLtrRtl(this.body).trim()); Message current = this; while (current.mergeable(current.next())) { current = current.next(); @@ -616,7 +616,7 @@ public class Message extends AbstractEntity { body.append("\n\n"); body.setSpan(new MergeSeparator(), body.length() - 2, body.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE); - body.append(current.getBody().trim()); + body.append(MessageUtils.filterLtrRtl(current.getBody()).trim()); } return body; } diff --git a/src/main/java/eu/siacs/conversations/utils/MessageUtils.java b/src/main/java/eu/siacs/conversations/utils/MessageUtils.java index 6abfea0b2..b6399ede1 100644 --- a/src/main/java/eu/siacs/conversations/utils/MessageUtils.java +++ b/src/main/java/eu/siacs/conversations/utils/MessageUtils.java @@ -31,24 +31,30 @@ package eu.siacs.conversations.utils; import java.net.MalformedURLException; import java.net.URL; +import java.util.regex.Pattern; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.http.AesGcmURLStreamHandler; public class MessageUtils { + + private static final Pattern LTR_RTL = Pattern.compile("(\\u200E[^\\u200F]*\\u200F){3,}"); + + private static final String EMPTY_STRING = ""; + public static String prepareQuote(Message message) { final StringBuilder builder = new StringBuilder(); final String body = message.getMergedBody().toString(); - for(String line : body.split("\n")) { + for (String line : body.split("\n")) { if (line.length() <= 0) { continue; } final char c = line.charAt(0); - if (c == '>' && UIHelper.isPositionFollowedByQuoteableCharacter(line,0) - || (c == '\u00bb' && !UIHelper.isPositionFollowedByQuote(line,0))) { + if (c == '>' && UIHelper.isPositionFollowedByQuoteableCharacter(line, 0) + || (c == '\u00bb' && !UIHelper.isPositionFollowedByQuote(line, 0))) { continue; } - if (builder.length() != 0 ) { + if (builder.length() != 0) { builder.append('\n'); } builder.append(line.trim()); @@ -79,4 +85,8 @@ public class MessageUtils { return false; } } + + public static String filterLtrRtl(String body) { + return LTR_RTL.matcher(body).replaceFirst(EMPTY_STRING); + } } diff --git a/src/main/java/eu/siacs/conversations/utils/UIHelper.java b/src/main/java/eu/siacs/conversations/utils/UIHelper.java index 5add25ed1..f153cde81 100644 --- a/src/main/java/eu/siacs/conversations/utils/UIHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/UIHelper.java @@ -283,7 +283,7 @@ public class UIHelper { } else if (message.getType() == Message.TYPE_FILE || message.getType() == Message.TYPE_IMAGE) { return new Pair<>(getFileDescriptionString(context, message), true); } else { - final String body = message.getBody(); + final String body = MessageUtils.filterLtrRtl(message.getBody()); if (body.startsWith(Message.ME_COMMAND)) { return new Pair<>(body.replaceAll("^" + Message.ME_COMMAND, UIHelper.getMessageDisplayName(message) + " "), false);