Skip to content

Commit

Permalink
fix: add rawTagName #269 #270
Browse files Browse the repository at this point in the history
  • Loading branch information
taoqf committed Mar 29, 2024
1 parent a1892f1 commit 9ac642d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/nodes/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions src/nodes/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
1 change: 1 addition & 0 deletions src/nodes/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions test/tests/issues/269-270.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { parse } = require('@test/test-target');

describe('issue 269 270', function () {
it('node.rawTagName', function () {
const root = parse('<div><!--this is comment here -->foo</div>', { 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(`<html>
<body>
<h1>TEST</h1>
<!-- Some comment here. -->
</body>
</html>`, { comment: true });
const div = root.childNodes[0];
const comment = div.querySelector('!--');
comment.text.should.eql(' Some comment here. ');
});
});

0 comments on commit 9ac642d

Please sign in to comment.