unused_css 0.0.1 → 0.1.0
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.
- checksums.yaml +4 -4
- data/README.md +80 -1
- data/app.rb +8 -0
- data/features/step_definitions/observation_steps.rb +8 -0
- data/features/{home_page.feature → unused_css.feature} +11 -1
- data/lib/unused_css/stylesheet.rb +5 -0
- data/lib/unused_css/version.rb +1 -1
- data/lib/unused_css.rb +7 -0
- data/public/stylesheets/style.css +6 -0
- data/views/index.erb +11 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f54b308512eec95945edaa62f63b1bbd2e80fef1
|
4
|
+
data.tar.gz: f38f1cef3d2c3aada5551dadc168103986b2506f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9dd40b52c228de3078306379c4ec5f6879825bf9a0b293ceac3d8dfb909e761bd3cebe0c3d3f203645dfba176239dd39f8a9d418703e9c1e5c5b46393eb4731e
|
7
|
+
data.tar.gz: 17e47ae4a706ddc728179eeb86213620cf420ca0234613179ab1845ace1f76bc7b5d21817ca08b30f12a134f3904499ae09072f74e6d33fe18d6671b9d66cf3e
|
data/README.md
CHANGED
@@ -3,4 +3,83 @@ Unused CSS
|
|
3
3
|
|
4
4
|
[](https://travis-ci.org/christemple/unused-css)
|
5
5
|
|
6
|
-
A Ruby gem, built from a need, to watch a suite of functional tests and gather all of the unused CSS styles.
|
6
|
+
A Ruby gem, built from a need, to watch a suite of functional tests and gather all of the unused CSS styles.
|
7
|
+
|
8
|
+
Install
|
9
|
+
-------
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem install unused_css
|
13
|
+
```
|
14
|
+
|
15
|
+
|
16
|
+
How to use
|
17
|
+
----------
|
18
|
+
|
19
|
+
Here's a typical 'features/support/env.rb' file using Unused CSS to watch the Watir webdriver as it runs your functional tests:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require "watir-webdriver"
|
23
|
+
require "unused_css"
|
24
|
+
|
25
|
+
$browser = Watir::Browser.new
|
26
|
+
$unused_css = UnusedCSS::Watcher.new
|
27
|
+
$unused_css.watch! $browser
|
28
|
+
|
29
|
+
at_exit do
|
30
|
+
$browser.close
|
31
|
+
|
32
|
+
# A list of all stylehseets together with their unused styles
|
33
|
+
puts "Unused CSS"
|
34
|
+
$unused_css.stylesheets.each do |stylesheet|
|
35
|
+
puts stylesheet.uri
|
36
|
+
stylesheet.styles.each { |style| puts style }
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
How it works
|
43
|
+
------------
|
44
|
+
|
45
|
+
Unused CSS will override the 'goto' method on Watir webdriver. After going to the specified web page, it will grab any
|
46
|
+
new stylesheets it may have found and scan all stylesheets it knows about removing any used css found on the page.
|
47
|
+
|
48
|
+
|
49
|
+
Checking for unused css/styles on your terms (AJAX scenario)
|
50
|
+
----------
|
51
|
+
|
52
|
+
I thought it would be nice to also put you in control of when you want to check for used styles on a page.
|
53
|
+
A page that relies on AJAX to dynamically load content will likely benefit from being able to do so.
|
54
|
+
|
55
|
+
In one of your step definitions you can simply call:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
When(/^I recheck the styles on the page$/) do
|
59
|
+
|
60
|
+
# Where $browser is an instance of Watir webdriver that you
|
61
|
+
# have called the unused css watch! method on
|
62
|
+
|
63
|
+
$browser.check_for_unused_styles!
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
Limitations
|
68
|
+
-----------
|
69
|
+
|
70
|
+
Unused CSS currently only works with Watir webdriver, I will be adding more webdrivers soon.
|
71
|
+
|
72
|
+
TODO
|
73
|
+
----
|
74
|
+
|
75
|
+
There are still a few things I am looking to do with this Ruby gem.
|
76
|
+
- Add support for more web drivers.
|
77
|
+
- Add ability to create report after watching the tests.
|
78
|
+
- ...not sure, you tell me :)
|
79
|
+
|
80
|
+
|
81
|
+
Feedback
|
82
|
+
--------
|
83
|
+
Please send me a message or an email and let me know what you think.
|
84
|
+
|
85
|
+
Chris Temple
|
data/app.rb
CHANGED
@@ -8,4 +8,12 @@ end
|
|
8
8
|
|
9
9
|
Then "I should see style '$style' was not used" do |style|
|
10
10
|
fail "#{style} style was used" unless $unused_css.stylesheets.styles.include? style
|
11
|
+
end
|
12
|
+
|
13
|
+
When(/^I wait for content to be loaded into the DOM via AJAX$/) do
|
14
|
+
$browser.div(id: "ajaxed-content").wait_until_present
|
15
|
+
end
|
16
|
+
|
17
|
+
When(/^I recheck the styles on the page$/) do
|
18
|
+
$browser.check_for_unused_styles!
|
11
19
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
Feature:
|
1
|
+
Feature: A user should be able to see when styles are unused
|
2
2
|
|
3
3
|
Scenario: Going to a page with a single stylesheet
|
4
4
|
Given I go to the home page
|
@@ -24,3 +24,13 @@ Feature: Home page
|
|
24
24
|
Then I should see style '.sub-heading span' was not used
|
25
25
|
Then I should see style '.old-error' was not used
|
26
26
|
|
27
|
+
|
28
|
+
Scenario: Going to a page that loads content via AJAX
|
29
|
+
Given I go to the home page
|
30
|
+
Then I should see style '#heading' was used
|
31
|
+
Then I should see style '.sub-heading' was not used
|
32
|
+
Then I should see style '#ajaxed-content' was not used
|
33
|
+
When I wait for content to be loaded into the DOM via AJAX
|
34
|
+
Then I should see 'I was pulled in by AJAX'
|
35
|
+
When I recheck the styles on the page
|
36
|
+
Then I should see style '#ajaxed-content' was used
|
@@ -22,6 +22,7 @@ end
|
|
22
22
|
|
23
23
|
|
24
24
|
class Stylesheets
|
25
|
+
include Enumerable
|
25
26
|
attr_accessor :stylesheets
|
26
27
|
|
27
28
|
def initialize
|
@@ -41,6 +42,10 @@ class Stylesheets
|
|
41
42
|
@stylesheets.find { |stylesheet| stylesheet.uri == uri }
|
42
43
|
end
|
43
44
|
|
45
|
+
def each &block
|
46
|
+
@stylesheets.each &block
|
47
|
+
end
|
48
|
+
|
44
49
|
def styles
|
45
50
|
@stylesheets.inject(Set.new) {|styles, stylesheet| styles.merge stylesheet.styles }
|
46
51
|
end
|
data/lib/unused_css/version.rb
CHANGED
data/lib/unused_css.rb
CHANGED
@@ -22,6 +22,13 @@ module UnusedCSS
|
|
22
22
|
stylesheet.remove_pseudo_styles!
|
23
23
|
end
|
24
24
|
end
|
25
|
+
|
26
|
+
def check_for_unused_styles!
|
27
|
+
@unused_css.stylesheets.each do |stylesheet|
|
28
|
+
stylesheet.styles.delete_if {|style| self.element(css: style).exist? }
|
29
|
+
stylesheet.remove_pseudo_styles!
|
30
|
+
end
|
31
|
+
end
|
25
32
|
end
|
26
33
|
watir_browser.instance_exec self, &unused_css_block
|
27
34
|
end
|
data/views/index.erb
CHANGED
@@ -4,9 +4,20 @@
|
|
4
4
|
<meta charset="UTF-8">
|
5
5
|
<title>Unused CSS</title>
|
6
6
|
<link rel="stylesheet" href="/stylesheets/style.css"/>
|
7
|
+
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.0.min.js"></script>
|
7
8
|
</head>
|
8
9
|
<body>
|
9
10
|
<div id="heading">Very nice</div>
|
10
11
|
<div class="old-error">No such style for old-error in style.css</div>
|
12
|
+
|
13
|
+
<script type="text/javascript">
|
14
|
+
$(function(){
|
15
|
+
setTimeout(function(){
|
16
|
+
$.get('/ajax-content', function(data){
|
17
|
+
$("body").append(data);
|
18
|
+
});
|
19
|
+
}, 1000);
|
20
|
+
});
|
21
|
+
</script>
|
11
22
|
</body>
|
12
23
|
</html>
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unused_css
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Temple
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: css_parser
|
@@ -126,10 +126,10 @@ files:
|
|
126
126
|
- Rakefile
|
127
127
|
- app.rb
|
128
128
|
- config.ru
|
129
|
-
- features/home_page.feature
|
130
129
|
- features/step_definitions/navigation_steps.rb
|
131
130
|
- features/step_definitions/observation_steps.rb
|
132
131
|
- features/support/env.rb
|
132
|
+
- features/unused_css.feature
|
133
133
|
- lib/unused_css.rb
|
134
134
|
- lib/unused_css/stylesheet.rb
|
135
135
|
- lib/unused_css/version.rb
|
@@ -165,7 +165,7 @@ specification_version: 4
|
|
165
165
|
summary: A Ruby gem, built from a need, to watch a suite of functional tests and gather
|
166
166
|
all of the unused CSS styles.
|
167
167
|
test_files:
|
168
|
-
- features/home_page.feature
|
169
168
|
- features/step_definitions/navigation_steps.rb
|
170
169
|
- features/step_definitions/observation_steps.rb
|
171
170
|
- features/support/env.rb
|
171
|
+
- features/unused_css.feature
|