Skip to content

Commit

Permalink
🐛 fix Issue ashi009#17
Browse files Browse the repository at this point in the history
  • Loading branch information
taoqf committed Feb 5, 2020
1 parent a777a88 commit 92ea19a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/nodes/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 3 additions & 1 deletion test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,12 @@ describe('HTML Parser', function () {
describe('#setAttribute', function () {
it('should edit the attributes of the element', function () {
var root = parseHTML('<p a=12></p>');
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('<p a="13"></p>');
});
it('should add an attribute to the element', function () {
Expand Down

0 comments on commit 92ea19a

Please sign in to comment.