stylesheet 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,7 +16,7 @@ module Stylesheet
16
16
 
17
17
  def parse(rules, parent)
18
18
  # clean out comments
19
- rules = rules.gsub(/\/\*[\s\S]*?\*\//, '')
19
+ rules = rules.gsub(/\/\*[\s\S]*?\*\//, '').gsub("/*", "").gsub("*/", "")
20
20
 
21
21
  # clean extraneous whitespace
22
22
  rules = rules.to_s.gsub(/\s+/m, " ").gsub(/([\};])\s/, '\1')
@@ -17,6 +17,8 @@ module Stylesheet
17
17
 
18
18
  re = /((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)\s*/
19
19
  css_text.to_s.strip.chomp(";").scan(re).flatten.each do |declaration|
20
+ next unless declaration.include?(":")
21
+
20
22
  property, value = declaration.split(":", 2)
21
23
  @declarations_list << declaration.strip
22
24
  @declarations[property.strip] = parse_value(value.strip)
@@ -62,6 +62,14 @@ module Stylesheet
62
62
 
63
63
  alias_method :rules, :css_rules
64
64
 
65
+ def import_rules
66
+ css_rules.select {|r| r.type == CssRule::IMPORT_RULE }
67
+ end
68
+
69
+ def style_rules
70
+ css_rules.select {|r| r.type == CssRule::STYLE_RULE }
71
+ end
72
+
65
73
  def parent_style_sheet
66
74
  @parent if @owner_rule
67
75
  end
@@ -1,3 +1,3 @@
1
1
  module Stylesheet
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -36,6 +36,17 @@ describe CssRuleList do
36
36
  }"
37
37
  end
38
38
 
39
+ let(:style_w_mismatched_comments) do
40
+ "body {
41
+ color: #444;
42
+ background-color: #535353;
43
+ }
44
+
45
+ */
46
+ p {
47
+ font-size: 90%;
48
+ }" end
49
+
39
50
  let(:style_w_empty_rules) do
40
51
  "#cboxOverlay{color:#ccc}#colorbox{}#cboxTopLeft{width:21px;}"
41
52
  end
@@ -73,6 +84,13 @@ describe CssRuleList do
73
84
  expect(rules[1].css_text).to eq "p { font-size: 90%;}"
74
85
  end
75
86
 
87
+ it "removes mismatched comments" do
88
+ rules = CssRuleList.new(style_w_mismatched_comments)
89
+ expect(rules.length).to eq 2
90
+ expect(rules[0].css_text).to eq "body { color: #444;background-color: #535353;}"
91
+ expect(rules[1].css_text).to eq "p { font-size: 90%;}"
92
+ end
93
+
76
94
  it "parses empty rules" do
77
95
  rules = CssRuleList.new(style_w_empty_rules)
78
96
  expect(rules.length).to eq 3
@@ -94,4 +94,18 @@ describe CssStyleDeclaration do
94
94
  expect(decl.declarations).to eq expected
95
95
  end
96
96
  end
97
+
98
+ describe "a declaration with an invalid rule" do
99
+ let(:css_text) do
100
+ "padding0;color:#1d6299;"
101
+ end
102
+
103
+ it "skips the invalid rule" do
104
+ rule = CssStyleRule.new(:css_text => "div.section { #{css_text} }")
105
+ decl = CssStyleDeclaration.new(:css_text => css_text, :parent_rule => rule)
106
+
107
+ expected = {"color" => "#1d6299"}
108
+ expect(decl.declarations).to eq expected
109
+ end
110
+ end
97
111
  end
@@ -142,6 +142,28 @@ describe CssStyleSheet do
142
142
  end
143
143
  end
144
144
 
145
+ describe "#import_rules" do
146
+ it "returns all import rules from style sheet" do
147
+ sheet = CssStyleSheet.new("http://example.com/css_import/stylesheets/screen.css")
148
+ expect(sheet.import_rules.length).to eq 9
149
+
150
+ sheet.import_rules.each do |rule|
151
+ expect(rule.type).to eq CssRule::IMPORT_RULE
152
+ end
153
+ end
154
+ end
155
+
156
+ describe "#style_rules" do
157
+ it "returns all style rules from style sheet" do
158
+ sheet = CssStyleSheet.new("http://example.com/css_import/stylesheets/screen.css")
159
+ expect(sheet.style_rules.length).to eq 1
160
+
161
+ sheet.style_rules.each do |rule|
162
+ expect(rule.type).to eq CssRule::STYLE_RULE
163
+ end
164
+ end
165
+ end
166
+
145
167
  describe "#owner_rule" do
146
168
  let(:parent) do
147
169
  CssStyleSheet.new("http://example.com/css_import/stylesheets/screen.css")
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.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: