-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent italic or strikethrough emojis on Android #534
Closed
+216
−26
Closed
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
32679d6
Fix emoji formatting on Android
Skalakid acb0e2d
Change emoji tag priority
Skalakid e2efc54
Fix adding strikethrough emojis
Skalakid 09de79f
Fix emoji formatting on the web
Skalakid fa634c6
Change Android range splitting logic
Skalakid 4995ab9
Update parser bundle
Skalakid 8cb08b3
Update range splitting algorithm
Skalakid 1743351
Add review changes
Skalakid e15c3c5
Fix spaces after : in for loops
Skalakid 6e88d1f
Move length condition to the parseRanges function
Skalakid e963e97
Optimize the algorithm
Skalakid 417ba57
Move JSON parsing logic to parseRanges function
Skalakid ee7174a
Merge branch 'main' into @Skalakid/fix-emoji-formatting
Skalakid 692de7c
Merge branch 'main' into @Skalakid/fix-emoji-formatting
Skalakid bb101a9
Merge branch 'main' into @Skalakid/fix-emoji-formatting
Skalakid cb4f160
Merge branch 'main' into @Skalakid/fix-emoji-formatting
Skalakid 62d2771
Improve range split function and add unit tests (#553)
289Adam289 8b86916
Skip 0 length ranges when splitting
Skalakid 8dbd1bc
Add try/finallt block
Skalakid e6abf8e
Add length check to currentRange
Skalakid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
android/src/main/java/com/expensify/livemarkdown/RangeSplitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.expensify.livemarkdown; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.facebook.systrace.Systrace; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RangeSplitter { | ||
public static ArrayList<MarkdownRange> splitRangesOnEmojis(@NonNull List<MarkdownRange> markdownRanges, @NonNull String type) { | ||
Systrace.beginSection(0, "splitRangesOnEmojis"); | ||
ArrayList<MarkdownRange> emojiRanges = new ArrayList<>(); | ||
ArrayList<MarkdownRange> oldRanges = new ArrayList<>(markdownRanges); | ||
ArrayList<MarkdownRange> newRanges = new ArrayList<>(); | ||
for (MarkdownRange range : oldRanges) { | ||
if (range.getType().equals("emoji")) { | ||
emojiRanges.add(range); | ||
} | ||
} | ||
|
||
int i = 0; | ||
int j = 0; | ||
while (i < oldRanges.size()) { | ||
MarkdownRange currentRange = oldRanges.get(i); | ||
if (!currentRange.getType().equals(type)) { | ||
newRanges.add(currentRange); | ||
i += 1; | ||
continue; | ||
} | ||
|
||
// Iterate through all emoji ranges before the end of the current range, splitting the current range at each intersection. | ||
while (j < emojiRanges.size()) { | ||
MarkdownRange emojiRange = emojiRanges.get(j); | ||
if (emojiRange.getStart() > currentRange.getEnd()) { | ||
break; | ||
} | ||
|
||
int currentStart = currentRange.getStart(); | ||
int currentEnd = currentRange.getEnd(); | ||
int emojiStart = emojiRange.getStart(); | ||
int emojiEnd = emojiRange.getEnd(); | ||
if (emojiStart >= currentStart && emojiEnd <= currentEnd) { // Intersection | ||
MarkdownRange newRange = new MarkdownRange(currentRange.getType(), currentStart, emojiStart - currentStart, currentRange.getDepth()); | ||
currentRange = new MarkdownRange(currentRange.getType(), emojiEnd, currentEnd - emojiEnd, currentRange.getDepth()); | ||
|
||
if (newRange.getLength() > 0) { | ||
newRanges.add(newRange); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this check be symmetric? So it covers both Also, let's rename Let's avoid creating |
||
} | ||
j += 1; | ||
} | ||
newRanges.add(currentRange); | ||
i += 1; | ||
} | ||
Systrace.endSection(0); | ||
return newRanges; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
android/src/test/java/com/expensify/livemarkdown/RangeSplitterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.expensify.livemarkdown; | ||
|
||
import static com.expensify.livemarkdown.RangeSplitter.splitRangesOnEmojis; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class RangeSplitterTest { | ||
|
||
@Test | ||
public void testNoOverlap() { | ||
List<MarkdownRange> markdownRanges = new ArrayList<>(); | ||
markdownRanges.add(new MarkdownRange("strikethrough", 0, 10, 1)); | ||
markdownRanges.add(new MarkdownRange("emoji", 12, 2, 1)); | ||
|
||
markdownRanges = splitRangesOnEmojis(markdownRanges, "strikethrough"); | ||
|
||
assertEquals(2, markdownRanges.size()); | ||
assertEquals(new MarkdownRange("strikethrough", 0, 10, 1), markdownRanges.get(0)); | ||
assertEquals(new MarkdownRange("emoji", 12, 2, 1), markdownRanges.get(1)); | ||
} | ||
|
||
@Test | ||
public void testOverlapDifferentType() { | ||
List<MarkdownRange> markdownRanges = new ArrayList<>(); | ||
markdownRanges.add(new MarkdownRange("strikethrough", 0, 10, 1)); | ||
markdownRanges.add(new MarkdownRange("emoji", 3, 4, 1)); | ||
|
||
markdownRanges = splitRangesOnEmojis(markdownRanges, "italic"); | ||
|
||
assertEquals(2, markdownRanges.size()); | ||
assertEquals(new MarkdownRange("strikethrough", 0, 10, 1), markdownRanges.get(0)); | ||
assertEquals(new MarkdownRange("emoji", 3, 4, 1), markdownRanges.get(1)); | ||
} | ||
|
||
@Test | ||
public void testSingleOverlap() { | ||
List<MarkdownRange> markdownRanges = new ArrayList<>(); | ||
markdownRanges.add(new MarkdownRange("strikethrough", 0, 10, 1)); | ||
markdownRanges.add(new MarkdownRange("emoji", 3, 4, 1)); // This range should split the strikethrough range | ||
|
||
markdownRanges = splitRangesOnEmojis(markdownRanges, "strikethrough"); | ||
|
||
// Sort is needed because ranges may get mixed while splitting | ||
Collections.sort(markdownRanges, (r1, r2) -> Integer.compare(r1.getStart(), r2.getStart())); | ||
|
||
assertEquals(3, markdownRanges.size()); | ||
assertEquals(new MarkdownRange("strikethrough", 0, 3, 1), markdownRanges.get(0)); | ||
assertEquals(new MarkdownRange("emoji", 3, 4, 1), markdownRanges.get(1)); | ||
assertEquals(new MarkdownRange("strikethrough", 7, 3, 1), markdownRanges.get(2)); | ||
} | ||
|
||
@Test | ||
public void testMultipleOverlapsMultipleTypes() { | ||
List<MarkdownRange> markdownRanges = new ArrayList<>(); | ||
markdownRanges.add(new MarkdownRange("italic", 0, 20, 1)); | ||
markdownRanges.add(new MarkdownRange("strikethrough", 2, 12, 1)); | ||
markdownRanges.add(new MarkdownRange("emoji", 3, 1, 1)); | ||
markdownRanges.add(new MarkdownRange("emoji", 8, 2, 1)); | ||
markdownRanges.add(new MarkdownRange("strikethrough", 22, 5, 1)); | ||
|
||
markdownRanges = splitRangesOnEmojis(markdownRanges, "strikethrough"); | ||
|
||
// Sort is needed because ranges may get mixed while splitting | ||
Collections.sort(markdownRanges, (r1, r2) -> Integer.compare(r1.getStart(), r2.getStart())); | ||
|
||
assertEquals(7, markdownRanges.size()); | ||
assertEquals(new MarkdownRange("italic", 0, 20, 1), markdownRanges.get(0)); | ||
assertEquals(new MarkdownRange("strikethrough", 2, 1, 1), markdownRanges.get(1)); | ||
assertEquals(new MarkdownRange("emoji", 3, 1, 1), markdownRanges.get(2)); | ||
assertEquals(new MarkdownRange("strikethrough", 4, 4, 1), markdownRanges.get(3)); | ||
assertEquals(new MarkdownRange("emoji", 8, 2, 1), markdownRanges.get(4)); | ||
assertEquals(new MarkdownRange("strikethrough", 10, 4, 1), markdownRanges.get(5)); | ||
assertEquals(new MarkdownRange("strikethrough", 22, 5, 1), markdownRanges.get(6)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please wrap
beginSection
/endSection
calls withtry/finally
block