Skip to content

Commit

Permalink
Add new scrubber suitable for strip tags but adding whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jhottenstein committed May 8, 2024
1 parent a105af7 commit f8f50fc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/rails/html/scrubbers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,33 @@ def scrub(node)
end
end
end

# === Rails::HTML::SpaceBlockElementScrubber
#
# +Rails::HTML::SpaceBlockElementScrubber+ removes all undisplayable tags and contents.
#
# It preserves text contents and inserts a space around block level elements.
class SpaceBlockElementScrubber < Loofah::Scrubber # :nodoc:
def initialize
@direction = :bottom_up
end

def scrub(node)
if Loofah::Elements::LINEBREAKERS.include?(node.name)
replacement = if Loofah::Elements::INLINE_LINE_BREAK.include?(node.name)
" "
else
" #{node.content} "
end
node.add_next_sibling(Nokogiri::XML::Text.new(replacement, node.document))
node.remove
elsif node.text?
return CONTINUE
else
node.before node.children
node.remove
end
end
end
end
end
14 changes: 14 additions & 0 deletions test/scrubbers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ def test_skips_text_nodes
end
end

class SpaceBlockElementScrubberTest < ScrubberTest
def setup
@scrubber = Rails::HTML::SpaceBlockElementScrubber.new
end

def test_removes_all_tags_and_keeps_the_text_content_and_adds_spaces
assert_scrubbed %(<div><h1>A list!</h1><ul><li>An element!</li></ul><script>alert("hi!")</script></div>), " A list! An element! "
end

def test_skips_text_nodes
assert_node_skipped("some text")
end
end

class ReturningStopFromScrubNodeTest < ScrubberTest
class ScrubStopper < Rails::HTML::PermitScrubber
def scrub_node(node)
Expand Down

0 comments on commit f8f50fc

Please sign in to comment.