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
Merged
Show file tree
Hide file tree
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
37 changes: 18 additions & 19 deletions packages/dom/src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,25 +448,24 @@ export function placeCaretAtVerticalEdge(
* @return {boolean} True if the element is an text field, false if not.
*/
export function isTextField( element ) {
try {
const { nodeName, selectionStart, contentEditable } = element;

return (
( nodeName === 'INPUT' && selectionStart !== null ) ||
nodeName === 'TEXTAREA' ||
contentEditable === 'true'
);
} catch ( error ) {
// Safari throws an exception when trying to get `selectionStart`
// on non-text <input> elements (which, understandably, don't
// have the text selection API). We catch this via a try/catch
// block, as opposed to a more explicit check of the element's
// input types, because of Safari's non-standard behavior. This
// also means we don't have to worry about the list of input
// types that support `selectionStart` changing as the HTML spec
// evolves over time.
return false;
}
const { nodeName, contentEditable } = element;
const nonTextInputs = [
'button',
'checkbox',
'hidden',
'file',
'radio',
'image',
'range',
'reset',
'submit',
'number',
];
return (
( nodeName === 'INPUT' && ! nonTextInputs.includes( element.type ) ) ||
nodeName === 'TEXTAREA' ||
contentEditable === 'true'
);
}

/**
Expand Down
13 changes: 11 additions & 2 deletions packages/dom/src/test/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ describe( 'DOM', () => {
const NON_TEXT_INPUT_TYPES = [
'button',
'checkbox',
'image',
'hidden',
'file',
'radio',
'image',
'range',
'reset',
'submit',
];

Expand All @@ -118,7 +121,13 @@ describe( 'DOM', () => {
*
* @type {string[]}
*/
const TEXT_INPUT_TYPES = [ 'text', 'password', 'search', 'url' ];
const TEXT_INPUT_TYPES = [
'text',
'password',
'search',
'url',
'email',
];

it( 'should return false for non-text input elements', () => {
NON_TEXT_INPUT_TYPES.forEach( ( type ) => {
Expand Down