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(runtime-dom): clear prev style with string and object #10313

Closed
wants to merge 1 commit 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
15 changes: 15 additions & 0 deletions packages/runtime-dom/__tests__/patchStyle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ describe(`runtime-dom: style patching`, () => {
expect(el.style.getPropertyValue('--custom')).toBe('100\\;')
})

it('should clear prev style with string and object', () => {
let el = document.createElement('div')
patchProp(el, 'style', null, { color: 'red' })
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
patchProp(el, 'style', { color: 'red' }, { fontSize: '12px' })
expect(el.style.cssText.replace(/\s/g, '')).toBe('font-size:12px;')

// reset el
el = document.createElement('div')
patchProp(el, 'style', null, 'color: red')
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
patchProp(el, 'style', 'color: red', { fontSize: '12px' })
expect(el.style.cssText.replace(/\s/g, '')).toBe('font-size:12px;')
})

it('shorthand properties', () => {
const el = document.createElement('div')
patchProp(
Expand Down
26 changes: 21 additions & 5 deletions packages/runtime-dom/src/modules/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ const displayRE = /(^|;)\s*display\s*:/

export function patchStyle(el: Element, prev: Style, next: Style) {
const style = (el as HTMLElement).style
const isCssString = isString(next)
const currentDisplay = style.display
let hasControlledDisplay = false
if (next && !isCssString) {
if (prev && !isString(prev)) {
for (const key in prev) {

const isNextString = isString(next)

if (next && !isNextString) {
const prevObj = isString(prev) ? parseStyleText(prev as string) : prev
if (prevObj) {
for (const key in prevObj) {
if (next[key] == null) {
setStyle(style, key, '')
}
Expand All @@ -27,7 +30,7 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
setStyle(style, key, next[key])
}
} else {
if (isCssString) {
if (isNextString) {
if (prev !== next) {
// #9821
const cssVarText = (style as any)[CSS_VAR_TEXT]
Expand All @@ -50,6 +53,19 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
}
}

function parseStyleText(cssText: string) {
const res: Record<string, string> = {}
const listDelimiter = /;(?![^(]*\))/g
const propertyDelimiter = /:(.+)/
cssText.split(listDelimiter).forEach(function (item) {
if (item) {
const tmp = item.split(propertyDelimiter)
tmp.length > 1 && (res[tmp[0].trim()] = tmp[1].trim())
}
})
return res
}

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

Expand Down