color_parser 0.0.2 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +0 -2
- data/Guardfile +9 -0
- data/README.md +17 -26
- data/Rakefile +18 -6
- data/color_parser.gemspec +10 -2
- data/lib/color_parser.rb +3 -134
- data/lib/color_parser/errors.rb +3 -0
- data/lib/color_parser/page.rb +14 -57
- data/lib/color_parser/stylesheet.rb +27 -123
- data/lib/color_parser/version.rb +1 -1
- data/{test → spec}/fixtures/css_color/frequency.html +0 -0
- data/{test → spec}/fixtures/css_color/stylesheets/color_styles.css +7 -7
- data/{test → spec}/fixtures/css_color/stylesheets/css_elements.css +0 -0
- data/{test → spec}/fixtures/css_color/stylesheets/frequency.css +9 -9
- data/{test → spec}/fixtures/css_color/stylesheets/imported_selectors.css +0 -0
- data/{test → spec}/fixtures/css_color/stylesheets/properties.css +0 -0
- data/spec/page_spec.rb +39 -0
- data/spec/spec_helper.rb +10 -0
- data/{test/test_request.rb → spec/stubs/fake_request.rb} +2 -2
- data/spec/stylesheet_spec.rb +125 -0
- data/spec/version_spec.rb +9 -0
- metadata +114 -123
- data/lib/color_parser/image.rb +0 -20
- data/test/color_parser_test.rb +0 -83
- data/test/fixtures/css/absolute.html +0 -15
- data/test/fixtures/css/inline.html +0 -34
- data/test/fixtures/css/inline_import.html +0 -16
- data/test/fixtures/css/invalid.html +0 -15
- data/test/fixtures/css/relative.html +0 -15
- data/test/fixtures/css/relative_root.html +0 -15
- data/test/fixtures/css/stylesheets/colors.css +0 -0
- data/test/fixtures/css/stylesheets/fonts.css +0 -0
- data/test/fixtures/css/stylesheets/print.css +0 -3
- data/test/fixtures/css/stylesheets/screen.css +0 -16
- data/test/fixtures/css_images/images/apple.png +0 -0
- data/test/fixtures/css_images/images/cantaloupe.png +0 -0
- data/test/fixtures/css_images/images/kiwi.jpg +0 -0
- data/test/fixtures/css_images/images/mango.png +0 -0
- data/test/fixtures/css_images/images/pineapple.png +0 -0
- data/test/fixtures/css_images/paths.html +0 -14
- data/test/fixtures/css_images/stylesheets/import_paths.css +0 -4
- data/test/fixtures/css_images/stylesheets/paths.css +0 -17
- data/test/fixtures/css_images/stylesheets/quotes.css +0 -14
- data/test/fixtures/css_import/index.html +0 -15
- data/test/fixtures/css_import/stylesheets/borders.css +0 -0
- data/test/fixtures/css_import/stylesheets/colors.css +0 -0
- data/test/fixtures/css_import/stylesheets/fonts.css +0 -3
- data/test/fixtures/css_import/stylesheets/ie.css +0 -3
- data/test/fixtures/css_import/stylesheets/images.css +0 -0
- data/test/fixtures/css_import/stylesheets/master.css +0 -12
- data/test/fixtures/css_import/stylesheets/print.css +0 -3
- data/test/fixtures/css_import/stylesheets/screen.css +0 -12
- data/test/fixtures/inline_images/absolute.html +0 -14
- data/test/fixtures/inline_images/images/apple.png +0 -0
- data/test/fixtures/inline_images/images/kiwi.jpg +0 -0
- data/test/fixtures/inline_images/relative.html +0 -14
- data/test/fixtures/inline_images/relative_root.html +0 -14
- data/test/image_test.rb +0 -27
- data/test/page_test.rb +0 -194
- data/test/stylesheet_test.rb +0 -257
- data/test/test_helper.rb +0 -6
- data/test/version_test.rb +0 -7
data/test/page_test.rb
DELETED
@@ -1,194 +0,0 @@
|
|
1
|
-
require_relative "test_helper"
|
2
|
-
|
3
|
-
describe Page do
|
4
|
-
def setup
|
5
|
-
ColorParser.request = ColorParser::TestRequest.new
|
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
|
-
page.url.must_equal 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
|
-
page.host.must_equal "example.com"
|
20
|
-
page.path.must_equal "/css/inline.html"
|
21
|
-
page.query.must_equal "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
|
-
page.stylesheets.length.must_equal 2
|
31
|
-
|
32
|
-
# stylesheet content
|
33
|
-
sheet = page.stylesheets.first
|
34
|
-
sheet.type.must_equal "inline"
|
35
|
-
sheet.text.must_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
|
-
page.stylesheets.length.must_equal 1
|
43
|
-
|
44
|
-
page.stylesheets[0].name.must_equal "inline_import.html"
|
45
|
-
page.stylesheets[0].stylesheets[0].name.must_equal "print.css"
|
46
|
-
page.stylesheets[0].stylesheets[1].name.must_equal "fonts.css"
|
47
|
-
page.stylesheets[0].stylesheets[2].name.must_equal "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
|
-
page.stylesheets.length.must_equal 2
|
56
|
-
|
57
|
-
# stylesheet content
|
58
|
-
sheet = page.stylesheets.first
|
59
|
-
sheet.type.must_equal "external"
|
60
|
-
sheet.text.must_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
|
-
page.stylesheets.length.must_equal 2
|
69
|
-
|
70
|
-
# stylesheet content
|
71
|
-
sheet = page.stylesheets.first
|
72
|
-
sheet.type.must_equal "external"
|
73
|
-
sheet.text.must_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
|
-
page.stylesheets.length.must_equal 2
|
82
|
-
|
83
|
-
# stylesheet content
|
84
|
-
sheet = page.stylesheets.first
|
85
|
-
sheet.type.must_equal "external"
|
86
|
-
sheet.text.must_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
|
-
css.length.must_equal 2
|
95
|
-
|
96
|
-
# 5 found
|
97
|
-
css[0].name.must_include "screen.css"
|
98
|
-
css[1].name.must_include "print.css"
|
99
|
-
|
100
|
-
css[0].stylesheets[0].name.must_include "master.css"
|
101
|
-
css[0].stylesheets[1].name.must_include "fonts.css"
|
102
|
-
css[0].stylesheets[2].name.must_include "ie.css"
|
103
|
-
css[0].stylesheets[3].name.must_include "images.css"
|
104
|
-
css[0].stylesheets[4].name.must_include "borders.css"
|
105
|
-
css[0].stylesheets[5].name.must_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
|
-
page.stylesheets.length.must_equal 1
|
114
|
-
page.stylesheets[0].name.must_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
|
-
images.size.must_equal 2
|
126
|
-
|
127
|
-
images[0].url.must_equal "http://example.com/inline_images/images/apple.png"
|
128
|
-
images[1].url.must_equal "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
|
-
images.size.must_equal 2
|
137
|
-
|
138
|
-
images[0].url.must_equal "http://example.com/inline_images/images/apple.png"
|
139
|
-
images[1].url.must_equal "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
|
-
|
147
|
-
images.size.must_equal 2
|
148
|
-
|
149
|
-
images[0].url.must_equal "http://example.com/inline_images/images/apple.png"
|
150
|
-
images[1].url.must_equal "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
|
-
images.size.must_equal 5
|
162
|
-
|
163
|
-
images[0].name.must_equal "mango.png"
|
164
|
-
images[1].name.must_equal "apple.png"
|
165
|
-
images[2].name.must_equal "kiwi.jpg"
|
166
|
-
images[3].name.must_equal "cantaloupe.png"
|
167
|
-
images[4].name.must_equal "pineapple.png"
|
168
|
-
end
|
169
|
-
|
170
|
-
|
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
|
-
colors["386ec0"].must_equal 4
|
179
|
-
colors["3a5dc4"].must_equal 3
|
180
|
-
colors["718ad7"].must_equal 2
|
181
|
-
colors["ff0000"].must_equal 1
|
182
|
-
colors["357ad1"].must_equal 1
|
183
|
-
colors["535353"].must_equal 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
|
-
page.colors_by_frequency.must_equal colors
|
192
|
-
end
|
193
|
-
|
194
|
-
end
|
data/test/stylesheet_test.rb
DELETED
@@ -1,257 +0,0 @@
|
|
1
|
-
require_relative "test_helper"
|
2
|
-
|
3
|
-
describe Stylesheet do
|
4
|
-
def setup
|
5
|
-
ColorParser.request = ColorParser::TestRequest.new
|
6
|
-
end
|
7
|
-
|
8
|
-
it "must initialize params" do
|
9
|
-
sheet = ColorParser::Stylesheet.new(text: "a { background: #fff; }",
|
10
|
-
type: "inline",
|
11
|
-
url: "http://example.com/css/inline.html?foo=bar")
|
12
|
-
sheet.type.must_equal "inline"
|
13
|
-
sheet.text.must_equal "a { background: #fff; }"
|
14
|
-
sheet.url.must_equal "http://example.com/css/inline.html?foo=bar"
|
15
|
-
sheet.host.must_equal "example.com"
|
16
|
-
sheet.path.must_equal "/css/inline.html"
|
17
|
-
sheet.query.must_equal "foo=bar"
|
18
|
-
end
|
19
|
-
|
20
|
-
|
21
|
-
# Import Quoting
|
22
|
-
|
23
|
-
it "should import stylesheets with double quotes" do
|
24
|
-
css = style("/css_import/stylesheets/screen.css")
|
25
|
-
|
26
|
-
imported = css.stylesheets[0]
|
27
|
-
imported.name.must_equal "master.css"
|
28
|
-
imported.text.must_include "a:link"
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should import stylesheets with single quotes" do
|
32
|
-
css = style("/css_import/stylesheets/screen.css")
|
33
|
-
|
34
|
-
imported = css.stylesheets[1]
|
35
|
-
imported.name.must_equal "fonts.css"
|
36
|
-
imported.text.must_include "font-family"
|
37
|
-
end
|
38
|
-
|
39
|
-
it "should import stylesheets with no quotes" do
|
40
|
-
css = style("/css_import/stylesheets/screen.css")
|
41
|
-
|
42
|
-
imported = css.stylesheets[2]
|
43
|
-
imported.name.must_equal "ie.css"
|
44
|
-
imported.text.must_include "background-color"
|
45
|
-
end
|
46
|
-
|
47
|
-
|
48
|
-
# Import Paths
|
49
|
-
|
50
|
-
it "should import from relative path" do
|
51
|
-
css = style("/css_import/stylesheets/screen.css")
|
52
|
-
|
53
|
-
imported = css.stylesheets[0]
|
54
|
-
imported.url.must_equal "http://example.com/css_import/stylesheets/master.css"
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should import from relative root path" do
|
58
|
-
css = style("/css_import/stylesheets/screen.css")
|
59
|
-
|
60
|
-
imported = css.stylesheets[1]
|
61
|
-
imported.url.must_equal "http://example.com/css_import/stylesheets/fonts.css"
|
62
|
-
end
|
63
|
-
|
64
|
-
it "should import from absolute path" do
|
65
|
-
css = style("/css_import/stylesheets/screen.css")
|
66
|
-
|
67
|
-
imported = css.stylesheets[2]
|
68
|
-
imported.url.must_equal "http://example.com/css_import/stylesheets/ie.css"
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should not fail on invalid import path" do
|
72
|
-
css = style("/css_import/stylesheets/screen.css")
|
73
|
-
css.stylesheets[6].must_be_nil
|
74
|
-
end
|
75
|
-
|
76
|
-
|
77
|
-
# Selector parsing
|
78
|
-
|
79
|
-
it "should parse selectors" do
|
80
|
-
css = style("/css_color/stylesheets/properties.css")
|
81
|
-
selectors = css.selectors
|
82
|
-
|
83
|
-
selectors.size.must_equal 4
|
84
|
-
|
85
|
-
selectors["p"].size.must_equal 2
|
86
|
-
selectors["div"].size.must_equal 1
|
87
|
-
selectors["h1"].size.must_equal 2
|
88
|
-
|
89
|
-
# imported
|
90
|
-
selectors["dl"].size.must_equal 1
|
91
|
-
end
|
92
|
-
|
93
|
-
it "should parse properties" do
|
94
|
-
css = style("/css_color/stylesheets/properties.css")
|
95
|
-
props = css.properties
|
96
|
-
|
97
|
-
props.size.must_equal 9
|
98
|
-
|
99
|
-
props.must_include ["background", "#000"]
|
100
|
-
props.must_include ["color", "#fff"]
|
101
|
-
props.must_include ["margin", "0"]
|
102
|
-
props.must_include ["background-color", "#ccc"]
|
103
|
-
props.must_include ["font-size", "100%"]
|
104
|
-
props.must_include ["border-color", "rgb(1,2,3)"]
|
105
|
-
|
106
|
-
# imported
|
107
|
-
props.must_include ["border", "1px solid red"]
|
108
|
-
end
|
109
|
-
|
110
|
-
|
111
|
-
# Color Parsing
|
112
|
-
|
113
|
-
it "should parse hex colors" do
|
114
|
-
css = style("/css_color/stylesheets/color_styles.css")
|
115
|
-
|
116
|
-
css.colors["386ec0"].wont_be_nil # 386ec0
|
117
|
-
end
|
118
|
-
|
119
|
-
it "should parse hex short colors" do
|
120
|
-
css = style("/css_color/stylesheets/color_styles.css")
|
121
|
-
|
122
|
-
css.colors["cc00cc"].wont_be_nil # c0c
|
123
|
-
end
|
124
|
-
|
125
|
-
it "should parse textual colors" do
|
126
|
-
css = style("/css_color/stylesheets/color_styles.css")
|
127
|
-
|
128
|
-
css.colors["008080"].wont_be_nil # teal
|
129
|
-
end
|
130
|
-
|
131
|
-
it "should parse rgb colors" do
|
132
|
-
css = style("/css_color/stylesheets/color_styles.css")
|
133
|
-
|
134
|
-
css.colors["718ad7"].wont_be_nil # rgb(113,138,215)
|
135
|
-
end
|
136
|
-
|
137
|
-
it "should parse rgb colors with space" do
|
138
|
-
css = style("/css_color/stylesheets/color_styles.css")
|
139
|
-
|
140
|
-
css.colors["3a5dc4"].wont_be_nil # rgb(58, 93, 196)
|
141
|
-
end
|
142
|
-
|
143
|
-
it "should parse rgba colors" do
|
144
|
-
css = style("/css_color/stylesheets/color_styles.css")
|
145
|
-
|
146
|
-
css.colors["29469e"].wont_be_nil # rgba(41,70,158,0.5);
|
147
|
-
end
|
148
|
-
|
149
|
-
it "should parse rgba colors with space" do
|
150
|
-
css = style("/css_color/stylesheets/color_styles.css")
|
151
|
-
|
152
|
-
css.colors["3f6aeb"].wont_be_nil # rgba(63, 106, 235, 0.5);
|
153
|
-
end
|
154
|
-
|
155
|
-
|
156
|
-
# Color weighting
|
157
|
-
|
158
|
-
it "should order colors by frequency" do
|
159
|
-
css = style("/css_color/stylesheets/frequency.css")
|
160
|
-
|
161
|
-
colors = {"386ec0" => 4, "3a5dc4" => 3,
|
162
|
-
"718ad7" => 2, "ff0000" => 1}
|
163
|
-
css.colors.must_equal colors
|
164
|
-
end
|
165
|
-
|
166
|
-
# BG colors
|
167
|
-
it "should order background colors by frequency" do
|
168
|
-
css = style("/css_color/stylesheets/frequency.css")
|
169
|
-
|
170
|
-
colors = {"386ec0" => 2, "3a5dc4" => 1}
|
171
|
-
css.bg_colors.must_equal colors
|
172
|
-
end
|
173
|
-
|
174
|
-
# Text colors
|
175
|
-
it "should order text colors by frequency" do
|
176
|
-
css = style("/css_color/stylesheets/frequency.css")
|
177
|
-
|
178
|
-
colors = {"386ec0" => 2, "718ad7" => 1}
|
179
|
-
css.text_colors.must_equal colors
|
180
|
-
end
|
181
|
-
|
182
|
-
# Border colors
|
183
|
-
it "should order border colors by frequency" do
|
184
|
-
css = style("/css_color/stylesheets/frequency.css")
|
185
|
-
|
186
|
-
colors = {"3a5dc4" => 2, "ff0000" => 1, "718ad7" => 1}
|
187
|
-
css.border_colors.must_equal colors
|
188
|
-
end
|
189
|
-
|
190
|
-
|
191
|
-
# Images Parsing
|
192
|
-
|
193
|
-
it "should parse images with double quotes" do
|
194
|
-
css = style("/css_images/stylesheets/quotes.css")
|
195
|
-
|
196
|
-
css.images[0].name.must_equal "apple.png"
|
197
|
-
end
|
198
|
-
|
199
|
-
it "should parse images with single quotes" do
|
200
|
-
css = style("/css_images/stylesheets/quotes.css")
|
201
|
-
|
202
|
-
css.images[1].name.must_equal "kiwi.jpg"
|
203
|
-
end
|
204
|
-
|
205
|
-
it "should parse images with no quotes" do
|
206
|
-
css = style("/css_images/stylesheets/quotes.css")
|
207
|
-
|
208
|
-
css.images[2].name.must_equal "cantaloupe.png"
|
209
|
-
end
|
210
|
-
|
211
|
-
|
212
|
-
# Image paths
|
213
|
-
|
214
|
-
it "should build image from absolute path" do
|
215
|
-
css = style("/css_images/stylesheets/paths.css")
|
216
|
-
urls = css.images.map {|image| image.url }
|
217
|
-
|
218
|
-
urls.size.must_equal 4
|
219
|
-
urls.must_include "http://example.com/css_images/images/apple.png"
|
220
|
-
end
|
221
|
-
|
222
|
-
it "should build image from relative path" do
|
223
|
-
css = style("/css_images/stylesheets/paths.css")
|
224
|
-
urls = css.images.map {|image| image.url }
|
225
|
-
|
226
|
-
urls.size.must_equal 4
|
227
|
-
urls.must_include "http://example.com/css_images/images/kiwi.jpg"
|
228
|
-
end
|
229
|
-
|
230
|
-
it "should build image from relative root path" do
|
231
|
-
css = style("/css_images/stylesheets/paths.css")
|
232
|
-
urls = css.images.map {|image| image.url }
|
233
|
-
|
234
|
-
urls.size.must_equal 4
|
235
|
-
urls.must_include "http://example.com/css_images/images/cantaloupe.png"
|
236
|
-
end
|
237
|
-
|
238
|
-
it "should build image from imported path" do
|
239
|
-
css = style("/css_images/stylesheets/paths.css")
|
240
|
-
urls = css.images.map {|image| image.url }
|
241
|
-
|
242
|
-
urls.size.must_equal 4
|
243
|
-
urls.must_include "http://example.com/css_images/images/pineapple.png"
|
244
|
-
end
|
245
|
-
end
|
246
|
-
|
247
|
-
private
|
248
|
-
|
249
|
-
def style(path)
|
250
|
-
ColorParser::Stylesheet.new(text: fixture(path),
|
251
|
-
url: "http://example.com#{path}")
|
252
|
-
end
|
253
|
-
|
254
|
-
def fixture(path)
|
255
|
-
fixture = "#{File.dirname(__FILE__)}/../test/fixtures#{path}"
|
256
|
-
File.read(fixture) if File.exist?(fixture)
|
257
|
-
end
|