Skip to content

Commit

Permalink
fix(compiler-core): should set <math> tag as block to retain MathML…
Browse files Browse the repository at this point in the history
… namespace after patching (vuejs#10891)

Co-authored-by: linzhe141 <linzhe141@qq.com>
  • Loading branch information
linzhe141 and linzhe141 authored May 28, 2024
1 parent 521988d commit 87c5443
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,18 @@ describe('compiler: element transform', () => {
})
})

test('<math> should be forced into blocks', () => {
const ast = parse(`<div><math/></div>`)
transform(ast, {
nodeTransforms: [transformElement],
})
expect((ast as any).children[0].children[0].codegenNode).toMatchObject({
type: NodeTypes.VNODE_CALL,
tag: `"math"`,
isBlock: true,
})
})

test('force block for runtime custom directive w/ children', () => {
const { node } = parseWithElementTransform(`<div v-foo>hello</div>`)
expect(node.isBlock).toBe(true)
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler-core/src/transforms/hoistStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ export function getConstantType(
if (
codegenNode.isBlock &&
node.tag !== 'svg' &&
node.tag !== 'foreignObject'
node.tag !== 'foreignObject' &&
node.tag !== 'math'
) {
return ConstantTypes.NOT_CONSTANT
}
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-core/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const transformElement: NodeTransform = (node, context) => {
// updates inside get proper isSVG flag at runtime. (#639, #643)
// This is technically web-specific, but splitting the logic out of core
// leads to too much unnecessary complexity.
(tag === 'svg' || tag === 'foreignObject'))
(tag === 'svg' || tag === 'foreignObject' || tag === 'math'))

// props
if (props.length > 0) {
Expand Down
38 changes: 36 additions & 2 deletions packages/runtime-dom/__tests__/nodeOps.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { nodeOps, svgNS } from '../src/nodeOps'

import { defineComponent, h, nextTick, ref } from 'vue'
import { mathmlNS, nodeOps, svgNS } from '../src/nodeOps'
import { render } from '@vue/runtime-dom'
describe('runtime-dom: node-ops', () => {
test("the <select>'s multiple attr should be set in createElement", () => {
const el = nodeOps.createElement('select', undefined, undefined, {
Expand Down Expand Up @@ -106,5 +107,38 @@ describe('runtime-dom: node-ops', () => {
expect(nodes[0]).toBe(parent.firstChild)
expect(nodes[1]).toBe(parent.childNodes[parent.childNodes.length - 2])
})

test('The math elements should keep their MathML namespace', async () => {
let root = document.createElement('div') as any

let countRef: any
const component = defineComponent({
data() {
return { value: 0 }
},
setup() {
const count = ref(0)
countRef = count
return {
count,
}
},
template: `
<div>
<math>
<mrow class="bar" v-if="count % 2">Bar</mrow>
<msup class="foo" v-else>Foo</msup>
</math>
</div>
`,
})
render(h(component), root)
const foo = root.querySelector('.foo')
expect(foo.namespaceURI).toBe(mathmlNS)
countRef.value++
await nextTick()
const bar = root.querySelector('.bar')
expect(bar.namespaceURI).toBe(mathmlNS)
})
})
})

0 comments on commit 87c5443

Please sign in to comment.