Skip to content

Commit 81c3cdc

Browse files
authored
Merge pull request #156 from preactjs/feature/custom-void-elements
Add voidElements config option
2 parents 22743d7 + df7c680 commit 81c3cdc

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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|undefined} [options.voidElements] RegeEx that matches elements that are considered void (self-closing)
2324
*/
2425
renderToString.render = renderToString;
2526

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

230-
let isVoid = String(nodeName).match(VOID_ELEMENTS);
231+
let isVoid = String(nodeName).match(VOID_ELEMENTS) || (opts.voidElements && String(nodeName).match(opts.voidElements));
231232
if (isVoid) s = s.replace(/>$/, ' />');
232233

233234
let pieces = [];

test/render.js

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

141+
it('should self-close custom void elements', () => {
142+
let rendered = render(<div><hello-world /></div>, null, { voidElements: /^hello-world$/ }),
143+
expected = `<div><hello-world /></div>`;
144+
145+
expect(rendered).to.equal(expected);
146+
});
147+
141148
it('does not close void elements with closing tags', () => {
142149
let rendered = render(<input><p>Hello World</p></input>),
143150
expected = `<input /><p>Hello World</p>`;

0 commit comments

Comments
 (0)