Skip to content

Commit 7fbc933

Browse files
authored
fix(compiler-core): fix svg with directives being incorrectly hoisted (#5919)
fix #5289
1 parent 3bdc41d commit 7fbc933

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap

+24
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,30 @@ return function render(_ctx, _cache) {
211211
}"
212212
`;
213213
214+
exports[`compiler: hoistStatic transform prefixIdentifiers should NOT hoist SVG with directives 1`] = `
215+
"const _Vue = Vue
216+
const { createElementVNode: _createElementVNode } = _Vue
217+
218+
const _hoisted_1 = /*#__PURE__*/_createElementVNode(\\"path\\", { d: \\"M2,3H5.5L12\\" }, null, -1 /* HOISTED */)
219+
const _hoisted_2 = [
220+
_hoisted_1
221+
]
222+
223+
return function render(_ctx, _cache) {
224+
with (_ctx) {
225+
const { createElementVNode: _createElementVNode, resolveDirective: _resolveDirective, openBlock: _openBlock, createElementBlock: _createElementBlock, withDirectives: _withDirectives } = _Vue
226+
227+
const _directive_foo = _resolveDirective(\\"foo\\")
228+
229+
return (_openBlock(), _createElementBlock(\\"div\\", null, [
230+
_withDirectives((_openBlock(), _createElementBlock(\\"svg\\", null, _hoisted_2)), [
231+
[_directive_foo]
232+
])
233+
]))
234+
}
235+
}"
236+
`;
237+
214238
exports[`compiler: hoistStatic transform prefixIdentifiers should NOT hoist elements with cached handlers + other bindings 1`] = `
215239
"import { normalizeClass as _normalizeClass, createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
216240

packages/compiler-core/__tests__/transforms/hoistStatic.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -616,5 +616,11 @@ describe('compiler: hoistStatic transform', () => {
616616
expect(root.hoists.length).toBe(0)
617617
expect(generate(root).code).toMatchSnapshot()
618618
})
619+
620+
test('should NOT hoist SVG with directives', () => {
621+
const root = transformWithHoist(`<div><svg v-foo><path d="M2,3H5.5L12"/></svg></div>`)
622+
expect(root.hoists.length).toBe(2)
623+
expect(generate(root).code).toMatchSnapshot()
624+
})
619625
})
620626
})

packages/compiler-core/src/transforms/hoistStatic.ts

+9
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ export function getConstantType(
230230
// static then they don't need to be blocks since there will be no
231231
// nested updates.
232232
if (codegenNode.isBlock) {
233+
// except set custom directives.
234+
for (let i = 0; i < node.props.length; i++) {
235+
const p = node.props[i]
236+
if (p.type === NodeTypes.DIRECTIVE) {
237+
constantCache.set(node, ConstantTypes.NOT_CONSTANT)
238+
return ConstantTypes.NOT_CONSTANT
239+
}
240+
}
241+
233242
context.removeHelper(OPEN_BLOCK)
234243
context.removeHelper(
235244
getVNodeBlockHelper(context.inSSR, codegenNode.isComponent)

0 commit comments

Comments
 (0)