Skip to content

Commit 63d8ad2

Browse files
dgrundelDaniel
andauthored
feat: add Range to HTMLComment nodes (#1229)
Co-authored-by: Daniel <daniel@grundel.local>
1 parent e0a1051 commit 63d8ad2

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

parser/v2/htmlcommentparser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ func (p htmlCommentParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
3535
return
3636
}
3737

38+
c.Range = NewRange(start, pi.Position())
3839
return c, true, nil
3940
}

parser/v2/htmlcommentparser_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,37 @@ func TestHTMLCommentParser(t *testing.T) {
1818
input: `<!-- single line comment -->`,
1919
expected: &HTMLComment{
2020
Contents: " single line comment ",
21+
Range: Range{
22+
From: Position{
23+
Index: 0,
24+
Line: 0,
25+
Col: 0,
26+
},
27+
To: Position{
28+
Index: 28,
29+
Line: 0,
30+
Col: 28,
31+
},
32+
},
2133
},
2234
},
2335
{
2436
name: "comment - no whitespace",
2537
input: `<!--no whitespace between sequence open and close-->`,
2638
expected: &HTMLComment{
2739
Contents: "no whitespace between sequence open and close",
40+
Range: Range{
41+
From: Position{
42+
Index: 0,
43+
Line: 0,
44+
Col: 0,
45+
},
46+
To: Position{
47+
Index: 52,
48+
Line: 0,
49+
Col: 52,
50+
},
51+
},
2852
},
2953
},
3054
{
@@ -36,20 +60,56 @@ func TestHTMLCommentParser(t *testing.T) {
3660
Contents: ` multiline
3761
comment
3862
`,
63+
Range: Range{
64+
From: Position{
65+
Index: 0,
66+
Line: 0,
67+
Col: 0,
68+
},
69+
To: Position{
70+
Index: 39,
71+
Line: 2,
72+
Col: 8,
73+
},
74+
},
3975
},
4076
},
4177
{
4278
name: "comment - with tag",
4379
input: `<!-- <p class="test">tag</p> -->`,
4480
expected: &HTMLComment{
4581
Contents: ` <p class="test">tag</p> `,
82+
Range: Range{
83+
From: Position{
84+
Index: 0,
85+
Line: 0,
86+
Col: 0,
87+
},
88+
To: Position{
89+
Index: 32,
90+
Line: 0,
91+
Col: 32,
92+
},
93+
},
4694
},
4795
},
4896
{
4997
name: "comments can contain tags",
5098
input: `<!-- <div> hello world </div> -->`,
5199
expected: &HTMLComment{
52100
Contents: ` <div> hello world </div> `,
101+
Range: Range{
102+
From: Position{
103+
Index: 0,
104+
Line: 0,
105+
Col: 0,
106+
},
107+
To: Position{
108+
Index: 33,
109+
Line: 0,
110+
Col: 33,
111+
},
112+
},
53113
},
54114
},
55115
}

parser/v2/templateparser_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,9 +768,21 @@ func TestTemplateParser(t *testing.T) {
768768
},
769769
Children: []Node{
770770
&Whitespace{Value: "\t"},
771-
&HTMLComment{Contents: " Single line "},
771+
&HTMLComment{
772+
Contents: " Single line ",
773+
Range: Range{
774+
From: Position{Index: 13, Line: 1, Col: 1},
775+
To: Position{Index: 33, Line: 1, Col: 21},
776+
},
777+
},
772778
&Whitespace{Value: "\n\t"},
773-
&HTMLComment{Contents: "\n\t\tMultiline\n\t"},
779+
&HTMLComment{
780+
Contents: "\n\t\tMultiline\n\t",
781+
Range: Range{
782+
From: Position{Index: 35, Line: 2, Col: 1},
783+
To: Position{Index: 56, Line: 4, Col: 4},
784+
},
785+
},
774786
&Whitespace{Value: "\n"},
775787
},
776788
},

parser/v2/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,7 @@ func (c *GoComment) Visit(v Visitor) error {
11351135
// HTMLComment.
11361136
type HTMLComment struct {
11371137
Contents string
1138+
Range Range
11381139
}
11391140

11401141
func (c *HTMLComment) IsNode() bool { return true }

0 commit comments

Comments
 (0)