From 6b6aae16c22209743958a303591181970fc3356f Mon Sep 17 00:00:00 2001 From: onmax Date: Tue, 17 Nov 2026 10:56:48 +0100 Subject: [PATCH 1/3] fix(svelte): create SVG and MathML elements in their namespaces `` only takes an element's namespace from its `xmlns`, so `MarkdownNode` created `svg`, `math`, and everything inside them as HTML elements on the client, where they don't draw. Each node now passes its namespace to its children through context: `svg` and `math` start theirs, and `foreignObject` holds HTML again. Co-Authored-By: Claude Opus 5.5 --- .../src/components/MarkdownNode.svelte | 19 +++++++++-- .../test/namespaces.svelte.test.ts | 34 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 packages/comark-svelte/test/namespaces.svelte.test.ts 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) + }) +}) From 12d95c0b7991fe002b4bc5f0e7f6a1f7083b608b Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Wed, 18 Nov 2026 12:37:15 +0100 Subject: [PATCH 2/3] Update @comark/svelte size in bundle test Updated the size of the @comark/svelte package in the test file. --- test/bundle.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bundle.test.ts b/test/bundle.test.ts index b0d64568..3fb4cae9 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.3k (65 files)", "@comark/nuxt": "10.6k (65 files)", "@comark/react": "42.9k (83 files)", - "@comark/svelte": "50.3k (91 files)", + "@comark/svelte": "51.0k (91 files)", "@comark/vue": "62.6k (89 files)", "comark": "439k (171 files)", } From 6e40a743b72dd881b53ab27adb30613b2078c56b Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Wed, 18 Nov 2026 12:49:26 +0100 Subject: [PATCH 3/3] Apply suggestion from @farnabaz --- test/bundle.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bundle.test.ts b/test/bundle.test.ts index d0c13063..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": "51.0k (93 files)", + "@comark/svelte": "51.2k (93 files)", "@comark/vue": "62.8k (91 files)", "comark": "444k (173 files)", }