diff --git a/lib/csscss/cli.rb b/lib/csscss/cli.rb index 8f387b8..a95bce9 100644 --- a/lib/csscss/cli.rb +++ b/lib/csscss/cli.rb @@ -8,6 +8,7 @@ def initialize(argv) @compass = false @ignored_properties = [] @ignored_selectors = [] + @use_shorthand = false end def run @@ -45,7 +46,8 @@ def execute RedundancyAnalyzer.new(contents).redundancies( minimum: @minimum, ignored_properties: @ignored_properties, - ignored_selectors: @ignored_selectors + ignored_selectors: @ignored_selectors, + use_shorthand: @use_shorthand ) end @@ -102,6 +104,10 @@ def parse(argv) @ignored_selectors = ignored_selectors end + opts.on('--use-shorthand', "Don't parse shorthand notation") do + @use_shorthand = true + end + opts.on("-V", "--version", "Show version") do |v| puts opts.ver exit diff --git a/lib/csscss/redundancy_analyzer.rb b/lib/csscss/redundancy_analyzer.rb index 04a1109..b27853d 100644 --- a/lib/csscss/redundancy_analyzer.rb +++ b/lib/csscss/redundancy_analyzer.rb @@ -9,6 +9,7 @@ def redundancies(opts = {}) minimum = opts[:minimum] ignored_properties = opts[:ignored_properties] || [] ignored_selectors = opts[:ignored_selectors] || [] + use_shorthand = opts[:use_shorthand] || false rule_sets = Parser::Css.parse(@raw_css) matches = {} @@ -20,7 +21,11 @@ def redundancies(opts = {}) rule_set.declarations.each do |dec| next if ignored_properties.include?(dec.property) - if parser = shorthand_parser(dec.property) + if !use_shorthand + parser = shorthand_parser(dec.property) + end + + if parser if new_decs = parser.parse(dec.property, dec.value) if dec.property == "border" %w(border-top border-right border-bottom border-left).each do |property| diff --git a/test/csscss/redundancy_analyzer_test.rb b/test/csscss/redundancy_analyzer_test.rb index efc8bfd..e63d720 100644 --- a/test/csscss/redundancy_analyzer_test.rb +++ b/test/csscss/redundancy_analyzer_test.rb @@ -266,6 +266,16 @@ module Csscss }) end + it "use shorthand" do + css = %$ + .foo { border: 1px solid #000; color: #000; padding: 10px 0; } + .bar { border: 1px solid #fff; color: #000; padding-top: 10px; } + $ + + redundancies = RedundancyAnalyzer.new(css).redundancies(use_shorthand:true) + redundancies[[sel(".bar"), sel(".foo")]].size.must_equal(1) + end + # TODO: someday # it "reports duplication within the same selector" do # css = %$