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

perf: should patch string style when value is changed #1310

Merged
merged 3 commits into from
Jun 11, 2020
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
16 changes: 16 additions & 0 deletions packages/runtime-dom/__tests__/patchStyle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ describe(`runtime-dom: style patching`, () => {
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
})

// #1309
it('should not patch same string style', () => {
const el = document.createElement('div')
const fn = jest.fn()
const value = (el.style.cssText = 'color:red;')
Object.defineProperty(el.style, 'cssText', {
get(): any {
return value
},
set: fn
})
patchProp(el, 'style', value, value)
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
expect(fn).not.toBeCalled()
})

it('plain object', () => {
const el = document.createElement('div')
patchProp(el, 'style', {}, { color: 'red' })
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime-dom/src/modules/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
if (!next) {
el.removeAttribute('style')
} else if (isString(next)) {
style.cssText = next
if (prev !== next) {
style.cssText = next
Copy link
Member

Choose a reason for hiding this comment

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

can we have a test to cover this pls?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm lazy on it....Yeah. I add test for it.

}
} else {
for (const key in next) {
setStyle(style, key, next[key] as string)
Expand Down