Skip to content

RichText: remove selection change listener during composition - #14449

Merged
ellatrix merged 1 commit into
masterfrom
fix/composing-input-selection
Mar 15, 2019
Merged

ellatrix merged 1 commit into
masterfrom
fix/composing-input-selection

Conversation

@ellatrix

Copy link
Copy Markdown
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
// `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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@youknowriad youknowriad left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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