Skip to content

Commit d8f9ce7

Browse files
committed
Fixed tags removal from notes with carriage returns (closes #942)
1 parent 2df9d1c commit d8f9ce7

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

omniNotes/src/main/java/it/feio/android/omninotes/utils/TagsHelper.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.HashSet;
3131
import java.util.List;
3232
import java.util.Map;
33+
import java.util.concurrent.atomic.AtomicReference;
3334
import lombok.experimental.UtilityClass;
3435
import org.apache.commons.lang3.StringUtils;
3536

@@ -94,15 +95,20 @@ public static String removeTags(String text, List<Tag> tagsToRemove) {
9495
if (StringUtils.isEmpty(text)) {
9596
return text;
9697
}
97-
String[] textCopy = new String[]{text};
98-
from(tagsToRemove).forEach(tagToRemove -> textCopy[0] = removeTag(textCopy[0], tagToRemove));
99-
return textCopy[0];
98+
var textCopy = new AtomicReference<>(text);
99+
from(tagsToRemove).forEach(tagToRemove -> textCopy.set(removeTag(textCopy.get(), tagToRemove)));
100+
return textCopy.get();
100101
}
101102

102103
private static String removeTag(String textCopy, Tag tagToRemove) {
103-
return from(textCopy.split(" "))
104+
var spaceProcessed = tokenizeAndRemoveTag(textCopy, " ", tagToRemove);
105+
return tokenizeAndRemoveTag(spaceProcessed, "\n", tagToRemove);
106+
}
107+
108+
private static String tokenizeAndRemoveTag(String text, String separator, Tag tagToRemove) {
109+
return from(text.split(separator))
104110
.map(word -> removeTagFromWord(word, tagToRemove))
105-
.reduce((s, s2) -> s + " " + s2)
111+
.reduce((s, s2) -> s + separator + s2)
106112
.toBlocking()
107113
.singleOrDefault("")
108114
.trim();

omniNotes/src/test/java/it/feio/android/omninotes/utils/TagsHelperTest.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636

3737
public class TagsHelperTest {
3838

39-
private static Tag TAG1 = new Tag("#mixed", 1);
40-
private static Tag TAG2 = new Tag("#tags", 1);
41-
private static Tag TAG3 = new Tag("#tag", 1);
42-
private static Tag TAG4 = new Tag("#numberedAfter123", 1);
43-
private static Tag TAG_INVALID = new Tag("#123numbered", 1);
39+
private static final Tag TAG1 = new Tag("#mixed", 1);
40+
private static final Tag TAG2 = new Tag("#tags", 1);
41+
private static final Tag TAG3 = new Tag("#tag", 1);
42+
private static final Tag TAG4 = new Tag("#numberedAfter123", 1);
43+
private static final Tag TAG_INVALID = new Tag("#123numbered", 1);
4444

4545
private Note note;
4646

@@ -129,7 +129,7 @@ public void TestTagWithComma() {
129129
}
130130

131131
@Test
132-
public void removeTags_specialCharsKeeped () {
132+
public void removeTags_specialCharsKept () {
133133
String text = "<>[],-.(){}!?\n\t text";
134134
String testString = text + " " + TAG1.getText();
135135

@@ -138,6 +138,16 @@ public void removeTags_specialCharsKeeped () {
138138
assertEquals(text, result);
139139
}
140140

141+
@Test
142+
public void removeTags_mixedCarriageReturns () {
143+
var text = "some text\n" + TAG1.getText() + " other following text ending with " + TAG2.getText();
144+
var expected = "some text\n other following text ending with " + TAG2.getText();
145+
146+
var result = TagsHelper.removeTags(text, singletonList(TAG1));
147+
148+
assertEquals(expected, result);
149+
}
150+
141151
@Test
142152
public void removeTagFromWord() {
143153
String word = TAG3 + "(and";

0 commit comments

Comments
 (0)