Skip to content
Myr

fix(jsx): normalize SVG attributes on the <svg> root element (#4893)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(jsx): normalize SVG attributes on the <svg> root element `<svg strokeWidth="1.5">` was output as-is by SSR, but the DOM renderer converted it to `stroke-width`. This broke icon libraries that set stroke/fill attributes on the <svg> root, and it caused SSR and DOM to produce different HTML for the same JSX. Background: - Icon libraries put these attributes on the <svg> root so the child paths can inherit them. Example from Heroicons: https://github.com/tailwindlabs/heroicons/blob/master/optimized/24/outline/pencil-square.svg - React also converts `strokeWidth` -> `stroke-width` on the root (checked with `react-dom/server@18.3.1` `renderToStaticMarkup`). - The HTML spec says that when the parser sees a `<svg>` start tag, it inserts "a foreign element for the token, with SVG namespace". So the <svg> element itself is in the SVG namespace, not only its children: https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody:adjust-svg-attributes Root cause: the factory put only the children of `<svg>` inside the namespace provider (`svg > Provider > children`). When SSR rendered the <svg>'s own attributes, `useContext(nameSpaceContext)` returned the default `""` instead of `"svg"`, so `toSVGAttributeName` was skipped. Fix: put the <svg> element itself inside the provider too (`Provider > svg > children`), so the <svg>'s own attributes also run with the `"svg"` context. The `<head>` branch of the factory is left unchanged. Its context value `"head"` is a Hono-internal flag used to stop <title> / <meta> / <script> from hoisting to document metadata when they are already inside <head>. It is not a spec namespace and is never read when rendering <head>'s own attributes. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(jsx): keep <svg> node shape and normalize at render time Preserve the JSXNode shape for `<svg>` (tag/type/ref/cloneElement compatibility) and switch SVG attribute normalization to also kick in when the rendered tag itself is `svg`, instead of wrapping the node in a namespace Provider.

authored by kobaken · committed by GitHub · parent 90d4182 · Browse files · 3 files+18 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

3 files+18 added−1 removed

  • tests:+17 added−0 removed
  • other:+1 added−1 removed

3 files+18 added−1 removed

modified:src/jsx/base.test.tsx+10 added
modified:src/jsx/base.ts+1 added-1 removed
183 unmodified lines
184185 buffer[0] += `<${tag}`186187 const normalizeKey: (key: string) => string =188 nameSpaceContext && useContext(nameSpaceContext) === 'svg'188 tag === 'svg' || (nameSpaceContext && useContext(nameSpaceContext) === 'svg')189 ? (key) => toSVGAttributeName(normalizeIntrinsicElementKey(key))190 : (key) => normalizeIntrinsicElementKey(key)191 for (let [key, v] of Object.entries(props)) {192 key = normalizeKey(key)
261 unmodified lines
modified:src/jsx/index.test.tsx+7 added
795 unmodified lines
796 )797 expect(template.toString()).toBe('<svg><g viewBox="0 0 10 10"></g></svg>')798 })799800 it('should normalize kebab-case attributes on the <svg> root element', () => {801 const template = <svg {...{ strokeWidth: '1.5', strokeLinecap: 'round' }} viewBox='0 0 16 16' />802 expect(template.toString()).toBe(803 '<svg stroke-width="1.5" stroke-linecap="round" viewBox="0 0 16 16"></svg>'804 )805 })806807 describe('attribute', () => {808 describe('camelCase', () => {809 test.each`810 key
437 unmodified lines