Class: CssTidy::Declaration
- Defined in:
- lib/modules/css_declaration.rb
Instance Attribute Summary collapse
-
#property ⇒ Object
Returns the value of attribute property.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #clear ⇒ Object
- #downcase_property ⇒ Object
- #fix_invalid_colors ⇒ Object
-
#important! ⇒ Object
remove !important and return true if it was replaced.
- #important? ⇒ Boolean
-
#initialize(property, value) ⇒ Declaration
constructor
sets the values we assume that any extraneous whitespace has been striped.
- #inspect(indent = '') ⇒ Object
- #optimize_colors ⇒ Object
- #optimize_filters ⇒ Object
- #optimize_font_weight ⇒ Object
- #optimize_mp_shorthands ⇒ Object
- #optimize_punctuation ⇒ Object
- #optimize_urls ⇒ Object
- #optimize_zeros ⇒ Object
- #to_s(format = :one_line) ⇒ Object
Methods inherited from CssBase
Constructor Details
#initialize(property, value) ⇒ Declaration
sets the values we assume that any extraneous whitespace has been striped
8 9 10 11 12 |
# File 'lib/modules/css_declaration.rb', line 8 def initialize(property, value) @property = property @value = value self end |
Instance Attribute Details
#property ⇒ Object
Returns the value of attribute property.
4 5 6 |
# File 'lib/modules/css_declaration.rb', line 4 def property @property end |
#value ⇒ Object
Returns the value of attribute value.
4 5 6 |
# File 'lib/modules/css_declaration.rb', line 4 def value @value end |
Instance Method Details
#==(other) ⇒ Object
126 127 128 |
# File 'lib/modules/css_declaration.rb', line 126 def == (other) @property == other.property && @value == other.value end |
#clear ⇒ Object
143 144 145 146 |
# File 'lib/modules/css_declaration.rb', line 143 def clear @property = '' @value = '' end |
#downcase_property ⇒ Object
122 123 124 |
# File 'lib/modules/css_declaration.rb', line 122 def downcase_property @property.downcase! unless @property.frozen? end |
#fix_invalid_colors ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/modules/css_declaration.rb', line 43 def fix_invalid_colors if @value !~ /["'=]/ CssTidy::INVALID_COLORS.each do |from,to| @value.gsub!(/#{from}/i, to) end end end |
#important! ⇒ Object
remove !important and return true if it was replaced
135 136 137 |
# File 'lib/modules/css_declaration.rb', line 135 def important! @value.gsub!(/!important/, '') end |
#important? ⇒ Boolean
139 140 141 |
# File 'lib/modules/css_declaration.rb', line 139 def important? @value =~ /!important/ end |
#inspect(indent = '') ⇒ Object
130 131 132 |
# File 'lib/modules/css_declaration.rb', line 130 def inspect(indent='') puts indent + @property + ':' + @value end |
#optimize_colors ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/modules/css_declaration.rb', line 22 def optimize_colors # Shorten colors from rgb(51,102,153) to #336699 # This makes it more likely that it'll get further compressed in the next step. @value.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. if @value !~ /["'=]/ @value.gsub!(/#([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3/i, '#\1\2\3') CssTidy::SHORTEN_COLORS.each do |from,to| @value.gsub!(/#{from}/i, to) end end end |
#optimize_filters ⇒ Object
65 66 67 68 |
# File 'lib/modules/css_declaration.rb', line 65 def optimize_filters # shorter opacity IE filter @value.gsub!(/progid:DXImageTransform\.Microsoft\.Alpha\(Opacity=/i, "alpha(opacity=") end |
#optimize_font_weight ⇒ Object
75 76 77 78 79 80 |
# File 'lib/modules/css_declaration.rb', line 75 def optimize_font_weight if @property =~ /font/ @value.gsub!(/bold/, '700') @value.gsub!(/normal/, '400') end end |
#optimize_mp_shorthands ⇒ Object
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 |
# File 'lib/modules/css_declaration.rb', line 82 def optimize_mp_shorthands if SHORTHANDS.has_key?(@property) important = important! ? '!important' : '' values = @value.split(/\s+/) values case values.length when 4 # 4 to 1 margin:5px 5px 5px 5px => margin:5px if values[0] == values[1] && values[0] == values[2] && values[0] == values[3] @value = "#{values[0]}" # 4 to 3 margin:0 0 10px 0 => margin:0 0 10px elsif values[0] == values[2] && values[1] == values[3] @value = "#{values[0]} #{values[1]}" # 4 to 2 margin:5px 0 5px 0 => margin:5px 0 elsif values[1] == values[3] @value = "#{values[0]} #{values[1]} #{values[2]}" end when 3 # 3 to 1 margin:5px 5px 5px => margin:5px if values[0] == values[1] && values[0] == values[2] @value = "#{values[0]}" # 3 to 2 margin:0 10px 0 => margin:0 10px elsif values[0] == values[2] @value = "#{values[0]} #{values[1]}" end when 2 # 2 to 1 margin:5px 5px => margin:5px if values[0] == values[1] @value = "#{values[0]}" end end @value << important end end |
#optimize_punctuation ⇒ Object
70 71 72 73 |
# File 'lib/modules/css_declaration.rb', line 70 def optimize_punctuation @value.gsub!(/\s+([!+\(\)\],])/, '\1') @value.gsub!(/([!+\(\[,])\s+/, '\1') end |
#optimize_urls ⇒ Object
117 118 119 120 |
# File 'lib/modules/css_declaration.rb', line 117 def optimize_urls # remove the quotes - they are optional @value.gsub!(/url\(('|")(.+?)('|")\)/, 'url(\2)') end |
#optimize_zeros ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/modules/css_declaration.rb', line 51 def optimize_zeros # Replace 0(%, em, ex, px, in, cm, mm, pt, pc) with just 0. @value.gsub!(/(\s|\A)([+-]?0)(?:%|em|ex|px|in|cm|mm|pt|pc)/i, '\1\2') if @property =~ /background(-position)?/i @value.gsub!(/(0 )+0/, '0 0') else # Replace 0 0 0 0; with 0. @value.gsub!(/\A(?:0 )+0\Z/, '0') end # Replace 0.6 with .6, but only when it is the first rule or preceded by a space. @value.gsub!(/(\s|\A)0+\.(\d+)/, '\1.\2') end |
#to_s(format = :one_line) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/modules/css_declaration.rb', line 14 def to_s(format=:one_line) unless @property.empty? && @value.empty? "#{@property}:#{@value}" else '' end end |