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}"),