-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Conversation
@@ -407,6 +413,11 @@ export class RichText extends Component { | |||
*/ | |||
onInput() { | |||
const record = this.createRecord(); | |||
|
|||
if ( ! record ) { | |||
return; |
There was a problem hiding this comment.
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 ); |
There was a problem hiding this comment.
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.
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. |
aae9c06
to
5bdaf3b
Compare
window.getSelection() can return an empty range, causing getRangeAt() to fail
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 I don't know if this helps in anyway. Looking at the docs for |
There was a problem hiding this 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.
window.getSelection() can return an empty range, causing getRangeAt() to fail
window.getSelection() can return an empty range, causing getRangeAt() to fail
Occasionally I see an error being thrown when clicking around a rich text editor:
I'm not entirely sure what triggers it, but I've got the steps down to a vague:
Not the best steps, but hopefully this shows it better:
The problem seems to be a
mousedown
event triggering an undo history event, and thewindow.getSelection()
not having any range.In this PR I've added a check to
window.getSelection()
so it looks atrangeCount
, as well as adds checks for all uses ofcreateRecord
.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: