mojodna-deadweight 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README.rdoc +79 -0
- data/Rakefile +58 -0
- data/VERSION +1 -0
- data/bin/deadweight +5 -0
- data/deadweight.gemspec +61 -0
- data/lib/deadweight.rb +148 -0
- data/lib/deadweight/cli.rb +175 -0
- data/test/deadweight_test.rb +44 -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 +86 -0
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,79 @@
|
|
|
1
|
+
= deadweight
|
|
2
|
+
|
|
3
|
+
Deadweight is RCov for CSS, kind of. 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
|
+
=== A Simple Example
|
|
6
|
+
|
|
7
|
+
# lib/tasks/deadweight.rake
|
|
8
|
+
|
|
9
|
+
require 'deadweight'
|
|
10
|
+
|
|
11
|
+
desc "run Deadweight (script/server needs to be running)"
|
|
12
|
+
task :deadweight do
|
|
13
|
+
dw = Deadweight.new
|
|
14
|
+
dw.stylesheets = %w( /stylesheets/style.css )
|
|
15
|
+
dw.pages = %w( / /page/1 /about )
|
|
16
|
+
puts dw.run
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
This will output all unused rules, one per line.
|
|
20
|
+
|
|
21
|
+
Alternately, you can run it from the command-line:
|
|
22
|
+
|
|
23
|
+
$ deadweight -s styles.css -s ie.css index.html about.html
|
|
24
|
+
|
|
25
|
+
You can pipe in CSS rules from STDIN:
|
|
26
|
+
|
|
27
|
+
$ cat styles.css | deadweight index.html
|
|
28
|
+
|
|
29
|
+
And you can use it as an HTTP proxy:
|
|
30
|
+
|
|
31
|
+
$ deadweight -l deadweight.log -s styles.css -w http://github.com/ -P
|
|
32
|
+
|
|
33
|
+
=== How You Install It
|
|
34
|
+
|
|
35
|
+
gem sources -a http://gems.github.com
|
|
36
|
+
sudo gem install aanand-deadweight
|
|
37
|
+
|
|
38
|
+
=== Things to Note
|
|
39
|
+
|
|
40
|
+
- By default, it looks at http://localhost:3000.
|
|
41
|
+
- 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+.
|
|
42
|
+
- You can optionally tell it to use Mechanize, and set up more complicated targets for scraping by specifying them as Procs.
|
|
43
|
+
- There is experimental support for Lyndon (http://github.com/defunkt/lyndon) with -L
|
|
44
|
+
|
|
45
|
+
=== A More Complex Example, In Light of All That
|
|
46
|
+
|
|
47
|
+
# lib/tasks/deadweight.rake
|
|
48
|
+
|
|
49
|
+
require 'deadweight'
|
|
50
|
+
|
|
51
|
+
desc "run Deadweight on staging server"
|
|
52
|
+
task :deadweight do
|
|
53
|
+
dw = Deadweight.new
|
|
54
|
+
|
|
55
|
+
dw.mechanize = true
|
|
56
|
+
|
|
57
|
+
dw.root = 'http://staging.example.com'
|
|
58
|
+
|
|
59
|
+
dw.stylesheets = %w( /stylesheets/style.css )
|
|
60
|
+
|
|
61
|
+
dw.pages = %w( / /page/1 /about )
|
|
62
|
+
|
|
63
|
+
dw.pages << proc {
|
|
64
|
+
fetch('/login')
|
|
65
|
+
form = agent.page.forms.first
|
|
66
|
+
form.username = 'username'
|
|
67
|
+
form.password = 'password'
|
|
68
|
+
agent.submit(form)
|
|
69
|
+
fetch('/secret-page')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
dw.ignore_selectors = /hover|lightbox|superimposed_kittens/
|
|
73
|
+
|
|
74
|
+
puts dw.run
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
== Copyright
|
|
78
|
+
|
|
79
|
+
Copyright (c) 2009 Aanand Prasad. See LICENSE for details.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
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 = "RCov for 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
|
+
rescue LoadError
|
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
require 'rake/testtask'
|
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
|
23
|
+
test.libs << 'lib' << 'test'
|
|
24
|
+
test.pattern = 'test/**/*_test.rb'
|
|
25
|
+
test.verbose = true
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
begin
|
|
29
|
+
require 'rcov/rcovtask'
|
|
30
|
+
Rcov::RcovTask.new do |test|
|
|
31
|
+
test.libs << 'test'
|
|
32
|
+
test.pattern = 'test/**/*_test.rb'
|
|
33
|
+
test.verbose = true
|
|
34
|
+
end
|
|
35
|
+
rescue LoadError
|
|
36
|
+
task :rcov do
|
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
task :default => :test
|
|
43
|
+
|
|
44
|
+
require 'rake/rdoctask'
|
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
|
46
|
+
if File.exist?('VERSION.yml')
|
|
47
|
+
config = YAML.load(File.read('VERSION.yml'))
|
|
48
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
|
49
|
+
else
|
|
50
|
+
version = ""
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
54
|
+
rdoc.title = "deadweight #{version}"
|
|
55
|
+
rdoc.rdoc_files.include('README*')
|
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
57
|
+
end
|
|
58
|
+
|
data/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.0.4
|
data/bin/deadweight
ADDED
data/deadweight.gemspec
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = %q{deadweight}
|
|
5
|
+
s.version = "0.0.4"
|
|
6
|
+
|
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
8
|
+
s.authors = ["Aanand Prasad"]
|
|
9
|
+
s.date = %q{2009-08-18}
|
|
10
|
+
s.default_executable = %q{deadweight}
|
|
11
|
+
s.email = %q{aanand.prasad@gmail.com}
|
|
12
|
+
s.executables = %q{deadweight}
|
|
13
|
+
s.extra_rdoc_files = [
|
|
14
|
+
"LICENSE",
|
|
15
|
+
"README.rdoc"
|
|
16
|
+
]
|
|
17
|
+
s.files = [
|
|
18
|
+
"LICENSE",
|
|
19
|
+
"README.rdoc",
|
|
20
|
+
"Rakefile",
|
|
21
|
+
"VERSION",
|
|
22
|
+
"deadweight.gemspec",
|
|
23
|
+
"bin/deadweight",
|
|
24
|
+
"lib/deadweight.rb",
|
|
25
|
+
"lib/deadweight/cli.rb",
|
|
26
|
+
"test/deadweight_test.rb",
|
|
27
|
+
"test/fixtures/index.html",
|
|
28
|
+
"test/fixtures/index2.html",
|
|
29
|
+
"test/fixtures/style.css",
|
|
30
|
+
"test/test_helper.rb"
|
|
31
|
+
]
|
|
32
|
+
s.homepage = %q{http://github.com/aanand/deadweight}
|
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
|
34
|
+
s.require_paths = ["lib"]
|
|
35
|
+
s.rubygems_version = %q{1.3.4}
|
|
36
|
+
s.summary = %q{RCov for CSS}
|
|
37
|
+
s.test_files = [
|
|
38
|
+
"test/deadweight_test.rb",
|
|
39
|
+
"test/test_helper.rb"
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
if s.respond_to? :specification_version then
|
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
44
|
+
s.specification_version = 3
|
|
45
|
+
|
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
47
|
+
s.add_runtime_dependency(%q<css_parser>, [">= 0"])
|
|
48
|
+
s.add_runtime_dependency(%q<hpricot>, [">= 0"])
|
|
49
|
+
else
|
|
50
|
+
s.add_dependency(%q<css_parser>, [">= 0"])
|
|
51
|
+
s.add_dependency(%q<hpricot>, [">= 0"])
|
|
52
|
+
s.add_dependency(%q<mechanize>, [">= 0"])
|
|
53
|
+
s.add_dependency(%q<Shoulda>, [">= 0"])
|
|
54
|
+
end
|
|
55
|
+
else
|
|
56
|
+
s.add_dependency(%q<css_parser>, [">= 0"])
|
|
57
|
+
s.add_dependency(%q<hpricot>, [">= 0"])
|
|
58
|
+
s.add_dependency(%q<mechanize>, [">= 0"])
|
|
59
|
+
s.add_dependency(%q<Shoulda>, [">= 0"])
|
|
60
|
+
end
|
|
61
|
+
end
|
data/lib/deadweight.rb
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
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
|
+
case page
|
|
66
|
+
# TODO this is ugly, but just listing IO doesn't work (since it's a Module)
|
|
67
|
+
when String, StringIO, File, IO
|
|
68
|
+
html = fetch(page)
|
|
69
|
+
else
|
|
70
|
+
result = instance_eval(&page)
|
|
71
|
+
|
|
72
|
+
html = case result
|
|
73
|
+
when String
|
|
74
|
+
result
|
|
75
|
+
else
|
|
76
|
+
@agent.page.body
|
|
77
|
+
end
|
|
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
|
+
# Handle IO objects appropriately
|
|
102
|
+
return path.read if path.respond_to?(:read)
|
|
103
|
+
|
|
104
|
+
log.info(path)
|
|
105
|
+
|
|
106
|
+
loc = root + path
|
|
107
|
+
|
|
108
|
+
if @mechanize
|
|
109
|
+
loc = "file://#{File.expand_path(loc)}" unless loc =~ %r{^\w+://}
|
|
110
|
+
page = agent.get(loc)
|
|
111
|
+
log.warn("#{path} redirected to #{page.uri}") unless page.uri.to_s == loc
|
|
112
|
+
page.body
|
|
113
|
+
else
|
|
114
|
+
open(loc).read
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
private
|
|
119
|
+
|
|
120
|
+
def has_pseudo_classes(selector)
|
|
121
|
+
selector =~ /::?[\w\-]+/
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def strip(selector)
|
|
125
|
+
selector.gsub(/::?[\w\-]+/, '')
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def log
|
|
129
|
+
@log ||= Logger.new(@log_file)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def initialize_agent
|
|
133
|
+
begin
|
|
134
|
+
require 'mechanize'
|
|
135
|
+
return WWW::Mechanize.new
|
|
136
|
+
rescue LoadError
|
|
137
|
+
log.info %{
|
|
138
|
+
=================================================================
|
|
139
|
+
Couldn't load 'mechanize', which is required for remote scraping.
|
|
140
|
+
Install it like so: gem install mechanize
|
|
141
|
+
=================================================================
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
raise
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
@@ -0,0 +1,175 @@
|
|
|
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
|
+
require 'webrick/httpproxy'
|
|
126
|
+
|
|
127
|
+
@proxy = WEBrick::HTTPProxyServer.new \
|
|
128
|
+
:AccessLog => [
|
|
129
|
+
[options[:log_file], WEBrick::AccessLog::COMMON_LOG_FORMAT],
|
|
130
|
+
[options[:log_file], WEBrick::AccessLog::REFERER_LOG_FORMAT]
|
|
131
|
+
],
|
|
132
|
+
:Logger => WEBrick::Log.new(options[:log_file]),
|
|
133
|
+
:Port => options[:proxy_port],
|
|
134
|
+
:ProxyContentHandler => lambda { |request, response|
|
|
135
|
+
|
|
136
|
+
parse_this = false
|
|
137
|
+
|
|
138
|
+
if options[:whitelist]
|
|
139
|
+
p options[:whitelist]
|
|
140
|
+
options[:whitelist].each do |x|
|
|
141
|
+
if response.request_uri.to_s[0..x.length - 1].downcase == x.downcase
|
|
142
|
+
parse_this = true
|
|
143
|
+
break
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
else
|
|
147
|
+
parse_this = true
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
if parse_this
|
|
151
|
+
dw.process!(response.body)
|
|
152
|
+
|
|
153
|
+
stdout.puts "After reviewing <#{response.request_uri}>, these were left:"
|
|
154
|
+
dw.unused_selectors.each do |k,v|
|
|
155
|
+
stdout.puts "#{k} { #{v} }"
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
trap('INT') do
|
|
161
|
+
@proxy.shutdown
|
|
162
|
+
|
|
163
|
+
# dump the remaining CSS rules if output is set
|
|
164
|
+
unless options[:output] == STDOUT
|
|
165
|
+
dw.unused_selectors.each do |k,v|
|
|
166
|
+
output.puts "#{k} { #{v} }"
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
@proxy.start
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
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,86 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mojodna-deadweight
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.4
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Aanand Prasad
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-08-18 00:00:00 -07: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
|
+
- LICENSE
|
|
46
|
+
- README.rdoc
|
|
47
|
+
- Rakefile
|
|
48
|
+
- VERSION
|
|
49
|
+
- deadweight.gemspec
|
|
50
|
+
- bin/deadweight
|
|
51
|
+
- lib/deadweight.rb
|
|
52
|
+
- lib/deadweight/cli.rb
|
|
53
|
+
- test/deadweight_test.rb
|
|
54
|
+
- test/fixtures/index.html
|
|
55
|
+
- test/fixtures/index2.html
|
|
56
|
+
- test/fixtures/style.css
|
|
57
|
+
- test/test_helper.rb
|
|
58
|
+
has_rdoc: false
|
|
59
|
+
homepage: http://github.com/aanand/deadweight
|
|
60
|
+
post_install_message:
|
|
61
|
+
rdoc_options:
|
|
62
|
+
- --charset=UTF-8
|
|
63
|
+
require_paths:
|
|
64
|
+
- lib
|
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - ">="
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: "0"
|
|
70
|
+
version:
|
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: "0"
|
|
76
|
+
version:
|
|
77
|
+
requirements: []
|
|
78
|
+
|
|
79
|
+
rubyforge_project:
|
|
80
|
+
rubygems_version: 1.2.0
|
|
81
|
+
signing_key:
|
|
82
|
+
specification_version: 3
|
|
83
|
+
summary: RCov for CSS
|
|
84
|
+
test_files:
|
|
85
|
+
- test/deadweight_test.rb
|
|
86
|
+
- test/test_helper.rb
|