Skip to content

Commit 8c7e394

Browse files
authored
Merge pull request rails#54226 from intrip/actiontext-plaintext-skip-script-and-style
Don't include `script` and `style` content to node plaintext conversion
2 parents b73e978 + bd0729c commit 8c7e394

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

actiontext/lib/action_text/content.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,11 @@ def render_attachment_galleries(&block)
123123
# content.to_plain_text # => "safeunsafe"
124124
#
125125
# NOTE: that the returned string is not HTML safe and should not be rendered in
126-
# browsers.
126+
# browsers without additional sanitization.
127127
#
128128
# content = ActionText::Content.new("<script>alert()</script>")
129129
# content.to_plain_text # => "<script>alert()</script>"
130+
# ActionText::ContentHelper.sanitizer.sanitize(content.to_plain_text) # => ""
130131
def to_plain_text
131132
render_attachments(with_full_attributes: false, &:to_plain_text).fragment.to_plain_text
132133
end

actiontext/lib/action_text/plain_text_conversion.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ def plain_text_for_node(node, index = 0)
2222
def plain_text_for_node_children(node)
2323
texts = []
2424
node.children.each_with_index do |child, index|
25+
next if skippable?(child)
26+
2527
texts << plain_text_for_node(child, index)
2628
end
2729
texts.join
2830
end
2931

32+
def skippable?(node)
33+
node.name == "script" || node.name == "style"
34+
end
35+
3036
def plain_text_method_for_node(node)
3137
:"plain_text_for_#{node.name}_node"
3238
end

actiontext/test/unit/plain_text_conversion_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,30 @@ class ActionText::PlainTextConversionTest < ActiveSupport::TestCase
144144
)
145145
end
146146

147+
test "script tags are ignored" do
148+
assert_converted_to(
149+
"Hello world!",
150+
<<~HTML
151+
<script type="javascript">
152+
console.log("message");
153+
</script>
154+
<div><strong>Hello </strong>world!</div>
155+
HTML
156+
)
157+
end
158+
159+
test "style tags are ignored" do
160+
assert_converted_to(
161+
"Hello world!",
162+
<<~HTML
163+
<style type="text/css">
164+
body { color: red; }
165+
</style>
166+
<div><strong>Hello </strong>world!</div>
167+
HTML
168+
)
169+
end
170+
147171
private
148172
def assert_converted_to(plain_text, html)
149173
assert_equal plain_text, ActionText::Content.new(html).to_plain_text

guides/source/action_text_overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ content as follows:
181181

182182
`ActionText::RichText#to_s` safely transforms RichText into an HTML String. On
183183
the other hand `ActionText::RichText#to_plain_text` returns a string that is not
184-
HTML safe and should not be rendered in browsers. You can learn more about
185-
Action Text's sanitization process in the [`ActionText::RichText`
184+
HTML safe and should not be rendered in browsers without additional sanitization.
185+
You can learn more about Action Text's sanitization process in the [`ActionText::RichText`
186186
documentation](https://api.rubyonrails.org/classes/ActionText/RichText.html).
187187

188188
NOTE: If there's an attached resource within `content` field, it might not show

0 commit comments

Comments
 (0)