Fix issue #4962, remove trailing whitespace in caption field when set to uploadMediaDetails (#5085)

* add methods to check and remove trailing whitespace

* fix filter to check and remove trailing whitespace

* fix filter to check and remove trailing space first

* add test and fix filter() and removeTrailingWhitespace()

* caption trailing whitespace: change solution to remove trailing whitespaces when set to uploadMediaDetails

* update code documentation

* add more tests include instance tab, carriage return and Japanese whitespace

* Caption field: Stop blocking Japanese Space, convert to English Space when set to uploadMediaDetails

* Change method name from convertJapSpaceToEngSpace to convertIdeographicSpaceToLatinSpace

* Change pattern name from JapSpacePattern to ideographicSpacePattern
This commit is contained in:
Jiaweeeeeen 2022-10-30 17:58:15 +08:00 committed by GitHub
parent 09564c3eca
commit 3488644938
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 4 deletions

View file

@ -34,6 +34,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.regex.Pattern;
import javax.inject.Inject;
import timber.log.Timber;
@ -197,7 +198,7 @@ public class UploadMediaDetailAdapter extends RecyclerView.Adapter<UploadMediaDe
removeButton.setOnClickListener(v -> removeDescription(uploadMediaDetail, position));
captionListener = new AbstractTextWatcher(
captionText -> uploadMediaDetails.get(position).setCaptionText(captionText));
captionText -> uploadMediaDetails.get(position).setCaptionText(convertIdeographicSpaceToLatinSpace(removeTrailingWhitespace(captionText))));
descriptionListener = new AbstractTextWatcher(
descriptionText -> uploadMediaDetails.get(position).setDescriptionText(descriptionText));
captionItemEditText.addTextChangedListener(captionListener);
@ -418,10 +419,45 @@ public class UploadMediaDetailAdapter extends RecyclerView.Adapter<UploadMediaDe
languageHistoryListView.setAdapter(recentLanguagesAdapter);
}
}
/**
* Checks if the source string contains trailing whitespace
* @param source input string
* @return true if contains trailing whitespace and false otherwise
*/
public Boolean checkTrailingWhitespace(String source) {
int len = source.length();
if (len == 0) {
return false;
}
return Character.isWhitespace(source.charAt(len - 1));
}
/**
* Removes any trailing whitespace from the source text.
* @param source input string
* @return a string without trailing whitespace
*/
public String removeTrailingWhitespace(String source) {
while (checkTrailingWhitespace(source)) {
source = source.substring(0, source.length() - 1);
}
return source;
}
/**
* Convert Ideographic space to Latin space
* @param source the source text
* @return a string with Latin spaces instead of Ideographic spaces
*/
public String convertIdeographicSpaceToLatinSpace(String source) {
Pattern ideographicSpacePattern = Pattern.compile("\\x{3000}");
return ideographicSpacePattern.matcher(source).replaceAll(" ");
}
}
public interface Callback {
void showAlert(int mediaDetailDescription, int descriptionInfo);
}

View file

@ -17,7 +17,7 @@ public class UploadMediaDetailInputFilter implements InputFilter {
*/
public UploadMediaDetailInputFilter() {
patterns = new Pattern[]{
Pattern.compile("[\\x{00A0}\\x{1680}\\x{180E}\\x{2000}-\\x{200B}\\x{2028}\\x{2029}\\x{202F}\\x{205F}\\x{3000}]"),
Pattern.compile("[\\x{00A0}\\x{1680}\\x{180E}\\x{2000}-\\x{200B}\\x{2028}\\x{2029}\\x{202F}\\x{205F}]"),
Pattern.compile("[\\x{202A}-\\x{202E}]"),
Pattern.compile("\\p{Cc}"),
Pattern.compile("\\x{FEFF}"),

View file

@ -247,4 +247,51 @@ class UploadMediaDetailAdapterUnitTest {
verify(adapterView, times(3)).adapter
}
@Test
fun testRemoveTrailingWhitespace() {
// empty space
val test1 = "test "
val expected1 = "test"
Assert.assertTrue(viewHolder.checkTrailingWhitespace(test1));
Assert.assertEquals(expected1, viewHolder.removeTrailingWhitespace(test1))
val test2 = "test test "
val expected2 = "test test"
Assert.assertTrue(viewHolder.checkTrailingWhitespace(test2));
Assert.assertEquals(expected2, viewHolder.removeTrailingWhitespace(test2))
// No whitespace
val test3 = "No trailing space";
val expected3 = "No trailing space";
Assert.assertFalse(viewHolder.checkTrailingWhitespace(test3))
Assert.assertEquals(expected3, viewHolder.removeTrailingWhitespace(test3))
}
@Test
fun testRemoveTrailingInstanceTab() {
val test = "test\t"
val expected = "test"
Assert.assertTrue(viewHolder.checkTrailingWhitespace(test));
Assert.assertEquals(expected, viewHolder.removeTrailingWhitespace(test))
}
@Test
fun testRemoveTrailingCarriageReturn() {
val test = "test\r"
val expected = "test"
Assert.assertTrue(viewHolder.checkTrailingWhitespace(test));
Assert.assertEquals(expected, viewHolder.removeTrailingWhitespace(test))
}
@Test
fun testCaptionJapaneseCharacters() {
val test1 = "テスト テスト"
val expected1 = "テスト テスト"
Assert.assertEquals(expected1, viewHolder.convertIdeographicSpaceToLatinSpace(test1));
val test2 = "テスト \r \t "
val expected2 = "テスト"
Assert.assertTrue(viewHolder.checkTrailingWhitespace(test2));
Assert.assertEquals(expected2, viewHolder.removeTrailingWhitespace(test2))
}
}

View file

@ -33,7 +33,7 @@ class UploadMediaDetailInputFilterTest {
builder.filters = arrayOf(UploadMediaDetailInputFilter())
//All unusual space characters
val tests = intArrayOf(0x00A0, 0x1680, 0x180E, 0x2000, 0x2005, 0x200B, 0x2028, 0x2029, 0x202F, 0x205F, 0x3000)
val tests = intArrayOf(0x00A0, 0x1680, 0x180E, 0x2000, 0x2005, 0x200B, 0x2028, 0x2029, 0x202F, 0x205F)
for (test: Int in tests) {
builder.insert(0, String(Character.toChars(test)))
Assert.assertEquals(builder.toString(), "")