Skip to content

fix(compiler-core): should set <math> tag as block to retain MathML namespace after patching #10891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
})