css_counter 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c98217fa8af20c35dcf512f7b84c759f7394b11f
4
+ data.tar.gz: 7a34b9593ac9838a09b71c29e85b50770fc21b1e
5
+ SHA512:
6
+ metadata.gz: 19e4b7c4ed115c746306525fd34b4c74fb93c9062a86272690d58896babf19f59a43a1b2cba97f6e84ce4403928db0648b88acc2d22521254464746c289b3e12
7
+ data.tar.gz: 0d03040c15af2b811b4d91a98ec4cab0e9ee99bba31dd03e604e4c2d464d2197c379b2f6b448c81d4490754340e83a4707db69cc24bdaa2099e9808b0a562832
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ v0.0.1 (14th February 2013)
2
+ * initial release
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Peter Yandell
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,21 @@
1
+ A (very) small library that counts selectors in a CSS file.
2
+
3
+ This is mainly useful to detect when your CSS files have crossed the 4095
4
+ selector limit imposed by Internet Explorer 9 and lower.
5
+
6
+ = Installation
7
+
8
+ gem install css_counter
9
+
10
+ = Usage
11
+
12
+ require 'css_counter'
13
+
14
+ css = <<-EOS
15
+ h1 { color: red }
16
+ h2 { color: green }
17
+ EOS
18
+
19
+ CssCounter.new(css).selectors
20
+ => 2
21
+
@@ -0,0 +1,39 @@
1
+ require 'crass'
2
+ require 'ir_b'
3
+
4
+ class CssCounter
5
+
6
+ def initialize(css)
7
+ @css = css
8
+ end
9
+
10
+ def selectors
11
+ @selectors ||= count_selectors_in_css_string(@css)
12
+ end
13
+
14
+ private
15
+
16
+ def count_selectors_in_css_string(string)
17
+ tree = Crass.parse(string)
18
+ selector_count = tree.select { |item|
19
+ item[:node] == :style_rule
20
+ }.map { |item|
21
+ item[:selector][:value].split(",")
22
+ }.flatten.size
23
+ media_queries = tree.select { |item|
24
+ item[:node] == :at_rule && item[:name] == "media"
25
+ }
26
+ media_queries.each do |mq|
27
+ selector_count += count_selectors_in_mq(string, mq)
28
+ end
29
+ selector_count
30
+ end
31
+
32
+ def count_selectors_in_mq(css, mq)
33
+ startpos = mq[:block][:tokens][1][:pos]
34
+ endpos = mq[:block][:tokens][-2][:pos]
35
+ local_css = css[startpos, endpos-startpos]
36
+ count_selectors_in_css_string(local_css)
37
+ end
38
+
39
+ end
@@ -0,0 +1,50 @@
1
+ require 'css_counter'
2
+
3
+ describe CssCounter do
4
+
5
+ describe "#count" do
6
+ context "with simple.css" do
7
+ let!(:input) { File.read(File.dirname(__FILE__) + "/fixtures/simple.css") }
8
+ subject { CssCounter.new(input).selectors }
9
+
10
+ it { should == 1 }
11
+ end
12
+
13
+ context "with simple-two.css" do
14
+ let!(:input) { File.read(File.dirname(__FILE__) + "/fixtures/simple-two.css") }
15
+ subject { CssCounter.new(input).selectors }
16
+
17
+ it { should == 2 }
18
+ end
19
+
20
+ context "with simple-three.css" do
21
+ let!(:input) { File.read(File.dirname(__FILE__) + "/fixtures/simple-three.css") }
22
+ subject { CssCounter.new(input).selectors }
23
+
24
+ it { should == 3 }
25
+ end
26
+
27
+ context "with multiple.css" do
28
+ let!(:input) { File.read(File.dirname(__FILE__) + "/fixtures/multiple.css") }
29
+ subject { CssCounter.new(input).selectors }
30
+
31
+ it { should == 2 }
32
+ end
33
+
34
+ context "with mediaquery.css" do
35
+ let!(:input) { File.read(File.dirname(__FILE__) + "/fixtures/mediaquery.css") }
36
+ subject { CssCounter.new(input).selectors }
37
+
38
+ it { should == 2 }
39
+ end
40
+
41
+ context "with mediaquery.css" do
42
+ let!(:input) { File.read(File.dirname(__FILE__) + "/fixtures/tc-application.css") }
43
+ subject { CssCounter.new(input).selectors }
44
+
45
+ # TODO blesscss reports this file has 4467 selectors. Who is correct?
46
+ #it { should == 4467 }
47
+ it { should == 4326 }
48
+ end
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: css_counter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - James Healy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: crass
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.2'
55
+ description: a (very) small library that counts selectors in a CSS file
56
+ email:
57
+ - james@yob.id.au
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - CHANGELOG
63
+ - MIT-LICENSE
64
+ - README.rdoc
65
+ - lib/css_counter.rb
66
+ - spec/css_counter_spec.rb
67
+ homepage: http://github.com/yob/css_counter
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options:
73
+ - "--title"
74
+ - CSS Counter
75
+ - "--line-numbers"
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.2.0
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: a (very) small library that counts selectors in a CSS file
94
+ test_files:
95
+ - spec/css_counter_spec.rb