Skip to content

Commit 8fb9598

Browse files
committed
Merge pull request aymerick#3 from alexsaveliev/master
Dealing with CSS comments
2 parents a06d6fa + ce9bdda commit 8fb9598

File tree

4 files changed

+186
-52
lines changed

4 files changed

+186
-52
lines changed

css/declaration.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,43 @@ func NewDeclaration() *Declaration {
1818

1919
// Returns string representation of the Declaration
2020
func (decl *Declaration) String() string {
21-
return decl.StringWithImportant(true)
21+
return decl.Str(false)
22+
}
23+
24+
// Returns string representation of the Declaration
25+
func (decl *Declaration) Str(diff bool) string {
26+
return decl.stringWithImportant(true, diff)
2227
}
2328

2429
// StringWithImportant returns string representation with optional !important part
2530
func (decl *Declaration) StringWithImportant(option bool) string {
31+
return decl.stringWithImportant(option, false)
32+
}
33+
34+
// StringWithImportant returns string representation with optional !important part
35+
func (decl *Declaration) stringWithImportant(option bool, diff bool) string {
2636
result := fmt.Sprintf("%s: %s", decl.Property, decl.Value)
2737

2838
if option && decl.Important {
2939
result += " !important"
3040
}
3141

32-
result += ";"
42+
if diff {
43+
result += fmt.Sprintf(" (%d, %d)", decl.Line, decl.Column)
44+
} else {
45+
result += ";"
46+
}
3347

3448
return result
3549
}
3650

3751
// Equal returns true if both Declarations are equals
3852
func (decl *Declaration) Equal(other *Declaration) bool {
39-
return (decl.Property == other.Property) && (decl.Value == other.Value) && (decl.Important == other.Important)
53+
return decl.Property == other.Property &&
54+
decl.Value == other.Value &&
55+
decl.Important == other.Important &&
56+
decl.Line == other.Line &&
57+
decl.Column == other.Column
4058
}
4159

4260
//

css/rule.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ type Selector struct {
3131
}
3232

3333
func (selector Selector) String() string {
34-
return fmt.Sprintf("Selector: %s (%d, %d)", selector.Value, selector.Line, selector.Column)
34+
return selector.str(false)
35+
}
36+
37+
func (selector Selector) str(diff bool) string {
38+
if diff {
39+
return fmt.Sprintf("Selector: %s (%d, %d)", selector.Value, selector.Line, selector.Column)
40+
} else {
41+
return selector.Value
42+
}
3543
}
3644

3745
// Rule represents a parsed CSS rule
@@ -147,7 +155,7 @@ func (rule *Rule) Diff(other *Rule) []string {
147155
} else {
148156
for i, sel := range rule.Selectors {
149157
if sel != other.Selectors[i] {
150-
result = append(result, fmt.Sprintf("Selector: \"%s\" | \"%s\"", sel, other.Selectors[i]))
158+
result = append(result, fmt.Sprintf("Selector: \"%s\" | \"%s\"", sel.str(true), other.Selectors[i].str(true)))
151159
}
152160
}
153161
}
@@ -157,7 +165,7 @@ func (rule *Rule) Diff(other *Rule) []string {
157165
} else {
158166
for i, decl := range rule.Declarations {
159167
if !decl.Equal(other.Declarations[i]) {
160-
result = append(result, fmt.Sprintf("Declaration: \"%s\" | \"%s\"", decl.String(), other.Declarations[i].String()))
168+
result = append(result, fmt.Sprintf("Declaration: \"%s\" | \"%s\"", decl.Str(true), other.Declarations[i].Str(true)))
161169
}
162170
}
163171
}
@@ -168,7 +176,7 @@ func (rule *Rule) Diff(other *Rule) []string {
168176

169177
for i, rule := range rule.Rules {
170178
if !rule.Equal(other.Rules[i]) {
171-
result = append(result, fmt.Sprintf("Rule: \"%s\" | \"%s\"", rule.String(), other.Rules[i].String()))
179+
result = append(result, fmt.Sprintf("Rule: \"%s\" | \"%s\"", rule.str(true), other.Rules[i].str(true)))
172180
}
173181
}
174182
}
@@ -178,6 +186,11 @@ func (rule *Rule) Diff(other *Rule) []string {
178186

179187
// Returns the string representation of a rule
180188
func (rule *Rule) String() string {
189+
return rule.str(false)
190+
}
191+
192+
// Returns the string representation of a rule
193+
func (rule *Rule) str(diff bool) string {
181194
result := ""
182195

183196
if rule.Kind == QualifiedRule {
@@ -206,11 +219,11 @@ func (rule *Rule) String() string {
206219

207220
if rule.EmbedsRules() {
208221
for _, subRule := range rule.Rules {
209-
result += fmt.Sprintf("%s%s\n", rule.indent(), subRule.String())
222+
result += fmt.Sprintf("%s%s\n", rule.indent(), subRule.str(diff))
210223
}
211224
} else {
212225
for _, decl := range rule.Declarations {
213-
result += fmt.Sprintf("%s%s\n", rule.indent(), decl.String())
226+
result += fmt.Sprintf("%s%s\n", rule.indent(), decl.Str(diff))
214227
}
215228
}
216229

@@ -222,24 +235,12 @@ func (rule *Rule) String() string {
222235

223236
// Returns identation spaces for declarations and rules
224237
func (rule *Rule) indent() string {
225-
result := ""
226-
227-
for i := 0; i < ((rule.EmbedLevel + 1) * indentSpace); i++ {
228-
result += " "
229-
}
230-
231-
return result
238+
return strings.Repeat(" ", (rule.EmbedLevel+1)*indentSpace)
232239
}
233240

234241
// Returns identation spaces for end of block character
235242
func (rule *Rule) indentEndBlock() string {
236-
result := ""
237-
238-
for i := 0; i < (rule.EmbedLevel * indentSpace); i++ {
239-
result += " "
240-
}
241-
242-
return result
243+
return strings.Repeat(" ", rule.EmbedLevel*indentSpace)
243244
}
244245

245246
func (rule *Rule) Sel() []string {

parser/parser.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ func (parser *Parser) ParseRule() (*css.Rule, error) {
127127
if parser.tokenAtKeyword() {
128128
return parser.parseAtRule()
129129
}
130-
131130
return parser.parseQualifiedRule()
132131
}
133132

@@ -195,11 +194,16 @@ func (parser *Parser) ParseDeclaration() (*css.Declaration, error) {
195194

196195
// finished
197196
break
198-
} else {
197+
} else if !parser.tokenIgnorable() {
199198
token := parser.shiftToken()
200199
curValue += token.Value
201200
curColumn = token.Column
202201
curLine = token.Line
202+
} else {
203+
token := parser.shiftToken()
204+
if token.Type != scanner.TokenComment {
205+
curValue += token.Value
206+
}
203207
}
204208
}
205209

@@ -279,6 +283,10 @@ func (parser *Parser) parseQualifiedRule() (*css.Rule, error) {
279283

280284
result.Declarations = declarations
281285

286+
// finished
287+
break
288+
} else if parser.tokenChar(";") {
289+
parser.shiftToken()
282290
// finished
283291
break
284292
} else {
@@ -310,7 +318,7 @@ func (parser *Parser) parseQualifiedRule() (*css.Rule, error) {
310318
selectorStart = true
311319
selectorValue = ""
312320
}
313-
case tok.Type != scanner.TokenS:
321+
case tok.Type != scanner.TokenS && tok.Type != scanner.TokenComment:
314322
{
315323
selectorValue += tok.Value
316324
if selectorStart {

0 commit comments

Comments
 (0)