@@ -86,7 +86,7 @@ local utf8 = require((path.."utf8"):sub(2))
86
86
87
87
local F = string.format
88
88
89
- local css = {VERSION = " 0.1 .0" }
89
+ local css = {_VERSION = " 1.0 .0" }
90
90
91
91
92
92
@@ -1754,15 +1754,15 @@ function css.minimize(tokensIn, options)
1754
1754
end
1755
1755
1756
1756
elseif tokType == " semicolon" then
1757
+ cb (token , tokType , i )
1758
+
1757
1759
if isAt " rule" then
1758
1760
currentProperty = nil
1759
1761
colonsAfterProp = 0
1760
1762
end
1761
1763
1762
1764
currentAtKeyword = nil -- Possible end of @charset ""; or similar.
1763
1765
1764
- cb (token , tokType , i )
1765
-
1766
1766
elseif tokType == " atKeyword" then
1767
1767
token = cb (token , tokType , i ) or token
1768
1768
currentAtKeyword = token .value
@@ -1822,9 +1822,9 @@ function css.minimize(tokensIn, options)
1822
1822
1823
1823
local function printPeek (tokens , i , count )
1824
1824
print (" ----------------" )
1825
- for j = i , math.min (i + count , # tokens ) do
1825
+ for j = math.min ( i + count , i ), math.max (i + count , i ) do
1826
1826
if not tokens [j ] then break end
1827
- print (j - i , tokens [j ].type , tokens [j ].value )
1827
+ print (j - i , tokens [j ].type , tokens [j ].value or " " )
1828
1828
end
1829
1829
print (" ----------------" )
1830
1830
end
@@ -1998,7 +1998,7 @@ function css.minimize(tokensIn, options)
1998
1998
1999
1999
-- Note: It seems CSS4 will add #RRGGBBAA and #RGBA formats, so this code will probably have to be updated.
2000
2000
if not isAny (# tokOut .value , 3 ,6 ) then
2001
- print (" Warning: Color value looks incorrect: #" .. tokOut .value )
2001
+ print (" [css] Warning: Color value looks incorrect: #" .. tokOut .value )
2002
2002
end
2003
2003
2004
2004
return tokOut
@@ -2012,8 +2012,8 @@ function css.minimize(tokensIn, options)
2012
2012
local tokNext = getNextNonWsToken (tokensIn , i + 1 , 1 )
2013
2013
2014
2014
if
2015
- tokNext and tokNext .type ~= " }" and tokNext . type ~= " semicolon"
2016
- and not (tokPrev and tokPrev .type == " {" )
2015
+ tokNext and not isAny ( tokNext .type , " }" , " semicolon" )
2016
+ and not (tokPrev and isAny ( tokPrev .type , " {" , " } " ) )
2017
2017
then
2018
2018
add (tokIn )
2019
2019
end
@@ -2072,6 +2072,61 @@ function css.minimize(tokensIn, options)
2072
2072
end
2073
2073
end )
2074
2074
2075
+ -- Remove empty rules.
2076
+ ---- ----------------------------
2077
+ local ruleBeginnings = {}
2078
+ local ruleDepth = 0
2079
+ local lastRuleStart = 0
2080
+ local isInSomethingInFileScope = false
2081
+
2082
+ tokensIn = tokensOut
2083
+ tokensOut = {}
2084
+
2085
+ eachToken (tokensIn , nil , nil , nil , function (tokIn , tokType , i )
2086
+ add (tokIn )
2087
+
2088
+ if not isInSomethingInFileScope and tokType ~= " comment" then
2089
+ lastRuleStart = # tokensOut
2090
+ isInSomethingInFileScope = true
2091
+
2092
+ elseif tokType == " {" then
2093
+ table.insert (ruleBeginnings , lastRuleStart )
2094
+ ruleDepth = ruleDepth + 1
2095
+
2096
+ lastRuleStart = # tokensOut + 1
2097
+
2098
+ elseif tokType == " }" then
2099
+ local ruleStart = table.remove (ruleBeginnings )
2100
+ ruleDepth = ruleDepth - 1
2101
+
2102
+ if not ruleStart then
2103
+ error (" [css] Uneven curly brackets." )
2104
+ end
2105
+
2106
+ local tokPrev = getNextToken (tokensOut , # tokensOut - 1 , - 1 )
2107
+ if tokPrev .type == " {" then
2108
+ for j = # tokensOut , ruleStart , - 1 do
2109
+ table.remove (tokensOut , j )
2110
+ end
2111
+ end
2112
+
2113
+ lastRuleStart = # tokensOut + 1
2114
+ if ruleDepth == 0 then
2115
+ isInSomethingInFileScope = false
2116
+ end
2117
+
2118
+ elseif tokType == " semicolon" and currentAtKeyword then
2119
+ lastRuleStart = # tokensOut + 1
2120
+ if ruleDepth == 0 then
2121
+ isInSomethingInFileScope = false
2122
+ end
2123
+ end
2124
+ end )
2125
+
2126
+ if tokensOut [1 ] and tokensOut [# tokensOut ].type == " semicolon" then
2127
+ table.remove (tokensOut )
2128
+ end
2129
+
2075
2130
-- Minimize specific properties.
2076
2131
---- ----------------------------
2077
2132
@@ -2136,7 +2191,14 @@ function css.minimize(tokensIn, options)
2136
2191
table.insert (tokensOut , i + 1 , newTokenWhitespace (" " ))
2137
2192
end
2138
2193
2139
- nextTokenIndex = i
2194
+ -- We also don't need any space before anymore.
2195
+ if tokensOut [i - 1 ] and tokensOut [i - 1 ].type == " whitespace" then
2196
+ table.remove (tokensOut , i - 1 )
2197
+ nextTokenIndex = i - 1
2198
+ else
2199
+ nextTokenIndex = i
2200
+ end
2201
+
2140
2202
return
2141
2203
end
2142
2204
0 commit comments