diff --git a/src/nodes/comment.ts b/src/nodes/comment.ts index 8ba5ff8..3b9e840 100644 --- a/src/nodes/comment.ts +++ b/src/nodes/comment.ts @@ -4,9 +4,9 @@ import NodeType from './type'; export default class CommentNode extends Node { public clone(): CommentNode { - return new CommentNode(this.rawText, null); + return new CommentNode(this.rawText, null, undefined, this.rawTagName); } - public constructor(public rawText: string, parentNode = null as HTMLElement | null, range?: [number, number]) { + public constructor(public rawText: string, parentNode = null as HTMLElement | null, range?: [number, number], public rawTagName = '!--') { super(parentNode, range); } diff --git a/src/nodes/node.ts b/src/nodes/node.ts index c9316ee..d6ca499 100644 --- a/src/nodes/node.ts +++ b/src/nodes/node.ts @@ -6,6 +6,7 @@ import HTMLElement from './html'; * Node Class as base class for TextNode and HTMLElement. */ export default abstract class Node { + abstract rawTagName: string; abstract nodeType: NodeType; public childNodes = [] as Node[]; public range: readonly [number, number]; diff --git a/src/nodes/text.ts b/src/nodes/text.ts index 0eb9cd5..649c309 100644 --- a/src/nodes/text.ts +++ b/src/nodes/text.ts @@ -21,6 +21,7 @@ export default class TextNode extends Node { * @type {Number} */ public nodeType = NodeType.TEXT_NODE; + public rawTagName = ''; private _rawText: string; private _trimmedRawText?: string; diff --git a/test/tests/issues/269-270.js b/test/tests/issues/269-270.js new file mode 100644 index 0000000..b0ec70d --- /dev/null +++ b/test/tests/issues/269-270.js @@ -0,0 +1,25 @@ +const { parse } = require('@test/test-target'); + +describe('issue 269 270', function () { + it('node.rawTagName', function () { + const root = parse('
foo
', { comment: true }); + const div = root.childNodes[0]; + div.rawTagName.should.eql('div'); + div.childNodes.length.should.eql(2); + const comment = div.childNodes[0]; + comment.rawTagName.should.eql('!--'); + const text = div.childNodes[1]; + text.rawTagName.should.eql(''); + }); + it('querySelector for comment nodes', function () { + const root = parse(` + +

TEST

+ + +`, { comment: true }); + const div = root.childNodes[0]; + const comment = div.querySelector('!--'); + comment.text.should.eql(' Some comment here. '); + }); +});