Skip to content

Commit

Permalink
Merge branch 'alt_copyright_in_image_views'
Browse files Browse the repository at this point in the history
  • Loading branch information
dohzya committed Nov 27, 2013
2 parents a263aaa + fca60ea commit e6312b8
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 9 deletions.
8 changes: 5 additions & 3 deletions lib/prismic/fragments/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ def get_view(key)
class ViewDoesNotExistException < Error ; end

class View
attr_accessor :url, :width, :height
attr_accessor :url, :width, :height, :alt, :copyright

def initialize(url, width, height)
def initialize(url, width, height, alt, copyright)
@url = url
@width = width
@height = height
@alt = alt
@copyright = copyright
end

def ratio
return @width / @height
end

def as_html(link_resolver=nil)
%(<img src="#@url" width="#@width" height="#@height" />)
%(<img src="#@url" alt="#@alt" width="#@width" height="#@height" />)
end

end
Expand Down
8 changes: 8 additions & 0 deletions lib/prismic/fragments/structured_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ def height
@view.height
end

def alt
@view.alt
end

def copyright
@view.copyright
end

def as_html(link_resolver)
view.as_html(link_resolver)
end
Expand Down
8 changes: 6 additions & 2 deletions lib/prismic/json_parsers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def image_parser(json)
def self.view_parser(json)
Prismic::Fragments::Image::View.new(json['url'],
json['dimensions']['width'],
json['dimensions']['height'])
json['dimensions']['height'],
json['alt'],
json['copyright'])
end

main = view_parser(json['value']['main'])
Expand Down Expand Up @@ -127,7 +129,9 @@ def self.span_parser(span)
view = Prismic::Fragments::Image::View.new(
block['url'],
block['dimensions']['width'],
block['dimensions']['height']
block['dimensions']['height'],
block['alt'],
block['copyright']
)
Prismic::Fragments::StructuredText::Block::Image.new(view)
when 'embed'
Expand Down
31 changes: 27 additions & 4 deletions spec/fragments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@
@url = 'my_url'
@width = 10
@height = 2
@view = Prismic::Fragments::Image::View.new(@url, @width, @height)
@view = Prismic::Fragments::Image::View.new(@url, @width, @height, "", "")
end

describe 'ratio' do
Expand All @@ -300,13 +300,23 @@
it "returns an element whose `height` attribute equals the height" do
Nokogiri::XML(@view.as_html).child.attribute('height').value.should == @height.to_s
end

it "if set, returns an element whose `alt` attribute equals the alt" do
@alt = "Alternative text"
@view.alt = @alt
Nokogiri::XML(@view.as_html).child.attribute('alt').value.should == @alt
end

# it "if not set, alt attribute is absent" do
# Nokogiri::XML(@view.as_html).child.attribute('alt').should == nil
# end
end
end

describe 'Image' do
before do
@main_view = Prismic::Fragments::Image::View.new('my_url', 10, 10)
@another_view = Prismic::Fragments::Image::View.new('my_url2', 20, 20)
@main_view = Prismic::Fragments::Image::View.new('my_url', 10, 10, "Alternative", "CC-BY")
@another_view = Prismic::Fragments::Image::View.new('my_url2', 20, 20, "", "")
@image = Prismic::Fragments::Image.new(@main_view, { 'another_view' => @another_view })
end

Expand All @@ -330,6 +340,7 @@
Nokogiri::XML(@image.as_html).child.attribute('src').value.should == Nokogiri::XML(@main_view.as_html).child.attribute('src').value
Nokogiri::XML(@image.as_html).child.attribute('width').value.should == Nokogiri::XML(@main_view.as_html).child.attribute('width').value
Nokogiri::XML(@image.as_html).child.attribute('height').value.should == Nokogiri::XML(@main_view.as_html).child.attribute('height').value
Nokogiri::XML(@image.as_html).child.attribute('alt').value.should == Nokogiri::XML(@main_view.as_html).child.attribute('alt').value
end
end
end
Expand Down Expand Up @@ -415,7 +426,7 @@

describe 'StructuredText::Image' do
before do
@view = Prismic::Fragments::Image::View.new('my_url', 10, 10)
@view = Prismic::Fragments::Image::View.new('my_url', 10, 10, "Aternative", "CC-BY")
@image = Prismic::Fragments::StructuredText::Block::Image.new(@view)
end

Expand All @@ -436,6 +447,18 @@
@image.height.should == @view.height
end
end

describe 'alt' do
it "returns the view's alt" do
@image.alt.should == @view.alt
end
end

describe 'copyright' do
it "returns the view's copyright" do
@image.copyright.should == @view.copyright
end
end
end

describe 'StructuredText::Hyperlink' do
Expand Down
8 changes: 8 additions & 0 deletions spec/json_parsers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@
"value": {
"main": {
"url": "url1",
"alt" : "Alternative",
"copyright" : "CC-BY",
"dimensions": {
"width": 500,
"height": 500
Expand All @@ -149,6 +151,8 @@
"views": {
"icon": {
"url": "url2",
"alt" : "Alternative2",
"copyright" : "CC-0",
"dimensions": {
"width": 250,
"height": 250
Expand All @@ -166,9 +170,13 @@
image.main.url.should == "url1"
image.main.width.should == 500
image.main.height.should == 500
image.main.alt.should == "Alternative"
image.main.copyright.should == "CC-BY"
image.views['icon'].url.should == "url2"
image.views['icon'].width.should == 250
image.views['icon'].height.should == 250
image.views['icon'].alt.should == "Alternative2"
image.views['icon'].copyright.should == "CC-0"
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/responses_mocks/document.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
"value": {
"main": {
"url": "https://wroomio.s3.amazonaws.com/lesbonneschoses/0417110ebf2dc34a3e8b7b28ee4e06ac82473b70.png",
"alt" : "Alternative text to image",
"copyright" : "CC-BY",
"dimensions": {
"width": 500,
"height": 500
Expand All @@ -64,6 +66,8 @@
"views": {
"icon": {
"url": "https://wroomio.s3.amazonaws.com/lesbonneschoses/babdc3421037f9af77720d8f5dcf1b84c912c6ba.png",
"alt" : "Alternative text to view",
"copyright" : "CC-BY",
"dimensions": {
"width": 250,
"height": 250
Expand Down
4 changes: 4 additions & 0 deletions spec/responses_mocks/fragments.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
"value": {
"main": {
"url": "https://wroomio.s3.amazonaws.com/lesbonneschoses/0417110ebf2dc34a3e8b7b28ee4e06ac82473b70.png",
"alt" : "Alternative text to image",
"copyright" : "CC-BY",
"dimensions": {
"width": 500,
"height": 500
Expand All @@ -68,6 +70,8 @@
"views": {
"icon": {
"url": "https://wroomio.s3.amazonaws.com/lesbonneschoses/babdc3421037f9af77720d8f5dcf1b84c912c6ba.png",
"alt" : "Alternative text to view",
"copyright" : "CC-BY",
"dimensions": {
"width": 250,
"height": 250
Expand Down

0 comments on commit e6312b8

Please sign in to comment.