Skip to content

Commit dc31b64

Browse files
authored
feat: add Range to DocType nodes (#1302)
1 parent be9d6c9 commit dc31b64

4 files changed

Lines changed: 22 additions & 0 deletions

File tree

parser/v2/doctypeparser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var docType = parse.Func(func(pi *parse.Input) (n Node, ok bool, err error) {
2727
err = parse.Error("unclosed DOCTYPE", start)
2828
return
2929
}
30+
r.Range = NewRange(start, pi.Position())
3031

3132
return r, true, nil
3233
})

parser/v2/doctypeparser_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,43 @@ func TestDocTypeParser(t *testing.T) {
1717
name: "HTML 5 doctype - uppercase",
1818
input: `<!DOCTYPE html>`,
1919
expected: &DocType{
20+
Range: Range{
21+
From: Position{},
22+
To: Position{Index: 15, Line: 0, Col: 15},
23+
},
2024
Value: "html",
2125
},
2226
},
2327
{
2428
name: "HTML 5 doctype - lowercase",
2529
input: `<!doctype html>`,
2630
expected: &DocType{
31+
Range: Range{
32+
From: Position{},
33+
To: Position{Index: 15, Line: 0, Col: 15},
34+
},
2735
Value: "html",
2836
},
2937
},
3038
{
3139
name: "HTML 4.01 doctype",
3240
input: `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">`,
3341
expected: &DocType{
42+
Range: Range{
43+
From: Position{},
44+
To: Position{Index: 102, Line: 0, Col: 102},
45+
},
3446
Value: `HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"`,
3547
},
3648
},
3749
{
3850
name: "XHTML 1.1",
3951
input: `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">`,
4052
expected: &DocType{
53+
Range: Range{
54+
From: Position{},
55+
To: Position{Index: 97, Line: 0, Col: 97},
56+
},
4157
Value: `html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"`,
4258
},
4359
},

parser/v2/templateparser_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,10 @@ func TestTemplateParser(t *testing.T) {
669669
},
670670
Children: []Node{
671671
&DocType{
672+
Range: Range{
673+
From: Position{Index: 15, Line: 1, Col: 0},
674+
To: Position{Index: 30, Line: 1, Col: 15},
675+
},
672676
Value: "html",
673677
},
674678
&Whitespace{Range: Range{

parser/v2/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ func (c *ExpressionCSSProperty) Visit(v Visitor) error {
364364

365365
// <!DOCTYPE html>
366366
type DocType struct {
367+
Range Range
367368
Value string
368369
}
369370

0 commit comments

Comments
 (0)