fix(mermaid): resolve C4Context white-page caused by missing xmlns:xlink namespace declaration#140
Merged
Merged
Conversation
…ink before DOMParser
mermaid.render() serialises SVG via innerHTML which omits the
xmlns:xlink namespace declaration even when xlink:href attributes
are present (e.g. person-icon <image> elements in C4Context).
DOMParser.parseFromString(svg, 'image/svg+xml') is a strict XML
parser; an undeclared 'xlink:' prefix causes it to return a
<parsererror> root instead of <svg>. The subsequent
wrapper.querySelector('svg') then returns null, initView() exits
early, and the diagram area renders as an empty white box.
Root cause is specific to C4Context because every Person() /
ExternalPerson() shape unconditionally calls drawImage2() which
sets xlink:href. Other diagram types only use xlink:href for
optional features (clickable links, image nodes) so most users
never hit it.
Fix: extract fixSvgNamespaces() utility that injects the
xmlns:xlink declaration into the SVG string before it reaches
DOMParser. The function is a no-op when the declaration is
already present (idempotent) and does not touch SVGs that have
no xlink: usage — so non-C4 diagrams are unaffected.
The DOMParser approach (introduced in v0.8.3 to avoid innerHTML
XSS warnings) is kept intact; only the input string is patched.
Adds vitest + happy-dom test suite for the new utility (10 tests):
- xmlns:xlink is added when missing
- existing declaration is not duplicated
- no-op for diagrams without xlink: usage
- DOM integration: querySelector('svg') is non-null after fix
Member
|
✅ Merged into dev-0.8.5 Review Summary:
The fix correctly handles the missing This will be included in the v0.8.5 release. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
C4Context diagrams render as a blank white box instead of showing the diagram.
Root cause
Commit
45b056a(v0.8.3) changed SVG insertion fromwrapper.innerHTML = svgtoDOMParser.parseFromString(svg, 'image/svg+xml')to avoid an innerHTML XSS warning.DOMParserwith'image/svg+xml'is a strict XML parser. It fails with a<parsererror>root when it encounters an undeclared namespace prefix.mermaid.render()serialises its output viainnerHTMLwhich omits XML namespace declarations — specificallyxmlns:xlink— even whenxlink:hrefattributes are present.C4Context always adds person icons via
<image xlink:href="data:..."/>(everyPerson()/ExternalPerson()shape callsdrawImage2()unconditionally):Why only C4Context?
Other diagram types use
xlink:hrefonly for optional features (clickable links, explicit image nodes). C4Context requires person icons unconditionally, so every diagram hits the issue.Fix
Extract
fixSvgNamespaces(svg)— a pure string utility that injects the missingxmlns:xlinkdeclaration into the SVG string before it reachesDOMParser:The
DOMParserapproach introduced in v0.8.3 is kept intact — only the input string is patched. No XSS regression.The function is idempotent: if
xmlns:xlinkis already present, or if noxlink:prefix is used, the string is returned unchanged.Tests
Adds
vitest+happy-domtest suite for the new utility (src/svg-utils.test.ts, 10 tests):xmlns:xlinkwhen missing; placed on<svg>tag; existing content preservedxlink:usage; fix applied for flowchart-with-linksDOMParserreturns<svg>root after fix;querySelector('svg')is non-nullFiles changed
src/svg-utils.tsfixSvgNamespacesutilitysrc/svg-utils.test.tssrc/client.tsfixSvgNamespacesbeforeDOMParservitest.config.tspackage.jsonvitest+happy-domdev deps; addtestscript