Skip to content

Commit 9a816dc

Browse files
dx(runtime-dom): warn when a style value ends in a semicolon (#7062)
1 parent fccfb18 commit 9a816dc

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/runtime-dom/__tests__/patchStyle.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,25 @@ describe(`runtime-dom: style patching`, () => {
8787
expect(el.style.cssText).toBe('')
8888
})
8989

90+
it('should warn for trailing semicolons', () => {
91+
const el = document.createElement('div')
92+
patchProp(el, 'style', null, { color: 'red;' })
93+
expect(
94+
`Unexpected semicolon at the end of 'color' style value: 'red;'`
95+
).toHaveBeenWarned()
96+
97+
patchProp(el, 'style', null, { '--custom': '100; ' })
98+
expect(
99+
`Unexpected semicolon at the end of '--custom' style value: '100; '`
100+
).toHaveBeenWarned()
101+
})
102+
103+
it('should not warn for escaped trailing semicolons', () => {
104+
const el = document.createElement('div')
105+
patchProp(el, 'style', null, { '--custom': '100\\;' })
106+
expect(el.style.getPropertyValue('--custom')).toBe('100\\;')
107+
})
108+
90109
// JSDOM doesn't support custom properties on style object so we have to
91110
// mock it here.
92111
function mockElementWithStyle() {

packages/runtime-dom/src/modules/style.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isString, hyphenate, capitalize, isArray } from '@vue/shared'
2-
import { camelize } from '@vue/runtime-core'
2+
import { camelize, warn } from '@vue/runtime-core'
33

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

@@ -35,6 +35,7 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
3535
}
3636
}
3737

38+
const semicolonRE = /[^\\];\s*$/
3839
const importantRE = /\s*!important$/
3940

4041
function setStyle(
@@ -46,6 +47,13 @@ function setStyle(
4647
val.forEach(v => setStyle(style, name, v))
4748
} else {
4849
if (val == null) val = ''
50+
if (__DEV__) {
51+
if (semicolonRE.test(val)) {
52+
warn(
53+
`Unexpected semicolon at the end of '${name}' style value: '${val}'`
54+
)
55+
}
56+
}
4957
if (name.startsWith('--')) {
5058
// custom property definition
5159
style.setProperty(name, val)

0 commit comments

Comments
 (0)