diff --git a/src/nodes/html.ts b/src/nodes/html.ts index c6b3534..b91df16 100644 --- a/src/nodes/html.ts +++ b/src/nodes/html.ts @@ -464,8 +464,9 @@ export default class HTMLElement extends Node { // remove whitespace between attributes const attrs = Object.keys(this.rawAttributes) .map((key) => { - const val = this.rawAttributes[key]; - return `${key}=${JSON.stringify(val)}`; + const val = this.quoteAttribute(this.rawAttributes[key]); + if (val === 'null' || val === '""') return key; + return `${key}=${val}`; }) .join(' '); this.rawAttrs = attrs; diff --git a/test/tests/html.js b/test/tests/html.js index f568aa4..1229111 100644 --- a/test/tests/html.js +++ b/test/tests/html.js @@ -300,6 +300,15 @@ describe('HTML Parser', function () { parseHTML('
\t\nHello\n\tWorld!
').removeWhitespace().firstChild.text.should.eql('HelloWorld!'); parseHTML('\t\n Hello \n\tWorld!
').removeWhitespace().firstChild.text.should.eql(' Hello World!'); }); + + it('should preserve consecutive backslashes in attribute values', function () { + const html = 'x'; + parseHTML(html).removeWhitespace().firstChild.toString().should.eql(html); + }); + + it('should preserve valueless attributes', function () { + parseHTML('').removeWhitespace().firstChild.toString().should.eql(''); + }); }); describe('#rawAttributes', function () {