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

Always check selection range when creating undo levels #11209

Merged
merged 1 commit into from
Feb 15, 2019

Conversation

johngodley
Copy link
Contributor

Occasionally I see an error being thrown when clicking around a rich text editor:

devtools_-_latest_local_wp-admin_post_php_post_195_action_edit

I'm not entirely sure what triggers it, but I've got the steps down to a vague:

  1. Quickly create a bunch of paragraphs
  2. Click inside the paragraph blocks, but not on the text
  3. Maybe get an error

Not the best steps, but hopefully this shows it better:

range

The problem seems to be a mousedown event triggering an undo history event, and the window.getSelection() not having any range.

In this PR I've added a check to window.getSelection() so it looks at rangeCount, as well as adds checks for all uses of createRecord.

I'm not entirely sure if this is the best solution, but it fixes the problem at hand. The error may indicate another problem I'm unaware of (with a better solution!)

The error only happens for me in Chrome (tested with 70.0.3538.77). It doesn't happen in Firefox 63 or Safari 12. The error is triggered more easily with the Unified Toolbar option enabled, although I've had it happen without.

Using Gutenberg master

Types of changes

Additional checks added for selection range in rich text editor

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@johngodley johngodley added [Type] Bug An existing feature does not function as intended [Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable labels Oct 29, 2018
@@ -407,6 +413,11 @@ export class RichText extends Component {
*/
onInput() {
const record = this.createRecord();

if ( ! record ) {
return;
Copy link
Member

Choose a reason for hiding this comment

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

The input event implies that there is user input. We cannot simply ignore this input and save nothing.

return null;
}

const range = selection.getRangeAt( 0 );
Copy link
Member

Choose a reason for hiding this comment

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

It might be better to still create the value, but not pass a range in this case.

@ellatrix
Copy link
Member

I would really love to see reproducible steps so I know exactly what is causing the issue. Not setting the right selection may cause bugs that will be hard to track down.

window.getSelection() can return an empty range, causing getRangeAt() to fail
@johngodley
Copy link
Contributor Author

Good suggestion on the range. I've changed this to pass null, and removed all the extra checks. I don't know if this will have any negative effect, but in testing it doesn't seem to. However, I'm very unaware of where the value goes after this point.

I still haven't been able to nail down exactly what the conditions are for causing the error, but it is consistent if you follow the steps I put originally.

I suspect it may be a TinyMCE thing as I also noticed that normally when I click around onSelectionChange is fired with mousedown. In the case of the empty range the mousedown event comes through onCreateUndoLevel, via TinyMCE's change handler.

I don't know if this helps in anyway.

Looking at the docs for Selection it does seem that an empty Selection (type None) is possible, so it probably makes sense to check for this.

Copy link
Member

@ellatrix ellatrix left a comment

Choose a reason for hiding this comment

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

This makes sense to me.

@ellatrix ellatrix merged commit 239b886 into master Feb 15, 2019
@ellatrix ellatrix deleted the fix/rich-text-selection-empty branch February 15, 2019 10:20
youknowriad pushed a commit that referenced this pull request Mar 6, 2019
window.getSelection() can return an empty range, causing getRangeAt() to fail
youknowriad pushed a commit that referenced this pull request Mar 6, 2019
window.getSelection() can return an empty range, causing getRangeAt() to fail
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 [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants