diff --git a/packages/comark-svelte/src/components/MarkdownNode.svelte b/packages/comark-svelte/src/components/MarkdownNode.svelte index c76d7484..f30379d6 100644 --- a/packages/comark-svelte/src/components/MarkdownNode.svelte +++ b/packages/comark-svelte/src/components/MarkdownNode.svelte @@ -20,6 +20,14 @@ naturally appears inline after the deepest trailing text node. const componentCache = new WeakMap>() + /** + * `` only knows an element's namespace from its `xmlns`, so each node passes its own down to its + * children: `svg` and `math` start theirs, and `foreignObject` holds HTML again. + */ + const NAMESPACE = Symbol('comark:namespace') + const SVG_NAMESPACE = 'http://www.w3.org/2000/svg' + const MATHML_NAMESPACE = 'http://www.w3.org/1998/Math/MathML' + function isPromiseLike(value: unknown): value is Promise { return !!value && typeof (value as { then?: unknown }).then === 'function' } @@ -73,6 +81,7 @@ naturally appears inline after the deepest trailing text node. import ComarkComponent from './ComarkComponent.svelte' import Resolve from './Resolve.svelte' import { applyNodeTransforms, isHtmlVoidElement, resolveAttributes, toNativeAttributes } from 'comark/utils' + import { getContext, setContext } from 'svelte' const EMPTY_RENDER_DATA: NodeRenderData = { frontmatter: {}, meta: {}, data: {}, props: {} } @@ -202,6 +211,12 @@ naturally appears inline after the deepest trailing text node. // check uses the raw attributes: resolving can omit all of them. let nativeProps = $derived(tag ? toNativeAttributes(tag, node, mappedProps) : mappedProps) + const parentNamespace = getContext<(() => string | undefined) | undefined>(NAMESPACE) + let namespace = $derived( + tag === 'svg' ? SVG_NAMESPACE : tag === 'math' ? MATHML_NAMESPACE : parentNamespace?.(), + ) + setContext(NAMESPACE, () => (tag === 'foreignObject' ? undefined : namespace)) + let hasOwnAttrs = $derived.by(() => { if (transformedProps) return true const attrs: Record = (Array.isArray(node) && node.length >= 2 ? node[1] : null) ?? {} @@ -300,9 +315,9 @@ naturally appears inline after the deepest trailing text node. {@render renderChildren()} {:else if isVoid} - + {:else if tag} - + {@render renderChildren()} {/if} diff --git a/packages/comark-svelte/test/namespaces.svelte.test.ts b/packages/comark-svelte/test/namespaces.svelte.test.ts new file mode 100644 index 00000000..46592478 --- /dev/null +++ b/packages/comark-svelte/test/namespaces.svelte.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from 'vitest' +import { render } from 'vitest-browser-svelte' +import MarkdownNode from '../src/components/MarkdownNode.svelte' + +const HTML = 'http://www.w3.org/1999/xhtml' +const SVG = 'http://www.w3.org/2000/svg' +const MATHML = 'http://www.w3.org/1998/Math/MathML' + +describe('element namespaces', () => { + it('creates MathML elements and their descendants in the MathML namespace', async () => { + const screen = await render(MarkdownNode, { + node: ['p', {}, ['math', {}, ['mfrac', {}, ['mi', {}, 'a'], ['mi', {}, 'b']]]], + }) + const { container } = screen + expect(container.querySelector('p')!.namespaceURI).toBe(HTML) + for (const tag of ['math', 'mfrac', 'mi']) expect(container.querySelector(tag)!.namespaceURI).toBe(MATHML) + }) + + it('creates SVG descendants in the SVG namespace and returns to HTML in foreignObject', async () => { + const screen = await render(MarkdownNode, { + node: [ + 'svg', + { viewBox: '0 0 10 10' }, + ['rect', { width: '10', height: '10' }], + ['foreignObject', {}, ['div', {}, 'x']], + ], + }) + const { container } = screen + expect(container.querySelector('svg')!.namespaceURI).toBe(SVG) + expect(container.querySelector('rect')!.namespaceURI).toBe(SVG) + expect(container.querySelector('foreignObject')!.namespaceURI).toBe(SVG) + expect(container.querySelector('div')!.namespaceURI).toBe(HTML) + }) +}) diff --git a/test/bundle.test.ts b/test/bundle.test.ts index a5137849..0523c194 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -75,7 +75,7 @@ describe('package bundle size', { timeout: 60_000 }, () => { "@comark/html": "17.5k (67 files)", "@comark/nuxt": "10.8k (67 files)", "@comark/react": "43.1k (85 files)", - "@comark/svelte": "50.5k (93 files)", + "@comark/svelte": "51.2k (93 files)", "@comark/vue": "62.8k (91 files)", "comark": "444k (173 files)", }