Skip to content

Fix incorrect namespaced attributes rendering #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/five-stingrays-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Fix casing of namespaced attribute names
11 changes: 4 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encodeEntities, styleObjToCss, UNSAFE_NAME, XLINK } from './util.js';
import { encodeEntities, styleObjToCss, UNSAFE_NAME } from './util.js';
import { options, h, Fragment } from 'preact';
import {
CHILDREN,
Expand Down Expand Up @@ -392,8 +392,8 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {
break;

default: {
if (isSvgMode && XLINK.test(name)) {
name = name.toLowerCase().replace(XLINK_REPLACE_REGEX, 'xlink:');
if (NAMESPACE_REPLACE_REGEX.test(name)) {
name = name.replace(NAMESPACE_REPLACE_REGEX, '$1:$2').toLowerCase();
} else if (UNSAFE_NAME.test(name)) {
continue;
} else if ((name[4] === '-' || name === 'draggable') && v != null) {
Expand All @@ -407,8 +407,6 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {
name === 'panose1'
? 'panose-1'
: name.replace(/([A-Z])/g, '-$1').toLowerCase();
} else if (XML_REPLACE_REGEX.test(name)) {
name = name.toLowerCase().replace(XML_REPLACE_REGEX, 'xml:');
}
} else if (HTML_LOWER_CASE.test(name)) {
name = name.toLowerCase();
Expand Down Expand Up @@ -458,8 +456,7 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {

const HTML_LOWER_CASE = /^accessK|^auto[A-Z]|^ch|^col|cont|cross|dateT|encT|form[A-Z]|frame|hrefL|inputM|maxL|minL|noV|playsI|readO|rowS|spellC|src[A-Z]|tabI|item[A-Z]/;
const SVG_CAMEL_CASE = /^ac|^ali|arabic|basel|cap|clipPath$|clipRule$|color|dominant|enable|fill|flood|font|glyph[^R]|horiz|image|letter|lighting|marker[^WUH]|overline|panose|pointe|paint|rendering|shape|stop|strikethrough|stroke|text[^L]|transform|underline|unicode|units|^v[^i]|^w|^xH/;
const XML_REPLACE_REGEX = /^xml:?/;
const XLINK_REPLACE_REGEX = /^xlink:?/;
const NAMESPACE_REPLACE_REGEX = /^(xlink|xmlns|xml)(:|[A-Z])/;
const SELF_CLOSING = new Set([
'area',
'base',
Expand Down
17 changes: 16 additions & 1 deletion test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ describe('render', () => {
);

expect(rendered).to.equal(
`<svg><image xlink:href="#"></image><foreignObject><div xlinkHref="#"></div></foreignObject><g><image xlink:href="#"></image></g></svg>`
`<svg><image xlink:href="#"></image><foreignObject><div xlink:href="#"></div></foreignObject><g><image xlink:href="#"></image></g></svg>`
);
});
});
Expand Down Expand Up @@ -1855,6 +1855,21 @@ describe('render', () => {
}
});

it('should replace namespaces', () => {
let rendered = render(
<svg
xmlns="http://www.w3.org/2000/svg"
xmlnsXlink="http://www.w3.org/1999/xlink"
>
<script xlinkHref="cool-script.js" />
</svg>
);

expect(rendered).to.equal(
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><script xlink:href="cool-script.js"></script></svg>'
);
});

it('should have correct HTML casing', () => {
let htmlAttributes = {
accept: 'accept',
Expand Down