csscss 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ## 1.1.0 - 4/12/2013 ##
2
+
3
+ * Fixes bug where CLI --no-color wasn't respected
4
+ * Added ruby version requirement for >= 1.9
5
+ * Added CONTRIBUTORS.md
6
+ * Fixes bugs with urls that have dashes in them
7
+ * Fixes bugs with urls containing encoded data (usually images)
8
+ * Deprecates CSSCSS_DEBUG in favor of --show-parser-errors
9
+ * Fixes line/column output during parser errors
10
+ * --compass now grabs config.rb by default if it exists
11
+ * Adds --compass-with-config that lets users specify a config
12
+ * Fixes parser error bug when trying to parse blank files
13
+ * Fixes bug where rules from multiple files aren't consolidated
14
+ * Adds --no-match-shorthand to allow users to opt out of shorthand matching
15
+
1
16
  ## 1.0.0 - 4/7/2013 ##
2
17
 
3
18
  * Allows the user to specify ignored properties and selectors
data/CONTRIBUTORS.md ADDED
@@ -0,0 +1,4 @@
1
+ * Zach Moazeni <zach.moazeni@gmail.com> @zmoazeni
2
+ * Carson McDonald @carsonmcdonald
3
+ * Martin Kuckert @MKuckert
4
+ * Ivan Lazarevic @kopipejst
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- csscss (1.0.0)
4
+ csscss (1.1.0)
5
5
  colorize
6
6
  parslet (~> 1.5)
