Skip to content

Commit d4e9b19

Browse files
authored
perf: only patch string style when value has changed (#1310)
fix #1309
1 parent 1f2926a commit d4e9b19

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ describe(`runtime-dom: style patching`, () => {
77
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
88
})
99

10+
// #1309
11+
it('should not patch same string style', () => {
12+
const el = document.createElement('div')
13+
const fn = jest.fn()
14+
const value = (el.style.cssText = 'color:red;')
15+
Object.defineProperty(el.style, 'cssText', {
16+
get(): any {
17+
return value
18+
},
19+
set: fn
20+
})
21+
patchProp(el, 'style', value, value)
22+
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
23+
expect(fn).not.toBeCalled()
24+
})
25+
1026
it('plain object', () => {
1127
const el = document.createElement('div')
1228
patchProp(el, 'style', {}, { color: 'red' })

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
88
if (!next) {
99
el.removeAttribute('style')
1010
} else if (isString(next)) {
11-
style.cssText = next
11+
if (prev !== next) {
12+
style.cssText = next
13+
}
1214
} else {
1315
for (const key in next) {
1416
setStyle(style, key, next[key] as string)

0 commit comments

Comments
 (0)