mathmlNamespace.spec.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // MathML logic is technically dom-specific, but the logic is placed in core
  2. // because splitting it out of core would lead to unnecessary complexity in both
  3. // the renderer and compiler implementations.
  4. // Related files:
  5. // - runtime-core/src/renderer.ts
  6. // - compiler-core/src/transforms/transformElement.ts
  7. import { vtcKey } from '../../runtime-dom/src/components/Transition'
  8. import { h, nextTick, ref, render } from '../src'
  9. describe('MathML support', () => {
  10. afterEach(() => {
  11. document.body.innerHTML = ''
  12. })
  13. test('should mount elements with correct html namespace', () => {
  14. const root = document.createElement('div')
  15. document.body.appendChild(root)
  16. const App = {
  17. template: `
  18. <math display="block" id="e0">
  19. <semantics id="e1">
  20. <mrow id="e2">
  21. <msup>
  22. <mi>x</mi>
  23. <mn>2</mn>
  24. </msup>
  25. <mo>+</mo>
  26. <mi>y</mi>
  27. </mrow>
  28. <annotation-xml encoding="text/html" id="e3">
  29. <div id="e4" />
  30. <svg id="e5" />
  31. </annotation-xml>
  32. </semantics>
  33. </math>
  34. `,
  35. }
  36. render(h(App), root)
  37. const e0 = document.getElementById('e0')!
  38. expect(e0.namespaceURI).toMatch('Math')
  39. expect(e0.querySelector('#e1')!.namespaceURI).toMatch('Math')
  40. expect(e0.querySelector('#e2')!.namespaceURI).toMatch('Math')
  41. expect(e0.querySelector('#e3')!.namespaceURI).toMatch('Math')
  42. expect(e0.querySelector('#e4')!.namespaceURI).toMatch('xhtml')
  43. expect(e0.querySelector('#e5')!.namespaceURI).toMatch('svg')
  44. })
  45. test('should patch elements with correct namespaces', async () => {
  46. const root = document.createElement('div')
  47. document.body.appendChild(root)
  48. const cls = ref('foo')
  49. const App = {
  50. setup: () => ({ cls }),
  51. template: `
  52. <div>
  53. <math id="f1" :class="cls">
  54. <annotation encoding="text/html">
  55. <div id="f2" :class="cls"/>
  56. </annotation>
  57. </math>
  58. </div>
  59. `,
  60. }
  61. render(h(App), root)
  62. const f1 = document.querySelector('#f1')!
  63. const f2 = document.querySelector('#f2')!
  64. expect(f1.getAttribute('class')).toBe('foo')
  65. expect(f2.className).toBe('foo')
  66. // set a transition class on the <div> - which is only respected on non-svg
  67. // patches
  68. ;(f2 as any)[vtcKey] = ['baz']
  69. cls.value = 'bar'
  70. await nextTick()
  71. expect(f1.getAttribute('class')).toBe('bar')
  72. expect(f2.className).toBe('bar baz')
  73. })
  74. })