Skip to content

Commit

Permalink
Merge branch 'main' of github.com:taoqf/node-html-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
taoqf committed Feb 21, 2023
2 parents c6c2c58 + 34d2551 commit 0d4f6bf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/nodes/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,13 +690,13 @@ export default class HTMLElement extends Node {
}
const attrs = {} as RawAttributes;
if (this.rawAttrs) {
const re = /([a-zA-Z()[\]#@$.?][a-zA-Z0-9-_:()[\]#]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/g;
const re = /([a-zA-Z()[\]#@$.?:][a-zA-Z0-9-_:()[\]#]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/g;
let match: RegExpExecArray;
while ((match = re.exec(this.rawAttrs))) {
const key = match[1];
let val = match[2] || null;
if (val && (val[0] === `'` || val[0] === `"`)) val = val.slice(1, val.length - 1);
attrs[key] = val;
attrs[key] = attrs[key] || val;
}
}
this._rawAttrs = attrs;
Expand Down
5 changes: 5 additions & 0 deletions test/tests/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ describe('HTML Parser', function () {
root.firstChild.getAttribute('a').should.eql('a1b');
});

it('should return value of the first attribute', function () {
const root = parseHTML('<p a="a1b" a="fail"></p>');
root.firstChild.getAttribute('a').should.eql('a1b');
});

it('should return null when there is no such attribute', function () {
const root = parseHTML('<p></p>');
should.equal(root.firstChild.getAttribute('b'), null);
Expand Down

0 comments on commit 0d4f6bf

Please sign in to comment.