From 1bb122c151b4e0a3a15343f82972a1654e630dc8 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Fri, 6 May 2022 16:46:01 +0100 Subject: [PATCH] DOM: Allow copying text from non-text input elements (#40192) * DOM: Fix inputFieldHasUncollapsedSelection for non-text inputs The selection state of non-text input elements (e.g. `number`, but even `email`) is opaque to Gutenberg, per the HTML spec [1]. Unaware of this nuance, `inputFieldHasUncollapsedSelection` would incorrectly report that some non-text input element had no text selected when in fact it did. This caused the block editor to take over `copy` events to copy whole blocks, preventing the user from copying what they had actually selected inside the input element in question. The workaround in this commit is to always assume that there could be an uncollapsed selection in an active element if that element's selection state is opaque. [1]: https://html.spec.whatwg.org/multipage/input.html#do-not-apply * Add input type 'time' to isTextField's blacklist * Unit tests: update expectations for isTextField() * inputFieldHasUncollapsedSelection: Rewrite comments, default to true * documentHasSelection: Accept any input types ... as oppposed to just text inputs (isTextField) and number inputs (isNumberInput). --- packages/dom/README.md | 12 ++--- .../dom/src/dom/document-has-selection.js | 10 ++--- .../dom/document-has-uncollapsed-selection.js | 8 ++-- .../input-field-has-uncollapsed-selection.js | 44 +++++++++++-------- packages/dom/src/dom/is-html-input-element.js | 2 +- packages/dom/src/dom/is-text-field.js | 2 + packages/dom/src/test/dom.js | 10 ++--- 7 files changed, 47 insertions(+), 41 deletions(-) diff --git a/packages/dom/README.md b/packages/dom/README.md index 41af65673bff0..d6ee8c5ee6125 100644 --- a/packages/dom/README.md +++ b/packages/dom/README.md @@ -28,8 +28,8 @@ _Returns_ ### documentHasSelection -Check whether the current document has a selection. This checks for both -focus in an input field and general text selection. +Check whether the current document has a selection. This includes focus in +input fields, textareas, and general rich-text selection. _Parameters_ @@ -57,9 +57,9 @@ _Returns_ ### documentHasUncollapsedSelection -Check whether the current document has any sort of selection. This includes -ranges of text across elements and any selection inside `` and -`