diff --git a/CHANGELOG.md b/CHANGELOG.md index 572ebbc..eec398b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased ## + +* Fixes bug where 0 and 0px are not reconciled as redundancies + ## 1.1.0 - 4/12/2013 ## * Fixes bug where CLI --no-color wasn't respected diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index f877ddf..9564593 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -2,3 +2,4 @@ * Carson McDonald @carsonmcdonald * Martin Kuckert @MKuckert * Ivan Lazarevic @kopipejst +* Matt DuVall @mduvall diff --git a/lib/csscss/parser/common.rb b/lib/csscss/parser/common.rb index 4a3d965..fe960c3 100644 --- a/lib/csscss/parser/common.rb +++ b/lib/csscss/parser/common.rb @@ -3,19 +3,21 @@ module Parser module Common include Parslet - UNITS = %w(px em ex in cm mm pt pc) + UNITS = %w(px em ex in cm mm pt pc %) - rule(:space) { match['\s'].repeat(1) } - rule(:space?) { space.maybe } - rule(:number) { match["0-9"] } - rule(:numbers) { number.repeat(1) } - rule(:decimal) { numbers >> str(".").maybe >> numbers.maybe } - rule(:percent) { decimal >> stri("%") >> space? } - rule(:length) { decimal >> stri_list(UNITS) >> space? } - rule(:identifier) { match["a-zA-Z"].repeat(1) } - rule(:inherit) { stri("inherit") } - rule(:eof) { any.absent? } - rule(:nada) { any.repeat.as(:nada) } + rule(:space) { match['\s'].repeat(1) } + rule(:space?) { space.maybe } + rule(:number) { match["0-9"] } + rule(:numbers) { number.repeat(1) } + rule(:decimal) { numbers >> str(".").maybe >> numbers.maybe } + rule(:percent) { decimal >> stri("%") >> space? } + rule(:non_zero_length) { decimal >> stri_list(UNITS) >> space? } + rule(:zero_length) { match["0"] } + rule(:length) { zero_length | non_zero_length } + rule(:identifier) { match["a-zA-Z"].repeat(1) } + rule(:inherit) { stri("inherit") } + rule(:eof) { any.absent? } + rule(:nada) { any.repeat.as(:nada) } rule(:http) { (match['a-zA-Z0-9.:/\-'] | str('\(') | str('\)')).repeat >> space? diff --git a/lib/csscss/redundancy_analyzer.rb b/lib/csscss/redundancy_analyzer.rb index fd0773c..be9de91 100644 --- a/lib/csscss/redundancy_analyzer.rb +++ b/lib/csscss/redundancy_analyzer.rb @@ -65,7 +65,6 @@ def redundancies(opts = {}) end end - # trims any derivative declarations alongside shorthand inverted_matches.each do |selectors, declarations| redundant_derivatives = declarations.select do |dec| diff --git a/lib/csscss/types.rb b/lib/csscss/types.rb index 0cbcd74..0d00367 100644 --- a/lib/csscss/types.rb +++ b/lib/csscss/types.rb @@ -36,8 +36,17 @@ def ==(other) end end + def normalized_value + zero_units = Csscss::Parser::Common::UNITS.map {|u| '0' + u} + if zero_units.include? value + "0" + else + value + end + end + def hash - [property, value].hash + [property, normalized_value].hash end def eql?(other) diff --git a/test/csscss/parser/common_test.rb b/test/csscss/parser/common_test.rb index 75fbde6..06486b8 100644 --- a/test/csscss/parser/common_test.rb +++ b/test/csscss/parser/common_test.rb @@ -106,6 +106,7 @@ class CommonTest @parser.length.must_parse "123px" @parser.length.must_parse "123EM" @parser.length.must_parse "1.23Pt" + @parser.length.must_parse "0" end end diff --git a/test/csscss/redundancy_analyzer_test.rb b/test/csscss/redundancy_analyzer_test.rb index 7de8b88..8f72e25 100644 --- a/test/csscss/redundancy_analyzer_test.rb +++ b/test/csscss/redundancy_analyzer_test.rb @@ -275,6 +275,18 @@ module Csscss }) end + it "matches 0 and 0px" do + css = %$ + .bar { padding: 0; } + .foo { padding: 0px; } + $ + + RedundancyAnalyzer.new(css).redundancies.must_equal({ + [sel(".bar"), sel(".foo")] => [dec("padding", "0")] + }) + end + + # TODO: someday # it "reports duplication within the same selector" do # css = %$ diff --git a/test/csscss/types_test.rb b/test/csscss/types_test.rb index de80520..a443bb8 100644 --- a/test/csscss/types_test.rb +++ b/test/csscss/types_test.rb @@ -69,5 +69,17 @@ module Csscss h[dec2].must_equal true h[dec3].must_equal true end + + it "stores values of normalized zeros for length units" do + dec1 = Declaration.new("padding", "0px") + + dec1.normalized_value.must_equal "0" + end + + it "stores values of non-normalized numbers for length units" do + dec1 = Declaration.new("padding", "1px") + + dec1.normalized_value.must_equal "1px" + end end end