svgNamespace.spec.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 { render, h, ref, nextTick } from '../src'
  8. describe('SVG support', () => {
  9. test('should mount elements with correct namespaces', () => {
  10. const root = document.createElement('div')
  11. document.body.appendChild(root)
  12. const App = {
  13. template: `
  14. <div id="e0">
  15. <svg id="e1">
  16. <foreignObject id="e2">
  17. <div id="e3"/>
  18. </foreignObject>
  19. </svg>
  20. </div>
  21. `
  22. }
  23. render(h(App), root)
  24. const e0 = document.getElementById('e0')!
  25. expect(e0.namespaceURI).toMatch('xhtml')
  26. expect(e0.querySelector('#e1')!.namespaceURI).toMatch('svg')
  27. expect(e0.querySelector('#e2')!.namespaceURI).toMatch('svg')
  28. expect(e0.querySelector('#e3')!.namespaceURI).toMatch('xhtml')
  29. })
  30. test('should patch elements with correct namespaces', async () => {
  31. const root = document.createElement('div')
  32. document.body.appendChild(root)
  33. const cls = ref('foo')
  34. const App = {
  35. setup: () => ({ cls }),
  36. template: `
  37. <div>
  38. <svg id="f1" :class="cls">
  39. <foreignObject>
  40. <div id="f2" :class="cls"/>
  41. </foreignObject>
  42. </svg>
  43. </div>
  44. `
  45. }
  46. render(h(App), root)
  47. const f1 = document.querySelector('#f1')!
  48. const f2 = document.querySelector('#f2')!
  49. expect(f1.getAttribute('class')).toBe('foo')
  50. expect(f2.className).toBe('foo')
  51. // set a transition class on the <div> - which is only respected on non-svg
  52. // patches
  53. ;(f2 as any)._vtc = ['baz']
  54. cls.value = 'bar'
  55. await nextTick()
  56. expect(f1.getAttribute('class')).toBe('bar')
  57. expect(f2.className).toBe('bar baz')
  58. })
  59. })