From: Evan You Date: Wed, 6 May 2020 15:22:49 +0000 (-0400) Subject: refactor: reuse parseStringStyle across compiler and runtime X-Git-Tag: v3.0.0-beta.10~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8df6bc01325e9353553766fda26432ce179c0ced;p=thirdparty%2Fvuejs%2Fcore.git refactor: reuse parseStringStyle across compiler and runtime --- diff --git a/packages/compiler-dom/src/transforms/transformStyle.ts b/packages/compiler-dom/src/transforms/transformStyle.ts index 765fab778e..2767debf70 100644 --- a/packages/compiler-dom/src/transforms/transformStyle.ts +++ b/packages/compiler-dom/src/transforms/transformStyle.ts @@ -5,6 +5,7 @@ import { SimpleExpressionNode, SourceLocation } from '@vue/compiler-core' +import { parseStringStyle } from '@vue/shared' // Parse inline CSS strings for static style attributes into an object. // This is a NodeTransform since it works on the static `style` attribute and @@ -30,19 +31,10 @@ export const transformStyle: NodeTransform = (node, context) => { } } -const listDelimiterRE = /;(?![^(]*\))/g -const propertyDelimiterRE = /:(.+)/ - -function parseInlineCSS( +const parseInlineCSS = ( cssText: string, loc: SourceLocation -): SimpleExpressionNode { - const res: Record = {} - cssText.split(listDelimiterRE).forEach(item => { - if (item) { - const tmp = item.split(propertyDelimiterRE) - tmp.length > 1 && (res[tmp[0].trim()] = tmp[1].trim()) - } - }) - return createSimpleExpression(JSON.stringify(res), false, loc, true) +): SimpleExpressionNode => { + const normalized = parseStringStyle(cssText) + return createSimpleExpression(JSON.stringify(normalized), false, loc, true) } diff --git a/packages/runtime-core/__tests__/vnode.spec.ts b/packages/runtime-core/__tests__/vnode.spec.ts index 341e840de7..b25a6682dd 100644 --- a/packages/runtime-core/__tests__/vnode.spec.ts +++ b/packages/runtime-core/__tests__/vnode.spec.ts @@ -258,7 +258,8 @@ describe('vnode', () => { } }) }) - test('style', () => { + + test('style w/ strings', () => { let props1: Data = { style: 'width:100px;right:10;top:10' } @@ -281,8 +282,8 @@ describe('vnode', () => { width: '300px', height: '300px', fontSize: 30, - right: 10, - top: 10 + right: '10', + top: '10' } }) }) diff --git a/packages/shared/src/normalizeProp.ts b/packages/shared/src/normalizeProp.ts index 3546fa04b7..5e0f84055a 100644 --- a/packages/shared/src/normalizeProp.ts +++ b/packages/shared/src/normalizeProp.ts @@ -1,14 +1,16 @@ import { isArray, isString, isObject, hyphenate } from './' import { isNoUnitNumericStyleProp } from './domAttrConfig' -export function normalizeStyle( - value: unknown -): Record | undefined { +export type NormalizedStyle = Record + +export function normalizeStyle(value: unknown): NormalizedStyle | undefined { if (isArray(value)) { const res: Record = {} for (let i = 0; i < value.length; i++) { - const styles = isString(value[i]) ? strStyleToObj(value[i]) : value[i] - const normalized = normalizeStyle(styles) + const item = value[i] + const normalized = normalizeStyle( + isString(item) ? parseStringStyle(item) : item + ) if (normalized) { for (const key in normalized) { res[key] = normalized[key] @@ -21,21 +23,21 @@ export function normalizeStyle( } } -function strStyleToObj(style: string) { - const ret: Record = {} - style - .replace(/\s*/g, '') - .split(';') - .forEach((item: string) => { - const [key, val] = item.split(':') - ret[key] = isNaN(Number(val)) ? val : Number(val) - }) +const listDelimiterRE = /;(?![^(]*\))/g +const propertyDelimiterRE = /:(.+)/ + +export function parseStringStyle(cssText: string): NormalizedStyle { + const ret: NormalizedStyle = {} + cssText.split(listDelimiterRE).forEach(item => { + if (item) { + const tmp = item.split(propertyDelimiterRE) + tmp.length > 1 && (ret[tmp[0].trim()] = tmp[1].trim()) + } + }) return ret } -export function stringifyStyle( - styles: Record | undefined -): string { +export function stringifyStyle(styles: NormalizedStyle | undefined): string { let ret = '' if (!styles) { return ret