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

[core] Handle selection edge case #7350

Merged
merged 2 commits into from
Jan 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ function writeToClipboardPolyfill(data: string) {
}

function hasNativeSelection(element: HTMLInputElement) {
if (window.getSelection()?.toString() !== '') {
Copy link
Member Author

@oliviertassinari oliviertassinari Jan 9, 2023

Choose a reason for hiding this comment

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

@cherniavskii I think that this wasn't exactly correct. Per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

If the object accessed or function called is undefined or null, it returns undefined instead of throwing an error.

So when the selection returns null, the chaining returns undefined which we then compare to ''. it's different, so hasNativeSelection say there is a selection. But there is no selection in this case, in theory, per https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection.

When called on an <iframe> that is not displayed (e.g., where display: none is set) Firefox will return null

// When getSelection is called on an <iframe> that is not displayed Firefox will return null.
if (window.getSelection()?.toString()) {
return true;
}

if (!element) {
return false;
}

if ((element.selectionEnd || 0) - (element.selectionStart || 0) > 0) {
// window.getSelection() returns an empty string in Firefox for selections inside a form element.
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=85686.
// Instead, we can use element.selectionStart that is only defined on form elements.
if (element && (element.selectionEnd || 0) - (element.selectionStart || 0) > 0) {
return true;
}

Expand Down