mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +01:00
This commit is contained in:
parent
d647c0674e
commit
d8724f4541
3 changed files with 212 additions and 0 deletions
|
|
@ -3,6 +3,7 @@ package fr.free.nrw.commons.upload;
|
|||
import android.app.Dialog;
|
||||
import android.content.Intent;
|
||||
import android.text.Editable;
|
||||
import android.text.InputFilter;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.LayoutInflater;
|
||||
|
|
@ -165,6 +166,9 @@ public class UploadMediaDetailAdapter extends RecyclerView.Adapter<UploadMediaDe
|
|||
captionInputLayout.setEndIconDrawable(R.drawable.mapbox_info_icon_default);
|
||||
captionInputLayout.setEndIconOnClickListener(v ->
|
||||
callback.showAlert(R.string.media_detail_caption, R.string.caption_info));
|
||||
Objects.requireNonNull(captionInputLayout.getEditText()).setFilters(new InputFilter[] {
|
||||
new UploadMediaDetailInputFilter()
|
||||
});
|
||||
|
||||
descInputLayout.setEndIconMode(TextInputLayout.END_ICON_CUSTOM);
|
||||
descInputLayout.setEndIconDrawable(R.drawable.mapbox_info_icon_default);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,80 @@
|
|||
package fr.free.nrw.commons.upload;
|
||||
|
||||
import android.text.InputFilter;
|
||||
import android.text.Spanned;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* An {@link InputFilter} class that removes characters blocklisted in Wikimedia titles. The list
|
||||
* of blocklisted characters is linked below.
|
||||
* @see <a href="https://commons.wikimedia.org/wiki/MediaWiki:Titleblacklist"></a>wikimedia.org</a>
|
||||
*/
|
||||
public class UploadMediaDetailInputFilter implements InputFilter {
|
||||
private final Pattern[] patterns;
|
||||
|
||||
/**
|
||||
* Initializes the blocklisted patterns.
|
||||
*/
|
||||
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{202A}-\\x{202E}]"),
|
||||
Pattern.compile("\\p{Cc}"),
|
||||
Pattern.compile("\\x{FEFF}"),
|
||||
Pattern.compile("\\x{00AD}"),
|
||||
Pattern.compile("[\\x{E000}-\\x{F8FF}\\x{FFF0}-\\x{FFFF}]"),
|
||||
Pattern.compile("[^\\x{0000}-\\x{FFFF}\\p{sc=Han}]")
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the source text contains any blocklisted characters.
|
||||
* @param source input text
|
||||
* @return contains a blocklisted character
|
||||
*/
|
||||
private Boolean checkBlocklisted(final CharSequence source) {
|
||||
for (final Pattern pattern: patterns) {
|
||||
if (pattern.matcher(source).find()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes any blocklisted characters from the source text.
|
||||
* @param source input text
|
||||
* @return a cleaned character sequence
|
||||
*/
|
||||
private CharSequence removeBlocklisted(CharSequence source) {
|
||||
for (final Pattern pattern: patterns) {
|
||||
source = pattern.matcher(source).replaceAll("");
|
||||
}
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters out any blocklisted characters.
|
||||
* @param source {@inheritDoc}
|
||||
* @param start {@inheritDoc}
|
||||
* @param end {@inheritDoc}
|
||||
* @param dest {@inheritDoc}
|
||||
* @param dstart {@inheritDoc}
|
||||
* @param dend {@inheritDoc}
|
||||
* @return {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
|
||||
int dend) {
|
||||
if (checkBlocklisted(source)) {
|
||||
if (start == dstart) {
|
||||
return dest;
|
||||
}
|
||||
|
||||
return removeBlocklisted(source);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue