From 5d74cadf2feabd347bbcd935ae76d7a471aec006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 16 Mar 2015 18:57:15 +0100 Subject: [PATCH] FIX: picture would be hidden when [details] block was closed in the composer --- lib/cooked_post_processor.rb | 5 +- spec/components/cooked_post_processor_spec.rb | 46 +++++++++++++++---- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index e58f17bd1..02686aaf1 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -98,7 +98,10 @@ class CookedPostProcessor return unless image_sizes.present? image_sizes.each do |image_size| url, size = image_size[0], image_size[1] - return [size["width"], size["height"]] if url && size && url.include?(src) + if url && url.include?(src) && + size && size["width"].to_i > 0 && size["height"].to_i > 0 + return [size["width"], size["height"]] + end end end diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 6b5ff79f4..02d50f82f 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -40,20 +40,48 @@ describe CookedPostProcessor do context ".post_process_images" do - context "with image_sizes" do - - let(:post) { build(:post_with_image_urls) } - let(:cpp) { CookedPostProcessor.new(post, image_sizes: {"http://foo.bar/image.png" => {"width" => 111, "height" => 222}}) } - - before { cpp.post_process_images } - - it "works" do + shared_examples "leave dimensions alone" do + it "doesn't use them" do # adds the width from the image sizes provided when no dimension is provided - expect(cpp.html).to match(/src="http:\/\/foo.bar\/image.png" width="111" height="222"/) + expect(cpp.html).to match(/src="http:\/\/foo.bar\/image.png" width="" height=""/) # adds the width from the image sizes provided expect(cpp.html).to match(/src="http:\/\/domain.com\/picture.jpg" width="50" height="42"/) expect(cpp).to be_dirty end + end + + context "with image_sizes" do + let(:post) { build(:post_with_image_urls) } + let(:cpp) { CookedPostProcessor.new(post, image_sizes: image_sizes) } + + before { cpp.post_process_images } + + context "valid" do + let(:image_sizes) { {"http://foo.bar/image.png" => {"width" => 111, "height" => 222}} } + + it "use them" do + # adds the width from the image sizes provided when no dimension is provided + expect(cpp.html).to match(/src="http:\/\/foo.bar\/image.png" width="111" height="222"/) + # adds the width from the image sizes provided + expect(cpp.html).to match(/src="http:\/\/domain.com\/picture.jpg" width="50" height="42"/) + expect(cpp).to be_dirty + end + end + + context "invalid width" do + let(:image_sizes) { {"http://foo.bar/image.png" => {"width" => 0, "height" => 222}} } + include_examples "leave dimensions alone" + end + + context "invalid height" do + let(:image_sizes) { {"http://foo.bar/image.png" => {"width" => 111, "height" => 0}} } + include_examples "leave dimensions alone" + end + + context "invalid width & height" do + let(:image_sizes) { {"http://foo.bar/image.png" => {"width" => 0, "height" => 0}} } + include_examples "leave dimensions alone" + end end