responsive_image_tag 0.1.1 → 0.1.2
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/Gemfile +1 -0
- data/Gemfile.lock +2 -0
- data/README.rdoc +5 -2
- data/VERSION +1 -1
- data/lib/rails/generators/responsive_image_tag/javascript/templates/responsive-image-tag.js +12 -5
- data/lib/responsive_image_tag.rb +15 -4
- data/responsive_image_tag.gemspec +4 -1
- data/test/helper.rb +1 -0
- data/test/test_responsive_image_tag.rb +55 -13
- metadata +17 -3
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -46,6 +46,9 @@ After that, you can use the new tag like so, in a view:
|
|
46
46
|
|
47
47
|
== Copyright
|
48
48
|
|
49
|
-
Copyright (c) 2011
|
50
|
-
|
49
|
+
Copyright (c) 2011 Headlondon, http://www.headlondon.com
|
50
|
+
|
51
|
+
Authors: Dave Hrycyszyn, Mairead Buchan
|
52
|
+
|
53
|
+
See LICENSE.txt for further details.
|
51
54
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
@@ -1,3 +1,6 @@
|
|
1
|
+
/*jslint browser:true */
|
2
|
+
/*global $, $$*/
|
3
|
+
|
1
4
|
//responsiveImageTag detects all noscript elements on the page with a class of responsivize.
|
2
5
|
|
3
6
|
//It retrieves both HTML5 data attributes and then creates an image tag with the correct source
|
@@ -5,8 +8,9 @@
|
|
5
8
|
//images are sent to mobile devices or any device smaller than 768px.
|
6
9
|
|
7
10
|
//The noscript tags are then removed from the page.
|
8
|
-
var responsiveImageTag =
|
9
|
-
|
11
|
+
var responsiveImageTag = {
|
12
|
+
|
13
|
+
replaceInitialImages:function() {
|
10
14
|
var platform = "desktop";
|
11
15
|
var responsiveImages = $$(".responsivize");
|
12
16
|
var i,
|
@@ -22,6 +26,8 @@ var responsiveImageTag = new function() {
|
|
22
26
|
var noScriptElem = responsiveImages[i];
|
23
27
|
var img = window.document.createElement("img");
|
24
28
|
|
29
|
+
img.alt = noScriptElem.getAttribute("data-alttext");
|
30
|
+
|
25
31
|
if(platform === "mobile"){
|
26
32
|
img.src = noScriptElem.getAttribute("data-mobilesrc");
|
27
33
|
}else{
|
@@ -32,9 +38,10 @@ var responsiveImageTag = new function() {
|
|
32
38
|
noScriptElem.previous().appendChild(img);
|
33
39
|
noScriptElem.style.display = "none";
|
34
40
|
}
|
35
|
-
}
|
36
|
-
}
|
41
|
+
}
|
42
|
+
};
|
43
|
+
|
37
44
|
document.observe("dom:loaded", function() {
|
38
45
|
responsiveImageTag.replaceInitialImages();
|
39
|
-
})
|
46
|
+
});
|
40
47
|
|
data/lib/responsive_image_tag.rb
CHANGED
@@ -9,15 +9,26 @@ module ResponsiveImageTag
|
|
9
9
|
def responsive_image_tag(small, big, options = {})
|
10
10
|
output = tag "span", {:class => "img-placeholder"}
|
11
11
|
output += tag "/span", nil, true
|
12
|
-
output += tag "noscript",
|
13
|
-
"data-fullsrc" => image_path(big),
|
14
|
-
"data-mobilesrc" => image_path(small),
|
15
|
-
:class => "responsivize"}, true
|
12
|
+
output += tag "noscript", noscript_attributes(small, big, options), true
|
16
13
|
output += image_tag(small, options)
|
17
14
|
output += tag "/noscript", nil, true
|
18
15
|
output
|
19
16
|
end
|
20
17
|
|
18
|
+
private
|
19
|
+
|
20
|
+
def noscript_attributes(small, big, options)
|
21
|
+
attrs = {
|
22
|
+
"data-fullsrc" => image_path(big),
|
23
|
+
"data-mobilesrc" => image_path(small),
|
24
|
+
:class => "responsivize"
|
25
|
+
}
|
26
|
+
unless options.nil? || options == {} || options[:alt].nil?
|
27
|
+
attrs.merge!(:alt => options[:alt])
|
28
|
+
end
|
29
|
+
attrs
|
30
|
+
end
|
31
|
+
|
21
32
|
end
|
22
33
|
|
23
34
|
ActionView::Base.send(:include, ResponsiveImageTag) if defined?(ActionView::Base)
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{responsive_image_tag}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Dave Hrycyszyn"]
|
@@ -48,12 +48,14 @@ Gem::Specification.new do |s|
|
|
48
48
|
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
49
49
|
s.add_development_dependency(%q<rcov>, [">= 0"])
|
50
50
|
s.add_development_dependency(%q<actionpack>, [">= 0"])
|
51
|
+
s.add_development_dependency(%q<nokogiri>, [">= 0"])
|
51
52
|
else
|
52
53
|
s.add_dependency(%q<minitest>, [">= 0"])
|
53
54
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
54
55
|
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
55
56
|
s.add_dependency(%q<rcov>, [">= 0"])
|
56
57
|
s.add_dependency(%q<actionpack>, [">= 0"])
|
58
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
57
59
|
end
|
58
60
|
else
|
59
61
|
s.add_dependency(%q<minitest>, [">= 0"])
|
@@ -61,6 +63,7 @@ Gem::Specification.new do |s|
|
|
61
63
|
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
62
64
|
s.add_dependency(%q<rcov>, [">= 0"])
|
63
65
|
s.add_dependency(%q<actionpack>, [">= 0"])
|
66
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
64
67
|
end
|
65
68
|
end
|
66
69
|
|
data/test/helper.rb
CHANGED
@@ -9,13 +9,23 @@ class TestResponsiveImageTag < MiniTest::Unit::TestCase
|
|
9
9
|
ENV["RAILS_ASSET_ID"] = ""
|
10
10
|
view = ActionView::Base.new
|
11
11
|
@tag = view.responsive_image_tag("small.jpg", "big.jpg")
|
12
|
+
@doc = Nokogiri::HTML.fragment(@tag)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should write in one <span>" do
|
16
|
+
assert_equal 1, @doc.xpath('span').length
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should put a img-placeholder class on the <span>" do
|
20
|
+
span_class = @doc.xpath('span').first.attributes["class"]
|
21
|
+
assert_equal "img-placeholder", span_class.value
|
12
22
|
end
|
13
23
|
|
14
24
|
it "should write an <img> tag" do
|
15
|
-
@tag.must_match "img"
|
25
|
+
@tag.must_match "<img"
|
16
26
|
end
|
17
27
|
|
18
|
-
it "should
|
28
|
+
it "should put a src attribute on <img" do
|
19
29
|
@tag.must_match "src="
|
20
30
|
end
|
21
31
|
|
@@ -31,26 +41,58 @@ class TestResponsiveImageTag < MiniTest::Unit::TestCase
|
|
31
41
|
@tag.must_match "data-fullsrc=\"/images/big.jpg\""
|
32
42
|
end
|
33
43
|
|
34
|
-
describe "
|
44
|
+
describe "the noscript tag" do
|
35
45
|
before do
|
36
|
-
|
37
|
-
@tag = view.responsive_image_tag("small.jpg", "big.jpg", :alt => "foo")
|
46
|
+
@noscript = @doc.xpath("noscript").first
|
38
47
|
end
|
39
|
-
|
40
|
-
|
48
|
+
|
49
|
+
it "should exist, once" do
|
50
|
+
assert_equal(1, @doc.xpath("noscript").length)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should have a data-fullsrc attribute" do
|
54
|
+
assert @noscript.attributes["data-fullsrc"]
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should point data-fullsrc at the big image" do
|
58
|
+
assert_equal "/images/big.jpg",
|
59
|
+
@noscript.attributes["data-fullsrc"].value
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should have a data-mobilesrc attribute" do
|
63
|
+
assert @noscript.attributes["data-mobilesrc"]
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should point data-mobilesrc at the small image" do
|
67
|
+
assert_equal "/images/small.jpg",
|
68
|
+
@noscript.attributes["data-mobilesrc"].value
|
41
69
|
end
|
42
|
-
end
|
43
70
|
|
44
|
-
|
71
|
+
it "should put the 'responsivize' class on the <noscript> tag" do
|
72
|
+
assert @noscript.attributes["class"]
|
73
|
+
assert @noscript.attributes["class"].value = "responsivize"
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "with an :alt option specified" do
|
45
79
|
before do
|
46
|
-
ENV["RAILS_ASSET_ID"] = "images/"
|
47
80
|
view = ActionView::Base.new
|
48
|
-
@tag = view.responsive_image_tag(
|
81
|
+
@tag = view.responsive_image_tag(
|
82
|
+
"small.jpg", "big.jpg", :alt => "foo")
|
83
|
+
@doc = Nokogiri::HTML.fragment(@tag)
|
49
84
|
end
|
50
85
|
|
51
|
-
it "should
|
52
|
-
@
|
86
|
+
it "should put an :alt attribute on the <noscript> tag" do
|
87
|
+
noscript = @doc.xpath("noscript").first
|
88
|
+
assert noscript.attributes["alt"]
|
53
89
|
end
|
90
|
+
|
91
|
+
it "should put a value of 'foo' into the :alt" do
|
92
|
+
noscript = @doc.xpath("noscript").first
|
93
|
+
assert_equal "foo", noscript.attributes["alt"].value
|
94
|
+
end
|
95
|
+
|
54
96
|
end
|
55
97
|
|
56
98
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: responsive_image_tag
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dave Hrycyszyn
|
@@ -92,6 +92,20 @@ dependencies:
|
|
92
92
|
requirement: *id005
|
93
93
|
prerelease: false
|
94
94
|
name: actionpack
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
type: :development
|
97
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
hash: 3
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
requirement: *id006
|
107
|
+
prerelease: false
|
108
|
+
name: nokogiri
|
95
109
|
description: Allows you to specify two images in a responsive_image_tag, and includes a javascript generator which will rewrite the DOM, requesting the correct image based on the screen size of the current client device.
|
96
110
|
email: dave.hrycyszyn@headlondon.com
|
97
111
|
executables: []
|