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

Add fix to make inputs of type email return true from isTextField #21162

Merged
merged 8 commits into from
Jul 7, 2020

Conversation

glendaviesnz
Copy link
Contributor

@glendaviesnz glendaviesnz commented Mar 26, 2020

Description

Currently input fields of type email return false from isTextField which causes the Gutenberg past handler to take over when pasting into an email input.

Fixes Automattic/jetpack#15121
Fixes #12780

How has this been tested?

Only tested manually at this staging using the email field in the Jetpack Simple Payments block.

Screenshots

before:

before

after:

after

Types of changes

Adds an additional check in order flag an input of type email as a text input to prevent Gutenberg's paste handler from hijacking the native input field copy/cut/paste events.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.

@glendaviesnz glendaviesnz self-assigned this Mar 26, 2020
@github-actions
Copy link

github-actions bot commented Mar 26, 2020

Size Change: -74 B (0%)

Total Size: 1.13 MB

Filename Size Change
build/dom/index.js 3.23 kB +38 B (1%)
build/edit-site/index.js 16.6 kB -112 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.14 kB 0 B
build/annotations/index.js 3.68 kB 0 B
build/api-fetch/index.js 3.4 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 7.67 kB 0 B
build/block-directory/style-rtl.css 944 B 0 B
build/block-directory/style.css 945 B 0 B
build/block-editor/index.js 110 kB 0 B
build/block-editor/style-rtl.css 10.7 kB 0 B
build/block-editor/style.css 10.7 kB 0 B
build/block-library/editor-rtl.css 7.57 kB 0 B
build/block-library/editor.css 7.57 kB 0 B
build/block-library/index.js 130 kB 0 B
build/block-library/style-rtl.css 7.78 kB 0 B
build/block-library/style.css 7.79 kB 0 B
build/block-library/theme-rtl.css 728 B 0 B
build/block-library/theme.css 729 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 48.2 kB 0 B
build/components/index.js 199 kB 0 B
build/components/style-rtl.css 15.8 kB 0 B
build/components/style.css 15.8 kB 0 B
build/compose/index.js 9.56 kB 0 B
build/core-data/index.js 11.4 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/data/index.js 8.44 kB 0 B
build/date/index.js 5.38 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 569 B 0 B
build/edit-navigation/index.js 9.92 kB 0 B
build/edit-navigation/style-rtl.css 1.02 kB 0 B
build/edit-navigation/style.css 1.02 kB 0 B
build/edit-post/index.js 304 kB 0 B
build/edit-post/style-rtl.css 5.57 kB 0 B
build/edit-post/style.css 5.57 kB 0 B
build/edit-site/style-rtl.css 3.03 kB 0 B
build/edit-site/style.css 3.03 kB 0 B
build/edit-widgets/index.js 9.35 kB 0 B
build/edit-widgets/style-rtl.css 2.45 kB 0 B
build/edit-widgets/style.css 2.45 kB 0 B
build/editor/editor-styles-rtl.css 537 B 0 B
build/editor/editor-styles.css 539 B 0 B
build/editor/index.js 45 kB 0 B
build/editor/style-rtl.css 3.77 kB 0 B
build/editor/style.css 3.77 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.73 kB 0 B
build/format-library/style-rtl.css 547 B 0 B
build/format-library/style.css 548 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keyboard-shortcuts/index.js 2.52 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.12 kB 0 B
build/list-reusable-blocks/style-rtl.css 476 B 0 B
build/list-reusable-blocks/style.css 476 B 0 B
build/media-utils/index.js 5.32 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.41 kB 0 B
build/nux/style-rtl.css 671 B 0 B
build/nux/style.css 668 B 0 B
build/plugins/index.js 2.56 kB 0 B
build/primitives/index.js 1.41 kB 0 B
build/priority-queue/index.js 788 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 13.9 kB 0 B
build/server-side-render/index.js 2.71 kB 0 B
build/shortcode/index.js 1.69 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.06 kB 0 B
build/viewport/index.js 1.85 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

@glendaviesnz glendaviesnz changed the title [WIP]: Add fix to make inputs of type email return true from isTextField Add fix to make inputs of type email return true from isTextField Mar 27, 2020
@glendaviesnz glendaviesnz marked this pull request as ready for review March 27, 2020 03:45
@glendaviesnz
Copy link
Contributor Author

