Skip to content

Commit 3455e63

Browse files
committed
Support colon attrs
1 parent 14b0ca6 commit 3455e63

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ const UNNAMED = [];
1818
const VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
1919

2020
const DASHED_ATTRS = /^(acceptC|httpE)/;
21-
const CAMEL_ATTRS = /^(viewB)/;
21+
const CAMEL_ATTRS = /^(viewB|isP)/;
22+
const COLON_ATTRS = /^(xmlS|xlinkH)/;
23+
const transformAttr = (attr, separator) =>
24+
attr.replace(/([A-Z])/g, (w) => separator + w.toLowerCase());
2225

2326
const UNSAFE_NAME = /[\s\n\\/='"\0<>]/;
2427

@@ -304,7 +307,9 @@ function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
304307

305308
// Convert attribute names to proper html casing
306309
if (DASHED_ATTRS.test(name)) {
307-
name = name.replace(/([A-Z])/g, (l) => '-' + l.toLowerCase());
310+
name = transformAttr(name, '-');
311+
} else if (COLON_ATTRS.test(name)) {
312+
name = transformAttr(name, ':');
308313
} else if (!CAMEL_ATTRS.test(name)) {
309314
name = name.toLowerCase();
310315
}

test/render.test.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ describe('render', () => {
103103
expect(rendered).to.equal(expected);
104104
});
105105

106+
it('should colonize certain attributes & leave certain attributes camelized', () => {
107+
let rendered = render(<svg xmlSpace="preserve" viewBox="0 0 10 10" />),
108+
expected = `<svg xml:space="preserve" viewBox="0 0 10 10"></svg>`;
109+
110+
expect(rendered).to.equal(expected);
111+
});
112+
106113
it('should include boolean aria-* attributes', () => {
107114
let rendered = render(<div aria-hidden aria-whatever={false} />),
108115
expected = `<div aria-hidden="true" aria-whatever="false"></div>`;
@@ -291,7 +298,7 @@ describe('render', () => {
291298
);
292299

293300
expect(rendered).to.equal(
294-
`<svg viewBox="0 0 100 100"><image xlink:href="#"></image><foreignObject><div xlinkhref="#"></div></foreignObject><g><image xlink:href="#"></image></g></svg>`
301+
`<svg viewBox="0 0 100 100"><image xlink:href="#"></image><foreignObject><div xlink:href="#"></div></foreignObject><g><image xlink:href="#"></image></g></svg>`
295302
);
296303
});
297304
});

0 commit comments

Comments
 (0)