Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.
8 changes: 7 additions & 1 deletion lib/csscss/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def initialize(argv)
@compass = false
@ignored_properties = []
@ignored_selectors = []
@use_shorthand = false
end

def run
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/csscss/redundancy_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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|
Expand Down
10 changes: 10 additions & 0 deletions test/csscss/redundancy_analyzer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = %$
Expand Down