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

Support de-camelCasing JSX html attributes #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
49 changes: 49 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,50 @@ const VOID_ELEMENTS = [
'wbr'
];

const MAPPED_ATTRIBUTES = {
acceptCharset: 'accept-charset',
accessKey: 'access-key',
allowFullScreen: 'allow-full-screen',
allowTransparency: 'allow-transparency',
autoPlay: 'auto-play',
cellPadding: 'cell-padding',
cellSpacing: 'cell-spacing',
charSet: 'char-set',
className: 'class-name',
colSpan: 'col-span',
contentEditable: 'content-editable',
contextMenu: 'context-menu',
crossOrigin: 'cross-origin',
dateTime: 'date-time',
encType: 'enc-type',
formAction: 'form-action',
formEncType: 'form-enc-type',
formMethod: 'form-method',
formNoValidate: 'form-no-validate',
formTarget: 'form-target',
frameBorder: 'frame-border',
hrefLang: 'href-lang',
httpEquiv: 'http-equiv',
inputMode: 'input-mode',
keyParams: 'key-params',
keyType: 'key-type',
marginHeight: 'margin-height',
marginWidth: 'margin-width',
maxLength: 'max-length',
mediaGroup: 'media-group',
minLength: 'min-length',
noValidate: 'no-validate',
radioGroup: 'radio-group',
readOnly: 'read-only',
rowSpan: 'row-span',
spellCheck: 'spell-check',
srcDoc: 'src-doc',
srcLang: 'src-lang',
srcSet: 'src-set',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'srcset'

tabIndex: 'tab-index',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'tabindex'

useMap: 'use-Map'
};


/** Render Preact JSX + Components to an HTML string.
* @name render
Expand Down Expand Up @@ -119,6 +163,11 @@ export default function renderToString(vnode, context, opts, inner, isSvgMode) {
if (attributes['class']) continue;
name = 'class';
}
else if (MAPPED_ATTRIBUTES.hasOwnProperty(name)) {
const actual = MAPPED_ATTRIBUTES[name];
if (attributes[actual]) continue;
name = actual;
}
else if (isSvgMode && name.match(/^xlink\:?(.+)/)) {
name = name.toLowerCase().replace(/^xlink\:?(.+)/, 'xlink:$1');
}
Expand Down
4 changes: 4 additions & 0 deletions test/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ describe('jsx', () => {
expect(renderJsx(<a style="" />)).to.equal('<a></a>');
});

it('should render supported camel cased attributes in normal form', () => {
expect(renderJsx(<meta httpEquiv="" />)).to.equal('<meta http-equiv="" />');
});

it('should render JSX attributes inline if short enough', () => {
expect(renderJsx(
<a b="c">bar</a>
Expand Down