glendaviesnz commented Mar 27, 2020

What are people's thoughts on this as at least a temporary fix for the likes of Automattic/jetpack#15121?

It is not a particularly nice approach having to list alternative ways to identify inputs that should be classed as text but that return selectStart as null, but can't think of a simple alternative at the moment that wouldn't still involve listing other input types that should be classed as text for the sake of the pasteHandler bypassing, or forcing components to manually trap events for inputs that aren't text, but should still maintain native copy/paste (played around with an option doing this here, but it is far from elegant!).

@@ -453,6 +453,7 @@ export function isTextField( element ) {

return (
( nodeName === 'INPUT' && selectionStart !== null ) ||
element.getAttribute( 'type' ) === 'email' ||
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably should make this ( nodeName === 'INPUT' && element.getAttribute( 'type' ) === 'email' ) will update it if there is some agreement to go with this approach.

Copy link
Member

Choose a reason for hiding this comment

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

I don't get it... is this input type not an input element?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem is that Chrome at least does not set selectStart for anything other than <input type="text">. For <input type="email"> selectStart is null, so this method returns false, but in all instances as far as I can tell we are going to want the block editor to treat an input of type email as a text input - sorry, I outlined this in the original ticket, but should have copied it over to this PR.

Copy link
Member

Choose a reason for hiding this comment

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

https://stackoverflow.com/a/21959157 seems to be related and links to this overview. type=number should be affected as well.

Copy link
Member

Choose a reason for hiding this comment

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

@ellatrix How would you like to see this fixed?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, the commit was part of #5513.

Copy link
Member

@ocean90 ocean90 May 4, 2020

Choose a reason for hiding this comment

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

Another idea might be restore the previous isInputField() and change isTextField() to use isInputField() + selectionStart. Then use isInputField() in documentHasSelection() which is the method used to check for the native copy handler.

Copy link
Member

Choose a reason for hiding this comment

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

@aduth Do have any thoughts on removing selectionStart from isTextField()?

Copy link
Member

@aduth aduth Jun 4, 2020

Choose a reason for hiding this comment

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

I could have been clearer with code comments. If I recall correctly, I think the main idea was to exclude certain <input> elements like "checkbox" and "button". If selectionStart is a problem for indicating the positive case of "is a text input", then maybe the logic can be inverted to the negative case of "is not an excluded input".

For example:

nodeName === 'INPUT' && type !== 'checkbox' && type !== 'button' /* && ... */

The existing unit tests should give some coverage here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At a quick glance it seems that of the possible 17 input types 7 could be classed as text in terms of user using standard keyboard/paste for text input, so if we want to remove selectionStart it probably makes more sense to identify the positive cases explicitly, so have updated the PR to take this approach - if there is some agreement on this will also take a look at tests to check we have this covered effectively.

'password',
'search',
'url',
'tel',
Copy link
Member

Choose a reason for hiding this comment

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

We probably have to add the date types as well since not all browsers (e.g. Safari) support them and fall back to a text field. Since that's actually the default fall back vor any unknown type we should probably go with a list which are definitely no text inputs. button, checkbox, hidden, upload, …

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, good point, have switched to the checking for non text inputs - I think the list is correct, let me know if I have missed anything.

packages/dom/src/dom.js Outdated Show resolved Hide resolved
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.

Makes more sense now. :)

@glendaviesnz
Copy link
Contributor Author

Looks like the e2e tests may have picked up some issues with this change, will try and take a look at that in next day or so.

@glendaviesnz glendaviesnz merged commit 0cdf574 into master Jul 7, 2020
@glendaviesnz glendaviesnz deleted the fix/email-field-past-bug branch July 7, 2020 02:37
@github-actions github-actions bot added this to the Gutenberg 8.6 milestone Jul 7, 2020
youknowriad pushed a commit that referenced this pull request Jul 13, 2020
…1162)

* Add fix to make inputs of type email return true from isTextField to allow native paste to handle pasting in email inputs

Co-authored-by: Glen Davies <glen.davies@a8c.com>
Co-authored-by: Ella van Durpe <wp@iseulde.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants