Skip to content

Commit a06d6fa

Browse files
committed
Merge pull request aymerick#2 from alexsaveliev/master
Emitting proper locations for comma-separated declarations
2 parents 798661f + 6552c9e commit a06d6fa

File tree

3 files changed

+108
-17
lines changed

3 files changed

+108
-17
lines changed

css/rule.go

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ type Selector struct {
3030
Column int
3131
}
3232

33+
func (selector Selector) String() string {
34+
return fmt.Sprintf("Selector: %s (%d, %d)", selector.Value, selector.Line, selector.Column)
35+
}
36+
3337
// Rule represents a parsed CSS rule
3438
type Rule struct {
3539
Kind RuleKind

parser/parser.go

+38-9
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,45 @@ func (parser *Parser) parseQualifiedRule() (*css.Rule, error) {
293293
}
294294
}
295295

296-
for i, s := range strings.Split(result.Prelude, ",") {
297-
result.Selectors = append(result.Selectors, &css.Selector{
298-
Value: s,
299-
Line: preludeTokens[i].Line,
300-
Column: preludeTokens[i].Column,
301-
})
302-
}
303-
for i, sel := range result.Sel() {
304-
result.Selectors[i].Value = strings.TrimSpace(sel)
296+
selectorValue := ""
297+
selectorStart := true
298+
var line int
299+
var col int
300+
301+
for _, tok := range preludeTokens {
302+
switch {
303+
case tok.Value == ",":
304+
{
305+
result.Selectors = append(result.Selectors, &css.Selector{
306+
Value: strings.TrimSpace(selectorValue),
307+
Line: line,
308+
Column: col,
309+
})
310+
selectorStart = true
311+
selectorValue = ""
312+
}
313+
case tok.Type != scanner.TokenS:
314+
{
315+
selectorValue += tok.Value
316+
if selectorStart {
317+
line = tok.Line
318+
col = tok.Column
319+
selectorStart = false
320+
}
321+
}
322+
case tok.Type == scanner.TokenS:
323+
{
324+
if !selectorStart {
325+
selectorValue += tok.Value
326+
}
327+
}
328+
}
305329
}
330+
result.Selectors = append(result.Selectors, &css.Selector{
331+
Value: strings.TrimSpace(selectorValue),
332+
Line: line,
333+
Column: col,
334+
})
306335

307336
// log.Printf("[parsed] Rule: %s", result.String())
308337

parser/parser_test.go

+66-8
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ body,
151151
&css.Selector{
152152
Value: "tr",
153153
Line: 1,
154-
Column: 6,
154+
Column: 8,
155155
},
156156
&css.Selector{
157157
Value: "td",
158158
Line: 1,
159-
Column: 7,
159+
Column: 12,
160160
},
161161
},
162162
Declarations: []*css.Declaration{
@@ -180,21 +180,22 @@ body,
180180
},
181181
&css.Selector{
182182
Value: "h1",
183-
Line: 5,
184-
Column: 5,
183+
Line: 6,
184+
Column: 3,
185185
},
186186
&css.Selector{
187187
Value: "h2",
188-
Line: 5,
189-
Column: 6,
188+
Line: 6,
189+
Column: 9,
190190
},
191191
&css.Selector{
192192
Value: "h3",
193-
Line: 6,
194-
Column: 3,
193+
Line: 7,
194+
Column: 5,
195195
},
196196
},
197197

198+
198199
Declarations: []*css.Declaration{
199200
{
200201
Property: "color",
@@ -715,3 +716,60 @@ func TestParseDeclarations(t *testing.T) {
715716
}
716717
}
717718
}
719+
720+
func TestMultipleDeclarations(t *testing.T) {
721+
input := `.btn:focus,
722+
.btn:active:focus,
723+
.btn.active:focus,
724+
.btn.focus,
725+
.btn:active.focus,
726+
.btn.active.focus {
727+
}`
728+
expectedRule := &css.Rule{
729+
Kind: css.QualifiedRule,
730+
Prelude: `.btn:focus,
731+
.btn:active:focus,
732+
.btn.active:focus,
733+
.btn.focus,
734+
.btn:active.focus,
735+
.btn.active.focus`,
736+
Selectors: []*css.Selector{
737+
&css.Selector{
738+
Value: ".btn:focus",
739+
Line: 1,
740+
Column: 1,
741+
},
742+
&css.Selector{
743+
Value: ".btn:active:focus",
744+
Line: 2,
745+
Column: 1,
746+
},
747+
&css.Selector{
748+
Value: ".btn.active:focus",
749+
Line: 3,
750+
Column: 1,
751+
},
752+
&css.Selector{
753+
Value: ".btn.focus",
754+
Line: 4,
755+
Column: 1,
756+
},
757+
&css.Selector{
758+
Value: ".btn:active.focus",
759+
Line: 5,
760+
Column: 1,
761+
},
762+
&css.Selector{
763+
Value: ".btn.active.focus",
764+
Line: 6,
765+
Column: 1,
766+
},
767+
},
768+
Declarations: []*css.Declaration{},
769+
}
770+
771+
stylesheet := MustParse(t, input, 1)
772+
rule := stylesheet.Rules[0]
773+
774+
MustEqualRule(t, rule, expectedRule)
775+
}

0 commit comments

Comments
 (0)