Skip to content

Commit 71566a0

Browse files
dgrundelDaniel
andauthored
feat: add Range to raw elements (RawElement, ScriptElement) (#1231)
Co-authored-by: Daniel <daniel@grundel.local>
1 parent 63d8ad2 commit 71566a0

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

parser/v2/raw.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ func (p rawElementParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
6262
// Cut the end element.
6363
_, _, _ = end.Parse(pi)
6464

65+
e.Range = NewRange(pi.PositionAt(start), pi.Position())
6566
return e, true, nil
6667
}

parser/v2/raw_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func TestRawElementParser(t *testing.T) {
3636
},
3737
},
3838
Contents: "contents",
39+
Range: Range{
40+
From: Position{Index: 0, Line: 0, Col: 0},
41+
To: Position{Index: 39, Line: 0, Col: 39},
42+
},
3943
},
4044
},
4145
{
@@ -56,6 +60,10 @@ func TestRawElementParser(t *testing.T) {
5660
},
5761
},
5862
Contents: ignoredContent,
63+
Range: Range{
64+
From: Position{Index: 0, Line: 0, Col: 0},
65+
To: Position{Index: 52, Line: 3, Col: 9},
66+
},
5967
},
6068
},
6169
}

parser/v2/scriptparser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ loop:
163163
}
164164
}
165165

166+
e.Range = NewRange(pi.PositionAt(start), pi.Position())
166167
return e, true, nil
167168
}
168169

parser/v2/scriptparser_test.go

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,14 @@ func TestScriptElementParser(t *testing.T) {
6767
expected *ScriptElement
6868
}{
6969
{
70-
name: "script: no content",
71-
input: `<script></script>`,
72-
expected: &ScriptElement{},
70+
name: "script: no content",
71+
input: `<script></script>`,
72+
expected: &ScriptElement{
73+
Range: Range{
74+
From: Position{Index: 0, Line: 0, Col: 0},
75+
To: Position{Index: 17, Line: 0, Col: 17},
76+
},
77+
},
7378
},
7479
{
7580
name: "script: vbscript",
@@ -107,6 +112,10 @@ func TestScriptElementParser(t *testing.T) {
107112
},
108113
}, false),
109114
},
115+
Range: Range{
116+
From: Position{Index: 0, Line: 0, Col: 0},
117+
To: Position{Index: 27, Line: 0, Col: 27},
118+
},
110119
},
111120
},
112121
{
@@ -133,6 +142,10 @@ func TestScriptElementParser(t *testing.T) {
133142
},
134143
}, false),
135144
},
145+
Range: Range{
146+
From: Position{Index: 0, Line: 0, Col: 0},
147+
To: Position{Index: 50, Line: 0, Col: 50},
148+
},
136149
},
137150
},
138151
{
@@ -159,6 +172,10 @@ func TestScriptElementParser(t *testing.T) {
159172
},
160173
}, false),
161174
},
175+
Range: Range{
176+
From: Position{Index: 0, Line: 0, Col: 0},
177+
To: Position{Index: 41, Line: 0, Col: 41},
178+
},
162179
},
163180
},
164181
{
@@ -185,6 +202,10 @@ func TestScriptElementParser(t *testing.T) {
185202
},
186203
}, false),
187204
},
205+
Range: Range{
206+
From: Position{Index: 0, Line: 0, Col: 0},
207+
To: Position{Index: 45, Line: 0, Col: 45},
208+
},
188209
},
189210
},
190211
{
@@ -206,6 +227,10 @@ func TestScriptElementParser(t *testing.T) {
206227
TrailingSpace: SpaceVertical,
207228
}, false),
208229
},
230+
Range: Range{
231+
From: Position{Index: 0, Line: 0, Col: 0},
232+
To: Position{Index: 29, Line: 2, Col: 9},
233+
},
209234
},
210235
},
211236
{
@@ -225,6 +250,10 @@ func TestScriptElementParser(t *testing.T) {
225250
}, true),
226251
NewScriptContentsScriptCode("';"),
227252
},
253+
Range: Range{
254+
From: Position{Index: 0, Line: 0, Col: 0},
255+
To: Position{Index: 38, Line: 0, Col: 38},
256+
},
228257
},
229258
},
230259
{
@@ -244,6 +273,10 @@ func TestScriptElementParser(t *testing.T) {
244273
}, true),
245274
NewScriptContentsScriptCode("\";"),
246275
},
276+
Range: Range{
277+
From: Position{Index: 0, Line: 0, Col: 0},
278+
To: Position{Index: 38, Line: 0, Col: 38},
279+
},
247280
},
248281
},
249282
{
@@ -266,6 +299,10 @@ to see if it works";</script>`,
266299
}, true),
267300
NewScriptContentsScriptCode("\\\nto see if it works\";"),
268301
},
302+
Range: Range{
303+
From: Position{Index: 0, Line: 0, Col: 0},
304+
To: Position{Index: 76, Line: 2, Col: 29},
305+
},
269306
},
270307
},
271308
{
@@ -285,6 +322,10 @@ to see if it works";</script>`,
285322
}, true),
286323
NewScriptContentsScriptCode("`;"),
287324
},
325+
Range: Range{
326+
From: Position{Index: 0, Line: 0, Col: 0},
327+
To: Position{Index: 38, Line: 0, Col: 38},
328+
},
288329
},
289330
},
290331
{
@@ -297,6 +338,10 @@ to see if it works";</script>`,
297338
NewScriptContentsScriptCode("\n"),
298339
NewScriptContentsScriptCode("// {{ name }}\n"),
299340
},
341+
Range: Range{
342+
From: Position{Index: 0, Line: 0, Col: 0},
343+
To: Position{Index: 32, Line: 2, Col: 9},
344+
},
300345
},
301346
},
302347
{
@@ -311,6 +356,10 @@ but it's commented out */
311356
NewScriptContentsScriptCode("\n"),
312357
NewScriptContentsScriptCode("/* There's some content\n{{ name }}\nbut it's commented out */\n"),
313358
},
359+
Range: Range{
360+
From: Position{Index: 0, Line: 0, Col: 0},
361+
To: Position{Index: 79, Line: 4, Col: 9},
362+
},
314363
},
315364
},
316365
{

parser/v2/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ type ScriptContents struct {
729729
type ScriptElement struct {
730730
Attributes []Attribute
731731
Contents []ScriptContents
732+
Range Range
732733
}
733734

734735
func (se *ScriptElement) IsNode() bool { return true }
@@ -793,6 +794,7 @@ type RawElement struct {
793794
Name string
794795
Attributes []Attribute
795796
Contents string
797+
Range Range
796798
}
797799

798800
func (e *RawElement) IsNode() bool { return true }

0 commit comments

Comments
 (0)