aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/views/action_text/attachables/_remote_image.html.erb8
-rw-r--r--lib/action_text/attribute.rb2
-rw-r--r--test/unit/content_test.rb37
3 files changed, 31 insertions, 16 deletions
diff --git a/app/views/action_text/attachables/_remote_image.html.erb b/app/views/action_text/attachables/_remote_image.html.erb
new file mode 100644
index 0000000000..3372f8d940
--- /dev/null
+++ b/app/views/action_text/attachables/_remote_image.html.erb
@@ -0,0 +1,8 @@
+<figure class="attachment attachment--preview">
+ <%= image_tag(remote_image.url, width: remote_image.width, height: remote_image.height) %>
+ <% if caption = remote_image.try(:caption) %>
+ <figcaption class="attachment__caption">
+ <%= caption %>
+ </figcaption>
+ <% end %>
+</figure>
diff --git a/lib/action_text/attribute.rb b/lib/action_text/attribute.rb
index 992a6a410a..2d520fa541 100644
--- a/lib/action_text/attribute.rb
+++ b/lib/action_text/attribute.rb
@@ -20,7 +20,7 @@ module ActionText
# If you wish to preload the dependent RichText model, you can use the named scope:
#
# Message.all.with_rich_text_content # Avoids N+1 queries when you just want the body, not the attachments.
- # Message.all.with_rich_text_content_and_emebds # Avoids N+1 queries when you just want the body and attachments.
+ # Message.all.with_rich_text_content_and_embeds # Avoids N+1 queries when you just want the body and attachments.
def has_rich_text(name)
class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{name}
diff --git a/test/unit/content_test.rb b/test/unit/content_test.rb
index c0b8ad024c..dada9cc714 100644
--- a/test/unit/content_test.rb
+++ b/test/unit/content_test.rb
@@ -3,25 +3,25 @@ require 'test_helper'
class ActionText::ContentTest < ActiveSupport::TestCase
test "equality" do
html = %Q(<div>test</div>)
- content = ActionText::Content.new(html)
- assert_equal content, ActionText::Content.new(html)
+ content = content_from_html(html)
+ assert_equal content, content_from_html(html)
assert_not_equal content, html
end
test "marshal serialization" do
- content = ActionText::Content.new("Hello!")
+ content = content_from_html("Hello!")
assert_equal content, Marshal.load(Marshal.dump(content))
end
test "roundtrips HTML without additional newlines" do
html = %Q(<div>a<br></div>)
- content = ActionText::Content.new(html)
+ content = content_from_html(html)
assert_equal html, content.to_html
end
test "extracts links" do
html = %Q(<a href="http://example.com/1">1</a><br><a href="http://example.com/1">1</a>)
- content = ActionText::Content.new(html)
+ content = content_from_html(html)
assert_equal ["http://example.com/1"], content.links
end
@@ -29,7 +29,7 @@ class ActionText::ContentTest < ActiveSupport::TestCase
attachable = create_file_blob(filename: "racecar.jpg", content_type: "image/jpg")
html = %Q(<action-text-attachment sgid="#{attachable.attachable_sgid}" caption="Captioned"></action-text-attachment>)
- content = ActionText::Content.new(html)
+ content = content_from_html(html)
assert_equal 1, content.attachments.size
attachment = content.attachments.first
@@ -40,7 +40,7 @@ class ActionText::ContentTest < ActiveSupport::TestCase
test "extracts remote image attachables" do
html = %Q(<action-text-attachment content-type="image" url="http://example.com/cat.jpg" width="100" height="100" caption="Captioned"></action-text-attachment>)
- content = ActionText::Content.new(html)
+ content = content_from_html(html)
assert_equal 1, content.attachments.size
attachment = content.attachments.first
@@ -57,51 +57,58 @@ class ActionText::ContentTest < ActiveSupport::TestCase
attachable = create_file_blob(filename: "racecar.jpg", content_type: "image/jpg")
html = %Q(<action-text-attachment sgid="#{attachable.attachable_sgid}"></action-text-attachment>)
attachable.destroy!
- content = ActionText::Content.new(html)
+ content = content_from_html(html)
assert_equal 1, content.attachments.size
assert_equal ActionText::Attachables::MissingAttachable, content.attachments.first.attachable
end
test "extracts missing attachables" do
html = %Q(<action-text-attachment sgid="missing"></action-text-attachment>)
- content = ActionText::Content.new(html)
+ content = content_from_html(html)
assert_equal 1, content.attachments.size
assert_equal ActionText::Attachables::MissingAttachable, content.attachments.first.attachable
end
test "converts Trix-formatted attachments" do
html = %Q(<figure data-trix-attachment='{"sgid":"123","contentType":"text/plain","width":100,"height":100}' data-trix-attributes='{"caption":"Captioned"}'></figure>)
- content = ActionText::Content.new(html)
+ content = content_from_html(html)
assert_equal 1, content.attachments.size
assert_equal %Q(<action-text-attachment sgid="123" content-type="text/plain" width="100" height="100" caption="Captioned"></action-text-attachment>), content.to_html
end
test "ignores Trix-formatted attachments with malformed JSON" do
html = %Q(<div data-trix-attachment='{"sgid":"garbage...'></div>)
- content = ActionText::Content.new(html)
+ content = content_from_html(html)
assert_equal 0, content.attachments.size
end
test "minifies attachment markup" do
html = %Q(<action-text-attachment sgid="123"><div>HTML</div></action-text-attachment>)
- assert_equal %Q(<action-text-attachment sgid="123"></action-text-attachment>), ActionText::Content.new(html).to_html
+ assert_equal %Q(<action-text-attachment sgid="123"></action-text-attachment>), content_from_html(html).to_html
end
test "canonicalizes attachment gallery markup" do
attachment_html = %Q(<action-text-attachment sgid="1" presentation="gallery"></action-text-attachment><action-text-attachment sgid="2" presentation="gallery"></action-text-attachment>)
html = %Q(<div class="attachment-gallery attachment-gallery--2">#{attachment_html}</div>)
- assert_equal %Q(<div>#{attachment_html}</div>), ActionText::Content.new(html).to_html
+ assert_equal %Q(<div>#{attachment_html}</div>), content_from_html(html).to_html
end
test "canonicalizes attachment gallery markup with whitespace" do
attachment_html = %Q(\n <action-text-attachment sgid="1" presentation="gallery"></action-text-attachment>\n <action-text-attachment sgid="2" presentation="gallery"></action-text-attachment>\n)
html = %Q(<div class="attachment-gallery attachment-gallery--2">#{attachment_html}</div>)
- assert_equal %Q(<div>#{attachment_html}</div>), ActionText::Content.new(html).to_html
+ assert_equal %Q(<div>#{attachment_html}</div>), content_from_html(html).to_html
end
test "canonicalizes nested attachment gallery markup" do
attachment_html = %Q(<action-text-attachment sgid="1" presentation="gallery"></action-text-attachment><action-text-attachment sgid="2" presentation="gallery"></action-text-attachment>)
html = %Q(<blockquote><div class="attachment-gallery attachment-gallery--2">#{attachment_html}</div></blockquote>)
- assert_equal %Q(<blockquote><div>#{attachment_html}</div></blockquote>), ActionText::Content.new(html).to_html
+ assert_equal %Q(<blockquote><div>#{attachment_html}</div></blockquote>), content_from_html(html).to_html
end
+
+ private
+ def content_from_html(html)
+ ActionText::Content.new(html).tap do |content|
+ assert_nothing_raised { content.to_s }
+ end
+ end
end