Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
* Carson McDonald @carsonmcdonald
* Martin Kuckert @MKuckert
* Ivan Lazarevic @kopipejst
* Matt DuVall @mduvall
26 changes: 14 additions & 12 deletions lib/csscss/parser/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should add % here. I was thinking of moving this check in the normalize method.


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?
Expand Down
1 change: 0 additions & 1 deletion lib/csscss/redundancy_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
11 changes: 10 additions & 1 deletion lib/csscss/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/csscss/parser/common_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions test/csscss/redundancy_analyzer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = %$
Expand Down
12 changes: 12 additions & 0 deletions test/csscss/types_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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