Skip to content

Commit 7b654ad

Browse files
committed
fix: issue #98 setattribute
1 parent 54491af commit 7b654ad

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/nodes/html.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ export default class HTMLElement extends Node {
511511
}
512512

513513
public hasAttribute(key: string) {
514-
return key in this.attrs;
514+
return key.toLowerCase() in this.attrs;
515515
}
516516

517517
/**
@@ -531,10 +531,18 @@ export default class HTMLElement extends Node {
531531
if (arguments.length < 2) {
532532
throw new Error('Failed to execute \'setAttribute\' on \'Element\'');
533533
}
534+
const k2 = key.toLowerCase();
534535
const attrs = this.rawAttributes;
536+
for (const k in attrs) {
537+
if (k.toLowerCase() === k2) {
538+
key = k;
539+
break;
540+
}
541+
}
535542
attrs[key] = String(value);
543+
// update this.attrs
536544
if (this._attrs) {
537-
this._attrs[key] = decode(attrs[key]);
545+
this._attrs[k2] = decode(attrs[key]);
538546
}
539547
// Update rawString
540548
this.rawAttrs = Object.keys(attrs).map((name) => {

test/98.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,20 @@ describe('getAttribute should be case insensitive', function () {
1414
const root = parse('<a onClick="listener()"></a>');
1515
root.firstChild.getAttribute('onclick').should.eql('listener()');
1616
});
17+
it('set attribute in lowercase', function () {
18+
const root = parse('<a onClick="listener()"></a>');
19+
const a = root.firstChild;
20+
a.setAttribute('onclick', 'listener2');
21+
a.getAttribute('onclick').should.eql('listener2');
22+
root.toString().should.eql('<a onClick="listener2"></a>');
23+
});
24+
it('add attributes', function () {
25+
const root = parse('<a onClick="listener()"></a>');
26+
const a = root.firstChild;
27+
a.setAttribute('onclick', 'listener2');
28+
a.getAttribute('onclick').should.eql('listener2');
29+
a.setAttribute('onDoubleClick', 'listener3');
30+
a.getAttribute('onDoubleClick').should.eql('listener3');
31+
root.toString().should.eql('<a onClick="listener2" onDoubleClick="listener3"></a>');
32+
});
1733
});

0 commit comments

Comments
 (0)