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

Handle nil inputs in Sanitize::Rails::Engine#clean and #clean! #8

Merged
merged 2 commits into from
Apr 22, 2014
Merged
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
4 changes: 2 additions & 2 deletions lib/sanitize/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ def cleaner
# means that text passed through `Sanitize::Rails::Engine.clean`
# will not be escaped by ActionView's XSS filtering utilities.
def clean(string)
::ActiveSupport::SafeBuffer.new string.dup.tap { |s| clean!(s) }
::ActiveSupport::SafeBuffer.new string.to_s.dup.tap { |s| clean!(s) }
end

# Sanitizes the given `string` in place and does NOT mark it as `html_safe`
#
def clean!(string)
cleaner.clean!(string)
cleaner.clean!(string.to_s).to_s
end

def callback_for(options) #:nodoc:
Expand Down
10 changes: 9 additions & 1 deletion test/sanitize_rails_engine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'sanitize/rails'

# Test suite for Sanitize::Rails::Engine
class SanitizeRailsEngineTest < MiniTest::Unit::TestCase
class SanitizeRailsEngineTest < Minitest::Test
def setup
@engine = Sanitize::Rails::Engine
end
Expand Down Expand Up @@ -50,4 +50,12 @@ def test_clean_returns_safe_buffers
new_string = @engine.clean string
assert_instance_of ::ActiveSupport::SafeBuffer, new_string
end

def test_clean_returns_blank_string_for_nil_input
assert_equal '', @engine.clean(nil)
end

def test_clean_bang_returns_blank_string_for_nil_input
assert_equal '', @engine.clean!(nil)
end
end
2 changes: 1 addition & 1 deletion test/sanitize_rails_string_extension_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'sanitize/rails'

# Test suite for Sanitize::Rails::Engine
class SanitizeRailsStringExtensionTest < MiniTest::Unit::TestCase
class SanitizeRailsStringExtensionTest < Minitest::Test
SanitizableString = Class.new(String) { include Sanitize::Rails::String }

def setup
Expand Down
1 change: 0 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
require 'minitest/unit'
require 'minitest/autorun'