Class: CssTidy::Tidy

Inherits:
Object
  • Object
show all
Defined in:
lib/modules/css_tidy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTidy

Returns a new instance of Tidy.



7
8
9
# File 'lib/modules/css_tidy.rb', line 7

def initialize
	@parser = Parser.new
end

Instance Attribute Details

#input_sizeObject (readonly)

Returns the value of attribute input_size.



5
6
7
# File 'lib/modules/css_tidy.rb', line 5

def input_size
  @input_size
end

#output_sizeObject (readonly)

Returns the value of attribute output_size.



5
6
7
# File 'lib/modules/css_tidy.rb', line 5

def output_size
  @output_size
end

Instance Method Details

#optimize(options) ⇒ Object



55
56
57
# File 'lib/modules/css_tidy.rb', line 55

def optimize(options)
	@stylesheet.optimize(options)
end

#split_lines(compressed_css, line_length) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/modules/css_tidy.rb', line 59

def split_lines(compressed_css, line_length)
	css = compressed_css.clone
    # Some source control tools don't like it when files containing lines longer
    # than, say 8000 characters, are checked in. The linebreak option is used in
    # that case to split long lines after a specific column.
    startIndex = 0
    index = 0
	length = css.length
    while (index < length)
      index += 1
      if (css[index - 1,1] === '}' && index - startIndex > line_length)
        css = css.slice(0, index) + "\n" + css.slice(index, length)
        startIndex = index
      end
    end
	css
end

#tidy(css, opts = {}) ⇒ Object



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
# File 'lib/modules/css_tidy.rb', line 11

def tidy(css, opts={})
	@input_size = css.length
	options = {
		:downcase_selectors 				=> true,
		:downcase_properties 				=> true,
		:keep_special_comments 			=> true, # comments that start with a !
		:keep_ie5_comment_hack			=> true, # ie5 comment hack => /*\*/ stuff /**/
		:keep_ie7_selector_comments	=> true, # empty comments in selectors => /**/ used for IE7 hack
		:keep_selector_comments			=> false, # other comments in selectors
		:keep_comments							=> false, # any other comments
		:optimize_colors						=> true,
		:fix_invalid_colors					=> false,
		:optimize_decimals					=> true,
		:optimize_zeros							=> true,
		:optimize_font_weight				=> true,
		:optimize_margin_padding		=> true,
		:optimize_filters						=> true,
		:optimize_urls							=> true,
		:optimize_selectors					=> false,
		:format											=> 0,
		:line_length								=> 0,
	}.merge(opts)

	@stylesheet = @parser.parse(css)
	optimize(options)

	case options[:format]
	when 1
		format = :multi_line
	else
		format = :one_line
	end

	compressed_css = @stylesheet.to_s(format)

   if (options[:line_length] > 0 && format == :one_line)
		compressed_css = split_lines(compressed_css, options[:line_length])
	end

	@output_size = compressed_css.length

	compressed_css
end