taoqf / node-html-parser

A very fast HTML parser, generating a simplified DOM, with basic element query support.
MIT License
1.11k stars 107 forks source link

removeAttribute makes boolean attributes render incorrectly. #258

Closed allain closed 9 months ago

allain commented 10 months ago

It appears that removing an attribute causes boolean attributes to be rendered incorrectly:

const {parse} = require("node-html-parser")
const inputEl = parse('<input>').firstChild

inputEl.setAttribute('checked', '')
inputEl.setAttribute('a', '')
console.log(inputEl.toString()) // => <input checked a> CORRECT

inputEl.removeAttribute('a')
console.log(inputEl.toString()) // => <input checked=""> INCORRECT

If it helps narrow it down, the bug does not happen if the boolean attribute was not added using setAttribute:

const {parse} = require("node-html-parser")
const inputEl = parse('<input checked>').firstChild

inputEl.setAttribute('a', '')
console.log(inputEl.toString()) // => <input checked a> CORRECT

inputEl.removeAttribute('a')
console.log(inputEl.toString()) // => <input checked> CORRECT