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

fix/[#21440] Copy/cut input values copying entire block #21457

Merged
merged 1 commit into from
Apr 8, 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
13 changes: 13 additions & 0 deletions packages/dom/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ _Returns_

- `boolean`: True if at the horizontal edge, false if not.

<a name="isNumberInput" href="#isNumberInput">#</a> **isNumberInput**

Check whether the given element is an input field of type number
and has a valueAsNumber

_Parameters_

- _element_ `HTMLElement`: The HTML element.

_Returns_

- `boolean`: True if the element is input and holds a number.

<a name="isTextField" href="#isTextField">#</a> **isTextField**

Check whether the given element is a text field, where text field is defined
Expand Down
18 changes: 18 additions & 0 deletions packages/dom/src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,20 @@ export function isTextField( element ) {
}
}

/**
* Check whether the given element is an input field of type number
* and has a valueAsNumber
*
* @param {HTMLElement} element The HTML element.
*
* @return {boolean} True if the element is input and holds a number.
*/
export function isNumberInput( element ) {
const { nodeName, type, valueAsNumber } = element;

return nodeName === 'INPUT' && type === 'number' && !! valueAsNumber;
}

/**
* Check wether the current document has a selection.
* This checks both for focus in an input field and general text selection.
Expand All @@ -480,6 +494,10 @@ export function documentHasSelection() {
return true;
}

if ( isNumberInput( document.activeElement ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add this inside isTextField or should it be a separate function? Or more precisely, do we consider a "number" input as a text field? I might think yes but curious what others think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Initially I extended isTextField but later I asked myself the same question and to me it makes more sense to have them separated. I don't consider numbers as text per se. Also curious what others think : )

return true;
}

const selection = window.getSelection();
const range = selection.rangeCount ? selection.getRangeAt( 0 ) : null;

Expand Down
16 changes: 16 additions & 0 deletions packages/dom/src/test/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
placeCaretAtHorizontalEdge,
isTextField,
__unstableStripHTML as stripHTML,
isNumberInput,
} from '../dom';

describe( 'DOM', () => {
Expand Down Expand Up @@ -143,6 +144,21 @@ describe( 'DOM', () => {
);
} );

it( 'should return false for empty input element of type number', () => {
const input = document.createElement( 'input' );
input.type = 'number';

expect( isNumberInput( input ) ).toBe( false );
} );

it( 'should return true for an input element of type number', () => {
const input = document.createElement( 'input' );
input.type = 'number';
input.valueAsNumber = 23;

expect( isNumberInput( input ) ).toBe( true );
} );

it( 'should return true for a contenteditable element', () => {
const div = document.createElement( 'div' );

Expand Down