Skip to content

Commit 9ac642d

Browse files
committedMar 29, 2024
fix: add rawTagName #269 #270
1 parent a1892f1 commit 9ac642d

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed
 

‎src/nodes/comment.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import NodeType from './type';
44

55
export default class CommentNode extends Node {
66
public clone(): CommentNode {
7-
return new CommentNode(this.rawText, null);
7+
return new CommentNode(this.rawText, null, undefined, this.rawTagName);
88
}
9-
public constructor(public rawText: string, parentNode = null as HTMLElement | null, range?: [number, number]) {
9+
public constructor(public rawText: string, parentNode = null as HTMLElement | null, range?: [number, number], public rawTagName = '!--') {
1010
super(parentNode, range);
1111
}
1212

‎src/nodes/node.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import HTMLElement from './html';
66
* Node Class as base class for TextNode and HTMLElement.
77
*/
88
export default abstract class Node {
9+
abstract rawTagName: string;
910
abstract nodeType: NodeType;
1011
public childNodes = [] as Node[];
1112
public range: readonly [number, number];

‎src/nodes/text.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default class TextNode extends Node {
2121
* @type {Number}
2222
*/
2323
public nodeType = NodeType.TEXT_NODE;
24+
public rawTagName = '';
2425

2526
private _rawText: string;
2627
private _trimmedRawText?: string;

‎test/tests/issues/269-270.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { parse } = require('@test/test-target');
2+
3+
describe('issue 269 270', function () {
4+
it('node.rawTagName', function () {
5+
const root = parse('<div><!--this is comment here -->foo</div>', { comment: true });
6+
const div = root.childNodes[0];
7+
div.rawTagName.should.eql('div');
8+
div.childNodes.length.should.eql(2);
9+
const comment = div.childNodes[0];
10+
comment.rawTagName.should.eql('!--');
11+
const text = div.childNodes[1];
12+
text.rawTagName.should.eql('');
13+
});
14+
it('querySelector for comment nodes', function () {
15+
const root = parse(`<html>
16+
<body>
17+
<h1>TEST</h1>
18+
<!-- Some comment here. -->
19+
</body>
20+
</html>`, { comment: true });
21+
const div = root.childNodes[0];
22+
const comment = div.querySelector('!--');
23+
comment.text.should.eql(' Some comment here. ');
24+
});
25+
});

0 commit comments

Comments
 (0)
Please sign in to comment.