mirror of
https://github.com/danbulant/node-html-parser
synced 2026-05-19 04:18:52 +00:00
🐛 fix Issue #17
This commit is contained in:
parent
a777a8814f
commit
92ea19ac9f
2 changed files with 10 additions and 5 deletions
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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 () {
|
||||
|
|
|
|||
Loading…
Reference in a new issue