This repository was archived by the owner on Jun 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Add alt and class #4
Open
levibrown
wants to merge
6
commits into
master
Choose a base branch
from
add_alt_and_class
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
28aafa7
add alt to picture tag + option to add class to picture element
385db1f
change class config to rbconfig
b4dfc27
update .travis.yml
397144a
revert .travis.yml
2129cd7
update readme
87a2317
fixes readme paperclip options
jessicard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
A Rails view helper extension to generate HTML5 `<picture>` tag markup | ||
from the W3C HTML Responsive Images Extension Proposal. | ||
|
||
[w3.org/community/respimg](http://www.w3.org/community/respimg) | ||
|
||
|
||
|
@@ -60,7 +60,7 @@ The vanilla usage with default sizes and media queries: | |
produces: | ||
|
||
```html | ||
<picture> | ||
<picture alt="Kitty cat!"> | ||
<source media="(min-width: 1600px)" srcset="cat-large.jpg 1x, [email protected] 2x" /> | ||
<source media="(min-width: 1000px)" srcset="cat-medium.jpg 1x, [email protected] 2x" /> | ||
<source media="(min-width: 768px)" srcset="cat-small.jpg 1x, [email protected] 2x" /> | ||
|
@@ -96,6 +96,11 @@ To preload a custom default placeholder image: | |
<%= picture_tag(image_path, default_image: '/images/placeholder.png'}) %> | ||
``` | ||
|
||
To add a class to the picture tag: | ||
|
||
```erb | ||
<%= picture_tag(image_path, picture_class: "some_class"}) %> | ||
``` | ||
|
||
All `image_tag` options are valid for `picture_tag`. | ||
See [image_tag Docs](http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html) | ||
|
@@ -107,16 +112,16 @@ Paperclip options for default media queries and sizes. | |
|
||
```ruby | ||
has_attached_file :image, { | ||
styles: { | ||
tiny: "320x", | ||
small: "480x", | ||
medium: "768x", | ||
large: "1000x", | ||
styles: { | ||
tiny: "320x", | ||
small: "480x", | ||
medium: "768x", | ||
large: "1000x", | ||
huge: "1600x", | ||
%s(tiny@2x) => "640x", | ||
%s(small@2x) => "960x", | ||
%s(medium@2x) => "1536x", | ||
%s(large@2x) => "2000x", | ||
%s(tiny@2x) => "640x", | ||
%s(small@2x) => "960x", | ||
%s(medium@2x) => "1536x", | ||
%s(large@2x) => "2000x", | ||
%s(large@2x) => "3200x" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are there two large@2x's? I think that may want to be changed to huge? |
||
} | ||
``` | ||
|
@@ -126,6 +131,7 @@ has_attached_file :image, { | |
|
||
- Add optional paperclip integration functionality | ||
- Implement Retina support | ||
- Add Travis ci Rails 4 support | ||
|
||
|
||
## Authors | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,108 @@ | ||
require 'spec_helper' | ||
describe PictureTag::ViewHelpers, :type => :helper do | ||
|
||
describe "split image path" do | ||
let(:split_image_path) { helper.split_image_path_from_extension("/path/something.s-original.jpg") } | ||
it "removes the -original" do | ||
split_image_path.first.should eq "/path/something.s" | ||
end | ||
|
||
it "splits jpg" do | ||
split_image_path.last.should eq "jpg" | ||
end | ||
|
||
end | ||
|
||
describe "building the source tag" do | ||
|
||
it "builds using a media query" do | ||
helper.build_source_tag('test.jpg', 'small', "(min-width: 100px)"). | ||
should eq "<source media='(min-width: 100px)' srcset='/images/test-small.jpg 1x, /images/[email protected] 2x' />" | ||
end | ||
|
||
it "builds without a media query" do | ||
helper.build_source_tag('/path/test.png', 'small'). | ||
should eq "<source srcset='/path/test-small.png 1x, /path/[email protected] 2x' />" | ||
end | ||
|
||
it "builds without an external path" do | ||
helper.build_source_tag('http://www.image/path/test.png', 'small',"(min-width: 100px)"). | ||
should eq "<source media='(min-width: 100px)' srcset='http://www.image/path/test-small.png 1x, http://www.image/path/[email protected] 2x' />" | ||
end | ||
|
||
it "builds given a default image" do | ||
helper.build_source_tag('/images/test.png', 'small', nil, {:default_image => '/images/test.png'}). | ||
should eq "<source srcset='/images/test.png' />" | ||
end | ||
|
||
end | ||
|
||
describe "default source and image" do | ||
it "builds source and img" do | ||
helper.add_default_source_and_image('test.jpg', 'small', {}). | ||
should eq "<source srcset='/images/test-small.jpg 1x, /images/[email protected] 2x' /><img alt=\"Test\" src=\"/images/test-small.jpg\" />" | ||
end | ||
|
||
it "lets you specify a default image size" do | ||
helper.add_default_source_and_image('test.jpg', 'any_size', {:default_size => :large}). | ||
should eq "<source srcset='/images/test-large.jpg 1x, /images/[email protected] 2x' /><img alt=\"Test\" src=\"/images/test-large.jpg\" />" | ||
end | ||
|
||
it "lets you specify a default image path" do | ||
helper.add_default_source_and_image('test.jpg', 'large', {:default_image => '/images/test.png'}). | ||
should eq "<source srcset='/images/test.png' /><img alt=\"Test\" src=\"/images/test.png\" />" | ||
end | ||
|
||
it "adds a class to the img tag" do | ||
helper.add_default_source_and_image('test.jpg', 'small', {:class => "span2"}). | ||
should eq "<source srcset='/images/test-small.jpg 1x, /images/[email protected] 2x' /><img alt=\"Test\" class=\"span2\" src=\"/images/test-small.jpg\" />" | ||
end | ||
|
||
end | ||
|
||
describe "determine sizes" do | ||
let(:sizes) { {:huge => "(min-width: 1600px)", :small => "(min-width: 500px)"} } | ||
it "puts the hash into a sorted array" do | ||
helper.determine_sizes(:sizes => sizes).should eq [[:huge, "(min-width: 1600px)"], [:small, "(min-width: 500px)"]] | ||
end | ||
|
||
it "excludes sizes larger than tha max_width" do | ||
helper.determine_sizes(:sizes => sizes, :max_width => "501px" ).should eq [[:small, "(min-width: 500px)"]] | ||
end | ||
|
||
it "keeps with an equal max width" do | ||
helper.determine_sizes(:sizes => sizes, :max_width => "500px" ).should eq [[:small, "(min-width: 500px)"]] | ||
end | ||
|
||
it "removes with a lesser max width" do | ||
helper.determine_sizes(:sizes => sizes, :max_width => "499px" ).should eq [] | ||
end | ||
|
||
end | ||
|
||
describe "options" do | ||
def html | ||
"<picture>" + | ||
def html(alt, picture_class="") | ||
"<picture alt='#{alt}'#{" class='" + picture_class + "'" unless picture_class.blank?}>" + | ||
"<source media='(min-width: 1600px)' srcset='/images/cat-huge.jpg 1x, /images/[email protected] 2x' />" + | ||
"<source media='(min-width: 1000px)' srcset='/images/cat-large.jpg 1x, /images/[email protected] 2x' />" + | ||
"<source media='(min-width: 768px)' srcset='/images/cat-medium.jpg 1x, /images/[email protected] 2x' />" + | ||
"<source media='(min-width: 480px)' srcset='/images/cat-small.jpg 1x, /images/[email protected] 2x' />" + | ||
"<source media='(min-width: 320px)' srcset='/images/cat-tiny.jpg 1x, /images/[email protected] 2x' />" + | ||
"<source srcset='/images/cat-tiny.jpg 1x, /images/[email protected] 2x' />" + | ||
"<img alt=\"Cat\" src=\"/images/cat-tiny.jpg\" />" + | ||
"<img alt=\"#{alt}\" src=\"/images/cat-tiny.jpg\" />" + | ||
"</picture>" | ||
end | ||
|
||
|
||
it "matches the complete html" do | ||
helper.picture_tag('/images/cat.jpg').should eq html | ||
helper.picture_tag('/images/cat.jpg').should eq html("Cat") | ||
end | ||
|
||
it "matches the complete html with an alt tag" do | ||
helper.picture_tag('/images/cat.jpg', :alt => "Kitty!").should eq html.gsub("Cat", "Kitty!") | ||
it "matches the complete html with an alt attribute" do | ||
helper.picture_tag('/images/cat.jpg', :alt => "Kitty!").should eq html("Kitty!") | ||
end | ||
|
||
it "matches the complete html with an class attribute" do | ||
helper.picture_tag('/images/cat.jpg', :alt => "Kitty!", :picture_class => "meow").should eq html("Kitty!", "meow") | ||
end | ||
|
||
it "prefixes the size to the filename" do | ||
|
@@ -108,7 +111,7 @@ def html | |
end | ||
|
||
it "overwrites the default sizes if sizes given" do | ||
h = "<picture>" + | ||
h = "<picture alt='Cat'>" + | ||
"<source media='(min-width: 1px)' srcset='/images/cat-hidden.jpg 1x, /images/[email protected] 2x' />" + | ||
"<source srcset='/images/cat-hidden.jpg 1x, /images/[email protected] 2x' />" + | ||
"<img alt=\"Cat\" src=\"/images/cat-hidden.jpg\" />" + | ||
|
@@ -117,11 +120,11 @@ def html | |
helper.picture_tag('/images/cat.jpg', :sizes => {:hidden => "(min-width: 1px)"}). | ||
should eq h | ||
end | ||
|
||
it "excludes source tags with a media query above the given amount" do | ||
helper.picture_tag("cat.jpg", :max_width => "500").split('source').should have(4).things | ||
end | ||
|
||
describe "prefixes the size to the filename" do | ||
it "without x" do | ||
helper.build_file_path('/path/test.png', 'small', true). | ||
|
@@ -137,13 +140,13 @@ def html | |
helper.build_file_path('path/test.png', 'small@2x', true). | ||
should eq "path/[email protected]" | ||
end | ||
|
||
it "without a path" do | ||
helper.build_file_path('test.png', 'small@2x', true). | ||
should eq "[email protected]" | ||
end | ||
|
||
end | ||
end | ||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@levibrown I think it would be better if this behaved like
image_tag
. Namely, the html class and alt should be passed in options with keysclass
andalt
. http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_tagThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