Skip to content

Commit 2641422

Browse files
committed
fix(compiler-dom): stringify eligible svg content
fix #4282
1 parent f258f5d commit 2641422

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,28 @@ describe('stringify static html', () => {
360360
]
361361
})
362362
})
363+
364+
test('should stringify svg', () => {
365+
const svg = `<svg width="50" height="50" viewBox="0 0 50 50" fill="none" xmlns="http://www.w3.org/2000/svg">`
366+
const repeated = `<rect width="50" height="50" fill="#C4C4C4"></rect>`
367+
const { ast } = compileWithStringify(
368+
`<div>${svg}${repeat(
369+
repeated,
370+
StringifyThresholds.ELEMENT_WITH_BINDING_COUNT
371+
)}</svg></div>`
372+
)
373+
expect(ast.hoists[0]).toMatchObject({
374+
type: NodeTypes.JS_CALL_EXPRESSION,
375+
callee: CREATE_STATIC,
376+
arguments: [
377+
JSON.stringify(
378+
`${svg}${repeat(
379+
repeated,
380+
StringifyThresholds.ELEMENT_WITH_BINDING_COUNT
381+
)}</svg>`
382+
),
383+
'1'
384+
]
385+
})
386+
})
363387
})

packages/compiler-dom/src/transforms/stringifyStatic.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ import {
2020
isVoidTag,
2121
isString,
2222
isSymbol,
23-
isKnownAttr,
23+
isKnownHtmlAttr,
2424
escapeHtml,
2525
toDisplayString,
2626
normalizeClass,
2727
normalizeStyle,
2828
stringifyStyle,
29-
makeMap
29+
makeMap,
30+
isKnownSvgAttr
3031
} from '@vue/shared'
32+
import { DOMNamespaces } from '../parserOptions'
3133

