web_resource_bundler 0.0.13
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.
- data/.bundle/config +2 -0
- data/.gitignore +21 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +10 -0
- data/README +118 -0
- data/Rakefile +29 -0
- data/VERSION +1 -0
- data/lib/web_resource_bundler.rb +195 -0
- data/lib/web_resource_bundler/content_management/block_data.rb +57 -0
- data/lib/web_resource_bundler/content_management/block_parser.rb +63 -0
- data/lib/web_resource_bundler/content_management/css_url_rewriter.rb +23 -0
- data/lib/web_resource_bundler/content_management/resource_file.rb +35 -0
- data/lib/web_resource_bundler/exceptions.rb +26 -0
- data/lib/web_resource_bundler/file_manager.rb +30 -0
- data/lib/web_resource_bundler/filters.rb +9 -0
- data/lib/web_resource_bundler/filters/base_filter.rb +28 -0
- data/lib/web_resource_bundler/filters/bundle_filter.rb +58 -0
- data/lib/web_resource_bundler/filters/bundle_filter/resource_packager.rb +49 -0
- data/lib/web_resource_bundler/filters/cdn_filter.rb +48 -0
- data/lib/web_resource_bundler/filters/image_encode_filter.rb +56 -0
- data/lib/web_resource_bundler/filters/image_encode_filter/css_generator.rb +85 -0
- data/lib/web_resource_bundler/filters/image_encode_filter/image_data.rb +51 -0
- data/lib/web_resource_bundler/rails_app_helpers.rb +65 -0
- data/lib/web_resource_bundler/settings.rb +46 -0
- data/lib/web_resource_bundler/web_resource_bundler_init.rb +17 -0
- data/spec/public/foo.css +4 -0
- data/spec/public/images/good.jpg +0 -0
- data/spec/public/images/logo.jpg +0 -0
- data/spec/public/images/sdfo.jpg +0 -0
- data/spec/public/images/too_big_image.jpg +0 -0
- data/spec/public/marketing.js +14 -0
- data/spec/public/salog20.js +6 -0
- data/spec/public/sample.css +6 -0
- data/spec/public/seal.js +10 -0
- data/spec/public/set_cookies.js +8 -0
- data/spec/public/styles/boo.css +4 -0
- data/spec/public/styles/for_import.css +7 -0
- data/spec/public/temp.css +1 -0
- data/spec/public/test.css +2 -0
- data/spec/sample_block_helper.rb +81 -0
- data/spec/spec_helper.rb +82 -0
- data/spec/web_resource_bundler/content_management/block_data_spec.rb +33 -0
- data/spec/web_resource_bundler/content_management/block_parser_spec.rb +100 -0
- data/spec/web_resource_bundler/content_management/css_url_rewriter_spec.rb +27 -0
- data/spec/web_resource_bundler/content_management/resource_file_spec.rb +37 -0
- data/spec/web_resource_bundler/file_manager_spec.rb +60 -0
- data/spec/web_resource_bundler/filters/bundle_filter/filter_spec.rb +40 -0
- data/spec/web_resource_bundler/filters/bundle_filter/resource_packager_spec.rb +41 -0
- data/spec/web_resource_bundler/filters/cdn_filter_spec.rb +76 -0
- data/spec/web_resource_bundler/filters/image_encode_filter/css_generator_spec.rb +104 -0
- data/spec/web_resource_bundler/filters/image_encode_filter/filter_spec.rb +73 -0
- data/spec/web_resource_bundler/filters/image_encode_filter/image_data_spec.rb +53 -0
- data/spec/web_resource_bundler/settings_spec.rb +45 -0
- data/spec/web_resource_bundler/web_resource_bundler_spec.rb +90 -0
- data/web_resource_bundler.gemspec +111 -0
- metadata +146 -0
@@ -0,0 +1,100 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "../../spec_helper"))
|
2
|
+
module WebResourceBundler
|
3
|
+
describe BlockParser do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@file_manager = FileManager.new(settings.resource_dir, settings.cache_dir)
|
7
|
+
@block_parser = BlockParser.new
|
8
|
+
end
|
9
|
+
|
10
|
+
#conditional comment spec
|
11
|
+
it "matches with different conditional comments" do
|
12
|
+
tests = ['<!--[if lte IE 7]>...<![endif]-->','<!--[if !(IE 7)]>...<![endif]-->',
|
13
|
+
'<!--[if IE]>...<![endif]-->']
|
14
|
+
tests.each do |test|
|
15
|
+
BlockParser::CONDITIONAL_BLOCK_PATTERN.should match(test)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
#links pattern spec
|
20
|
+
it "matches with css or js link" do
|
21
|
+
tests = ["\<link href='\/cache\/style.css' \/\>","\<script src='\/cache\/script.js' \/script\>"]
|
22
|
+
tests.each do |link|
|
23
|
+
BlockParser::LINK_PATTERN.should match(link)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#remove_links" do
|
28
|
+
it "deletes all links to resources (js, css) from block" do
|
29
|
+
block = @sample_block_helper.construct_links_block(styles, scripts)
|
30
|
+
block += @sample_block_helper.sample_inline_block
|
31
|
+
block += @sample_block_helper.construct_links_block(styles, scripts)
|
32
|
+
block += @sample_block_helper.sample_inline_block
|
33
|
+
@block_parser.remove_links(block).should == @sample_block_helper.sample_inline_block + @sample_block_helper.sample_inline_block
|
34
|
+
end
|
35
|
+
|
36
|
+
it "doesn't delete links to non js or css resource, like favicon for example" do
|
37
|
+
text = "<link href='1.css' /><link rel='shortcut icon' href='/favicon.ico' />"
|
38
|
+
@block_parser.remove_links(text).should == "<link rel='shortcut icon' href='/favicon.ico' />"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "doesn't touch remote resources" do
|
42
|
+
text = "<link href='http://google.com/1.css' type='text/css' rel='stylesheet' />"
|
43
|
+
@block_parser.remove_links(text).include?(text).should be_true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#parse" do
|
48
|
+
|
49
|
+
it "returns BlockData object" do
|
50
|
+
@block_parser.parse("").is_a?(BlockData).should be_true
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
it "returns empty BlockData when block is empty" do
|
56
|
+
data = @block_parser.parse("")
|
57
|
+
data.files.should be_empty
|
58
|
+
end
|
59
|
+
|
60
|
+
def compare_block_datas(a,b)
|
61
|
+
a.files.size.should == b.files.size
|
62
|
+
(a.files.map {|f| f.path} - b.files.map{|f| f.path}).should be_empty
|
63
|
+
a.child_blocks.size.should == b.child_blocks.size
|
64
|
+
a.condition.should == b.condition
|
65
|
+
end
|
66
|
+
|
67
|
+
it "return BlockData with all content and child inline blocks" do
|
68
|
+
block_data = @block_parser.parse(@sample_block_helper.sample_block)
|
69
|
+
compare_block_datas(block_data, @sample_block_helper.sample_block_data)
|
70
|
+
compare_block_datas(block_data.child_blocks[0], @sample_block_helper.sample_block_data.child_blocks[0])
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "#find_files" do
|
76
|
+
|
77
|
+
it "returns array of css and js files linked in block" do
|
78
|
+
result = @block_parser.find_files(@sample_block_helper.construct_links_block(styles, scripts))
|
79
|
+
files = styles + scripts
|
80
|
+
result.each do |f|
|
81
|
+
files.include?(f.path).should be_true
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
it "recognize only css and js files" do
|
86
|
+
block = "<link href='/rss.atom' /> <link href='valid.css' />"
|
87
|
+
result = @block_parser.find_files(block)
|
88
|
+
result.first.path.should == 'valid.css'
|
89
|
+
end
|
90
|
+
|
91
|
+
it "recognize files only on disk, not full urls" do
|
92
|
+
block = "<link href='http://glogle.com/web.css' /> <link href='valid.css' />"
|
93
|
+
result = @block_parser.find_files(block)
|
94
|
+
result.first.path.should == 'valid.css'
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "../../spec_helper"))
|
2
|
+
describe WebResourceBundler::CssUrlRewriter do
|
3
|
+
describe "#rewrite_relative_path" do
|
4
|
+
it "returns absolute path using url and css file path" do
|
5
|
+
tests = {
|
6
|
+
"../image.gif" => "/styles/image.gif",
|
7
|
+
"./image.gif" => "/styles/skin/image.gif",
|
8
|
+
"../../image.gif" => "/image.gif",
|
9
|
+
"../.././1.gif" => '/1.gif',
|
10
|
+
'.././../styles/./../styles/3.jpg' => '/styles/3.jpg'
|
11
|
+
}
|
12
|
+
css_file_path = "/styles/skin/1.css"
|
13
|
+
tests.each do |key, value|
|
14
|
+
CssUrlRewriter.rewrite_relative_path(css_file_path, key).should == value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
describe "#rewrite_content_urls" do
|
19
|
+
it "rewrites all urls in css block" do
|
20
|
+
css = "abracada: url\t('../image.gif'); \n backaground-image: url(\"../../image.gif\");background: url(./i/backgrounds/menu.png) repeat-x 0 100%; }"
|
21
|
+
result = "abracada: url('\/styles\/image.gif'); \n backaground-image: url('\/image.gif');background: url('/styles/skin/i/backgrounds/menu.png') repeat-x 0 100%; }"
|
22
|
+
CssUrlRewriter.rewrite_content_urls!("/styles/skin/1.css", css).should == result
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "../../spec_helper"))
|
2
|
+
describe WebResourceBundler::ResourceFile do
|
3
|
+
describe "#new_css_file" do
|
4
|
+
it "creates new resource file of css type" do
|
5
|
+
f = WebResourceBundler::ResourceFile.new_css_file('a', 'b')
|
6
|
+
f.path.should == 'a'
|
7
|
+
f.content.should == 'b'
|
8
|
+
f.type.should == WebResourceBundler::ResourceFileType::CSS
|
9
|
+
end
|
10
|
+
end
|
11
|
+
describe "#new_js_file" do
|
12
|
+
it "creates new resource file of css type" do
|
13
|
+
f = WebResourceBundler::ResourceFile.new_js_file('a', 'b')
|
14
|
+
f.path.should == 'a'
|
15
|
+
f.content.should == 'b'
|
16
|
+
f.type.should == WebResourceBundler::ResourceFileType::JS
|
17
|
+
end
|
18
|
+
end
|
19
|
+
describe "#new_mhtml_file" do
|
20
|
+
it "creates new resource file of mhtml type" do
|
21
|
+
f = WebResourceBundler::ResourceFile.new_mhtml_file('a', 'b')
|
22
|
+
f.path.should == 'a'
|
23
|
+
f.content.should == 'b'
|
24
|
+
f.type.should == WebResourceBundler::ResourceFileType::MHTML
|
25
|
+
end
|
26
|
+
end
|
27
|
+
describe "#clone" do
|
28
|
+
it "creates full clone of resource file object" do
|
29
|
+
f = WebResourceBundler::ResourceFile.new_css_file('a', 'b')
|
30
|
+
clon = f.clone
|
31
|
+
f.object_id.should_not == clon.object_id
|
32
|
+
f.path.object_id.should_not == clon.path.object_id
|
33
|
+
f.content.object_id.should_not == clon.content.object_id
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "../spec_helper"))
|
2
|
+
describe WebResourceBundler::FileManager do
|
3
|
+
before(:each) do
|
4
|
+
@settings = settings
|
5
|
+
end
|
6
|
+
|
7
|
+
def create_stub_file(name)
|
8
|
+
File.open(File.join(@settings.resource_dir, name), "w") do |file|
|
9
|
+
file.print "hi there"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
before(:each) do
|
14
|
+
temp_dir = File.join(@settings.resource_dir, 'temp')
|
15
|
+
Dir.mkdir(temp_dir) unless File.exist?(temp_dir)
|
16
|
+
@bundle_url = 'temp/bundle.dat'
|
17
|
+
@bundle_path = File.join(@settings.resource_dir, @bundle_url)
|
18
|
+
create_stub_file(@bundle_url)
|
19
|
+
@manager = FileManager.new(@settings.resource_dir, @settings.cache_dir)
|
20
|
+
end
|
21
|
+
|
22
|
+
after(:each) do
|
23
|
+
FileUtils.rm_rf(File.join(@settings.resource_dir, 'temp'))
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#create_cache_dir" do
|
27
|
+
it "creates cache dir if it doesn't exists" do
|
28
|
+
dir_path = File.join(@settings.resource_dir, @settings.cache_dir)
|
29
|
+
FileUtils.rm_rf(dir_path)
|
30
|
+
@manager.create_cache_dir
|
31
|
+
File.exist?(dir_path).should == true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#get_content" do
|
36
|
+
it "reads file and returns its content" do
|
37
|
+
@manager.get_content(@bundle_url).should == 'hi there'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#full_path" do
|
42
|
+
|
43
|
+
it "returns full path using file url" do
|
44
|
+
@manager.full_path(@bundle_url).should == @bundle_path
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#exist?" do
|
50
|
+
|
51
|
+
it "returns false when no such file in resource dir" do
|
52
|
+
@manager.exist?("non_existent_file.data").should == false
|
53
|
+
end
|
54
|
+
|
55
|
+
it "return true when file exists in resource dir" do
|
56
|
+
@manager.exist?(@bundle_url).should == true
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "../../../spec_helper"))
|
2
|
+
describe WebResourceBundler::Filters::BundleFilter::Filter do
|
3
|
+
before(:each) do
|
4
|
+
clean_cache_dir
|
5
|
+
@settings = settings
|
6
|
+
@bundle_settings = bundle_settings
|
7
|
+
@filter = Filters::BundleFilter::Filter.new(@bundle_settings, FileManager.new(@settings.resource_dir, @settings.cache_dir))
|
8
|
+
@block_data = @sample_block_helper.sample_block_data
|
9
|
+
css_type = ResourceFileType::CSS
|
10
|
+
js_type = ResourceFileType::JS
|
11
|
+
items = [@block_data.styles.map {|f| f.path}.sort] + @bundle_settings[:md5_additional_data]
|
12
|
+
@css_md5_value = Digest::MD5.hexdigest(items.flatten.join('|'))
|
13
|
+
@css_bundle_file = File.join(@settings.cache_dir, [css_type[:name] + '_' + @css_md5_value, 'en', css_type[:ext]].join('.'))
|
14
|
+
items = [@block_data.scripts.map {|f| f.path}.sort] + @bundle_settings[:md5_additional_data]
|
15
|
+
js_md5_value = Digest::MD5.hexdigest(items.flatten.join('|'))
|
16
|
+
@js_bundle_file = File.join(@settings.cache_dir, [js_type[:name] + '_' + js_md5_value, 'en', js_type[:ext]].join('.'))
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#apply" do
|
20
|
+
it "bundles each block_data resources in single file" do
|
21
|
+
@filter.apply!(@block_data)
|
22
|
+
@block_data.styles.first.path.should == @css_bundle_file
|
23
|
+
@block_data.scripts.first.path.should == @js_bundle_file
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#get_md5" do
|
28
|
+
it "returns md5 from sorted filepaths and another additional data" do
|
29
|
+
@filter.get_md5(@block_data.styles).should == @css_md5_value
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#bundle_filepath" do
|
34
|
+
it "returns filename of bundle constructed from passed files" do
|
35
|
+
@filter.bundle_filepath(WebResourceBundler::ResourceFileType::CSS, @block_data.styles).should == @css_bundle_file
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "../../../spec_helper"))
|
2
|
+
require 'digest/md5'
|
3
|
+
describe WebResourceBundler::Filters::BundleFilter::ResourcePackager do
|
4
|
+
before(:each) do
|
5
|
+
@settings = settings
|
6
|
+
file_manager = FileManager.new(@settings.resource_dir, @settings.cache_dir)
|
7
|
+
@file_packager = Filters::BundleFilter::ResourcePackager.new(@settings, file_manager)
|
8
|
+
@file_paths = styles.map do |url|
|
9
|
+
File.join(@settings.resource_dir, url)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#extract_imported_files!" do
|
14
|
+
it "returns array of imported css files" do
|
15
|
+
content = "@import 'import/first.css';\n@import 'import/second.css';"
|
16
|
+
imported_files = @file_packager.extract_imported_files!(content, 'styles/base.css')
|
17
|
+
content.should == "\n"
|
18
|
+
imported_files.should == ['styles/import/first.css', 'styles/import/second.css']
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#bundle_files" do
|
23
|
+
it "throws ResourceNotFoundError exception if one of imported files not found" do
|
24
|
+
#creating file with content with imported unexistent files
|
25
|
+
files = [WebResourceBundler::ResourceFile.new_css_file('styles/base.css', "@import 'import/first.css';\n@import 'import/second.css';")]
|
26
|
+
lambda { @file_packager.bundle_files(files) }.should raise_error(Exceptions::ResourceNotFoundError)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should bundle files in original order' do
|
30
|
+
files = [
|
31
|
+
WebResourceBundler::ResourceFile.new_js_file('files/jquery.js', 'JQUERY_FILE'),
|
32
|
+
WebResourceBundler::ResourceFile.new_js_file('files/jquery.carousel.js','JQUERY_CAROUSEL_FILE')
|
33
|
+
]
|
34
|
+
result = @file_packager.bundle_files(files)
|
35
|
+
(result =~ /JQUERY_FILE/).should < (result =~ /JQUERY_CAROUSEL_FILE/)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "../../spec_helper"))
|
2
|
+
describe WebResourceBundler::Filters::CdnFilter do
|
3
|
+
before(:each) do
|
4
|
+
@settings = settings
|
5
|
+
@cdn_settings = cdn_settings
|
6
|
+
@cdn_settings[:http_hosts] = ['http://boogle.com']
|
7
|
+
@cdn_settings[:https_hosts] = ['http://froogle.com']
|
8
|
+
@settings[:cdn_filter][:http_hosts] = @cdn_settings[:http_hosts]
|
9
|
+
@settings[:cdn_filter][:https_hosts] = @cdn_settings[:https_hosts]
|
10
|
+
@file_manager = FileManager.new(@settings.resource_dir, @settings.cache_dir)
|
11
|
+
@filter = Filters::CdnFilter::Filter.new(@cdn_settings, @file_manager)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#host_for_image" do
|
15
|
+
it "returns host for image using its hash" do
|
16
|
+
@cdn_settings[:http_hosts] << 'http://froogle.com'
|
17
|
+
url = '/images/1.gif'
|
18
|
+
@filter.host_for_image(url).should == @cdn_settings[:http_hosts][url.hash % @cdn_settings[:http_hosts].size]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns https host if request was https" do
|
22
|
+
@cdn_settings[:protocol] = 'https'
|
23
|
+
@filter = Filters::CdnFilter::Filter.new(@cdn_settings, @file_manager)
|
24
|
+
url = '/images/1.gif'
|
25
|
+
@filter.host_for_image(url).should == @cdn_settings[:https_hosts][url.hash % @cdn_settings[:https_hosts].size]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#new_filename" do
|
30
|
+
it "adds cdn_ prefix to original file name" do
|
31
|
+
path = 'styles/1.css'
|
32
|
+
@filter.new_filepath(path).should == 'cache/cdn_1.css'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#rewrite_content_urls!" do
|
37
|
+
before(:each) do
|
38
|
+
@file_path = '/styles/skin/1.css'
|
39
|
+
end
|
40
|
+
|
41
|
+
it "adds hosts to image urls" do
|
42
|
+
content = "background: url('../images/1.png');"
|
43
|
+
@filter.rewrite_content_urls!(@file_path, content)
|
44
|
+
content.should == "background: url('http://boogle.com/styles/images/1.png');"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "doesn't add hosts for images encoded in base64" do
|
48
|
+
content = "background:url('data:image/png;base64,iVBORw0KGg); *background:url(mhtml:http://domain.com/cache/base64_ie_style_9648c01be7e50284958eb07877c70e03.en.css!rails5804) no-repeat 0 100%;"
|
49
|
+
clon = content.dup
|
50
|
+
@filter.rewrite_content_urls!(@file_path, content.dup)
|
51
|
+
content.should == clon
|
52
|
+
end
|
53
|
+
|
54
|
+
it "binds image to one particular host" do
|
55
|
+
@cdn_settings[:http_hosts] << 'http://froogle.com'
|
56
|
+
@filter = Filters::CdnFilter::Filter.new(@cdn_settings, @file_manager)
|
57
|
+
content = "background: url('../images/1.png');background-image: url('../images/1.png');"
|
58
|
+
host = @cdn_settings[:http_hosts]['/styles/images/1.png'.hash % @cdn_settings[:http_hosts].size]
|
59
|
+
url = "#{host}/styles/images/1.png"
|
60
|
+
@filter.rewrite_content_urls!(@file_path, content)
|
61
|
+
content.should == "background: url('#{url}');background-image: url('#{url}');"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#apply" do
|
66
|
+
it "rewrites urls properly in all css file of given block_data" do
|
67
|
+
file = WebResourceBundler::ResourceFile.new_css_file('/temp.css', "background: url('./images/1.png');background-image: url('./images/1.png');")
|
68
|
+
block_data = BlockData.new
|
69
|
+
block_data.files = [file]
|
70
|
+
@filter.apply!(block_data)
|
71
|
+
block_data.files.first.path.should == File.join(@settings.cache_dir, 'cdn_temp.css')
|
72
|
+
block_data.files.first.content.should == "background: url('http://boogle.com/images/1.png');background-image: url('http://boogle.com/images/1.png');"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "../../../spec_helper"))
|
2
|
+
module WebResourceBundler::Filters::ImageEncodeFilter
|
3
|
+
describe CssGenerator do
|
4
|
+
before(:each) do
|
5
|
+
@settings = Settings.new base64_settings
|
6
|
+
@generator = CssGenerator.new(@settings, FileManager.new(@settings.resource_dir, @settings.cache_dir))
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#pattern" do
|
10
|
+
it "should match with correct original tags" do
|
11
|
+
correct_values = ["#{CssGenerator::TAGS[0]}:url('temp/image.png');",
|
12
|
+
" #{CssGenerator::TAGS[1]}\t :\n url('temp/image.png') ;",
|
13
|
+
"background:url('temp/1.png') repeat 0 0;"]
|
14
|
+
correct_values.each do |v|
|
15
|
+
v.should match(CssGenerator::PATTERN)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should not match with incorrect tags" do
|
20
|
+
incorrect_values = ["handy_dandy_tag:url('goody/truly.png')","background-image:urlec('asd/asdf.gif');",
|
21
|
+
"background-imageis:url('asdf/asdf.jpg');}"]
|
22
|
+
incorrect_values.each do |v|
|
23
|
+
v.should_not match(CssGenerator::PATTERN)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#encode_images_basic" do
|
29
|
+
|
30
|
+
before(:each) do
|
31
|
+
@content = "background-image: url('images/logo.jpg'); background: url('images/logo.jpg');"
|
32
|
+
@images = @generator.encode_images_basic!(@content) do |image_data, tag|
|
33
|
+
tag + image_data.extension
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
it "substitute each image tag (image should exist and has proper size) with result of a yield" do
|
38
|
+
@content.should == "background-image: jpg; background: jpg;"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns hash of images found and with proper size" do
|
42
|
+
@images.size.should == 1
|
43
|
+
@images['images/logo.jpg'].should be_an_instance_of(ImageData)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
context "no images in css file" do
|
49
|
+
before(:each) do
|
50
|
+
@path = 'path'
|
51
|
+
@content = 'margin: 10px;'
|
52
|
+
@file = WebResourceBundler::ResourceFile.new_css_file(@path, @content)
|
53
|
+
end
|
54
|
+
describe "#encode_images!" do
|
55
|
+
it "content isn't changed if no images found" do
|
56
|
+
images = @generator.encode_images!(@file.content)
|
57
|
+
@file.content.should == @content
|
58
|
+
images.should be_empty
|
59
|
+
end
|
60
|
+
end
|
61
|
+
describe "#encode_images_for_ie" do
|
62
|
+
it "returns original content if no images found" do
|
63
|
+
@generator.encode_images_for_ie!(@file.content, 'cache/1.mhtml')
|
64
|
+
@file.content.should == @content
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
69
|
+
context "css files has images" do
|
70
|
+
before(:each) do
|
71
|
+
@path = 'style.css'
|
72
|
+
@content = "background: #eeeeee url('images/logo.jpg') repeat-x 0 100%;"
|
73
|
+
@file = WebResourceBundler::ResourceFile.new_css_file(@path, @content)
|
74
|
+
end
|
75
|
+
describe "#encode_images" do
|
76
|
+
it "return images hash" do
|
77
|
+
images = @generator.encode_images!(@content)
|
78
|
+
images.size.should == 1
|
79
|
+
end
|
80
|
+
|
81
|
+
it "modifies content with encoded images" do
|
82
|
+
@generator.encode_images!(@file.content)
|
83
|
+
@file.content.include?("background: #eeeeee url('data:image").should be_true
|
84
|
+
@file.content.include?("repeat-x 0 100%").should be_true
|
85
|
+
end
|
86
|
+
end
|
87
|
+
describe "#encode_images_for_ie" do
|
88
|
+
it "changes urls to mhtml link" do
|
89
|
+
@generator.encode_images_for_ie!(@content, 'cache/1.mhtml')
|
90
|
+
@content.include?("mhtml:#{@settings.protocol}://#{@settings.domain}/cache/1.mhtml!").should be_true
|
91
|
+
@content.include?("background: #eeeeee url(mhtml:").should be_true
|
92
|
+
@content.include?("repeat-x 0 100%").should be_true
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "#construct_mhtml_link" do
|
98
|
+
it "should create link without public folder" do
|
99
|
+
@generator.construct_mhtml_link("temp.css").should == "http://#{@settings.domain}/temp.css"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|