aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/string_ext_test.rb
diff options
context:
space:
mode:
authorYehuda Katz <wycats@Yehuda-Katz.local>2010-01-31 19:17:42 -0800
committerYehuda Katz <wycats@Yehuda-Katz.local>2010-01-31 19:39:13 -0800
commit4cbb9db0a5ff65b0a626a5b043331abefd89e717 (patch)
treea112ce55f5521ff31abf0e4357afcc170fc0a143 /activesupport/test/core_ext/string_ext_test.rb
parent1c83fd2eff3fd174e1aba0512aa2dd8ecdadb2c7 (diff)
downloadrails-4cbb9db0a5ff65b0a626a5b043331abefd89e717.tar.gz
rails-4cbb9db0a5ff65b0a626a5b043331abefd89e717.tar.bz2
rails-4cbb9db0a5ff65b0a626a5b043331abefd89e717.zip
For performance reasons, you can no longer call html_safe! on Strings. Instead, all Strings are always not html_safe?. Instead, you can get a SafeBuffer from a String by calling #html_safe, which will SafeBuffer.new(self).
* Additionally, instead of doing concat("</form>".html_safe), you can do safe_concat("</form>"), which will skip both the flag set, and the flag check. * For the first pass, I converted virtually all #html_safe!s to #html_safe, and the tests pass. A further optimization would be to try to use #safe_concat as much as possible, reducing the performance impact if we know up front that a String is safe.
Diffstat (limited to 'activesupport/test/core_ext/string_ext_test.rb')
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb67
1 files changed, 34 insertions, 33 deletions
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index 9a805bc010..ca26f91e8c 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -342,12 +342,12 @@ class OutputSafetyTest < ActiveSupport::TestCase
end
test "A string can be marked safe" do
- @string.html_safe!
- assert @string.html_safe?
+ string = @string.html_safe
+ assert string.html_safe?
end
test "Marking a string safe returns the string" do
- assert_equal @string, @string.html_safe!
+ assert_equal @string, @string.html_safe
end
test "A fixnum is safe by default" do
@@ -361,7 +361,7 @@ class OutputSafetyTest < ActiveSupport::TestCase
end
end
- @string.html_safe!
+ @string.html_safe
@string << klass.new
assert_equal "helloother", @string
@@ -369,44 +369,44 @@ class OutputSafetyTest < ActiveSupport::TestCase
end
test "Adding a safe string to another safe string returns a safe string" do
- @other_string = "other".html_safe!
- @string.html_safe!
- @combination = @other_string + @string
+ @other_string = "other".html_safe
+ string = @string.html_safe
+ @combination = @other_string + string
assert_equal "otherhello", @combination
assert @combination.html_safe?
end
- test "Adding an unsafe string to a safe string returns an unsafe string" do
- @other_string = "other".html_safe!
- @combination = @other_string + @string
- @other_combination = @string + @other_string
+ test "Adding an unsafe string to a safe string escapes it and returns a safe string" do
+ @other_string = "other".html_safe
+ @combination = @other_string + "<foo>"
+ @other_combination = @string + "<foo>"
- assert_equal "otherhello", @combination
- assert_equal "helloother", @other_combination
+ assert_equal "other&lt;foo&gt;", @combination
+ assert_equal "hello<foo>", @other_combination
- assert !@combination.html_safe?
+ assert @combination.html_safe?
assert !@other_combination.html_safe?
end
test "Concatting safe onto unsafe yields unsafe" do
@other_string = "other"
- @string.html_safe!
+ @string.html_safe
@other_string.concat(@string)
assert !@other_string.html_safe?
end
- test "Concatting unsafe onto safe yields unsafe" do
- @other_string = "other".html_safe!
-
- @other_string.concat(@string)
- assert !@other_string.html_safe?
+ test "Concatting unsafe onto safe yields escaped safe" do
+ @other_string = "other".html_safe
+ string = @other_string.concat("<foo>")
+ assert_equal "other&lt;foo&gt;", string
+ assert string.html_safe?
end
test "Concatting safe onto safe yields safe" do
- @other_string = "other".html_safe!
- @string.html_safe!
+ @other_string = "other".html_safe
+ @string.html_safe
@other_string.concat(@string)
assert @other_string.html_safe?
@@ -414,31 +414,32 @@ class OutputSafetyTest < ActiveSupport::TestCase
test "Concatting safe onto unsafe with << yields unsafe" do
@other_string = "other"
- @string.html_safe!
+ @string.html_safe
@other_string << @string
assert !@other_string.html_safe?
end
- test "Concatting unsafe onto safe with << yields unsafe" do
- @other_string = "other".html_safe!
-
- @other_string << @string
- assert !@other_string.html_safe?
+ test "Concatting unsafe onto safe with << yields escaped safe" do
+ @other_string = "other".html_safe
+ string = @other_string << "<foo>"
+ assert_equal "other&lt;foo&gt;", string
+ assert string.html_safe?
end
test "Concatting safe onto safe with << yields safe" do
- @other_string = "other".html_safe!
- @string.html_safe!
+ @other_string = "other".html_safe
+ @string.html_safe
@other_string << @string
assert @other_string.html_safe?
end
test "Concatting a fixnum to safe always yields safe" do
- @string.html_safe!
- @string.concat(13)
- assert @string.html_safe?
+ string = @string.html_safe
+ string = string.concat(13)
+ assert_equal "hello".concat(13), string
+ assert string.html_safe?
end
end