From 9ac642d7b6a97c84fd4471b8eaa0bd9debf5e325 Mon Sep 17 00:00:00 2001 From: taoqf Date: Fri, 29 Mar 2024 09:08:53 +0800 Subject: [PATCH] fix: add rawTagName #269 #270 --- src/nodes/comment.ts | 4 ++-- src/nodes/node.ts | 1 + src/nodes/text.ts | 1 + test/tests/issues/269-270.js | 25 +++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/tests/issues/269-270.js 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. '); + }); +});