Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3019448

Browse files
authored
fix(compiler-vapor): properly handling of class and style bindings on SVG (#14383)
close #14382
1 parent d91096c commit 3019448

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

‎packages/compiler-vapor/__tests__/transforms/__snapshots__/vBind.spec.ts.snap‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,12 +597,12 @@ export function render(_ctx) {
597597
`;
598598
599599
exports[`compiler v-bind > :class w/ svg elements 1`] = `
600-
"import { setAttr as _setAttr, renderEffect as _renderEffect, template as _template } from 'vue';
600+
"import { setClass as _setClass, renderEffect as _renderEffect, template as _template } from 'vue';
601601
const t0 = _template("<svg>", true, 1)
602602
603603
export function render(_ctx) {
604604
const n0 = t0()
605-
_renderEffect(() => _setAttr(n0, "class", _ctx.cls, true))
605+
_renderEffect(() => _setClass(n0, _ctx.cls, true))
606606
return n0
607607
}"
608608
`;
@@ -618,6 +618,17 @@ export function render(_ctx) {
618618
}"
619619
`;
620620
621+
exports[`compiler v-bind > :style w/ svg elements 1`] = `
622+
"import { setStyle as _setStyle, renderEffect as _renderEffect, template as _template } from 'vue';
623+
const t0 = _template("<svg>", true, 1)
624+
625+
export function render(_ctx) {
626+
const n0 = t0()
627+
_renderEffect(() => _setStyle(n0, _ctx.style))
628+
return n0
629+
}"
630+
`;
631+
621632
exports[`compiler v-bind > :textContext 1`] = `
622633
"import { setText as _setText, renderEffect as _renderEffect, template as _template } from 'vue';
623634
const t0 = _template("<div>", true)

‎packages/compiler-vapor/__tests__/transforms/vBind.spec.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,16 @@ describe('compiler v-bind', () => {
717717
<svg :class="cls"/>
718718
`)
719719
expect(code).matchSnapshot()
720-
expect(code).contains('_setAttr(n0, "class", _ctx.cls, true))')
720+
// should pass isSVG: true to the helper
721+
expect(code).contains('_setClass(n0, _ctx.cls, true))')
722+
})
723+
724+
test(':style w/ svg elements', () => {
725+
const { code } = compileWithVBind(`
726+
<svg :style="style"/>
727+
`)
728+
expect(code).matchSnapshot()
729+
expect(code).contains('_setStyle(n0, _ctx.style))')
721730
})
722731

723732
test('v-bind w/ svg elements', () => {

‎packages/compiler-vapor/src/generators/prop.ts‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,31 +177,31 @@ function getRuntimeHelper(
177177
const tagName = tag.toUpperCase()
178178
const isSVG = isSVGTag(tag)
179179

180-
// 1. SVG: always attribute
181-
if (isSVG) {
182-
return extend({ isSVG: true }, helpers.setAttr)
183-
}
184-
185180
if (modifier) {
186181
if (modifier === '.') {
187-
return getSpecialHelper(key, tagName) || helpers.setDOMProp
182+
return getSpecialHelper(key, tagName, isSVG) || helpers.setDOMProp
188183
} else {
189-
return helpers.setAttr
184+
return isSVG ? extend({ isSVG: true }, helpers.setAttr) : helpers.setAttr
190185
}
191186
}
192187

193-
// 2. special handling for value / style / class / textContent / innerHTML
194-
const helper = getSpecialHelper(key, tagName)
188+
// 1. special handling for value / style / class / textContent / innerHTML
189+
const helper = getSpecialHelper(key, tagName, isSVG)
195190
if (helper) {
196191
return helper
197192
}
198193

199-
// 3. Aria DOM properties shared between all Elements in
194+
// 2. Aria DOM properties shared between all Elements in
200195
// https://developer.mozilla.org/en-US/docs/Web/API/Element
201196
if (/aria[A-Z]/.test(key)) {
202197
return helpers.setDOMProp
203198
}
204199

200+
// 3. SVG: always attribute
201+
if (isSVG) {
202+
return extend({ isSVG: true }, helpers.setAttr)
203+
}
204+
205205
// 4. respect shouldSetAsAttr used in vdom and setDynamicProp for consistency
206206
// also fast path for presence of hyphen (covers data-* and aria-*)
207207
if (shouldSetAsAttr(tagName, key) || key.includes('-')) {
@@ -216,12 +216,14 @@ function getRuntimeHelper(
216216
function getSpecialHelper(
217217
keyName: string,
218218
tagName: string,
219+
isSVG: boolean,
219220
): HelperConfig | undefined {
220221
// special case for 'value' property
221222
if (keyName === 'value' && canSetValueDirectly(tagName)) {
222223
return helpers.setValue
223224
} else if (keyName === 'class') {
224-
return helpers.setClass
225+
// for svg, class should be set as attribute
226+
return extend({ isSVG }, helpers.setClass)
225227
} else if (keyName === 'style') {
226228
return helpers.setStyle
227229
} else if (keyName === 'innerHTML') {

0 commit comments

Comments
 (0)