From 92ea19ac9f049ac40a4d18f01b83b7a4f92506d3 Mon Sep 17 00:00:00 2001 From: taoqf Date: Wed, 5 Feb 2020 14:13:04 +0800 Subject: [PATCH] :bug: fix Issue #17 --- src/nodes/html.ts | 11 +++++++---- test/html.js | 4 +++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/nodes/html.ts b/src/nodes/html.ts index 2972f35..338d4af 100644 --- a/src/nodes/html.ts +++ b/src/nodes/html.ts @@ -445,15 +445,18 @@ export default class HTMLElement extends Node { * @param {string|number} value The value to set, or null / undefined to remove an attribute */ setAttribute(key: string, value: string | number) { - // Invalidate current this.attributes - if (this._attrs) { - delete this._attrs; - } const attrs = this.rawAttributes; if (value === undefined || value === null) { delete attrs[key]; + // Update this.attribute + if (this._attrs && this._attrs[key]) { + delete this._attrs[key]; + } } else { attrs[key] = String(value); + if (this._attrs) { + this._attrs[key] = decode(attrs[key]); + } } // Update rawString this.rawAttrs = Object.keys(attrs).map((name) => { diff --git a/test/html.js b/test/html.js index b5b0c7c..2dca353 100644 --- a/test/html.js +++ b/test/html.js @@ -334,10 +334,12 @@ describe('HTML Parser', function () { describe('#setAttribute', function () { it('should edit the attributes of the element', function () { var root = parseHTML('

'); + var attr = root.firstChild.attributes; root.firstChild.setAttribute('a', 13); - root.firstChild.attributes.should.eql({ + attr.should.eql({ 'a': '13', }); + root.firstChild.getAttribute('a').should.eql('13'); root.firstChild.toString().should.eql('

'); }); it('should add an attribute to the element', function () {