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

dx(runtime-dom): warn when a style value ends in a semicolon #7062

Merged
merged 1 commit into from
Nov 9, 2022
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
19 changes: 19 additions & 0 deletions packages/runtime-dom/__tests__/patchStyle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ describe(`runtime-dom: style patching`, () => {
expect(el.style.cssText).toBe('')
})

it('should warn for trailing semicolons', () => {
const el = document.createElement('div')
patchProp(el, 'style', null, { color: 'red;' })
expect(
`Unexpected semicolon at the end of 'color' style value: 'red;'`
).toHaveBeenWarned()

patchProp(el, 'style', null, { '--custom': '100; ' })
expect(
`Unexpected semicolon at the end of '--custom' style value: '100; '`
).toHaveBeenWarned()
})

it('should not warn for escaped trailing semicolons', () => {
const el = document.createElement('div')
patchProp(el, 'style', null, { '--custom': '100\\;' })
expect(el.style.getPropertyValue('--custom')).toBe('100\\;')
})

// JSDOM doesn't support custom properties on style object so we have to
// mock it here.
function mockElementWithStyle() {
Expand Down
10 changes: 9 additions & 1 deletion packages/runtime-dom/src/modules/style.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isString, hyphenate, capitalize, isArray } from '@vue/shared'
import { camelize } from '@vue/runtime-core'
import { camelize, warn } from '@vue/runtime-core'

type Style = string | Record<string, string | string[]> | null

Expand Down Expand Up @@ -35,6 +35,7 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
}
}

const semicolonRE = /[^\\];\s*$/
const importantRE = /\s*!important$/

function setStyle(
Expand All @@ -46,6 +47,13 @@ function setStyle(
val.forEach(v => setStyle(style, name, v))
} else {
if (val == null) val = ''
if (__DEV__) {
if (semicolonRE.test(val)) {
warn(
`Unexpected semicolon at the end of '${name}' style value: '${val}'`
)
}
}
if (name.startsWith('--')) {
// custom property definition
style.setProperty(name, val)
Expand Down