Skip to content

Commit dafd70e

Browse files
committed
internal/*: add support for raw values (for grid)
Note that this doesn't strip whitespace from raw values.
1 parent b63e73a commit dafd70e

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

internal/ast/value.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ type MathParenthesizedExpression struct {
9696
Value Value
9797
}
9898

99+
// Raw is an otherwise non-structured but valid value.
100+
type Raw struct {
101+
Span
102+
103+
Value string
104+
}
105+
99106
// Comma is a single comma. Some declarations require commas,
100107
// e.g. font-family fallbacks or transitions.
101108
type Comma struct {
@@ -110,6 +117,7 @@ func (MathParenthesizedExpression) isValue() {}
110117
func (Comma) isValue() {}
111118
func (Identifier) isValue() {}
112119
func (HexColor) isValue() {}
120+
func (Raw) isValue() {}
113121

114122
var _ Value = String{}
115123
var _ Value = Dimension{}
@@ -119,3 +127,4 @@ var _ Value = MathParenthesizedExpression{}
119127
var _ Value = Comma{}
120128
var _ Value = Identifier{}
121129
var _ Value = HexColor{}
130+
var _ Value = Raw{}

internal/integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func TestIntegration(t *testing.T) {
1616
"testdata/comments.css",
1717
"testdata/bem.css",
1818
"testdata/font-face.css",
19+
"testdata/grid.css",
1920
} {
2021
t.Run(c, func(t *testing.T) {
2122

internal/parser/parser.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,15 @@ func (p *parser) parseDeclarationBlock() *ast.DeclarationBlock {
141141
case lexer.EOF:
142142
p.lexer.Errorf("unexpected EOF")
143143

144+
case lexer.Comma:
145+
decl.Values = append(decl.Values, &ast.Comma{Span: p.lexer.TokenSpan()})
146+
p.lexer.Next()
147+
144148
case lexer.Delim:
145149
if p.lexer.CurrentString != "!" {
146-
p.lexer.Errorf("unexpected token: %s", p.lexer.CurrentString)
150+
decl.Values = append(decl.Values, &ast.Raw{Span: p.lexer.TokenSpan(), Value: p.lexer.CurrentString})
151+
p.lexer.Next()
152+
continue
147153
}
148154
p.lexer.Next()
149155

@@ -154,10 +160,6 @@ func (p *parser) parseDeclarationBlock() *ast.DeclarationBlock {
154160
p.lexer.Next()
155161
decl.Important = true
156162

157-
case lexer.Comma:
158-
decl.Values = append(decl.Values, &ast.Comma{Span: p.lexer.TokenSpan()})
159-
p.lexer.Next()
160-
161163
default:
162164
val := p.parseValue()
163165
if val == nil {

internal/printer/printer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ func (p *printer) print(in ast.Node) {
363363
}
364364
p.s.WriteRune(')')
365365

366+
case *ast.Raw:
367+
p.s.WriteString(node.Value)
368+
366369
default:
367370
panic(fmt.Sprintf("unknown ast node: %s", reflect.TypeOf(in).String()))
368371
}

internal/testdata/grid.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.a {
2+
grid-column: 1 / span 2;
3+
grid-template: "text" auto "meter" 1fr / 1fr;
4+
}
5+
6+
.b {
7+
grid-column: 3 ;
8+
grid-row: 1 / span 2;
9+
}
10+
11+
.c {
12+
grid-column: 1 ;
13+
grid-row: 2 ;
14+
}
15+
16+
.d {
17+
grid-column: 2 ;
18+
grid-row: 2 ;
19+
}

0 commit comments

Comments
 (0)