Skip to content

Commit e5573ed

Browse files
committed
Add voidElements config option
1 parent 09d7675 commit e5573ed

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const SHALLOW = { shallow: true };
66
// components without names, kept as a hash for later comparison to return consistent UnnamedComponentXX names.
77
const UNNAMED = [];
88

9-
const VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
9+
const DEFAULT_VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
1010

1111
const noop = () => {};
1212

@@ -20,6 +20,7 @@ const noop = () => {};
2020
* @param {Boolean} [options.shallow=false] If `true`, renders nested Components as HTML elements (`<Foo a="b" />`).
2121
* @param {Boolean} [options.xml=false] If `true`, uses self-closing tags for elements without children.
2222
* @param {Boolean} [options.pretty=false] If `true`, adds whitespace for readability
23+
* @param {RegEx} [options.voidElements=DEFAULT_VOID_ELEMENTS] RegeEx that matches elements that are considered void (self-closing)
2324
*/
2425
renderToString.render = renderToString;
2526

@@ -219,7 +220,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
219220
s = `<${nodeName}${s}>`;
220221
if (String(nodeName).match(/[\s\n\\/='"\0<>]/)) throw new Error(`${nodeName} is not a valid HTML tag name in ${s}`);
221222

222-
let isVoid = String(nodeName).match(VOID_ELEMENTS);
223+
let isVoid = String(nodeName).match(opts.voidElements || DEFAULT_VOID_ELEMENTS);
223224
if (isVoid) s = s.replace(/>$/, ' />');
224225

225226
let pieces = [];

test/render.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ describe('render', () => {
131131
expect(rendered).to.equal(expected);
132132
});
133133

134+
it('should self-close custom void elements', () => {
135+
let rendered = render(<div><hello-world /></div>, null, { voidElements: /^hello-world$/ }),
136+
expected = `<div><hello-world /></div>`;
137+
138+
expect(rendered).to.equal(expected);
139+
});
140+
134141
it('does not close void elements with closing tags', () => {
135142
let rendered = render(<input><p>Hello World</p></input>),
136143
expected = `<input /><p>Hello World</p>`;

0 commit comments

Comments
 (0)