petehouston / flutter-emoji

👉 A light-weight Emoji 📦 for Flutter and Dart-based applications with all up-to-date emojis 😄. Made from 💯% ☕ with ❤️!
https://petehouston.com
MIT License
81 stars 31 forks source link

Emoji Regex #8

Open febg11 opened 4 years ago

febg11 commented 4 years ago

I was wondering if you could help me with blocking emojis from being typed into input boxes.

The emoji 🌬️ (and a few others) still get typed into input boxes that have been blacklisted with your regex.

   return BlacklistingTextInputFormatter(
        EmojiParser.REGEX_EMOJI
    );

They dont get typed but they get counted as input. E.g. if you have a hint in the text box (or character count) and type the emoji the hint will disappear and the character count will increase to 1. There is actually no input in the box, just empty space.

febg11 commented 4 years ago

If i get the string after typing them in the code unit field has [65039, 65039, 65039, 65039, 65039, 65039] as a value. This seems to be this https://unicode-table.com/en/FE0F/

febg11 commented 4 years ago

Could you try and use this regex it seems to fix the problem https://github.com/mathiasbynens/emoji-regex/blob/master/index.js

petehouston commented 4 years ago

@febg11 it's kinda different, the regex by EmojiParser is to parse emoji, not to filter. If you want to filter emoji runes from TextField or similar widgets, you need to filter the \ufe0f character.

Use this

TextField(
  onChanged: (text) {
    print("Word count: " + text.length.toString());
  },
  inputFormatters: [
    BlacklistingTextInputFormatter(RegExp(
        r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]|\ufe0f)'
    ))
  ],
)
febg11 commented 4 years ago

Hi @petehouston

Sorry for not replying for a long time I only just got around to doing this. I have tested your regex which fixes a lot of my problems however it does not detect the English, Welsh and Scottish flags as well as the unofficial flags which can be seen on here.

This is part of my test that fails.

    String regexNew = r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]|\ufe0f)';
    RegExp regex = RegExp(regexNew);

    print("Starting british flags");
    String flags2 = "🏴󠁧󠁢󠁥󠁮󠁧󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁷󠁬󠁳󠁿";
    String flags2Output = flags2.replaceAll(regex, "");
    expect(flags2Output, "");
    print("Starting Unofficial Flags Emojis");
    String unofficialFlagsEmojis = "🏴󠁢󠁳󠁡󠁫󠁿🏴󠁢󠁳󠁢󠁹󠁿🏴󠁢󠁳󠁢󠁩󠁿🏴󠁢󠁳󠁢󠁰󠁿🏴󠁢󠁳󠁣󠁯󠁿🏴󠁢󠁳󠁣󠁳󠁿🏴󠁢󠁳󠁣󠁥󠁿🏴󠁢󠁳󠁣󠁫󠁿🏴󠁢󠁳󠁥󠁧󠁿🏴󠁢󠁳󠁥󠁸󠁿🏴󠁢󠁳󠁦󠁰󠁿🏴󠁢󠁳󠁧󠁣󠁿🏴󠁢󠁳󠁨󠁩󠁿🏴󠁢󠁳󠁨󠁴󠁿🏴󠁢󠁳󠁩󠁮󠁿🏴󠁢󠁳󠁬󠁩󠁿🏴󠁢󠁳󠁭󠁣󠁿🏴󠁢󠁳󠁭󠁧󠁿🏴󠁢󠁳󠁭󠁩󠁿🏴󠁢󠁳󠁮󠁯󠁿🏴󠁢󠁳󠁮󠁳󠁿🏴󠁢󠁳󠁮󠁥󠁿🏴󠁢󠁳󠁲󠁩󠁿🏴󠁢󠁳󠁲󠁣󠁿🏴󠁢󠁳󠁳󠁳󠁿🏴󠁢󠁳󠁳󠁯󠁿🏴󠁢󠁳󠁳󠁡󠁿🏴󠁢󠁳󠁳󠁥󠁿🏴󠁢󠁳󠁳󠁷󠁿🏴󠁢󠁳󠁷󠁧󠁿";
    String unofficialFlagsEmojisOutput = unofficialFlagsEmojis.replaceAll(regex, "");
    expect(unofficialFlagsEmojisOutput, "");

I am not sure what the unofficial flags even are.

Could you help me adjust the regex to ensure that the British flags and possibly the unofficial flags are black listed in the regex.

Thank you so much for all you have done.

petehouston commented 4 years ago

Interesting. I will take a look on those emojis.

febg11 commented 4 years ago

Thanks Pete I really appreciate your help 😊

febg11 commented 4 years ago

Hi Pete did you manage to take a look at this?