From 9b91ebf31711391cbaf7ca943135a254b0b7b8d6 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 14 Aug 2023 11:43:04 +0200 Subject: [PATCH] Fix tags issue with RTL and bookmark --- .../mastodon/helper/SpannableHelper.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/mastodon/helper/SpannableHelper.java b/app/src/main/java/app/fedilab/android/mastodon/helper/SpannableHelper.java index 3869b1bd2..25c118ed1 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/helper/SpannableHelper.java +++ b/app/src/main/java/app/fedilab/android/mastodon/helper/SpannableHelper.java @@ -113,6 +113,22 @@ public class SpannableHelper { return convert(context, text, status, account, announcement, viewWeakReference, callback, true); } + + public static boolean isRTL(String s) { + for (int i = 0; i < s.length(); i++) { + byte d = Character.getDirectionality(s.charAt(i)); + if (d == Character.DIRECTIONALITY_RIGHT_TO_LEFT || + d == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC || + d == Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING || + d == Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE + ) { + return true; + } + } + + return false; + } + public static Spannable convert(Context context, String text, Status status, Account account, Announcement announcement, WeakReference viewWeakReference, Status.Callback callback, boolean convertHtml) { @@ -196,8 +212,12 @@ public class SpannableHelper { String sb = Pattern.compile("\\A[A-Za-z0-9_]").matcher(markdownItem.code).find() ? "\\b" : ""; String eb = Pattern.compile("[A-Za-z0-9_]\\z").matcher(markdownItem.code).find() ? "\\b" : "\\B"; - - Pattern p = Pattern.compile(sb + "(" + Pattern.quote(markdownItem.code) + ")" + eb, Pattern.CASE_INSENSITIVE); + Pattern p; + if (!isRTL(initialContent.toString())) { + p = Pattern.compile(sb + "(" + Pattern.quote(markdownItem.code) + ")" + eb, Pattern.UNICODE_CASE); + } else { + p = Pattern.compile(eb + "(" + Pattern.quote(markdownItem.code) + ")" + sb, Pattern.UNICODE_CASE); + } Matcher m = p.matcher(content); int fetchPosition = 1; while (m.find()) {