deadweight 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +6 -0
- data/LICENSE +20 -0
- data/README.rdoc +83 -0
- data/Rakefile +59 -0
- data/VERSION +1 -0
- data/bin/deadweight +5 -0
- data/deadweight.gemspec +64 -0
- data/lib/deadweight.rb +145 -0
- data/lib/deadweight/cli.rb +187 -0
- data/test/cli_test.rb +20 -0
- data/test/deadweight_test.rb +60 -0
- data/test/fixtures/index.html +16 -0
- data/test/fixtures/index2.html +21 -0
- data/test/fixtures/style.css +24 -0
- data/test/test_helper.rb +10 -0
- metadata +92 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Aanand Prasad
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
= deadweight
|
2
|
+
|
3
|
+
Deadweight is a CSS coverage tool. Given a set of stylesheets and a set of URLs, it determines which selectors are actually used and reports which can be "safely" deleted.
|
4
|
+
|
5
|
+
=== Screencast!
|
6
|
+
|
7
|
+
Ryan Bates has worked his magic once again. Head over here for an excellent introduction to deadweight: http://railscasts.com/episodes/180-finding-unused-css
|
8
|
+
|
9
|
+
=== A Simple Example
|
10
|
+
|
11
|
+
# lib/tasks/deadweight.rake
|
12
|
+
|
13
|
+
require 'deadweight'
|
14
|
+
|
15
|
+
desc "run Deadweight (script/server needs to be running)"
|
16
|
+
task :deadweight do
|
17
|
+
dw = Deadweight.new
|
18
|
+
dw.stylesheets = %w( /stylesheets/style.css )
|
19
|
+
dw.pages = %w( / /page/1 /about )
|
20
|
+
puts dw.run
|
21
|
+
end
|
22
|
+
|
23
|
+
This will output all unused rules, one per line.
|
24
|
+
|
25
|
+
Alternately, you can run it from the command-line:
|
26
|
+
|
27
|
+
$ deadweight -s styles.css -s ie.css index.html about.html
|
28
|
+
|
29
|
+
You can pipe in CSS rules from STDIN:
|
30
|
+
|
31
|
+
$ cat styles.css | deadweight index.html
|
32
|
+
|
33
|
+
And you can use it as an HTTP proxy:
|
34
|
+
|
35
|
+
$ deadweight -l deadweight.log -s styles.css -w http://github.com/ -P
|
36
|
+
|
37
|
+
=== How You Install It
|
38
|
+
|
39
|
+
gem sources -a http://gems.github.com
|
40
|
+
sudo gem install aanand-deadweight
|
41
|
+
|
42
|
+
=== Things to Note
|
43
|
+
|
44
|
+
- By default, it looks at http://localhost:3000.
|
45
|
+
- It's completely dumb about any classes, IDs or tags that are only added by your Javascript layer, but you can filter them out by setting +ignore_selectors+.
|
46
|
+
- You can optionally tell it to use Mechanize, and set up more complicated targets for scraping by specifying them as Procs.
|
47
|
+
- There is experimental support for Lyndon (http://github.com/defunkt/lyndon) with -L
|
48
|
+
|
49
|
+
=== A More Complex Example, In Light of All That
|
50
|
+
|
51
|
+
# lib/tasks/deadweight.rake
|
52
|
+
|
53
|
+
require 'deadweight'
|
54
|
+
|
55
|
+
desc "run Deadweight on staging server"
|
56
|
+
task :deadweight do
|
57
|
+
dw = Deadweight.new
|
58
|
+
|
59
|
+
dw.mechanize = true
|
60
|
+
|
61
|
+
dw.root = 'http://staging.example.com'
|
62
|
+
|
63
|
+
dw.stylesheets = %w( /stylesheets/style.css )
|
64
|
+
|
65
|
+
dw.pages = %w( / /page/1 /about )
|
66
|
+
|
67
|
+
dw.pages << proc {
|
68
|
+
fetch('/login')
|
69
|
+
form = agent.page.forms.first
|
70
|
+
form.username = 'username'
|
71
|
+
form.password = 'password'
|
72
|
+
agent.submit(form)
|
73
|
+
fetch('/secret-page')
|
74
|
+
}
|
75
|
+
|
76
|
+
dw.ignore_selectors = /hover|lightbox|superimposed_kittens/
|
77
|
+
|
78
|
+
puts dw.run
|
79
|
+
end
|
80
|
+
|
81
|
+
== Copyright
|
82
|
+
|
83
|
+
Copyright (c) 2009 Aanand Prasad. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "deadweight"
|
8
|
+
gem.summary = "A coverage tool for finding unused CSS"
|
9
|
+
gem.email = "aanand.prasad@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/aanand/deadweight"
|
11
|
+
gem.authors = ["Aanand Prasad"]
|
12
|
+
|
13
|
+
gem.add_dependency('css_parser')
|
14
|
+
gem.add_dependency('hpricot')
|
15
|
+
end
|
16
|
+
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/*_test.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/*_test.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
if File.exist?('VERSION.yml')
|
48
|
+
config = YAML.load(File.read('VERSION.yml'))
|
49
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
50
|
+
else
|
51
|
+
version = ""
|
52
|
+
end
|
53
|
+
|
54
|
+
rdoc.rdoc_dir = 'rdoc'
|
55
|
+
rdoc.title = "deadweight #{version}"
|
56
|
+
rdoc.rdoc_files.include('README*')
|
57
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
58
|
+
end
|
59
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/bin/deadweight
ADDED
data/deadweight.gemspec
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{deadweight}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Aanand Prasad"]
|
12
|
+
s.date = %q{2009-09-25}
|
13
|
+
s.default_executable = %q{deadweight}
|
14
|
+
s.email = %q{aanand.prasad@gmail.com}
|
15
|
+
s.executables = ["deadweight"]
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"LICENSE",
|
18
|
+
"README.rdoc"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
".gitignore",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"bin/deadweight",
|
28
|
+
"deadweight.gemspec",
|
29
|
+
"lib/deadweight.rb",
|
30
|
+
"lib/deadweight/cli.rb",
|
31
|
+
"test/cli_test.rb",
|
32
|
+
"test/deadweight_test.rb",
|
33
|
+
"test/fixtures/index.html",
|
34
|
+
"test/fixtures/index2.html",
|
35
|
+
"test/fixtures/style.css",
|
36
|
+
"test/test_helper.rb"
|
37
|
+
]
|
38
|
+
s.homepage = %q{http://github.com/aanand/deadweight}
|
39
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = %q{1.3.4}
|
42
|
+
s.summary = %q{A coverage tool for finding unused CSS}
|
43
|
+
s.test_files = [
|
44
|
+
"test/cli_test.rb",
|
45
|
+
"test/deadweight_test.rb",
|
46
|
+
"test/test_helper.rb"
|
47
|
+
]
|
48
|
+
|
49
|
+
if s.respond_to? :specification_version then
|
50
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
51
|
+
s.specification_version = 3
|
52
|
+
|
53
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
54
|
+
s.add_runtime_dependency(%q<css_parser>, [">= 0"])
|
55
|
+
s.add_runtime_dependency(%q<hpricot>, [">= 0"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<css_parser>, [">= 0"])
|
58
|
+
s.add_dependency(%q<hpricot>, [">= 0"])
|
59
|
+
end
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<css_parser>, [">= 0"])
|
62
|
+
s.add_dependency(%q<hpricot>, [">= 0"])
|
63
|
+
end
|
64
|
+
end
|
data/lib/deadweight.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'css_parser'
|
2
|
+
require 'hpricot'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
class Deadweight
|
7
|
+
attr_accessor :root, :stylesheets, :rules, :pages, :ignore_selectors, :mechanize, :log_file
|
8
|
+
attr_reader :unused_selectors
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@root = 'http://localhost:3000'
|
12
|
+
@stylesheets = []
|
13
|
+
@pages = []
|
14
|
+
@rules = ""
|
15
|
+
@ignore_selectors = []
|
16
|
+
@mechanize = false
|
17
|
+
@log_file = STDERR
|
18
|
+
end
|
19
|
+
|
20
|
+
def analyze(html)
|
21
|
+
doc = Hpricot(html)
|
22
|
+
|
23
|
+
found_selectors = []
|
24
|
+
|
25
|
+
@unused_selectors.collect do |selector, declarations|
|
26
|
+
# We test against the selector stripped of any pseudo classes,
|
27
|
+
# but we report on the selector with its pseudo classes.
|
28
|
+
unless doc.search(strip(selector)).empty?
|
29
|
+
log.info(" #{selector}")
|
30
|
+
selector
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Find all unused CSS selectors and return them as an array.
|
36
|
+
def run
|
37
|
+
css = CssParser::Parser.new
|
38
|
+
|
39
|
+
@stylesheets.each do |path|
|
40
|
+
css.add_block!(fetch(path))
|
41
|
+
end
|
42
|
+
|
43
|
+
css.add_block!(rules)
|
44
|
+
|
45
|
+
@unused_selectors = {}
|
46
|
+
total_selectors = 0
|
47
|
+
|
48
|
+
css.each_selector do |selector, declarations, specificity|
|
49
|
+
unless @unused_selectors[selector]
|
50
|
+
total_selectors += 1
|
51
|
+
@unused_selectors[selector] = declarations unless selector =~ ignore_selectors
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Remove selectors with pseudo classes that already have an equivalent
|
56
|
+
# without the pseudo class. Keep the ones that don't, we need to test
|
57
|
+
# them.
|
58
|
+
@unused_selectors.keys.each do |selector|
|
59
|
+
if has_pseudo_classes(selector) && @unused_selectors.include?(strip(selector))
|
60
|
+
@unused_selectors.delete(selector)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
pages.each do |page|
|
65
|
+
if page.respond_to?(:read)
|
66
|
+
html = page.read
|
67
|
+
elsif page.respond_to?(:call)
|
68
|
+
result = instance_eval(&page)
|
69
|
+
|
70
|
+
html = case result
|
71
|
+
when String
|
72
|
+
result
|
73
|
+
else
|
74
|
+
@agent.page.body
|
75
|
+
end
|
76
|
+
else
|
77
|
+
html = fetch(page)
|
78
|
+
end
|
79
|
+
|
80
|
+
process!(html)
|
81
|
+
end
|
82
|
+
|
83
|
+
log.info "found #{@unused_selectors.size} unused selectors out of #{total_selectors} total"
|
84
|
+
|
85
|
+
@unused_selectors
|
86
|
+
end
|
87
|
+
|
88
|
+
def process!(html)
|
89
|
+
analyze(html).each do |selector|
|
90
|
+
@unused_selectors.delete(selector)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Returns the Mechanize instance, if +mechanize+ is set to +true+.
|
95
|
+
def agent
|
96
|
+
@agent ||= initialize_agent
|
97
|
+
end
|
98
|
+
|
99
|
+
# Fetch a path, using Mechanize if +mechanize+ is set to +true+.
|
100
|
+
def fetch(path)
|
101
|
+
log.info(path)
|
102
|
+
|
103
|
+
loc = root + path
|
104
|
+
|
105
|
+
if @mechanize
|
106
|
+
loc = "file://#{File.expand_path(loc)}" unless loc =~ %r{^\w+://}
|
107
|
+
page = agent.get(loc)
|
108
|
+
log.warn("#{path} redirected to #{page.uri}") unless page.uri.to_s == loc
|
109
|
+
page.body
|
110
|
+
else
|
111
|
+
open(loc).read
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
def has_pseudo_classes(selector)
|
118
|
+
selector =~ /::?[\w\-]+/
|
119
|
+
end
|
120
|
+
|
121
|
+
def strip(selector)
|
122
|
+
selector.gsub(/::?[\w\-]+/, '')
|
123
|
+
end
|
124
|
+
|
125
|
+
def log
|
126
|
+
@log ||= Logger.new(@log_file)
|
127
|
+
end
|
128
|
+
|
129
|
+
def initialize_agent
|
130
|
+
begin
|
131
|
+
require 'mechanize'
|
132
|
+
return WWW::Mechanize.new
|
133
|
+
rescue LoadError
|
134
|
+
log.info %{
|
135
|
+
=================================================================
|
136
|
+
Couldn't load 'mechanize', which is required for remote scraping.
|
137
|
+
Install it like so: gem install mechanize
|
138
|
+
=================================================================
|
139
|
+
}
|
140
|
+
|
141
|
+
raise
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
@@ -0,0 +1,187 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'deadweight'
|
3
|
+
|
4
|
+
class Deadweight
|
5
|
+
class CLI
|
6
|
+
attr_reader :stdout, :stdin, :stderr
|
7
|
+
attr_reader :arguments, :options
|
8
|
+
attr_reader :output
|
9
|
+
|
10
|
+
def self.execute(stdout, stdin, stderr, arguments = [])
|
11
|
+
@options = {
|
12
|
+
:log_file => stderr,
|
13
|
+
:output => stdout,
|
14
|
+
:proxy_port => 8002
|
15
|
+
}
|
16
|
+
|
17
|
+
self.parse_options(arguments)
|
18
|
+
self.new(stdout, stdin, stderr, arguments, @options).execute!
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.option_parser
|
22
|
+
@option_parser ||= OptionParser.new do |opts|
|
23
|
+
opts.banner = "Usage: #{$0} [options] <url> [<url> ...]"
|
24
|
+
|
25
|
+
@options[:stylesheets] = []
|
26
|
+
|
27
|
+
opts.on("-L", "--lyndon", "Pre-process HTML with Lyndon") do
|
28
|
+
@options[:lyndon] = true
|
29
|
+
end
|
30
|
+
|
31
|
+
opts.on("-l", "--log FILE", "Where to write log messages") do |v|
|
32
|
+
@options[:log_file] = v
|
33
|
+
end
|
34
|
+
|
35
|
+
opts.on("-P", "--proxy", "Run in proxy mode") do
|
36
|
+
@options[:proxy] = true
|
37
|
+
end
|
38
|
+
|
39
|
+
opts.on("-p", "--proxy-port", "Port to run the proxy on") do |v|
|
40
|
+
@options[:proxy] = true
|
41
|
+
@options[:proxy_port] = v.to_i
|
42
|
+
end
|
43
|
+
|
44
|
+
opts.on("-O", "--output FILE",
|
45
|
+
"Where to output orphaned CSS rules") do |v|
|
46
|
+
@options[:output] = File.new(v, "w")
|
47
|
+
end
|
48
|
+
|
49
|
+
opts.on("-s", "--stylesheet FILE",
|
50
|
+
"Apply the specified stylesheet to the target") do |v|
|
51
|
+
@options[:stylesheets] << v
|
52
|
+
end
|
53
|
+
|
54
|
+
opts.on("-w", "--whitelist URL-PREFIX",
|
55
|
+
"Specifies a prefix for URLs to process") do |v|
|
56
|
+
@options[:whitelist] ||= []
|
57
|
+
@options[:whitelist] << v
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.parse_options(arguments = [])
|
63
|
+
self.option_parser.parse!(arguments)
|
64
|
+
@options
|
65
|
+
end
|
66
|
+
|
67
|
+
def initialize(stdout, stdin, stderr, arguments = [], options = {})
|
68
|
+
@stdout = stdout
|
69
|
+
@stdin = stdin
|
70
|
+
@stderr = stderr
|
71
|
+
@arguments = arguments
|
72
|
+
@options = options
|
73
|
+
@output = options[:output]
|
74
|
+
end
|
75
|
+
|
76
|
+
def execute!
|
77
|
+
if options[:proxy]
|
78
|
+
proxy
|
79
|
+
elsif arguments.empty?
|
80
|
+
stdout.puts self.class.option_parser.help
|
81
|
+
else
|
82
|
+
process
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def process
|
87
|
+
# TODO pass stylesheets + pages as args
|
88
|
+
dw = Deadweight.new
|
89
|
+
|
90
|
+
# TODO this should be the default
|
91
|
+
dw.root = ""
|
92
|
+
|
93
|
+
dw.log_file = options[:log_file]
|
94
|
+
|
95
|
+
dw.stylesheets = options[:stylesheets]
|
96
|
+
|
97
|
+
dw.rules = stdin.read if stdin.stat.size > 0
|
98
|
+
|
99
|
+
if options[:lyndon]
|
100
|
+
arguments.each do |file|
|
101
|
+
dw.pages << IO.popen("cat #{file} | lyndon 2> /dev/null")
|
102
|
+
end
|
103
|
+
else
|
104
|
+
dw.pages = arguments
|
105
|
+
end
|
106
|
+
|
107
|
+
unused_rules = dw.run
|
108
|
+
unused_rules.each do |k,v|
|
109
|
+
output.puts "#{k} { #{v} }"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def proxy
|
114
|
+
dw = Deadweight.new
|
115
|
+
|
116
|
+
# TODO note the boilerplate shared with #process
|
117
|
+
dw.root = ""
|
118
|
+
dw.log_file = options[:log_file]
|
119
|
+
dw.stylesheets = options[:stylesheets]
|
120
|
+
dw.rules = stdin.read if stdin.stat.size > 0
|
121
|
+
|
122
|
+
# initialize selectors
|
123
|
+
dw.run
|
124
|
+
|
125
|
+
stdout.puts "#{dw.unused_selectors.length} rules loaded."
|
126
|
+
|
127
|
+
require 'webrick/httpproxy'
|
128
|
+
|
129
|
+
@proxy = WEBrick::HTTPProxyServer.new \
|
130
|
+
:AccessLog => [
|
131
|
+
[options[:log_file], WEBrick::AccessLog::COMMON_LOG_FORMAT],
|
132
|
+
[options[:log_file], WEBrick::AccessLog::REFERER_LOG_FORMAT]
|
133
|
+
],
|
134
|
+
:Logger => WEBrick::Log.new(options[:log_file]),
|
135
|
+
:Port => options[:proxy_port],
|
136
|
+
:ProxyContentHandler => lambda { |request, response|
|
137
|
+
|
138
|
+
parse_this = false
|
139
|
+
|
140
|
+
if options[:whitelist]
|
141
|
+
options[:whitelist].each do |x|
|
142
|
+
sliced_request_uri = response.request_uri.to_s[0..x.length - 1]
|
143
|
+
if sliced_request_uri.downcase == x.downcase
|
144
|
+
parse_this = true
|
145
|
+
break
|
146
|
+
end
|
147
|
+
end
|
148
|
+
else
|
149
|
+
parse_this = true
|
150
|
+
end
|
151
|
+
|
152
|
+
if parse_this && response.header["content-type"] =~ /text\/html/
|
153
|
+
# TODO this slows things down significantly; better would be to
|
154
|
+
# remove the Accept-Encoding header during the request phase
|
155
|
+
body = if response.header["content-encoding"] == "gzip"
|
156
|
+
Zlib::GzipReader.new(StringIO.new(response.body)).read
|
157
|
+
elsif response.header["content-encoding"] == "deflate"
|
158
|
+
Zlib::Inflate.inflate(response.body)
|
159
|
+
else
|
160
|
+
response.body
|
161
|
+
end
|
162
|
+
dw.process!(body)
|
163
|
+
|
164
|
+
stdout.puts "After reviewing <#{response.request_uri}>, there were #{dw.unused_selectors.length} rules left"
|
165
|
+
# stdout.puts "After reviewing <#{response.request_uri}>, these were left:"
|
166
|
+
# dw.unused_selectors.each do |k,v|
|
167
|
+
# stdout.puts "#{k} { #{v} }"
|
168
|
+
# end
|
169
|
+
end
|
170
|
+
}
|
171
|
+
|
172
|
+
trap('INT') do
|
173
|
+
@proxy.shutdown
|
174
|
+
|
175
|
+
# dump the remaining CSS rules if output is set
|
176
|
+
unless options[:output] == STDOUT
|
177
|
+
dw.unused_selectors.each do |k,v|
|
178
|
+
output.puts "#{k} { #{v} }"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
@proxy.start
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
data/test/cli_test.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CliTest < Test::Unit::TestCase
|
4
|
+
COMMAND = "ruby -rubygems -Ilib bin/deadweight -s test/fixtures/style.css test/fixtures/index.html 2>/dev/null"
|
5
|
+
|
6
|
+
should "output unused selectors on STDOUT" do
|
7
|
+
@result = `#{COMMAND}`.split("\n")
|
8
|
+
|
9
|
+
assert_equal 1, @result.grep(/^#foo \.bar \.baz \{/).length
|
10
|
+
assert_equal 0, @result.grep(/^#foo \{/).length
|
11
|
+
assert_equal 0, @result.grep(/^#foo .bar \{/).length
|
12
|
+
end
|
13
|
+
|
14
|
+
should "accept CSS rules on STDIN" do
|
15
|
+
@result = `echo ".something { display: block; }" | #{COMMAND}`.split("\n")
|
16
|
+
|
17
|
+
assert_equal 1, @result.grep(/^\.something \{/).length
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DeadweightTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@dw = Deadweight.new
|
6
|
+
@dw.log_file = 'test.log'
|
7
|
+
@dw.root = File.dirname(__FILE__) + '/fixtures'
|
8
|
+
@dw.stylesheets << '/style.css'
|
9
|
+
@dw.pages << '/index.html'
|
10
|
+
|
11
|
+
@result = @dw.run
|
12
|
+
end
|
13
|
+
|
14
|
+
should "report unused selectors" do
|
15
|
+
assert @result.include?('#foo .bar .baz')
|
16
|
+
end
|
17
|
+
|
18
|
+
should "not report used selectors" do
|
19
|
+
assert !@result.include?('#foo')
|
20
|
+
assert !@result.include?('#foo .bar')
|
21
|
+
end
|
22
|
+
|
23
|
+
should 'strip pseudo classes from selectors' do
|
24
|
+
# #oof:hover (#oof does not exist)
|
25
|
+
assert @result.include?('#oof:hover'), @result.inspect
|
26
|
+
|
27
|
+
# #foo:hover (#foo does exist)
|
28
|
+
assert !@result.include?('#foo:hover')
|
29
|
+
|
30
|
+
# #rab:hover::selection (#rab does not exist)
|
31
|
+
assert @result.include?('#rab:hover::selection')
|
32
|
+
end
|
33
|
+
|
34
|
+
should "accept Procs as targets" do
|
35
|
+
@dw.mechanize = true
|
36
|
+
|
37
|
+
@dw.pages << proc {
|
38
|
+
fetch('/index.html')
|
39
|
+
agent.page.links.first.click
|
40
|
+
}
|
41
|
+
|
42
|
+
assert @dw.run.empty?
|
43
|
+
end
|
44
|
+
|
45
|
+
should "accept IO objects as targets" do
|
46
|
+
@dw.pages << File.new(File.dirname(__FILE__) + '/fixtures/index2.html')
|
47
|
+
|
48
|
+
assert @dw.run.empty?
|
49
|
+
end
|
50
|
+
|
51
|
+
should "allow individual CSS rules to be appended" do
|
52
|
+
@dw.rules = ".something { display: block; }"
|
53
|
+
|
54
|
+
assert @dw.run.include?(".something")
|
55
|
+
end
|
56
|
+
|
57
|
+
should 'provide the results of its last run with #unused_selectors' do
|
58
|
+
assert_equal @result, @dw.unused_selectors
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
<html>
|
2
|
+
<head>
|
3
|
+
<link rel="stylesheet" type="text/css" href="style.css">
|
4
|
+
</head>
|
5
|
+
|
6
|
+
<body>
|
7
|
+
<div id="foo">
|
8
|
+
<div class="bar">
|
9
|
+
<div class="baz">
|
10
|
+
hello again
|
11
|
+
</div>
|
12
|
+
</div>
|
13
|
+
</div>
|
14
|
+
|
15
|
+
<div id="oof"></div>
|
16
|
+
|
17
|
+
<div id="rab"></div>
|
18
|
+
</body>
|
19
|
+
</html>
|
20
|
+
|
21
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#foo {
|
2
|
+
color: green;
|
3
|
+
}
|
4
|
+
|
5
|
+
#foo .bar {
|
6
|
+
color: blue;
|
7
|
+
}
|
8
|
+
|
9
|
+
#foo .bar .baz {
|
10
|
+
color: red;
|
11
|
+
}
|
12
|
+
|
13
|
+
/* pseudo classes */
|
14
|
+
#foo:hover {
|
15
|
+
color: red;
|
16
|
+
}
|
17
|
+
|
18
|
+
#oof:hover {
|
19
|
+
color: white;
|
20
|
+
}
|
21
|
+
|
22
|
+
#rab:hover::selection {
|
23
|
+
color: black;
|
24
|
+
}
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deadweight
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aanand Prasad
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-25 00:00:00 -04:00
|
13
|
+
default_executable: deadweight
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: css_parser
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: hpricot
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email: aanand.prasad@gmail.com
|
37
|
+
executables:
|
38
|
+
- deadweight
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- bin/deadweight
|
52
|
+
- deadweight.gemspec
|
53
|
+
- lib/deadweight.rb
|
54
|
+
- lib/deadweight/cli.rb
|
55
|
+
- test/cli_test.rb
|
56
|
+
- test/deadweight_test.rb
|
57
|
+
- test/fixtures/index.html
|
58
|
+
- test/fixtures/index2.html
|
59
|
+
- test/fixtures/style.css
|
60
|
+
- test/test_helper.rb
|
61
|
+
has_rdoc: true
|
62
|
+
homepage: http://github.com/aanand/deadweight
|
63
|
+
licenses: []
|
64
|
+
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options:
|
67
|
+
- --charset=UTF-8
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
version:
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.3.4
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: A coverage tool for finding unused CSS
|
89
|
+
test_files:
|
90
|
+
- test/cli_test.rb
|
91
|
+
- test/deadweight_test.rb
|
92
|
+
- test/test_helper.rb
|