Skip to content

Commit 47f55d8

Browse files
author
Richard Hulse
committed
Rename project to ruby-css-toolkit
1 parent ba61d0a commit 47f55d8

File tree

6 files changed

+80
-129
lines changed

6 files changed

+80
-129
lines changed

bin/css-compressor renamed to bin/css-toolkit

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
require 'getoptlong'
55

6-
require File.join(File.dirname(__FILE__), *%w[.. lib css_compressor])
6+
require File.join(File.dirname(__FILE__), *%w[.. lib css_toolkit])
77

88
# options
99

@@ -18,7 +18,7 @@ $outfile = nil
1818
# Explain to the user how to use css compressor
1919

2020
def usage
21-
puts ' CSS compressor |'
21+
puts ' CSS Optimizer |'
2222
puts '----------------------------|'
2323
puts ''
2424
puts '--help, -h Display this help'

lib/css_toolkit.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'modules/yui_compressor'
2+
3+
module CssToolkit
4+
include YuiCompressor
5+
6+
def yui_compressor(css, line_length)
7+
yui = Yui.new()
8+
yui.compress(css, line_length)
9+
end
10+
11+
end

lib/modules/yui_compressor.rb

Lines changed: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
# This is a Ruby port of the YUI CSS compressor
22
# See LICENSE for license information
33

4-
module CssCompressor
4+
module YuiCompressor
55
# Compress CSS rules using a variety of techniques
66

7-
class CSS
7+
class Yui
88
attr_reader :input_size, :output_size
9-
def initialize(options = {})
10-
@options = {
11-
:use_tidy => true, # use the extra CSS optimisations
12-
:tidy_test => false, # used during unit tests only
13-
}.merge(options)
9+
def initialize()
1410
@preservedTokens = []
1511
@comments = []
1612
@input_size = 0
1713
@output_size = 0
1814
end
1915

20-
def compress(css)
16+
def compress(css, line_length=0)
2117
@input_size = css.length
2218

2319
css = process_comments_and_strings(css)
@@ -88,11 +84,8 @@ def compress(css)
8884
# See SF bug #1980989
8985
css.gsub!(/[;]+/, ';');
9086

91-
# this does some extra smart cleaning of rules to remove redundancy
92-
css = tidy_css(css) if @options[:use_tidy]
93-
9487
#restore preserved comments and strings
95-
css = restore_preserved_comments_and_strings(css) unless @options[:tidy_test]
88+
css = restore_preserved_comments_and_strings(css)
9689

9790
# top and tail whitespace
9891
css.strip!
@@ -210,73 +203,6 @@ def restore_preserved_comments_and_strings(clean_css)
210203

211204
css
212205
end
213-
214-
def tidy_css(clean_css)
215-
css = clean_css.clone
216-
217-
# color swaps
218-
swaps = {
219-
'white' => '#fff',
220-
'black' => '#000',
221-
'fuchsia' => '#f0f',
222-
'yellow' => '#ff0',
223-
'#f00' => 'red',
224-
'#800000' => 'maroon',
225-
'#ffa500' => 'orange',
226-
'#808000' => 'olive',
227-
'#800080' => 'purple',
228-
'#008000' => 'green',
229-
'#000080' => 'navy',
230-
'#008080' => 'teal',
231-
'#c0c0c0' => 'silver',
232-
'#808080' => 'gray',
233-
}
234-
235-
swaps.each do |from, to|
236-
css.gsub!(/:#{from}(;|\})/, ":#{to}\\1")
237-
end
238-
239-
css = split_lines(css)
240-
css = restore_lines(css)
241-
242-
css
243-
end
244-
245-
def split_lines(clean_css)
246-
css = clean_css.clone
247-
248-
startIndex = 0
249-
endIndex = 0
250-
totallen = css.length
251-
252-
css.gsub!("@", "\n@")
253-
254-
# split @ declarations like @import onto their own lines
255-
while (startIndex = css.index("@", startIndex))
256-
endIndex = css.index(/;|\{/, startIndex)
257-
258-
unless endIndex
259-
endIndex = totallen
260-
end
261-
css = css.slice(0..endIndex).to_s + "\n" + css.slice(endIndex+1, totallen).to_s
262-
startIndex = endIndex
263-
totallen += 1 # to allow for the extra \n
264-
end
265-
css.gsub!("}", "}\n")
266-
css.gsub!("{", "\n{")
267-
css.gsub!(/\n+/, "\n")
268-
css.gsub!("*/", "*/\n")
269-
css.strip!
270-
271-
css
272-
end
273-
274-
def restore_lines(clean_css)
275-
css = clean_css.clone
276-
css.gsub!("\n", '')
277-
278-
css
279-
end
280206
end
281207

282208
end

test/css_compressor_test.rb

Lines changed: 0 additions & 47 deletions
This file was deleted.

test/test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../lib/'))
22
require 'rubygems'
33
require 'test/unit'
4-
require 'css_compressor'
4+
require 'css_toolkit'

test/yui_compressor_test.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require File.dirname(__FILE__) + '/test_helper'
2+
3+
# Test cases for preping a file for parsing
4+
class YuiCompressorTest < Test::Unit::TestCase
5+
include CssToolkit
6+
7+
def setup
8+
@yui = Yui.new()
9+
10+
# build our tests based on the available files
11+
# test_files = Dir.glob(File.join(File.dirname(__FILE__), 'yuicss/*.css'))
12+
# test_files.each do |file|
13+
# base_name = File.basename(file, ".css")
14+
# class self <<
15+
# def test_yui_#{basename}
16+
# test_css = File.read(file)
17+
# expected_css = File.read(file + '.min')
18+
# assert_equal(expected_css, @yui.compress(test_css))
19+
# end
20+
#
21+
# end
22+
23+
end
24+
25+
# basic test
26+
def test_pass_through
27+
assert_equal('body{margin:5px}', @yui.compress('body{margin:5px}'))
28+
end
29+
30+
def test_whitespace_reduction
31+
assert_equal('a b', @yui.compress(' a b '))
32+
end
33+
34+
def test_whitespace_reduction_with_tab
35+
assert_equal('a b', @yui.compress(" a \t b "))
36+
end
37+
38+
def test_whitespace_and_unix_newline
39+
assert_equal('a b', @yui.compress(" a\n b"))
40+
end
41+
42+
def test_whitespace_and_windows_newline
43+
assert_equal('a b', @yui.compress(" a \r\nb "))
44+
end
45+
46+
# test all the files in the yui directory
47+
def test_yui_css
48+
test_files = Dir.glob(File.join(File.dirname(__FILE__), 'yuicss/*.css'))
49+
test_files.each do |file|
50+
test_css = File.read(file)
51+
expected_css = File.read(file + '.min')
52+
53+
test_name = File.basename(file, ".css")
54+
assert_block "Couldn't do the thing - #{test_name}" do
55+
expected_css == @yui.compress(test_css) # do_the_thing
56+
end
57+
# assert_equal(), "'yui-#{test_name}' failed" )
58+
end
59+
end
60+
61+
end

0 commit comments

Comments
 (0)