7
7
 
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  [![Build Status](https://travis-ci.org/zmoazeni/csscss.png?branch=master)](https://travis-ci.org/zmoazeni/csscss)
2
+ [![Code Climate](https://codeclimate.com/github/zmoazeni/csscss.png)](https://codeclimate.com/github/zmoazeni/csscss)
2
3
 
3
4
  ## What is it? ##
4
5
 
@@ -20,6 +21,8 @@ First you need to install it. It is currently packaged as a ruby gem:
20
21
 
21
22
  $ gem install csscss
22
23
 
24
+ Note: csscss only works on ruby 1.9.x and up. It will have trouble with ruby 1.8.x.
25
+
23
26
  Then you can run it in at the command line against CSS files.
24
27
 
25
28
  $ csscss path/to/styles.css path/to/other-styles.css
data/csscss.gemspec CHANGED
@@ -10,13 +10,15 @@ Gem::Specification.new do |gem|
10
10
  gem.email = ["zach.moazeni@gmail.com"]
11
11
  gem.summary = %q{A CSS redundancy analyzer that analyzes redundancy.}
12
12
  gem.description = %q{csscss will parse any CSS files you give it and let you know which rulesets have duplicated declarations.}
13
- gem.homepage = "http://zmoazeni.github.com/csscss/"
13
+ gem.homepage = "http://zmoazeni.github.io/csscss/"
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
+ gem.required_ruby_version = ">= 1.9"
21
+
20
22
  gem.add_dependency "parslet", "~> 1.5"
21
23
  gem.add_dependency "colorize"
22
24
  end
data/lib/csscss/cli.rb CHANGED
@@ -8,6 +8,7 @@ module Csscss
8
8
  @compass = false
9
9
  @ignored_properties = []
10
10
  @ignored_selectors = []
11
+ @match_shorthand = true
11
12
  end
12
13
 
13
14
  def run
@@ -16,14 +17,14 @@ module Csscss
16
17
  end
17
18
 
18
19
  def execute
20
+ warn_old_debug_flag if ENV["CSSCSS_DEBUG"]
19
21
 
20
- all_redundancies = @argv.map do |filename|
21
- contents = if %w(.scss .sass).include?(File.extname(filename).downcase) && !(filename =~ URI.regexp)
22
+ all_contents = @argv.map do |filename|
23
+ if %w(.scss .sass).include?(File.extname(filename).downcase) && !(filename =~ URI.regexp)
22
24
  begin
23
25
  require "sass"
24
26
  rescue LoadError
25
- puts "Must install sass gem before parsing sass/scss files"
26
- exit 1
27
+ abort "Must install sass gem before parsing sass/scss files"
27
28
  end
28
29
 
29
30
  sass_options = {cache:false}
@@ -41,36 +42,31 @@ module Csscss
41
42
  else
42
43
  open(filename) {|f| f.read }
43
44
  end
44
-
45
- RedundancyAnalyzer.new(contents).redundancies(minimum: @minimum,
46
- ignored_properties: @ignored_properties,
47
- ignored_selectors: @ignored_selectors)
48
- end
49
-
50
- combined_redundancies = all_redundancies.inject({}) do |combined, redundancies|
51
- if combined.empty?
52
- redundancies
45
+ end.join("\n")
46
+
47
+ unless all_contents.strip.empty?
48
+ redundancies = RedundancyAnalyzer.new(all_contents).redundancies(
49
+ minimum: @minimum,
50
+ ignored_properties: @ignored_properties,
51
+ ignored_selectors: @ignored_selectors,
52
+ match_shorthand: @match_shorthand
53
+ )
54
+
55
+ if @json
56
+ puts JSONReporter.new(redundancies).report
53
57
  else
54
- combined.merge(redundancies) do |_, v1, v2|
55
- (v1 + v2).uniq
56
- end
58
+ report = Reporter.new(redundancies).report(verbose:@verbose, color:@color)
59
+ puts report unless report.empty?
57
60
  end
58
61
  end
59
62
 
60
- if @json
61
- puts JSONReporter.new(combined_redundancies).report
62
- else
63
- report = Reporter.new(combined_redundancies).report(verbose:@verbose, color:true)
64
- puts report unless report.empty?
65
- end
66
-
67
63
  rescue Parslet::ParseFailed => e
68
- line, column = e.cause.source.line_and_column
64
+ line, column = e.cause.source.line_and_column(e.cause.pos)
69
65
  puts "Had a problem parsing the css at line: #{line}, column: #{column}".red
70
- if ENV['CSSCSS_DEBUG'] == 'true'
66
+ if @show_parser_errors || ENV['CSSCSS_DEBUG'] == 'true'
71
67
  puts e.cause.ascii_tree.red
72
68
  else
73
- puts "Run with CSSCSS_DEBUG=true for verbose parser errors".red
69
+ puts "Run with --show-parser-errors for verbose parser errors".red
74
70
  end
75
71
  exit 1
76
72
  end
@@ -84,7 +80,7 @@ module Csscss
84
80
  @verbose = v
85
81
  end
86
82
 
87
- opts.on("--[no-]color", "Colorizes output") do |c|
83
+ opts.on("--[no-]color", "Colorize output (default is true)") do |c|
88
84
  @color = c
89
85
  end
90
86
 
@@ -105,21 +101,27 @@ module Csscss
105
101
  exit
106
102
  end
107
103
 
108
- opts.on("--[no-]compass", "Enables compass extensions when parsing sass/scss") do |compass|
109
- if @compass = compass
110
- begin
111
- require "compass"
112
- rescue LoadError
113
- puts "Must install compass gem before enabling its extensions"
114
- exit 1
115
- end
116
- end
104
+ opts.on("--[no-]compass", "Enable compass extensions when parsing sass/scss (default is false)") do |compass|
105
+ enable_compass if @compass = compass
106
+ end
107
+
108
+ opts.on("--compass-with-config config", "Enable compass extensions when parsing sass/scss and pass config file") do |config|
109
+ @compass = true
110
+ enable_compass(config)
111
+ end
112
+
113
+ opts.on("--[no-]match-shorthand", "Expands shorthand rules and matches on explicit rules (default is true)") do |match_shorthand|
114
+ @match_shorthand = match_shorthand
117
115
  end
118
116
 
119
117
  opts.on("-j", "--[no-]json", "Output results in JSON") do |j|
120
118
  @json = j
121
119
  end
122
120
 
121
+ opts.on("--show-parser-errors", "Print verbose parser errors") do |show_parser_errors|
122
+ @show_parser_errors = show_parser_errors
123
+ end
124
+
123
125
  opts.on_tail("-h", "--help", "Show this message") do
124
126
  print_help(opts)
125
127
  end
@@ -137,6 +139,22 @@ module Csscss
137
139
  exit
138
140
  end
139
141
 
142
+ def warn_old_debug_flag
143
+ $stderr.puts "CSSCSS_DEBUG is now deprecated. Use --show-parser-errors instead".red
144
+ end
145
+
146
+ def enable_compass(config = nil)
147
+ require "compass"
148
+
149
+ if config
150
+ Compass.add_configuration(config)
151
+ else
152
+ Compass.add_configuration("config.rb") if File.exist?("config.rb")
153
+ end
154
+ rescue LoadError
155
+ abort "Must install compass gem before enabling its extensions"
156
+ end
157
+
140
158
  class << self
141
159
  def run(argv)
142
160
  new(argv).run
@@ -18,12 +18,17 @@ module Csscss
18
18
  rule(:nada) { any.repeat.as(:nada) }
19
19
 
20
20
  rule(:http) {
21
- (match['a-zA-Z.:/'] | str('\(') | str('\)')).repeat >> space?
21
+ (match['a-zA-Z0-9.:/\-'] | str('\(') | str('\)')).repeat >> space?
22
+ }
23
+
24
+ rule(:data) {
25
+ stri("data:") >> match['a-zA-Z0-9.:/+;,=\-'].repeat >> space?
22
26
  }
23
27
 
24
28
  rule(:url) {
25
29
  stri("url") >> parens do
26
30
  (any_quoted { http } >> space?) |
31
+ (any_quoted { data } >> space?) |
27
32
  http
28
33
  end
29
34
  }
@@ -9,6 +9,7 @@ module Csscss
9
9
  minimum = opts[:minimum]
10
10
  ignored_properties = opts[:ignored_properties] || []
11
11
  ignored_selectors = opts[:ignored_selectors] || []
12
+ match_shorthand = opts.fetch(:match_shorthand, true)
12
13
 
13
14
  rule_sets = Parser::Css.parse(@raw_css)
14
15
  matches = {}
@@ -20,7 +21,7 @@ module Csscss
20
21
  rule_set.declarations.each do |dec|
21
22
  next if ignored_properties.include?(dec.property)
22
23
 
23
- if parser = shorthand_parser(dec.property)
24
+ if match_shorthand && parser = shorthand_parser(dec.property)
24
25
  if new_decs = parser.parse(dec.property, dec.value)
25
26
  if dec.property == "border"
26
27
  %w(border-top border-right border-bottom border-left).each do |property|
@@ -1,3 +1,3 @@
1
1
  module Csscss
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -115,6 +115,15 @@ module Csscss::Parser
115
115
  @parser.http.must_parse 'foo\(bar\).jpg'
116
116
  @parser.http.must_parse 'http://foo\(bar\).jpg'
117
117
  @parser.http.must_parse 'http://foo.com/baz/\(bar\).jpg'
118
+ @parser.http.must_parse '//foo.com/foo.jpg'
119
+ @parser.http.must_parse 'https://foo.com/foo.jpg'
120
+ @parser.http.must_parse 'http://foo100.com/foo.jpg'
121
+ @parser.http.must_parse 'http://foo-bar.com/foo.jpg'
122
+ end
123
+
124
+ it "parses data" do
125
+ @parser.data.must_parse 'data:image/jpg;base64,IMGDATAGOESHERE=='
126
+ @parser.data.must_parse 'data:image/svg+xml;base64,IMGDATAGOESHERE4/5/h/1+=='
118
127
  end
119
128
 
120
129
  it "parses urls" do
@@ -124,6 +133,7 @@ module Csscss::Parser
124
133
  @parser.url.must_parse "url('foo.jpg')"
125
134
  @parser.url.must_parse "url('foo.jpg' )"
126
135
  @parser.url.must_parse 'url(foo\(bar\).jpg)'
136
+ @parser.url.must_parse "url('data:image/svg+xml;base64,IMGDATAGOESHERE4/5/h/1+==')"
127
137
  end
128
138
  end
129
139
  end
@@ -68,7 +68,7 @@ module Csscss
68
68
  .baz {
69
69
  margin: 3px 3px 30px 3px;
70
70
  padding: 10px 30px;
71
- background: white url(images/bg-bolt-inactive.png) no-repeat 99% 5px;
71
+ background: blue url(images/bg-bolt-inactive.png) no-repeat 99% 5px;
72
72
 
73
73
  -webkit-border-radius: 4px;
74
74
  -moz-border-radius: 4px;
@@ -184,6 +184,15 @@ module Csscss
184
184
  })
185
185
  end
186
186
 
187
+ it "doesn't match shorthand when explicitly turned off" do
188
+ css = %$
189
+ .foo { background-color: #fff }
190
+ .bar { background: #fff }
191
+ $
192
+
193
+ RedundancyAnalyzer.new(css).redundancies(match_shorthand:false).must_equal({})
194
+ end
195
+
187
196
  it "3-way case consolidation" do
188
197
  css = %$
189
198
  .bar { background: #fff }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csscss
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-08 00:00:00.000000000 Z
12
+ date: 2013-04-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parslet
@@ -55,6 +55,7 @@ files:
55
55
  - .gitignore
56
56
  - .travis.yml
57
57
  - CHANGELOG.md
58
+ - CONTRIBUTORS.md
58
59
  - Gemfile
59
60
  - Gemfile.lock
60
61
  - LICENSE.txt
@@ -106,7 +107,7 @@ files:
106
107
  - test/csscss/types_test.rb
107
108
  - test/just_parse.rb
108
109
  - test/test_helper.rb
109
- homepage: http://zmoazeni.github.com/csscss/
110
+ homepage: http://zmoazeni.github.io/csscss/
110
111
  licenses: []
111
112
  post_install_message:
112
113
  rdoc_options: []
@@ -117,10 +118,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
118
  requirements:
118
119
  - - ! '>='
119
120
  - !ruby/object:Gem::Version
120
- version: '0'
121
- segments:
122
- - 0
123
- hash: 2330083466520748341
121
+ version: '1.9'
124
122
  required_rubygems_version: !ruby/object:Gem::Requirement
125
123
  none: false
126
124
  requirements:
@@ -129,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
127
  version: '0'
130
128
  segments:
131
129
  - 0
132
- hash: 2330083466520748341
130
+ hash: -506187209623006521
133
131
  requirements: []
134
132
  rubyforge_project:
135
133
  rubygems_version: 1.8.25