2018-10-08 17:44:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-04 19:43:11 -05:00
|
|
|
require "test_helper"
|
2018-10-02 13:56:27 -04:00
|
|
|
|
|
|
|
class ActionText::ContentTest < ActiveSupport::TestCase
|
|
|
|
test "equality" do
|
2019-01-04 19:43:11 -05:00
|
|
|
html = "<div>test</div>"
|
2018-10-06 12:11:10 -04:00
|
|
|
content = content_from_html(html)
|
|
|
|
assert_equal content, content_from_html(html)
|
2018-10-02 13:56:27 -04:00
|
|
|
assert_not_equal content, html
|
|
|
|
end
|
|
|
|
|
|
|
|
test "marshal serialization" do
|
2018-10-06 12:11:10 -04:00
|
|
|
content = content_from_html("Hello!")
|
2018-10-02 13:56:27 -04:00
|
|
|
assert_equal content, Marshal.load(Marshal.dump(content))
|
|
|
|
end
|
|
|
|
|
|
|
|
test "roundtrips HTML without additional newlines" do
|
2019-01-04 19:43:11 -05:00
|
|
|
html = "<div>a<br></div>"
|
2018-10-06 12:11:10 -04:00
|
|
|
content = content_from_html(html)
|
2018-10-02 13:56:27 -04:00
|
|
|
assert_equal html, content.to_html
|
2018-02-08 12:01:46 -05:00
|
|
|
end
|
2018-10-02 13:56:27 -04:00
|
|
|
|
|
|
|
test "extracts links" do
|
2019-01-04 19:43:11 -05:00
|
|
|
html = '<a href="http://example.com/1">1</a><br><a href="http://example.com/1">1</a>'
|
2018-10-06 12:11:10 -04:00
|
|
|
content = content_from_html(html)
|
2018-10-02 13:56:27 -04:00
|
|
|
assert_equal ["http://example.com/1"], content.links
|
|
|
|
end
|
|
|
|
|
|
|
|
test "extracts attachables" do
|
|
|
|
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>)
|
|
|
|
|
2018-10-06 12:11:10 -04:00
|
|
|
content = content_from_html(html)
|
2018-10-02 13:56:27 -04:00
|
|
|
assert_equal 1, content.attachments.size
|
|
|
|
|
|
|
|
attachment = content.attachments.first
|
|
|
|
assert_equal "Captioned", attachment.caption
|
|
|
|
assert_equal attachable, attachment.attachable
|
|
|
|
end
|
|
|
|
|
|
|
|
test "extracts remote image attachables" do
|
2019-01-04 19:43:11 -05:00
|
|
|
html = '<action-text-attachment content-type="image" url="http://example.com/cat.jpg" width="100" height="100" caption="Captioned"></action-text-attachment>'
|
2018-10-02 13:56:27 -04:00
|
|
|
|
2018-10-06 12:11:10 -04:00
|
|
|
content = content_from_html(html)
|
2018-10-02 13:56:27 -04:00
|
|
|
assert_equal 1, content.attachments.size
|
|
|
|
|
|
|
|
attachment = content.attachments.first
|
|
|
|
assert_equal "Captioned", attachment.caption
|
|
|
|
|
|
|
|
attachable = attachment.attachable
|
|
|
|
assert_kind_of ActionText::Attachables::RemoteImage, attachable
|
|
|
|
assert_equal "http://example.com/cat.jpg", attachable.url
|
|
|
|
assert_equal "100", attachable.width
|
|
|
|
assert_equal "100", attachable.height
|
|
|
|
end
|
|
|
|
|
|
|
|
test "identifies destroyed attachables as missing" do
|
|
|
|
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!
|
2018-10-06 12:11:10 -04:00
|
|
|
content = content_from_html(html)
|
2018-10-02 13:56:27 -04:00
|
|
|
assert_equal 1, content.attachments.size
|
|
|
|
assert_equal ActionText::Attachables::MissingAttachable, content.attachments.first.attachable
|
|
|
|
end
|
|
|
|
|
2018-10-02 19:36:01 -04:00
|
|
|
test "extracts missing attachables" do
|
2019-01-04 19:43:11 -05:00
|
|
|
html = '<action-text-attachment sgid="missing"></action-text-attachment>'
|
2018-10-06 12:11:10 -04:00
|
|
|
content = content_from_html(html)
|
2018-10-02 19:36:01 -04:00
|
|
|
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>)
|
2018-10-06 12:11:10 -04:00
|
|
|
content = content_from_html(html)
|
2018-10-02 19:36:01 -04:00
|
|
|
assert_equal 1, content.attachments.size
|
2019-01-04 19:43:11 -05:00
|
|
|
assert_equal '<action-text-attachment sgid="123" content-type="text/plain" width="100" height="100" caption="Captioned"></action-text-attachment>', content.to_html
|
2018-10-02 19:36:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "ignores Trix-formatted attachments with malformed JSON" do
|
|
|
|
html = %Q(<div data-trix-attachment='{"sgid":"garbage...'></div>)
|
2018-10-06 12:11:10 -04:00
|
|
|
content = content_from_html(html)
|
2018-10-02 19:36:01 -04:00
|
|
|
assert_equal 0, content.attachments.size
|
|
|
|
end
|
|
|
|
|
|
|
|
test "minifies attachment markup" do
|
2019-01-04 19:43:11 -05:00
|
|
|
html = '<action-text-attachment sgid="123"><div>HTML</div></action-text-attachment>'
|
|
|
|
assert_equal '<action-text-attachment sgid="123"></action-text-attachment>', content_from_html(html).to_html
|
2018-10-02 19:36:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "canonicalizes attachment gallery markup" do
|
2019-01-04 19:43:11 -05:00
|
|
|
attachment_html = '<action-text-attachment sgid="1" presentation="gallery"></action-text-attachment><action-text-attachment sgid="2" presentation="gallery"></action-text-attachment>'
|
2018-10-02 19:36:01 -04:00
|
|
|
html = %Q(<div class="attachment-gallery attachment-gallery--2">#{attachment_html}</div>)
|
2019-01-04 19:43:11 -05:00
|
|
|
assert_equal "<div>#{attachment_html}</div>", content_from_html(html).to_html
|
2018-10-02 19:36:01 -04:00
|
|
|
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>)
|
2019-01-04 19:43:11 -05:00
|
|
|
assert_equal "<div>#{attachment_html}</div>", content_from_html(html).to_html
|
2018-10-02 19:36:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "canonicalizes nested attachment gallery markup" do
|
2019-01-04 19:43:11 -05:00
|
|
|
attachment_html = '<action-text-attachment sgid="1" presentation="gallery"></action-text-attachment><action-text-attachment sgid="2" presentation="gallery"></action-text-attachment>'
|
2018-10-02 19:36:01 -04:00
|
|
|
html = %Q(<blockquote><div class="attachment-gallery attachment-gallery--2">#{attachment_html}</div></blockquote>)
|
2019-01-04 19:43:11 -05:00
|
|
|
assert_equal "<blockquote><div>#{attachment_html}</div></blockquote>", content_from_html(html).to_html
|
2018-10-02 19:36:01 -04:00
|
|
|
end
|
2018-10-06 12:11:10 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def content_from_html(html)
|
|
|
|
ActionText::Content.new(html).tap do |content|
|
|
|
|
assert_nothing_raised { content.to_s }
|
|
|
|
end
|
|
|
|
end
|
2018-02-08 12:01:46 -05:00
|
|
|
end
|