From 2bee38f0e5307875a23f33907b9778ac3b49b6fe Mon Sep 17 00:00:00 2001 From: usagizmo Date: Sun, 11 Apr 2021 13:49:52 +0900 Subject: [PATCH] Change this.id to a required field --- src/nodes/html.ts | 2 +- test/112.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/112.js diff --git a/src/nodes/html.ts b/src/nodes/html.ts index 2215a1d..78a7798 100644 --- a/src/nodes/html.ts +++ b/src/nodes/html.ts @@ -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+/) : [], @@ -139,7 +140,6 @@ export default class HTMLElement extends Node { ) ); if (keyAttrs.id) { - this.id = keyAttrs.id; if (!rawAttrs) { this.rawAttrs = `id="${keyAttrs.id}"`; } diff --git a/test/112.js b/test/112.js new file mode 100644 index 0000000..f93eca6 --- /dev/null +++ b/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('
'); + }); + 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('
'); + }); +});