Skip to content

Commit 9196a5b

Browse files
jagthedrummerrafaelfranca
authored andcommitted
ActionText: Strip content attribute if it's empty during render_attachments
Images and other attachments added to a rich text area aren't being displayed in the editor when you go back to edit the record again. Fixes: rails#52233 Update actiontext/lib/action_text/content.rb Co-authored-by: Petrik de Heus <[email protected]> add a missing end after merging a suggestion from github move the changelog entry to the top Update actiontext/lib/action_text/content.rb Co-authored-by: Willian Gustavo Veiga <[email protected]> Update actiontext/test/unit/content_test.rb Co-authored-by: Petrik de Heus <[email protected]> Update actiontext/test/unit/content_test.rb Co-authored-by: Petrik de Heus <[email protected]> clean up after merging suggestions on GitHub
1 parent efeee78 commit 9196a5b

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

actiontext/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
* Strip `content` attribute if the key is present but the value is empty
2+
3+
*Jeremy Green*
4+
15
* Rename `rich_text_area` methods into `rich_textarea`
26

37
Old names are still available as aliases.
48

59
*Sean Doyle*
610

7-
811
* Only sanitize `content` attribute when present in attachments.
912

1013
*Petrik de Heus*

actiontext/lib/action_text/content.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ def append_attachables(attachables)
9797

9898
def render_attachments(**options, &block)
9999
content = fragment.replace(ActionText::Attachment.tag_name) do |node|
100-
if node.key? "content"
101-
node["content"] = sanitize_content_attachment(node["content"])
100+
if node.key?("content")
101+
sanitized_content = sanitize_content_attachment(node.remove_attribute("content").to_s)
102+
node["content"] = sanitized_content if sanitized_content.present?
102103
end
103104
block.call(attachment_for_node(node, **options))
104105
end

actiontext/test/unit/content_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ class ActionText::ContentTest < ActiveSupport::TestCase
164164
assert_equal trix_html, content_from_html(html).to_trix_html.strip
165165
end
166166

167+
test "removes content attribute if it's value is empty" do
168+
html = '<action-text-attachment sgid="123" content=""></action-text-attachment>'
169+
trix_html = '<figure data-trix-attachment="{&quot;sgid&quot;:&quot;123&quot;}"></figure>'
170+
assert_equal trix_html, content_from_html(html).to_trix_html.strip
171+
end
172+
173+
test "removes content attribute if it's value is empty after sanitization" do
174+
html = '<action-text-attachment sgid="123" content="<script></script>"></action-text-attachment>'
175+
trix_html = '<figure data-trix-attachment="{&quot;sgid&quot;:&quot;123&quot;}"></figure>'
176+
assert_equal trix_html, content_from_html(html).to_trix_html.strip
177+
end
178+
167179
test "does not add missing content attribute" do
168180
html = '<action-text-attachment sgid="123"></action-text-attachment>'
169181
trix_html = '<figure data-trix-attachment="{&quot;sgid&quot;:&quot;123&quot;}"></figure>'

0 commit comments

Comments
 (0)