Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new scrubber suitable for strip tags but adding whitespace #183

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/rails/html/scrubbers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,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?
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
Loading