From 68469840923d9793115ee4558c25fbb40dcce684 Mon Sep 17 00:00:00 2001 From: Kiril Zhelyazkov Date: Tue, 7 Apr 2020 13:53:24 +0300 Subject: [PATCH] fix/[#21440] Copy/cut input values copying entire block --- packages/dom/README.md | 13 +++++++++++++ packages/dom/src/dom.js | 18 ++++++++++++++++++ packages/dom/src/test/dom.js | 16 ++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/packages/dom/README.md b/packages/dom/README.md index d52e47552f342e..d45b8439915786 100644 --- a/packages/dom/README.md +++ b/packages/dom/README.md @@ -118,6 +118,19 @@ _Returns_ - `boolean`: True if at the horizontal edge, false if not. +# **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. + # **isTextField** Check whether the given element is a text field, where text field is defined diff --git a/packages/dom/src/dom.js b/packages/dom/src/dom.js index c1ae1fbc17395d..8271abe1efcd9f 100644 --- a/packages/dom/src/dom.js +++ b/packages/dom/src/dom.js @@ -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. @@ -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; diff --git a/packages/dom/src/test/dom.js b/packages/dom/src/test/dom.js index a2352d75efbd8a..eabb2ed968bd1a 100644 --- a/packages/dom/src/test/dom.js +++ b/packages/dom/src/test/dom.js @@ -6,6 +6,7 @@ import { placeCaretAtHorizontalEdge, isTextField, __unstableStripHTML as stripHTML, + isNumberInput, } from '../dom'; describe( 'DOM', () => { @@ -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' );