1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
# coding: utf-8
require "spec_helper"
describe "CSSminify" do
context "application" do
it "minifies CSS" do
source = File.open(File.expand_path("../sample.css", __FILE__), "r:UTF-8").read
minified = CSSminify.compress(source)
minified.length.should < source.length
lambda {
CSSminify.compress(minified)
}.should_not raise_error
end
it "honors the specified maximum line length" do
source = <<-EOS
.classname1 {
border: none;
background: none;
outline: none;
}
.classname2 {
border: none;
background: none;
outline: none;
}
EOS
minified = CSSminify.compress(source, 30)
minified.split("\n").length.should eq(2)
minified.should eq(".classname1{border:0;background:0;outline:0}\n.classname2{border:0;background:0;outline:0}")
end
it "handles strings as input format" do
lambda {
CSSminify.compress(File.open(File.expand_path("../sample.css", __FILE__), "r:UTF-8").read).should_not be_empty
}.should_not raise_error
end
it "handles files as input format" do
lambda {
CSSminify.compress(File.open(File.expand_path("../sample.css", __FILE__), "r:UTF-8")).should_not be_empty
}.should_not raise_error
end
it "works as both class and class instance" do
lambda {
CSSminify.compress(File.open(File.expand_path("../sample.css", __FILE__), "r:UTF-8").read).should_not be_empty
CSSminify.new.compress(File.open(File.expand_path("../sample.css", __FILE__), "r:UTF-8").read).should_not be_empty
}.should_not raise_error
end
end
context "compression" do
it "removes comments and white space" do
source = <<-EOS
/*****
Multi-line comment
before a new class name
*****/
.classname {
/* comment in declaration block */
font-weight: normal;
}
EOS
CSSminify.compress(source).should eq('.classname{font-weight:normal}')
end
it "preserves special comments" do
source = <<-EOS
/*!
(c) Very Important Comment
*/
.classname {
/* comment in declaration block */
font-weight: normal;
}
EOS
result = <<-EOS
/*!
(c) Very Important Comment
*/.classname{font-weight:normal}
EOS
(CSSminify.compress(source) + "\n").should eq(result)
end
it "removes last semi-colon in a block" do
source = <<-EOS
.classname {
border-top: 1px;
border-bottom: 2px;
}
EOS
CSSminify.compress(source).should eq('.classname{border-top:1px;border-bottom:2px}')
end
it "removes extra semi-colons" do
source = <<-EOS
.classname {
border-top: 1px; ;
border-bottom: 2px;;;
}
EOS
CSSminify.compress(source).should eq('.classname{border-top:1px;border-bottom:2px}')
end
it "removes empty declarations" do
source = <<-EOS
.empty { ;}
.nonempty {border: 0;}
EOS
CSSminify.compress(source).should eq('.nonempty{border:0}')
end
it "simplifies zero values" do
source = <<-EOS
a {
margin: 0px 0pt 0em 0%;
background-position: 0 0ex;
padding: 0in 0cm 0mm 0pc
}
EOS
CSSminify.compress(source).should eq('a{margin:0;background-position:0 0;padding:0}')
end
it "removes leading zeros from floats" do
source = <<-EOS
.classname {
margin: 0.6px 0.333pt 1.2em 8.8cm;
}
EOS
CSSminify.compress(source).should eq('.classname{margin:.6px .333pt 1.2em 8.8cm}')
end
it "simplifies color values but preserves filter properties, RGBa values and ID strings" do
source = <<-EOS
.color-me {
color: rgb(123, 123, 123);
border-color: #ffeedd;
background: none repeat scroll 0 0 rgb(255, 0,0);
}
EOS
CSSminify.compress(source).should eq('.color-me{color:#7b7b7b;border-color:#fed;background:none repeat scroll 0 0 #f00}')
source = <<-EOS
#AABBCC {
color: rgba(1, 2, 3, 4);
filter: chroma(color="#FFFFFF");
}
EOS
CSSminify.compress(source).should eq('#AABBCC{color:rgba(1,2,3,4);filter:chroma(color="#FFFFFF")}')
end
it "only keeps the first charset declaration" do
source = <<-EOS
@charset "utf-8";
#foo {
border-width: 1px;
}
/* second css, merged */
@charset "another one";
#bar {
border-width: 10px;
}
EOS
CSSminify.compress(source).should eq('@charset "utf-8";#foo{border-width:1px}#bar{border-width:10px}')
end
it "simplifies the IE opacity filter syntax" do
source = <<-EOS
.classname {
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=80)"; /* IE 8 */
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=80); /* IE < 8 */
}
EOS
CSSminify.compress(source).should eq('.classname{-ms-filter:"alpha(opacity=80)";filter:alpha(opacity=80)}')
end
it "replaces 'none' values with 0 where allowed" do
source = <<-EOS
.classname {
border: none;
background: none;
outline: none;
}
EOS
CSSminify.compress(source).should eq('.classname{border:0;background:0;outline:0}')
end
it "tolerates underscore/star hacks" do
source = <<-EOS
#element {
width: 1px;
*width: 2px;
_width: 3px;
}
EOS
CSSminify.compress(source).should eq('#element{width:1px;*width:2px;_width:3px}')
end
it "tolerates child selector hacks" do
source = <<-EOS
html >/**/ body p {
color: blue;
}
EOS
CSSminify.compress(source).should eq('html>/**/body p{color:blue}')
end
it "tolerates IE5/Mac hacks" do
source = <<-EOS
/* Ignore the next rule in IE mac \\*/
.selector {
color: khaki;
}
/* Stop ignoring in IE mac */
EOS
CSSminify.compress(source).should eq('/*\*/.selector{color:khaki}/**/')
end
it "tolerates box model hacks" do
source = <<-EOS
#elem {
width: 100px; /* IE */
voice-family: "\\"}\\"";
voice-family:inherit;
width: 200px; /* others */
}
html>body #elem {
width: 200px; /* others */
}
EOS
CSSminify.compress(source).should eq('#elem{width:100px;voice-family:"\"}\"";voice-family:inherit;width:200px}html>body #elem{width:200px}')
end
it "should pass all the original tests included in the YUI compressor package" do
puts "Now running original YUI compressor test files:"
files = Dir.glob(File.join(File.dirname(__FILE__), 'tests', '*.css'))
for file in files do
print " -- testing #{file} ..."
CSSminify.compress(File.read(file)).chomp.strip.should eq(File.read(file + '.min').chomp.strip)
print "successful\n"
end
end
end
end
|