From bd7734eaab435068284cc8ca9db486082474f388 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Wed, 8 Apr 2020 15:36:55 +0200 Subject: [PATCH] fix rendering of void elements (aka unclosed) In HTML5s, self-closing are only foreign elements (from MathML and SVG namespace). What you call as unclosed elements is called void elements in HTML5 spec. https://html.spec.whatwg.org/multipage/syntax.html#elements-2 --- src/nodes/html.ts | 7 ++----- test/html.js | 4 ++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/nodes/html.ts b/src/nodes/html.ts index 2f796bb..4f41a13 100644 --- a/src/nodes/html.ts +++ b/src/nodes/html.ts @@ -157,13 +157,10 @@ export default class HTMLElement extends Node { public toString() { const tag = this.tagName; if (tag) { - const is_un_closed = /^meta$/i.test(tag); - const is_self_closed = /^(img|br|hr|area|base|input|doctype|link)$/i.test(tag); + const is_void = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/i.test(tag); const attrs = this.rawAttrs ? ' ' + this.rawAttrs : ''; - if (is_un_closed) { + if (is_void) { return `<${tag}${attrs}>`; - } else if (is_self_closed) { - return `<${tag}${attrs} />`; } else { return `<${tag}${attrs}>${this.innerHTML}`; } diff --git a/test/html.js b/test/html.js index 26cc404..10bab7e 100644 --- a/test/html.js +++ b/test/html.js @@ -89,7 +89,7 @@ describe('HTML Parser', function () { lowerCaseTagName: true }); - root.toString().should.eql('SISREG III

CONSULTA AO CADASTRO DE PACIENTES SUS



Processando...


'); + root.toString().should.eql('SISREG III

CONSULTA AO CADASTRO DE PACIENTES SUS



Processando...


'); // root.toString().firstChild.should.eql(div); }); @@ -432,7 +432,7 @@ describe('HTML Parser', function () { const root = parseHTML(''); const input = root.firstChild; input.removeAttribute('required'); - input.toString().should.eql(''); + input.toString().should.eql(''); }); describe('#hasAttribute', function () {