Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Optionally leave shorthand instead parsing values #20

Closed
wants to merge 7 commits into from

3 participants

Ivan Lazarevic Zach Moazeni Tam Vo
Ivan Lazarevic

Using shorthand notation in CSS is good practice so even if there are duplicates in most cases is better to leave them.

Example test.css:

.foo { 
    border: 1px solid #000; 
    color: #000; 
    padding: 10px 5px; 
}
.bar { 
    border: 1px solid #fff; 
    color: #000; 
    padding: 10px 2px; 
}

If we run csscss -v test.css -n 1 we'll get that there are 5 duplicates:

{.bar} AND {.foo} share 5 rules

  • border-style: solid
  • border-width: 1px
  • color: #000
  • padding-bottom: 10px
  • padding-top: 10px

However, in this case splitting border or padding shorthand notation is not good practice in CSS because file will be larger.
With --use-shorthand option shorthand will not be parsed so we'll get only one duplicate.

csscss -v test.css -n 1 --use-shorthand

{.bar} AND {.foo} share 1 rule

  • color: #000
Zach Moazeni
Owner

Thanks for submitting this @kopipejst! I'll take a look at this. I'm still on the fence whether shorthand matching should be the default or not but I'm thinking that regardless the default, it should be optional.

Zach Moazeni zmoazeni referenced this pull request from a commit
Zach Moazeni Adds --no-match-shorthand so users can opt out of shorthand matching
I'm fine with keeping shorthand matching the default, but I respect some
people don't like it.

refs: #20
16918da
Zach Moazeni zmoazeni referenced this pull request from a commit
Zach Moazeni Adds @kopipejst to CONTRIBUTORS for his help
Especially for #20
cc55bd3
Zach Moazeni
Owner

@kopipejst @tamvo I've just committed something similar but with the reverse logic. Default stays the same but you can now run csscss --no-match-shorthand ... to match on exact rules. Thanks for the pull request. I'm going to go ahead and close this unmerged.

Zach Moazeni zmoazeni closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
8 lib/csscss/cli.rb
View
@@ -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
7 lib/csscss/redundancy_analyzer.rb
View
@@ -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|
10 test/csscss/redundancy_analyzer_test.rb
View
@@ -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 = %$
Something went wrong with that request. Please try again.