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

RichText: remove selection change listener during composition #14449

Merged
merged 1 commit into from
Mar 15, 2019

Conversation

ellatrix
Copy link
Member

Description

This should be included in WP 5.2. It worked fine in WP 5.1.

Fixes #14434.

This bug can also be reproduced with other keyboards, including an American keyboard.

  1. Create a new post and paragraph.
  2. Press option+`. You started composing a character.
  3. Press any vowel, e.g. a.

Expected: à. Actual: .

Cause: the selection change event listener is called after input, which overwrites the DOM if the selection state is different (and it is different, but shouldn't be recorded). The overwrite happens for the format boundaries.

Solution: remove the selection change listener during composition.

Includes a minor clean up, as I was experimenting with different events bound to Editable.

How has this been tested?

See above.

Screenshots

Types of changes

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.

@ellatrix ellatrix added [Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable [Type] Regression Related to a regression in the latest release labels Mar 15, 2019
@ellatrix ellatrix added this to the 5.3 (Gutenberg) milestone Mar 15, 2019
@ellatrix ellatrix force-pushed the fix/composing-input-selection branch from c43764b to 9c8585a Compare March 15, 2019 11:49
@youknowriad youknowriad added the [Release] Do Not Punt Used to indicate the issue or pull request must not be moved from the assigned milestone label Mar 15, 2019
@@ -373,6 +373,8 @@ export class RichText extends Component {
// Browsers setting `isComposing` to `true` will usually emit a final
// `input` event when the characters are composed.
if ( event && event.nativeEvent.isComposing ) {
// Also don't update any selection.
document.removeEventListener( 'selectionchange', this.onSelectionChange );
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think there's a simpler way togo instead of this selection enabling/disabling? maybe a flag?

Copy link
Member Author

Choose a reason for hiding this comment

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

I see this as the simplest way. There's no need to run a listener if it's not needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

I mean, I think it's "cleanliest" if you remove listeners if you don't need them, add them back if you do need them again. I made the mistake before not to do that, and it had a negative performance impact (#12480), so I make a habit of taking this "cleaner" approach.

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

This fixes the issue for me. I'm wondering, is it possible that the event is not rebound properly in a scenario like this:

  • Trigger a composition ^ for instance
  • Leave the RichText by clicking outside or something else?

@ellatrix
Copy link
Member Author

@youknowriad Thanks for the review! To answer your question: the browser always triggers a compositionend event, also when a composition is cancelled. When you leave the field, composition ends and the final character is just ^.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable [Release] Do Not Punt Used to indicate the issue or pull request must not be moved from the assigned milestone [Type] Regression Related to a regression in the latest release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Duplicated first Chinese word when typing
2 participants