2
0

svgNamespace.spec.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SVG 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('SVG 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. <div id="e0">
  19. <svg id="e1">
  20. <foreignObject id="e2">
  21. <div id="e3"/>
  22. <svg id="e4"/>
  23. <math id="e5"/>
  24. </foreignObject>
  25. </svg>
  26. </div>
  27. `,
  28. }
  29. render(h(App), root)
  30. const e0 = document.getElementById('e0')!
  31. expect(e0.namespaceURI).toMatch('xhtml')
  32. expect(e0.querySelector('#e1')!.namespaceURI).toMatch('svg')
  33. expect(e0.querySelector('#e2')!.namespaceURI).toMatch('svg')
  34. expect(e0.querySelector('#e3')!.namespaceURI).toMatch('xhtml')
  35. expect(e0.querySelector('#e4')!.namespaceURI).toMatch('svg')
  36. expect(e0.querySelector('#e5')!.namespaceURI).toMatch('Math')
  37. })
  38. test('should patch elements with correct namespaces', async () => {
  39. const root = document.createElement('div')
  40. document.body.appendChild(root)
  41. const cls = ref('foo')
  42. const App = {
  43. setup: () => ({ cls }),
  44. template: `
  45. <div>
  46. <svg id="f1" :class="cls">
  47. <foreignObject>
  48. <div id="f2" :class="cls"/>
  49. </foreignObject>
  50. </svg>
  51. </div>
  52. `,
  53. }
  54. render(h(App), root)
  55. const f1 = document.querySelector('#f1')!
  56. const f2 = document.querySelector('#f2')!
  57. expect(f1.getAttribute('class')).toBe('foo')
  58. expect(f2.className).toBe('foo')
  59. // set a transition class on the <div> - which is only respected on non-svg
  60. // patches
  61. ;(f2 as any)[vtcKey] = ['baz']
  62. cls.value = 'bar'
  63. await nextTick()
  64. expect(f1.getAttribute('class')).toBe('bar')
  65. expect(f2.className).toBe('bar baz')
  66. })
  67. })