Skip to content
This repository was archived by the owner on Jun 11, 2022. It is now read-only.

Add alt and class #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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" />
Expand Down Expand Up @@ -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"}) %>
Copy link
Contributor

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 keys class and alt. http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_tag

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

```

All `image_tag` options are valid for `picture_tag`.
See [image_tag Docs](http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html)
Expand All @@ -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"

Choose a reason for hiding this comment

The 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?

}
```
Expand All @@ -126,6 +131,7 @@ has_attached_file :image, {

- Add optional paperclip integration functionality
- Implement Retina support
- Add Travis ci Rails 4 support


## Authors
Expand Down
9 changes: 6 additions & 3 deletions lib/picture_tag-rails/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ module ViewHelpers

def picture_tag(image_path, options={})
sizes = determine_sizes(options)
alt_text = "alt='#{options[:alt] ||= alt_tag(image_path)}'"
picture_class = options[:picture_class].blank? ? "" : " class='#{options[:picture_class]}'"

html = "<picture>"
html = "<picture #{alt_text + picture_class}>"
sizes.each do |size, media_query|
html << build_source_tag(image_path, size, media_query, options)
end
Expand Down Expand Up @@ -36,7 +38,7 @@ def build_source_tag(image_path, size, media_query=nil, options={})
srcset = options[:default_image] if options[:default_image].eql?(image_path)
"<source #{"media='#{media_query}' " if media_query}srcset='#{srcset}' />"
end

def add_default_source_and_image(image_path, size, options)
prefix_size = options[:prefix_size]
if options[:default_image]
Expand All @@ -47,7 +49,7 @@ def add_default_source_and_image(image_path, size, options)
options[:prefix_size] = false
size = options[:default_size]
end

img_src = build_file_path(image_path, size, prefix_size, options)
html = build_source_tag(image_path, size, nil, options)
html << image_tag(img_src, normalize_options(options, image_path))
Expand All @@ -60,6 +62,7 @@ def normalize_options(options, image_path)
options[:max_width] = nil
options[:default_size] = nil
options[:default_image] = nil
options[:picture_class] = nil
options
end

Expand Down
67 changes: 35 additions & 32 deletions spec/lib/view_helpers_spec.rb
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
Expand All @@ -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\" />" +
Expand All @@ -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).
Expand All @@ -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
6 changes: 3 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

require "action_controller/railtie"

module Config
module RbConfig
class Application < ::Rails::Application
config.active_support.deprecation = :stderr
config.active_support.deprecation = :stderr
end
end
Config::Application.initialize!
RbConfig::Application.initialize!

require 'rspec/rails'
require 'picture_tag-rails'