Skip to content

Commit a6b07a1

Browse files
committed
CSS Parser correctly working.
1 parent 4e08eca commit a6b07a1

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

Block.go

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cssminify
22

33
import (
44
"bytes"
5-
"fmt"
65
"io/ioutil"
76
"strings"
87
)
@@ -25,58 +24,52 @@ const (
2524
STARTING_COMMENT = 0
2625
IN_COMMENT = 1
2726
CLOSING_COMMENT = 2
28-
IN_SELECTOR = 3
29-
IN_PROPERTY = 4
30-
IN_VALUE = 5
27+
COMMENT_CLOSED = 3
28+
IN_SELECTOR = 4
29+
IN_PROPERTY = 5
30+
IN_VALUE = 6
3131
)
3232

3333
func Blocks(file string) []Block {
3434
var (
3535
blocks []Block
3636
letter byte
3737
current []byte
38-
beforeState byte
38+
oldCurrent []byte
3939
state byte
40+
commentState byte
4041
currentBlock Block
4142
currentPair Pair
4243
)
4344

4445
content := []byte(readFile(file))
4546

4647
for letter, content = stripLetter(content); letter != 0; letter, content = stripLetter(content) {
47-
if state == STARTING_COMMENT && letter != '*' {
48-
state = beforeState
49-
}
50-
if state == CLOSING_COMMENT && letter != '/' {
51-
state = beforeState
52-
}
5348
switch letter {
5449
case '/':
55-
switch state {
50+
switch commentState {
5651
case CLOSING_COMMENT:
57-
state = beforeState
58-
5952
// Since we don't keep comments
60-
current = []byte{}
53+
current = oldCurrent
54+
commentState = COMMENT_CLOSED
6155
default:
62-
if state != IN_COMMENT {
63-
beforeState = state
64-
state = STARTING_COMMENT
56+
if commentState != IN_COMMENT {
57+
commentState = STARTING_COMMENT
6558
current = append(current, letter)
6659
}
6760
}
6861
case '*':
69-
switch state {
62+
switch commentState {
7063
case STARTING_COMMENT:
71-
beforeState = state
72-
state = IN_COMMENT
64+
oldCurrent = current[:len(current)-1]
65+
commentState = IN_COMMENT
7366
current = append(current, letter)
7467
case IN_COMMENT:
75-
state = CLOSING_COMMENT
68+
commentState = CLOSING_COMMENT
7669
current = append(current, letter)
7770
}
7871
case '{':
79-
if state != IN_COMMENT {
72+
if commentState != IN_COMMENT {
8073
if state == IN_SELECTOR {
8174
state = IN_PROPERTY
8275
currentBlock.selector = current
@@ -86,7 +79,7 @@ func Blocks(file string) []Block {
8679
}
8780
}
8881
case '}':
89-
if state != IN_COMMENT {
82+
if commentState != IN_COMMENT {
9083
if state == IN_VALUE && !bytes.Equal(nil, current) {
9184
state = IN_PROPERTY
9285
currentPair.value = current
@@ -102,7 +95,7 @@ func Blocks(file string) []Block {
10295
}
10396
}
10497
case ':':
105-
if state != IN_COMMENT {
98+
if commentState != IN_COMMENT {
10699
if state == IN_PROPERTY && !bytes.Equal(nil, current) {
107100
state = IN_VALUE
108101
currentPair.property = current
@@ -116,7 +109,7 @@ func Blocks(file string) []Block {
116109
}
117110
}
118111
case ';':
119-
if state != IN_COMMENT {
112+
if commentState != IN_COMMENT {
120113
if state == IN_VALUE {
121114
state = IN_PROPERTY
122115
currentPair.value = current
@@ -130,7 +123,7 @@ func Blocks(file string) []Block {
130123
}
131124
}
132125
default:
133-
if state != IN_COMMENT {
126+
if commentState != IN_COMMENT {
134127
if state == 0 {
135128
state = IN_SELECTOR
136129
}

Minify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import (
66

77
func Minify(blocks []Block) {
88
for _, block := range blocks {
9-
fmt.Printf("%s", block.selector)
9+
fmt.Printf("%s\n", block.selector)
1010
}
1111
}

0 commit comments

Comments
 (0)