css_parser 1.1.4 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -300,11 +300,13 @@ public
300
300
 
301
301
  # Looks for long format CSS background properties (e.g. <tt>background-color</tt>) and
302
302
  # converts them into a shorthand CSS <tt>background</tt> property.
303
+ #
304
+ # Leaves properties declared !important alone.
303
305
  def create_background_shorthand! # :nodoc:
304
306
  new_value = ''
305
307
  ['background-color', 'background-image', 'background-repeat',
306
308
  'background-position', 'background-attachment'].each do |property|
307
- if @declarations.has_key?(property)
309
+ if @declarations.has_key?(property) and not @declarations[property][:is_important]
308
310
  new_value += @declarations[property][:value] + ' '
309
311
  @declarations.delete(property)
310
312
  end
data/lib/css_parser.rb CHANGED
@@ -7,7 +7,7 @@ require 'stringio'
7
7
  require 'iconv'
8
8
 
9
9
  module CssParser
10
- VERSION = '1.1.4'
10
+ VERSION = '1.1.5'
11
11
 
12
12
  # Merge multiple CSS RuleSets by cascading according to the CSS 2.1 cascading rules
13
13
  # (http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order).
@@ -75,20 +75,28 @@ module CssParser
75
75
 
76
76
  rule_set.each_declaration do |property, value, is_important|
77
77
  # Add the property to the list to be folded per http://www.w3.org/TR/CSS21/cascade.html#cascading-order
78
- if not properties.has_key?(property) or
79
- is_important or # step 2
80
- properties[property][:specificity] < specificity or # step 3
81
- properties[property][:specificity] == specificity # step 4
82
- properties[property] = {:value => value, :specificity => specificity, :is_important => is_important}
78
+ if not properties.has_key?(property)
79
+ properties[property] = {:value => value, :specificity => specificity, :is_important => is_important}
80
+ elsif properties[property][:specificity] < specificity or properties[property][:specificity] == specificity
81
+ unless properties[property][:is_important]
82
+ properties[property] = {:value => value, :specificity => specificity, :is_important => is_important}
83
+ end
83
84
  end
85
+
86
+ if is_important
87
+ properties[property] = {:value => value, :specificity => specificity, :is_important => is_important}
88
+ end
84
89
  end
85
90
  end
86
91
 
87
92
  merged = RuleSet.new(nil, nil)
88
93
 
89
- # TODO: what about important
90
94
  properties.each do |property, details|
91
- merged[property.strip] = details[:value].strip
95
+ if details[:is_important]
96
+ merged[property.strip] = details[:value].strip.gsub(/\;\Z/, '') + '!important'
97
+ else
98
+ merged[property.strip] = details[:value].strip
99
+ end
92
100
  end
93
101
 
94
102
  merged.create_shorthand!
@@ -10,6 +10,7 @@ class CssParserBasicTests < Test::Unit::TestCase
10
10
  html, body, p { margin: 0px; }
11
11
  p { padding: 0px; }
12
12
  #content { font: 12px/normal sans-serif; }
13
+ .content { color: red; }
13
14
  EOT
14
15
  end
15
16
 
@@ -17,6 +18,8 @@ class CssParserBasicTests < Test::Unit::TestCase
17
18
  @cp.add_block!(@css)
18
19
  assert_equal 'margin: 0px;', @cp.find_by_selector('body').join(' ')
19
20
  assert_equal 'margin: 0px; padding: 0px;', @cp.find_by_selector('p').join(' ')
21
+ assert_equal 'font: 12px/normal sans-serif;', @cp.find_by_selector('#content').join(' ')
22
+ assert_equal 'color: red;', @cp.find_by_selector('.content').join(' ')
20
23
  end
21
24
 
22
25
  def test_adding_block
data/test/test_merging.rb CHANGED
@@ -85,4 +85,19 @@ class MergingTests < Test::Unit::TestCase
85
85
  merged = CssParser.merge(rs)
86
86
  assert_equal rs.object_id, merged.object_id
87
87
  end
88
+
89
+ def test_merging_important
90
+ rs1 = RuleSet.new(nil, 'color: black !important;')
91
+ rs2 = RuleSet.new(nil, 'color: red;')
92
+ merged = CssParser.merge(rs1, rs2)
93
+ assert_equal 'black !important;', merged['color']
94
+ end
95
+
96
+ def test_merging_shorthand_important
97
+ rs1 = RuleSet.new(nil, 'background: black none !important;')
98
+ rs2 = RuleSet.new(nil, 'background-color: red;')
99
+ merged = CssParser.merge(rs1, rs2)
100
+ assert_equal 'black !important;', merged['background-color']
101
+ end
102
+
88
103
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 1
8
- - 4
9
- version: 1.1.4
8
+ - 5
9
+ version: 1.1.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Alex Dunae
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-11 00:00:00 -08:00
17
+ date: 2011-01-21 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies: []
20
20