svgNamespace.spec.ts 2.0 KB

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