Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #10964 TextInput maxLength cuts Unicode characters (Emojis) #22463

Closed
wants to merge 1 commit into from

Conversation

hzxiaosheng
Copy link

@hzxiaosheng hzxiaosheng commented Nov 30, 2018

Fixes #10964
Fixes #10929

Summary:

  1. When maxLength is set on TextInput, and allowedLength is 1, when the user picks an emoji character which length is 2 (or 3, 4), then maxLegnth will call [@"😁" substringToIndex:1] which will result in clearing all text already input (issue [iOS][TextInput] emoji after a new line make the line disappear #10929 [iOS][TextInput] emoji after a new line make the line disappear).

  2. Fix for this is to check the existence of emoji characters(NSStringEnumerationByComposedCharacterSequences)and shrink allowedLength, ensure that emoji character was rejected as an unit.

Test Plan:

Use the RNTester app and navigate to TextInput test page and we can test input emojis in the "TextInput maxLength" section to replay the bug and verify this commit.

Changelog:

[IOS] [BUG] [TextInput] Fixes #10964 TextInput maxLength cuts Unicode characters (Emojis).

Summary:

1. When maxLength is set on TextInput, and allowedLength is 1, when the user picks an emoji character which length is 2 (or 3, 4), then maxLegnth will call `[@"😁" substringToIndex:1]` which will result in clearing  all text already input (issue facebook#10929 [iOS][TextInput] emoji after a new line make the line disappear).

2. Fix for this is to check the existence of emoji characters(NSStringEnumerationByComposedCharacterSequences)and shrink allowedLength, ensure that emoji character was rejected as an unit.
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 30, 2018
@pull-bot
Copy link

Warnings
⚠️

📋 Changelog - This PR appears to be missing Changelog.

Generated by 🚫 dangerJS

@hzxiaosheng hzxiaosheng changed the title Fixe TextInput maxLength cuts Unicode characters (Emojis) #10964 Fixes #10964 TextInput maxLength cuts Unicode characters (Emojis) Nov 30, 2018
@janicduplessis
Copy link
Contributor

Should we provide first class support for this and actually use the number of characters instead of the number of bytes in the string? This would mean counting characters with [NSString enumerateSubstringsInRange] and compare that to the maxLength instead of using [NSString length].

If we go for this we might want to also update the Android implementation for consistency.

@janicduplessis
Copy link
Contributor

cc @cpojer I think you like emojis

Copy link
Contributor

@zhongwuzw zhongwuzw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hzxiaosheng Hey, please rebase the branch first, sorry for that, it's our fault.
And I think we need treat any emoji as 1 glyph, so here may wrong, because we still use substring's length to compare with allowedLength.

// Fix TextInput maxlength cuts Unicode caracters (Emojis) #10964
[text enumerateSubstringsInRange:NSMakeRange(0, text.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop){
// ensure emoji character was not cut
if(substringRange.location + substring.length > allowedLength){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hzxiaosheng Hey, please rebase the branch first, sorry for that, it's our fault.
And I think we need treat any emoji as 1 glyph, so here may wrong, because we still use substring's length to compare with allowedLength.

@cpojer
Copy link
Contributor

cpojer commented Mar 18, 2019

@hzxiaosheng could you rebase and fix up this PR?

@zhongwuzw
Copy link
Contributor

@cpojer Seems he/she is inactive recently, if you want to fix ASAP, I'll take over this issue. 😂

@cpojer
Copy link
Contributor

cpojer commented Mar 19, 2019

Closing because of inactivity. Feel free to send a PR with a proper fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Component: Text Component: TextInput Related to the TextInput component.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants