color_parser 0.1.0 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +0 -2
  3. data/Guardfile +2 -2
  4. data/README.md +9 -18
  5. data/color_parser.gemspec +3 -3
  6. data/lib/color_parser.rb +2 -48
  7. data/lib/color_parser/page.rb +14 -57
  8. data/lib/color_parser/stylesheet.rb +27 -116
  9. data/lib/color_parser/version.rb +1 -1
  10. data/spec/fixtures/css_color/stylesheets/color_styles.css +7 -7
  11. data/spec/fixtures/css_color/stylesheets/frequency.css +9 -9
  12. data/spec/page_spec.rb +27 -182
  13. data/spec/stylesheet_spec.rb +43 -153
  14. metadata +31 -126
  15. data/lib/color_parser/color.rb +0 -154
  16. data/lib/color_parser/image.rb +0 -20
  17. data/lib/color_parser/request.rb +0 -21
  18. data/spec/color_parser_spec.rb +0 -88
  19. data/spec/color_spec.rb +0 -10
  20. data/spec/fixtures/css/absolute.html +0 -15
  21. data/spec/fixtures/css/inline.html +0 -34
  22. data/spec/fixtures/css/inline_import.html +0 -16
  23. data/spec/fixtures/css/invalid.html +0 -15
  24. data/spec/fixtures/css/relative.html +0 -15
  25. data/spec/fixtures/css/relative_root.html +0 -15
  26. data/spec/fixtures/css/stylesheets/colors.css +0 -0
  27. data/spec/fixtures/css/stylesheets/fonts.css +0 -0
  28. data/spec/fixtures/css/stylesheets/print.css +0 -3
  29. data/spec/fixtures/css/stylesheets/screen.css +0 -16
  30. data/spec/fixtures/css_images/images/apple.png +0 -0
  31. data/spec/fixtures/css_images/images/cantaloupe.png +0 -0
  32. data/spec/fixtures/css_images/images/kiwi.jpg +0 -0
  33. data/spec/fixtures/css_images/images/mango.png +0 -0
  34. data/spec/fixtures/css_images/images/pineapple.png +0 -0
  35. data/spec/fixtures/css_images/paths.html +0 -14
  36. data/spec/fixtures/css_images/stylesheets/import_paths.css +0 -4
  37. data/spec/fixtures/css_images/stylesheets/paths.css +0 -17
  38. data/spec/fixtures/css_images/stylesheets/quotes.css +0 -14
  39. data/spec/fixtures/css_import/index.html +0 -15
  40. data/spec/fixtures/css_import/stylesheets/borders.css +0 -0
  41. data/spec/fixtures/css_import/stylesheets/colors.css +0 -0
  42. data/spec/fixtures/css_import/stylesheets/fonts.css +0 -3
  43. data/spec/fixtures/css_import/stylesheets/ie.css +0 -3
  44. data/spec/fixtures/css_import/stylesheets/images.css +0 -0
  45. data/spec/fixtures/css_import/stylesheets/master.css +0 -12
  46. data/spec/fixtures/css_import/stylesheets/print.css +0 -3
  47. data/spec/fixtures/css_import/stylesheets/screen.css +0 -12
  48. data/spec/fixtures/inline_images/absolute.html +0 -14
  49. data/spec/fixtures/inline_images/images/apple.png +0 -0
  50. data/spec/fixtures/inline_images/images/kiwi.jpg +0 -0
  51. data/spec/fixtures/inline_images/relative.html +0 -14
  52. data/spec/fixtures/inline_images/relative_root.html +0 -14
  53. data/spec/image_spec.rb +0 -33
  54. data/spec/request_spec.rb +0 -12
@@ -1,34 +1,34 @@
1
1
  /* backgrounds */
2
- div {
2
+ div.one {
3
3
  background-color: #3a5dc4;
4
4
  }
5
- div {
5
+ div.two {
6
6
  background: #386ec0 url("/images/something.png");
7
7
  }
8
- div {
8
+ div.three {
9
9
  background: #386ec0 url("/images/something.png");
10
10
  }
11
11
 
12
12
 
13
13
  /* text */
14
- div {
14
+ div.four {
15
15
  color: #718ad7;
16
16
  }
17
- h1 {
17
+ h1.five {
18
18
  color: #386ec0;
19
19
  }
20
- h2 {
20
+ h2.six {
21
21
  color: #386ec0;
22
22
  }
23
23
 
24
24
  /* borders */
25
- div {
25
+ div.seven {
26
26
  border-color: #718ad7;
27
27
  }
28
- div {
28
+ div.eight {
29
29
  border: 1px solid #3a5dc4;
30
30
  }
31
- div {
31
+ div.nine {
32
32
  border: 1px solid #3a5dc4;
33
33
  }
34
34
 
@@ -2,193 +2,38 @@ require 'spec_helper'
2
2
 
3
3
  describe Page do
4
4
  before(:each) do
5
- ColorParser.request = FakeRequest.new
5
+ Stylesheet.request = FakeRequest.new
6
6
  end
7
-
8
- it "should initialize url" do
9
- url = "http://example.com/css/inline.html?foo=bar"
10
- page = ColorParser::Page.new(url)
11
-
12
- expect(page.url).to eq url
13
- end
14
-
15
- it "should parse url" do
16
- url = "http://example.com/css/inline.html?foo=bar"
17
- page = ColorParser::Page.new(url)
18
-
19
- expect(page.host).to eq "example.com"
20
- expect(page.path).to eq "/css/inline.html"
21
- expect(page.query).to eq "foo=bar"
22
- end
23
-
24
- # Stylesheet sources
25
- it "should build styles from inline css" do
26
- url = "http://example.com/css/inline.html"
27
- page = ColorParser::Page.new(url)
28
-
29
- # 2 found
30
- expect(page.stylesheets.length).to eq 2
31
-
32
- # stylesheet content
33
- sheet = page.stylesheets.first
34
- expect(sheet.type).to eq "inline"
35
- expect(sheet.text).to include "background"
36
- end
37
-
38
- it "should build styles with inine css with import" do
39
- url = "http://example.com/css/inline_import.html"
40
- page = ColorParser::Page.new(url)
41
-
42
- expect(page.stylesheets.length).to eq 1
43
-
44
- expect(page.stylesheets[0].name).to eq "inline_import.html"
45
- expect(page.stylesheets[0].stylesheets[0].name).to eq "print.css"
46
- expect(page.stylesheets[0].stylesheets[1].name).to eq "fonts.css"
47
- expect(page.stylesheets[0].stylesheets[2].name).to eq "colors.css"
48
- end
49
-
50
- it "should build styles from external relative css" do
51
- url = "http://example.com/css/relative.html"
52
- page = ColorParser::Page.new(url)
53
-
54
- # 2 found
55
- expect(page.stylesheets.length).to eq 2
56
-
57
- # stylesheet content
58
- sheet = page.stylesheets.first
59
- expect(sheet.type).to eq "external"
60
- expect(sheet.text).to include "background"
61
- end
62
-
63
- it "should build styles from external relative root css" do
64
- url = "http://example.com/css/relative_root.html"
65
- page = ColorParser::Page.new(url)
66
-
67
- # 2 found
68
- expect(page.stylesheets.length).to eq 2
69
-
70
- # stylesheet content
71
- sheet = page.stylesheets.first
72
- expect(sheet.type).to eq "external"
73
- expect(sheet.text).to include "background"
74
- end
75
-
76
- it "should build styles from external absolute css" do
77
- url = "http://example.com/css/relative.html"
78
- page = ColorParser::Page.new(url)
79
-
80
- # 2 found
81
- expect(page.stylesheets.length).to eq 2
82
-
83
- # stylesheet content
84
- sheet = page.stylesheets.first
85
- expect(sheet.type).to eq "external"
86
- expect(sheet.text).to include "background"
87
- end
88
-
89
- it "should build styles from imported css" do
90
- url = "http://example.com/css_import/index.html"
91
- page = ColorParser::Page.new(url)
92
- css = page.stylesheets
93
-
94
- expect(css.length).to eq 2
95
-
96
- # 5 found
97
- expect(css[0].name).to include "screen.css"
98
- expect(css[1].name).to include "print.css"
99
-
100
- expect(css[0].stylesheets[0].name).to include "master.css"
101
- expect(css[0].stylesheets[1].name).to include "fonts.css"
102
- expect(css[0].stylesheets[2].name).to include "ie.css"
103
- expect(css[0].stylesheets[3].name).to include "images.css"
104
- expect(css[0].stylesheets[4].name).to include "borders.css"
105
- expect(css[0].stylesheets[5].name).to include "colors.css"
106
- end
107
-
108
- it "should not fail from an invalid css path" do
109
- url = "http://example.com/css/invalid.html"
110
- page = ColorParser::Page.new(url)
111
-
112
- # 1 found
113
- expect(page.stylesheets.length).to eq 1
114
- expect(page.stylesheets[0].name).to include "screen.css"
115
- end
116
-
117
-
118
- # IMAGES
119
-
120
- it "should build images from inline images with relative paths" do
121
- url = "http://example.com/inline_images/relative.html"
122
- page = ColorParser::Page.new(url)
123
- images = page.images
124
-
125
- expect(images.size).to eq 2
126
-
127
- expect(images[0].url).to eq "http://example.com/inline_images/images/apple.png"
128
- expect(images[1].url).to eq "http://example.com/inline_images/images/kiwi.jpg"
129
- end
130
-
131
- it "should build images from inline images with relative root paths" do
132
- url = "http://example.com/inline_images/relative_root.html"
133
- page = ColorParser::Page.new(url)
134
- images = page.images
135
-
136
- expect(images.size).to eq 2
137
-
138
- expect(images[0].url).to eq "http://example.com/inline_images/images/apple.png"
139
- expect(images[1].url).to eq "http://example.com/inline_images/images/kiwi.jpg"
140
- end
141
-
142
- it "should build images from inline images with absolute paths" do
143
- url = "http://example.com/inline_images/absolute.html"
144
- page = ColorParser::Page.new(url)
145
- images = page.images
146
7
 
147
- expect(images.size).to eq 2
148
-
149
- expect(images[0].url).to eq "http://example.com/inline_images/images/apple.png"
150
- expect(images[1].url).to eq "http://example.com/inline_images/images/kiwi.jpg"
151
- end
152
-
153
-
154
- # STYLESHEET IMAGES
155
-
156
- it "should combine images from inline external and import styles" do
157
- url = "http://example.com/css_images/paths.html"
158
- page = ColorParser::Page.new(url)
159
- images = page.images
160
-
161
- expect(images.size).to eq 5
8
+ describe ".new" do
9
+
10
+ it "should initialize url" do
11
+ url = "http://example.com/css/inline.html?foo=bar"
12
+ page = ColorParser::Page.new(url)
162
13
 
163
- expect(images[0].name).to eq "mango.png"
164
- expect(images[1].name).to eq "apple.png"
165
- expect(images[2].name).to eq "kiwi.jpg"
166
- expect(images[3].name).to eq "cantaloupe.png"
167
- expect(images[4].name).to eq "pineapple.png"
14
+ expect(page.url).to eq url
15
+ end
168
16
  end
169
17
 
18
+ describe "#colors" do
19
+ let :page do
20
+ ColorParser::Page.new("http://example.com/css_color/frequency.html")
21
+ end
170
22
 
171
- # STYLESHEET COLORS
172
-
173
- it "should combine colors from external and import styles" do
174
- url = "http://example.com/css_color/frequency.html"
175
- page = ColorParser::Page.new(url)
176
- colors = page.colors
177
-
178
- expect(colors["386ec0"]).to eq 4
179
- expect(colors["3a5dc4"]).to eq 3
180
- expect(colors["718ad7"]).to eq 2
181
- expect(colors["ff0000"]).to eq 1
182
- expect(colors["357ad1"]).to eq 1
183
- expect(colors["535353"]).to eq 1
184
- end
185
-
186
- it "should solot colors by frequency" do
187
- url = "http://example.com/css_color/frequency.html"
188
- page = ColorParser::Page.new(url)
189
-
190
- colors = ["386ec0", "3a5dc4", "718ad7", "535353", "357ad1", "ff0000"]
191
- expect(page.colors_by_frequency).to eq colors
23
+ it "should combine colors from external and import styles" do
24
+ colors = page.colors
25
+
26
+ expect(colors["386ec0"]).to eq 4
27
+ expect(colors["3a5dc4"]).to eq 3
28
+ expect(colors["718ad7"]).to eq 2
29
+ expect(colors["ff0000"]).to eq 1
30
+ expect(colors["357ad1"]).to eq 1
31
+ expect(colors["535353"]).to eq 1
32
+ end
33
+
34
+ it "should solot colors by frequency" do
35
+ colors = ["386ec0", "3a5dc4", "718ad7", "535353", "357ad1", "ff0000"]
36
+ expect(page.colors_by_frequency).to eq colors
37
+ end
192
38
  end
193
-
194
39
  end
@@ -2,96 +2,42 @@ require 'spec_helper'
2
2
 
3
3
  describe Stylesheet do
4
4
  before(:each) do
5
- ColorParser.request = FakeRequest.new
5
+ Stylesheet.request = FakeRequest.new
6
6
  end
7
7
 
8
- describe "#new" do
9
- it "must initialize params" do
10
- sheet = ColorParser::Stylesheet.new(text: "a { background: #fff; }",
11
- type: "inline",
12
- url: "http://example.com/css/inline.html?foo=bar")
13
- expect(sheet.type).to eq "inline"
14
- expect(sheet.text).to eq "a { background: #fff; }"
15
- expect(sheet.url).to eq "http://example.com/css/inline.html?foo=bar"
16
- expect(sheet.host).to eq "example.com"
17
- expect(sheet.path).to eq "/css/inline.html"
18
- expect(sheet.query).to eq "foo=bar"
19
- end
20
- end
21
-
22
- describe "import quoting" do
8
+ describe "#rules" do
23
9
  let :style do
24
- ColorParser::Stylesheet.new(text: fixture("/css_import/stylesheets/screen.css"),
25
- url: "http://example.com/css_import/stylesheets/screen.css")
26
- end
27
-
28
- it "should import stylesheets with double quotes" do
29
- imported = style.stylesheets[0]
30
- expect(imported.name).to eq "master.css"
31
- expect(imported.text).to include "a:link"
10
+ url = "http://example.com/css_color/stylesheets/properties.css"
11
+ css = ::Stylesheet::CssStyleSheet.new(url)
12
+
13
+ stylesheet = ColorParser::Stylesheet.new(css)
32
14
  end
15
+
16
+ it "should parse rules" do
17
+ rules = style.rules
18
+
19
+ expect(rules.size).to eq 4
33
20
 
34
- it "should import stylesheets with single quotes" do
35
- imported = style.stylesheets[1]
36
- expect(imported.name).to eq "fonts.css"
37
- expect(imported.text).to include "font-family"
38
- end
21
+ expect(rules["p"].size).to eq 3
22
+ expect(rules["div"].size).to eq 2
23
+ expect(rules["h1"].size).to eq 3
39
24
 
40
- it "should import stylesheets with no quotes" do
41
- imported = style.stylesheets[2]
42
- expect(imported.name).to eq "ie.css"
43
- expect(imported.text).to include "background-color"
25
+ # imported
26
+ expect(rules["dl"].size).to eq 1
44
27
  end
45
28
  end
46
-
47
- describe "import paths" do
48
- let :style do
49
- ColorParser::Stylesheet.new(text: fixture("/css_import/stylesheets/screen.css"),
50
- url: "http://example.com/css_import/stylesheets/screen.css")
51
- end
52
-
53
- it "should import from relative path" do
54
- imported = style.stylesheets[0]
55
- expect(imported.url).to eq "http://example.com/css_import/stylesheets/master.css"
56
- end
57
-
58
- it "should import from relative root path" do
59
- imported = style.stylesheets[1]
60
- expect(imported.url).to eq "http://example.com/css_import/stylesheets/fonts.css"
61
- end
62
29
 
63
- it "should import from absolute path" do
64
- imported = style.stylesheets[2]
65
- expect(imported.url).to eq "http://example.com/css_import/stylesheets/ie.css"
66
- end
67
-
68
- it "should not fail on invalid import path" do
69
- expect(style.stylesheets[6]).to be_nil
70
- end
71
- end
72
-
73
- describe "selector parsing" do
30
+ describe "#properties" do
74
31
  let :style do
75
- ColorParser::Stylesheet.new(text: fixture("/css_color/stylesheets/properties.css"),
76
- url: "http://example.com/css_color/stylesheets/properties.css")
32
+ url = "http://example.com/css_color/stylesheets/properties.css"
33
+ css = ::Stylesheet::CssStyleSheet.new(url)
34
+
35
+ stylesheet = ColorParser::Stylesheet.new(css)
77
36
  end
78
37
 
79
- it "should parse selectors" do
80
- selectors = style.selectors
81
-
82
- expect(selectors.size).to eq 4
83
-
84
- expect(selectors["p"].size).to eq 2
85
- expect(selectors["div"].size).to eq 1
86
- expect(selectors["h1"].size).to eq 2
87
-
88
- # imported
89
- expect(selectors["dl"].size).to eq 1
90
- end
91
-
92
38
  it "should parse properties" do
93
39
  props = style.properties
94
-
40
+
95
41
  expect(props.size).to eq 9
96
42
 
97
43
  expect(props).to include ["background", "#000"]
@@ -106,65 +52,69 @@ describe Stylesheet do
106
52
  end
107
53
  end
108
54
 
109
- describe "color parsing" do
55
+ describe "#colors" do
110
56
  let :style do
111
- ColorParser::Stylesheet.new(text: fixture("/css_color/stylesheets/color_styles.css"),
112
- url: "http://example.com/css_color/stylesheets/color_styles.css")
113
- end
57
+ url = "http://example.com/css_color/stylesheets/color_styles.css"
58
+ css = ::Stylesheet::CssStyleSheet.new(url)
114
59
 
60
+ stylesheet = ColorParser::Stylesheet.new(css)
61
+ end
62
+
115
63
  it "should parse hex colors" do
116
64
  expect(style.colors["386ec0"]).to be # 386ec0
117
65
  end
118
-
66
+
119
67
  it "should parse hex short colors" do
120
68
  expect(style.colors["cc00cc"]).to be # c0c
121
69
  end
122
-
70
+
123
71
  it "should parse textual colors" do
124
72
  expect(style.colors["008080"]).to be # teal
125
73
  end
126
-
74
+
127
75
  it "should parse rgb colors" do
128
76
  expect(style.colors["718ad7"]).to be # rgb(113,138,215)
129
77
  end
130
-
78
+
131
79
  it "should parse rgb colors with space" do
132
80
  expect(style.colors["3a5dc4"]).to be # rgb(58, 93, 196)
133
81
  end
134
-
82
+
135
83
  it "should parse rgba colors" do
136
84
  expect(style.colors["29469e"]).to be # rgba(41,70,158,0.5);
137
85
  end
138
-
86
+
139
87
  it "should parse rgba colors with space" do
140
88
  expect(style.colors["3f6aeb"]).to be # rgba(63, 106, 235, 0.5);
141
89
  end
142
90
  end
143
-
91
+
144
92
  describe "color weighting" do
145
93
  let :style do
146
- ColorParser::Stylesheet.new(text: fixture("/css_color/stylesheets/frequency.css"),
147
- url: "http://example.com/css_color/stylesheets/frequency.css")
148
- end
94
+ url = "http://example.com/css_color/stylesheets/frequency.css"
95
+ css = ::Stylesheet::CssStyleSheet.new(url)
149
96
 
97
+ stylesheet = ColorParser::Stylesheet.new(css)
98
+ end
99
+
150
100
  it "should order colors by frequency" do
151
101
  colors = {"386ec0" => 4, "3a5dc4" => 3,
152
102
  "718ad7" => 2, "ff0000" => 1}
153
103
  expect(style.colors).to eq colors
154
104
  end
155
-
105
+
156
106
  # BG colors
157
107
  it "should order background colors by frequency" do
158
108
  colors = {"386ec0" => 2, "3a5dc4" => 1}
159
109
  expect(style.bg_colors).to eq colors
160
110
  end
161
-
111
+
162
112
  # Text colors
163
113
  it "should order text colors by frequency" do
164
114
  colors = {"386ec0" => 2, "718ad7" => 1}
165
115
  expect(style.text_colors).to eq colors
166
116
  end
167
-
117
+
168
118
  # Border colors
169
119
  it "should order border colors by frequency" do
170
120
  colors = {"3a5dc4" => 2, "ff0000" => 1, "718ad7" => 1}
@@ -172,64 +122,4 @@ describe Stylesheet do
172
122
  end
173
123
  end
174
124
 
175
- describe "image parsing" do
176
- let :style do
177
- ColorParser::Stylesheet.new(text: fixture("/css_images/stylesheets/quotes.css"),
178
- url: "http://example.com/css_images/stylesheets/quotes.css")
179
- end
180
-
181
- it "should parse images with double quotes" do
182
- expect(style.images[0].name).to eq "apple.png"
183
- end
184
-
185
- it "should parse images with single quotes" do
186
- expect(style.images[1].name).to eq "kiwi.jpg"
187
- end
188
-
189
- it "should parse images with no quotes" do
190
- expect(style.images[2].name).to eq "cantaloupe.png"
191
- end
192
- end
193
-
194
- describe "image paths" do
195
- let :style do
196
- ColorParser::Stylesheet.new(text: fixture("/css_images/stylesheets/paths.css"),
197
- url: "http://example.com/css_images/stylesheets/paths.css")
198
- end
199
-
200
- it "should build image from absolute path" do
201
- urls = style.images.map {|image| image.url }
202
-
203
- expect(urls.size).to eq 4
204
- expect(urls).to include "http://example.com/css_images/images/apple.png"
205
- end
206
-
207
- it "should build image from relative path" do
208
- urls = style.images.map {|image| image.url }
209
-
210
- expect(urls.size).to eq 4
211
- expect(urls).to include "http://example.com/css_images/images/kiwi.jpg"
212
- end
213
-
214
- it "should build image from relative root path" do
215
- urls = style.images.map {|image| image.url }
216
-
217
- expect(urls.size).to eq 4
218
- expect(urls).to include "http://example.com/css_images/images/cantaloupe.png"
219
- end
220
-
221
- it "should build image from imported path" do
222
- urls = style.images.map {|image| image.url }
223
-
224
- expect(urls.size).to eq 4
225
- expect(urls).to include "http://example.com/css_images/images/pineapple.png"
226
- end
227
- end
228
- end
229
-
230
- private
231
-
232
- def fixture(path)
233
- fixture = "#{File.dirname(__FILE__)}/../spec/fixtures#{path}"
234
- File.read(fixture) if File.exist?(fixture)
235
125
  end