@@ -2,7 +2,6 @@ package cssminify
2
2
3
3
import (
4
4
"bytes"
5
- "fmt"
6
5
"io/ioutil"
7
6
"strings"
8
7
)
@@ -25,58 +24,52 @@ const (
25
24
STARTING_COMMENT = 0
26
25
IN_COMMENT = 1
27
26
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
31
31
)
32
32
33
33
func Blocks (file string ) []Block {
34
34
var (
35
35
blocks []Block
36
36
letter byte
37
37
current []byte
38
- beforeState byte
38
+ oldCurrent [] byte
39
39
state byte
40
+ commentState byte
40
41
currentBlock Block
41
42
currentPair Pair
42
43
)
43
44
44
45
content := []byte (readFile (file ))
45
46
46
47
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
- }
53
48
switch letter {
54
49
case '/' :
55
- switch state {
50
+ switch commentState {
56
51
case CLOSING_COMMENT :
57
- state = beforeState
58
-
59
52
// Since we don't keep comments
60
- current = []byte {}
53
+ current = oldCurrent
54
+ commentState = COMMENT_CLOSED
61
55
default :
62
- if state != IN_COMMENT {
63
- beforeState = state
64
- state = STARTING_COMMENT
56
+ if commentState != IN_COMMENT {
57
+ commentState = STARTING_COMMENT
65
58
current = append (current , letter )
66
59
}
67
60
}
68
61
case '*' :
69
- switch state {
62
+ switch commentState {
70
63
case STARTING_COMMENT :
71
- beforeState = state
72
- state = IN_COMMENT
64
+ oldCurrent = current [: len ( current ) - 1 ]
65
+ commentState = IN_COMMENT
73
66
current = append (current , letter )
74
67
case IN_COMMENT :
75
- state = CLOSING_COMMENT
68
+ commentState = CLOSING_COMMENT
76
69
current = append (current , letter )
77
70
}
78
71
case '{' :
79
- if state != IN_COMMENT {
72
+ if commentState != IN_COMMENT {
80
73
if state == IN_SELECTOR {
81
74
state = IN_PROPERTY
82
75
currentBlock .selector = current
@@ -86,7 +79,7 @@ func Blocks(file string) []Block {
86
79
}
87
80
}
88
81
case '}' :
89
- if state != IN_COMMENT {
82
+ if commentState != IN_COMMENT {
90
83
if state == IN_VALUE && ! bytes .Equal (nil , current ) {
91
84
state = IN_PROPERTY
92
85
currentPair .value = current
@@ -102,7 +95,7 @@ func Blocks(file string) []Block {
102
95
}
103
96
}
104
97
case ':' :
105
- if state != IN_COMMENT {
98
+ if commentState != IN_COMMENT {
106
99
if state == IN_PROPERTY && ! bytes .Equal (nil , current ) {
107
100
state = IN_VALUE
108
101
currentPair .property = current
@@ -116,7 +109,7 @@ func Blocks(file string) []Block {
116
109
}
117
110
}
118
111
case ';' :
119
- if state != IN_COMMENT {
112
+ if commentState != IN_COMMENT {
120
113
if state == IN_VALUE {
121
114
state = IN_PROPERTY
122
115
currentPair .value = current
@@ -130,7 +123,7 @@ func Blocks(file string) []Block {
130
123
}
131
124
}
132
125
default :
133
- if state != IN_COMMENT {
126
+ if commentState != IN_COMMENT {
134
127
if state == 0 {
135
128
state = IN_SELECTOR
136
129
}
0 commit comments