stylesheet 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/stylesheet.rb CHANGED
@@ -21,6 +21,7 @@ require 'stylesheet/css_charset_rule'
21
21
  require 'stylesheet/css_import_rule'
22
22
  require 'stylesheet/css_media_rule'
23
23
  require 'stylesheet/css_font_face_rule'
24
+ require 'stylesheet/css_null_rule'
24
25
 
25
26
  require 'stylesheet/css_style_declaration'
26
27
 
@@ -0,0 +1,16 @@
1
+ module Stylesheet
2
+ class CssNullRule < CssRule
3
+
4
+ def type
5
+ CssRule::NULL_RULE
6
+ end
7
+
8
+ def self.matches_rule?(text)
9
+ true
10
+ end
11
+
12
+ private
13
+
14
+ def parse_css_text; end
15
+ end
16
+ end
@@ -1,5 +1,6 @@
1
1
  module Stylesheet
2
2
  class CssRule
3
+ NULL_RULE = 0
3
4
  STYLE_RULE = 1
4
5
  CHARSET_RULE = 2
5
6
  IMPORT_RULE = 3
@@ -50,7 +51,7 @@ module Stylesheet
50
51
 
51
52
  private
52
53
 
53
- def parse_css_text(css_text)
54
+ def parse_css_text
54
55
  raise NotImplementedError
55
56
  end
56
57
  end
@@ -22,7 +22,7 @@ module Stylesheet
22
22
  rules = rules.to_s.gsub(/\s+/m, " ").gsub(/([\};])\s/, '\1')
23
23
 
24
24
  directive_re = "@.+?;"
25
- rules_re = ".+?\{.+?\}"
25
+ rules_re = ".+?\{.*?\}"
26
26
  split_rules = rules.scan(/(#{directive_re}|#{rules_re})/im).map {|r| r[0] }
27
27
 
28
28
  split_rules.map do |css_text|
@@ -15,7 +15,8 @@ module Stylesheet
15
15
  def css_text=(css_text)
16
16
  @declarations_list = []
17
17
 
18
- css_text.to_s.strip.chomp(";").split(";").each do |declaration|
18
+ re = /((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)\s*/
19
+ css_text.to_s.strip.chomp(";").scan(re).flatten.each do |declaration|
19
20
  property, value = declaration.split(":", 2)
20
21
  @declarations_list << declaration.strip
21
22
  @declarations[property.strip] = parse_value(value.strip)
@@ -1,3 +1,3 @@
1
1
  module Stylesheet
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -35,6 +35,10 @@ describe CssRuleList do
35
35
  font-size: 90%;
36
36
  }"
37
37
  end
38
+
39
+ let(:style_w_empty_rules) do
40
+ "#cboxOverlay{color:#ccc}#colorbox{}#cboxTopLeft{width:21px;}"
41
+ end
38
42
 
39
43
  describe ".new" do
40
44
  it "parses charset rules" do
@@ -68,6 +72,14 @@ describe CssRuleList do
68
72
  expect(rules[0].css_text).to eq "body { color: #444;background-color: #535353;}"
69
73
  expect(rules[1].css_text).to eq "p { font-size: 90%;}"
70
74
  end
75
+
76
+ it "parses empty rules" do
77
+ rules = CssRuleList.new(style_w_empty_rules)
78
+ expect(rules.length).to eq 3
79
+ expect(rules[0].css_text).to eq "#cboxOverlay{color:#ccc}"
80
+ expect(rules[1].css_text).to eq "#colorbox{}"
81
+ expect(rules[2].css_text).to eq "#cboxTopLeft{width:21px;}"
82
+ end
71
83
  end
72
84
 
73
85
  describe "#[]" do
@@ -26,6 +26,15 @@ describe CssRule do
26
26
  src: url(\"http://example.com/fonts/VeraSeBd.ttf\");
27
27
  }"
28
28
  end
29
+
30
+ let(:moz_doc_text) do
31
+ "@-moz-document url-prefix() {
32
+ .rec_details .rec-ellipsis:before {
33
+ float:right;
34
+ content: url(/fade.png);
35
+ }
36
+ }"
37
+ end
29
38
 
30
39
  describe ".factory" do
31
40
  it "should build an a css style rule" do
@@ -58,6 +67,12 @@ describe CssRule do
58
67
  :parent_style_sheet => Object.new)
59
68
  expect(rule).to be_kind_of(CssFontFaceRule)
60
69
  end
70
+
71
+ it "should build a null rule" do
72
+ rule = CssRule.factory(:css_text => moz_doc_text,
73
+ :parent_style_sheet => Object.new)
74
+ expect(rule).to be_kind_of(CssNullRule)
75
+ end
61
76
  end
62
77
 
63
78
  describe "#css_text" do
@@ -79,4 +79,19 @@ describe CssStyleDeclaration do
79
79
  expect(declaration.overflow).to eq ""
80
80
  end
81
81
  end
82
+
83
+ describe "a declaration with base64 encoded rule" do
84
+ let(:css_text) do
85
+ "background:url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIi);color:#1d6299;"
86
+ end
87
+
88
+ it "returns the css style declaration for rules with base64 data" do
89
+ rule = CssStyleRule.new(:css_text => "div.section { #{css_text} }")
90
+ decl = CssStyleDeclaration.new(:css_text => css_text, :parent_rule => rule)
91
+
92
+ expected = {"background" => "url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIi)",
93
+ "color" => "#1d6299"}
94
+ expect(decl.declarations).to eq expected
95
+ end
96
+ end
82
97
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stylesheet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
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-05-13 00:00:00.000000000 Z
12
+ date: 2013-05-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: curb
@@ -148,6 +148,7 @@ files:
148
148
  - lib/stylesheet/css_font_face_rule.rb
149
149
  - lib/stylesheet/css_import_rule.rb
150
150
  - lib/stylesheet/css_media_rule.rb
151
+ - lib/stylesheet/css_null_rule.rb
151
152
  - lib/stylesheet/css_rule.rb
152
153
  - lib/stylesheet/css_rule_list.rb
153
154
  - lib/stylesheet/css_style_declaration.rb