Skip to content
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

Fix HTML + SVG attribute casing #308

Merged
merged 1 commit into from
Jul 27, 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/fifty-chefs-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Fix incorrect casing of HTML attributes and SVG attributes
20 changes: 20 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {
v = styleObjToCss(v);
}
break;
case 'acceptCharset':
name = 'accept-charset';
break;
case 'httpEquiv':
name = 'http-equiv';
break;

default: {
if (isSvgMode && XLINK.test(name)) {
Expand All @@ -395,6 +401,17 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {
// `draggable` is an enumerated attribute and not Boolean. A value of `true` or `false` is mandatory
// https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/draggable
v += '';
} else if (isSvgMode) {
if (SVG_CAMEL_CASE.test(name)) {
name =
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 @@ -439,6 +456,9 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {
return s + '>' + html + '</' + type + '>';
}

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 SELF_CLOSING = new Set([
'area',
Expand Down
Loading