Skip to content

Commit

Permalink
fix/[#21440] Copy/cut input values copying entire block (#21457)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirilzh authored Apr 8, 2020
1 parent aa05f36 commit a3a06e5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
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 ) ) {
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

0 comments on commit a3a06e5

Please sign in to comment.