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

[WIP] fix(driver:keyboard): fix typing negative numbers with existing text … #4768

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 8 additions & 1 deletion packages/driver/src/cypress/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const Promise = require('bluebird')
const $elements = require('../dom/elements')
const $selection = require('../dom/selection')
const $Cypress = require('../cypress')
// eslint-disable-next-line no-unused-vars
const debug = require('debug')('cypress:driver:keyboard')

const isSingleDigitRe = /^\d$/
const isStartingDigitRe = /^\d/
Expand Down Expand Up @@ -563,6 +565,9 @@ const $Keyboard = {
const isNumberInputType = $elements.isInput(el) && $elements.isType(el, 'number')

if (isNumberInputType) {
const selectionBounds = $selection.getSelectionBounds(el)
// debug('selectionbounds:', selectionBounds)
const selectionLength = selectionBounds.end - selectionBounds.start
const { selectionStart } = el
const valueLength = $elements.getNativeProp(el, 'value').length
const isDigitsInText = isStartingDigitRe.test(options.chars)
Expand All @@ -575,9 +580,11 @@ const $Keyboard = {
return
}

const isFirstSymbol = !valueLength || (selectionLength === valueLength)

//# only type '.' and '-' if it is the first symbol and there already is a value, or if
//# '.' or '-' are appended to a digit. If not, value cannot be set.
Copy link
Member

Choose a reason for hiding this comment

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

I think there may be some missing logic still to match the spec - notably allowing e, E, and +. Also - I'd like a link to the spec in the comments if possible because otherwise I'm just like - where did this logic definition even come from?

if (isDigit && ((prevChar === '.') || ((prevChar === '-') && !valueLength))) {
if (isDigit && ((prevChar === '.') || ((prevChar === '-') && isFirstSymbol))) {
options.prevChar = key
key = prevChar + key
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,14 @@ describe('src/cy/commands/actions/type', () => {
.should('have.value', '-123.12')
})

it('can type negative numbers with currently active selection', () => {
cy.get('#number-without-value')
.type('999')
.type('{selectall}')
.type('-123.12')
.should('have.value', '-123.12')
})

it('type=number blurs consistently', () => {
let blurred = 0

Expand Down