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(custom-elements): cast numbers with Number rules #4393

Closed
wants to merge 5 commits 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
34 changes: 34 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
renderSlot,
VueElement
} from '../src'
import { parseNumber } from '../src/apiCustomElement'

describe('defineCustomElement', () => {
const container = document.createElement('div')
Expand Down Expand Up @@ -394,3 +395,36 @@ describe('defineCustomElement', () => {
})
})
})

describe('parseNumber', () => {
it('handles strings', () => {
expect(parseNumber('')).toBe('')
expect(parseNumber(null)).toBe('')
expect(parseNumber('Something else')).toBe('Something else')
})

it('numbers', () => {
expect(parseNumber('0')).toBe(0)
expect(parseNumber('1')).toBe(1)
expect(parseNumber('1.1')).toBe(1.1)
expect(parseNumber('123e-1')).toBe(12.3)
expect(parseNumber('Infinity')).toBe(Infinity)
})

it('NaN', () => {
expect(parseNumber('NaN')).toBeNaN()
expect(parseNumber('nan')).not.toBeNaN()
})

// all of these are handled by Number
it('string non decimal bases', () => {
expect(parseNumber('0b0')).toBe(0)
expect(parseNumber('0b1')).toBe(1)

expect(parseNumber('0o3')).toBe(3)
expect(parseNumber('0o0')).toBe(0)

expect(parseNumber('0x0')).toBe(0)
expect(parseNumber('0xf')).toBe(15)
})
})
11 changes: 9 additions & 2 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
ConcreteComponent,
ComponentOptions
} from '@vue/runtime-core'
import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared'
import { camelize, extend, hyphenate, isArray } from '@vue/shared'
import { hydrate, render } from '.'

export type VueElementConstructor<P = {}> = {
Expand Down Expand Up @@ -246,7 +246,7 @@ export class VueElement extends BaseClass {
}

protected _setAttr(key: string) {
this._setProp(camelize(key), toNumber(this.getAttribute(key)), false)
this._setProp(camelize(key), parseNumber(this.getAttribute(key)), false)
}

/**
Expand Down Expand Up @@ -342,3 +342,10 @@ export class VueElement extends BaseClass {
}
}
}

export function parseNumber(value: string | null): number | string {
// for Number('') and Number(null) as they both become 0
if (!value) return ''
const casted = Number(value)
return value === 'NaN' || !Number.isNaN(casted) ? casted : value
}
Loading