Skip to content

Commit 78c6129

Browse files
committed
[bugfix] do not close void elements with a closing tag
1 parent 6941558 commit 78c6129

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
236236
if (String(nodeName).match(/[\s\n\\/='"\0<>]/)) throw new Error(`${nodeName} is not a valid HTML tag name in ${s}`);
237237

238238
let isVoid = String(nodeName).match(VOID_ELEMENTS) || (opts.voidElements && String(nodeName).match(opts.voidElements));
239-
if (isVoid) s = s.replace(/>$/, ' />');
240-
241239
let pieces = [];
242240

243241
let children;
@@ -297,7 +295,10 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
297295
return s.substring(0, s.length-1) + ' />';
298296
}
299297

300-
if (!isVoid) {
298+
if (isVoid && !children) {
299+
s = s.replace(/>$/, ' />');
300+
}
301+
else {
301302
if (pretty && ~s.indexOf('\n')) s += '\n';
302303
s += `</${nodeName}>`;
303304
}

test/render.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ describe('render', () => {
202202
});
203203

204204
it('does not close void elements with closing tags', () => {
205-
let rendered = render(<input><p>Hello World</p></input>),
206-
expected = `<input /><p>Hello World</p>`;
205+
let rendered = render(<link>http://preactjs.com</link>),
206+
expected = `<link>http://preactjs.com</link>`;
207207

208208
expect(rendered).to.equal(expected);
209209
});

0 commit comments

Comments
 (0)