Skip to content

Commit

Permalink
Change this.id to a required field
Browse files Browse the repository at this point in the history
  • Loading branch information
usagizmo committed Apr 11, 2021
1 parent f567abf commit 2bee38f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/nodes/html.ts
Expand Up @@ -131,6 +131,7 @@ export default class HTMLElement extends Node {
super(parentNode);
this.rawTagName = tagName;
this.rawAttrs = rawAttrs || '';
this.id = keyAttrs.id || '';
this.childNodes = [];
this.classList = new DOMTokenList(
keyAttrs.class ? keyAttrs.class.split(/\s+/) : [],
Expand All @@ -139,7 +140,6 @@ export default class HTMLElement extends Node {
)
);
if (keyAttrs.id) {
this.id = keyAttrs.id;
if (!rawAttrs) {
this.rawAttrs = `id="${keyAttrs.id}"`;
}
Expand Down
17 changes: 17 additions & 0 deletions test/112.js
@@ -0,0 +1,17 @@
const { parse, HTMLElement } = require('../dist');

// https://github.com/taoqf/node-html-parser/pull/112
describe('pull/112', function () {
it('this.id is set to an empty string', async function () {
const el = new HTMLElement('div', {}, '', null);
el.id.should.eql('')
should.equal(el.getAttribute('id'), undefined);
el.toString().should.eql('<div></div>');
});
it('this.id is set to the value of keyAttrs', async function () {
const el = new HTMLElement('div', { id: 'id' }, 'id="id"', null);
el.id.should.eql('id')
el.getAttribute('id').should.eql('id')
el.toString().should.eql('<div id="id"></div>');
});
});

0 comments on commit 2bee38f

Please sign in to comment.