Class: YUI::CSS
- Inherits:
-
Object
- Object
- YUI::CSS
- Defined in:
- lib/yui/css_compressor.rb
Overview
Methods for working with CSS.
Class Attribute Summary collapse
-
.input_size ⇒ Object
readonly
Returns the value of attribute input_size.
-
.output_size ⇒ Object
readonly
Returns the value of attribute output_size.
Class Method Summary collapse
-
.compress(css, line_length = 0) ⇒ String
Compress CSS using the Ruby port of the YUI Compressor (by <a href=“github.com/rhulse”>Richard Hulse</a>).
Class Attribute Details
.input_size ⇒ Object (readonly)
Returns the value of attribute input_size.
51 52 53 |
# File 'lib/yui/css_compressor.rb', line 51 def input_size @input_size end |
.output_size ⇒ Object (readonly)
Returns the value of attribute output_size.
51 52 53 |
# File 'lib/yui/css_compressor.rb', line 51 def output_size @output_size end |
Class Method Details
.compress(css, line_length = 0) ⇒ String
Compress CSS using the Ruby port of the YUI Compressor (by <a href=“github.com/rhulse”>Richard Hulse</a>).
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 |
# File 'lib/yui/css_compressor.rb', line 62 def compress(css, line_length=0) @preservedTokens = [] @comments = [] @output_size = 0 @input_size = css.length css = process_comments_and_strings(css) # Normalize all whitespace strings to single spaces. Easier to work with that way. css.gsub!(/\s+/, ' ') # Remove the spaces before the things that should not have spaces before them. # But, be careful not to turn "p :link {...}" into "p:link{...}" # Swap out any pseudo-class colons with the token, and then swap back. css.gsub!(/(?:^|\})[^\{:]+\s+:+[^\{]*\{/) do |match| match.gsub(':', '___PSEUDOCLASSCOLON___') end css.gsub!(/\s+([!\{\};:>+\(\)\],])/, '\1') css.gsub!(/([!\{\}:;>+\(\[,])\s+/, '\1') css.gsub!('___PSEUDOCLASSCOLON___', ':') # special case for IE css.gsub!(/:first-(line|letter)(\{|,)/, ':first-\1 \2') # no space after the end of a preserved comment css.gsub!(/\*\/ /, '*/') # If there is a @charset, then only allow one, and push to the top of the file. css.gsub!(/^(.*)(@charset "[^"]*";)/i, '\2\1') css.gsub!(/^(\s*@charset [^;]+;\s*)+/i, '\1') # Put the space back in some cases, to support stuff like # @media screen and (-webkit-min-device-pixel-ratio:0){ css.gsub!(/\band\(/i, "and (") # remove unnecessary semicolons css.gsub!(/;+\}/, '}') # Replace 0(%, em, ex, px, in, cm, mm, pt, pc) with just 0. css.gsub!(/([\s:])([+-]?0)(?:%|em|ex|px|in|cm|mm|pt|pc)/i, '\1\2') # Replace 0 0 0 0; with 0. css.gsub!(/:(?:0 )+0(;|\})/, ':0\1') # Restore background-position:0 0; if required css.gsub!(/background-position:0(;|\})/i, 'background-position:0 0\1') # Replace 0.6 with .6, but only when preceded by : or a space. css.gsub!(/(:|\s)0+\.(\d+)/, '\1.\2') # Shorten colors from rgb(51,102,153) to #336699 # This makes it more likely that it'll get further compressed in the next step. css.gsub!(/rgb\s*\(\s*([0-9,\s]+)\s*\)/) do |match| '#' << $1.scan(/\d+/).map{|n| n.to_i.to_s(16).rjust(2, '0') }.join end # Shorten colors from #AABBCC to #ABC. Note that we want to make sure # the color is not preceded by either ", " or =. Indeed, the property # filter: chroma(color="#FFFFFF"); # would become # filter: chroma(color="#FFF"); # which makes the filter break in IE. css.gsub!(/([^"'=\s])(\s?)\s*#([0-9a-f])\3([0-9a-f])\4([0-9a-f])\5/i, '\1\2#\3\4\5') # shorter opacity IE filter css.gsub!(/progid:DXImageTransform\.Microsoft\.Alpha\(Opacity=/i, "alpha(opacity=") # Remove empty rules. css.gsub!(/[^\};\{\/]+\{\}/, '') if (line_length > 0) # 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 end # Replace multiple semi-colons in a row by a single one # See SF bug #1980989 css.gsub!(/[;]+/, ';') #restore preserved comments and strings css = restore_preserved_comments_and_strings(css) # top and tail whitespace css.strip! @output_size = css.length css end |