3234
export const enum StringifyThresholds {
3335
ELEMENT_WITH_BINDING_COUNT = 5,
@@ -138,8 +140,14 @@ const getHoistedNode = (node: TemplateChildNode) =>
138140
node.codegenNode.hoisted
139141

140142
const dataAriaRE = /^(data|aria)-/
141-
const isStringifiableAttr = (name: string) => {
142-
return isKnownAttr(name) || dataAriaRE.test(name)
143+
const isStringifiableAttr = (name: string, ns: DOMNamespaces) => {
144+
return (
145+
(ns === DOMNamespaces.HTML
146+
? isKnownHtmlAttr(name)
147+
: ns === DOMNamespaces.SVG
148+
? isKnownSvgAttr(name)
149+
: false) || dataAriaRE.test(name)
150+
)
143151
}
144152

145153
const replaceHoist = (
@@ -175,6 +183,7 @@ function analyzeNode(node: StringifiableNode): [number, number] | false {
175183
let ec = node.props.length > 0 ? 1 : 0 // element w/ binding count
176184
let bailed = false
177185
const bail = (): false => {
186+
debugger
178187
bailed = true
179188
return false
180189
}
@@ -187,15 +196,18 @@ function analyzeNode(node: StringifiableNode): [number, number] | false {
187196
for (let i = 0; i < node.props.length; i++) {
188197
const p = node.props[i]
189198
// bail on non-attr bindings
190-
if (p.type === NodeTypes.ATTRIBUTE && !isStringifiableAttr(p.name)) {
199+
if (
200+
p.type === NodeTypes.ATTRIBUTE &&
201+
!isStringifiableAttr(p.name, node.ns)
202+
) {
191203
return bail()
192204
}
193205
if (p.type === NodeTypes.DIRECTIVE && p.name === 'bind') {
194206
// bail on non-attr bindings
195207
if (
196208
p.arg &&
197209
(p.arg.type === NodeTypes.COMPOUND_EXPRESSION ||
198-
(p.arg.isStatic && !isStringifiableAttr(p.arg.content)))
210+
(p.arg.isStatic && !isStringifiableAttr(p.arg.content, node.ns)))
199211
) {
200212
return bail()
201213
}

packages/shared/src/domAttrConfig.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const isNoUnitNumericStyleProp = /*#__PURE__*/ makeMap(
6666
* Don't also forget to allow `data-*` and `aria-*`!
6767
* Generated from https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
6868
*/
69-
export const isKnownAttr = /*#__PURE__*/ makeMap(
69+
export const isKnownHtmlAttr = /*#__PURE__*/ makeMap(
7070
`accept,accept-charset,accesskey,action,align,allow,alt,async,` +
7171
`autocapitalize,autocomplete,autofocus,autoplay,background,bgcolor,` +
7272
`border,buffered,capture,challenge,charset,checked,cite,class,code,` +
@@ -83,3 +83,48 @@ export const isKnownAttr = /*#__PURE__*/ makeMap(
8383
`start,step,style,summary,tabindex,target,title,translate,type,usemap,` +
8484
`value,width,wrap`
8585
)
86+
87+
/**
88+
* Generated from https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute
89+
*/
90+
export const isKnownSvgAttr = /*#__PURE__*/ makeMap(
91+
`xmlns,accent-height,accumulate,additive,alignment-baseline,alphabetic,amplitude,` +
92+
`arabic-form,ascent,attributeName,attributeType,azimuth,baseFrequency,` +
93+
`baseline-shift,baseProfile,bbox,begin,bias,by,calcMode,cap-height,class,` +
94+
`clip,clipPathUnits,clip-path,clip-rule,color,color-interpolation,` +
95+
`color-interpolation-filters,color-profile,color-rendering,` +
96+
`contentScriptType,contentStyleType,crossorigin,cursor,cx,cy,d,decelerate,` +
97+
`descent,diffuseConstant,direction,display,divisor,dominant-baseline,dur,dx,` +
98+
`dy,edgeMode,elevation,enable-background,end,exponent,fill,fill-opacity,` +
99+
`fill-rule,filter,filterRes,filterUnits,flood-color,flood-opacity,` +
100+
`font-family,font-size,font-size-adjust,font-stretch,font-style,` +
101+
`font-variant,font-weight,format,from,fr,fx,fy,g1,g2,glyph-name,` +
102+
`glyph-orientation-horizontal,glyph-orientation-vertical,glyphRef,` +
103+
`gradientTransform,gradientUnits,hanging,height,href,hreflang,horiz-adv-x,` +
104+
`horiz-origin-x,id,ideographic,image-rendering,in,in2,intercept,k,k1,k2,k3,` +
105+
`k4,kernelMatrix,kernelUnitLength,kerning,keyPoints,keySplines,keyTimes,` +
106+
`lang,lengthAdjust,letter-spacing,lighting-color,limitingConeAngle,local,` +
107+
`marker-end,marker-mid,marker-start,markerHeight,markerUnits,markerWidth,` +
108+
`mask,maskContentUnits,maskUnits,mathematical,max,media,method,min,mode,` +
109+
`name,numOctaves,offset,opacity,operator,order,orient,orientation,origin,` +
110+
`overflow,overline-position,overline-thickness,panose-1,paint-order,path,` +
111+
`pathLength,patternContentUnits,patternTransform,patternUnits,ping,` +
112+
`pointer-events,points,pointsAtX,pointsAtY,pointsAtZ,preserveAlpha,` +
113+
`preserveAspectRatio,primitiveUnits,r,radius,referrerPolicy,refX,refY,rel,` +
114+
`rendering-intent,repeatCount,repeatDur,requiredExtensions,requiredFeatures,` +
115+
`restart,result,rotate,rx,ry,scale,seed,shape-rendering,slope,spacing,` +
116+
`specularConstant,specularExponent,speed,spreadMethod,startOffset,` +
117+
`stdDeviation,stemh,stemv,stitchTiles,stop-color,stop-opacity,` +
118+
`strikethrough-position,strikethrough-thickness,string,stroke,` +
119+
`stroke-dasharray,stroke-dashoffset,stroke-linecap,stroke-linejoin,` +
120+
`stroke-miterlimit,stroke-opacity,stroke-width,style,surfaceScale,` +
121+
`systemLanguage,tabindex,tableValues,target,targetX,targetY,text-anchor,` +
122+
`text-decoration,text-rendering,textLength,to,transform,transform-origin,` +
123+
`type,u1,u2,underline-position,underline-thickness,unicode,unicode-bidi,` +
124+
`unicode-range,units-per-em,v-alphabetic,v-hanging,v-ideographic,` +
125+
`v-mathematical,values,vector-effect,version,vert-adv-y,vert-origin-x,` +
126+
`vert-origin-y,viewBox,viewTarget,visibility,width,widths,word-spacing,` +
127+
`writing-mode,x,x-height,x1,x2,xChannelSelector,xlink:actuate,xlink:arcrole,` +
128+
`xlink:href,xlink:role,xlink:show,xlink:title,xlink:type,xml:base,xml:lang,` +
129+
`xml:space,y,y1,y2,yChannelSelector,z,zoomAndPan`
130+
)

0 commit comments

Comments
 (0)