-
Notifications
You must be signed in to change notification settings - Fork 344
Escape HTML Entities #763
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
base: master
Are you sure you want to change the base?
Escape HTML Entities #763
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,7 +201,7 @@ | |
|
||
def test_pretty_state | ||
state = JSON.create_pretty_state | ||
assert_equal({ | ||
Check failure on line 204 in test/json/json_generator_test.rb
|
||
:allow_nan => false, | ||
:array_nl => "\n", | ||
:as_json => false, | ||
|
@@ -215,12 +215,13 @@ | |
:object_nl => "\n", | ||
:space => " ", | ||
:space_before => "", | ||
:escape_html_entities => false, | ||
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s }) | ||
end | ||
|
||
def test_safe_state | ||
state = JSON::State.new | ||
assert_equal({ | ||
Check failure on line 224 in test/json/json_generator_test.rb
|
||
:allow_nan => false, | ||
:array_nl => "", | ||
:as_json => false, | ||
|
@@ -234,12 +235,13 @@ | |
:object_nl => "", | ||
:space => "", | ||
:space_before => "", | ||
:escape_html_entities => false, | ||
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s }) | ||
end | ||
|
||
def test_fast_state | ||
state = JSON.create_fast_state | ||
assert_equal({ | ||
Check failure on line 244 in test/json/json_generator_test.rb
|
||
:allow_nan => false, | ||
:array_nl => "", | ||
:as_json => false, | ||
|
@@ -253,6 +255,7 @@ | |
:object_nl => "", | ||
:space => "", | ||
:space_before => "", | ||
:escape_html_entities => false, | ||
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s }) | ||
end | ||
|
||
|
@@ -382,7 +385,7 @@ | |
state[:foo] = :bar | ||
assert_equal :bar, state[:foo] | ||
assert_equal :bar, state['foo'] | ||
state_hash = state.to_hash | ||
Check failure on line 388 in test/json/json_generator_test.rb
|
||
assert_kind_of Hash, state_hash | ||
assert_equal :bar, state_hash[:foo] | ||
end | ||
|
@@ -394,14 +397,14 @@ | |
state['foo'] = :bar | ||
assert_equal :bar, state[:foo] | ||
assert_equal :bar, state['foo'] | ||
state_hash = state.to_hash | ||
Check failure on line 400 in test/json/json_generator_test.rb
|
||
assert_kind_of Hash, state_hash | ||
assert_equal :bar, state_hash[:foo] | ||
end | ||
|
||
def test_json_state_to_h_roundtrip | ||
state = JSON.state.new | ||
assert_equal state.to_h, JSON.state.new(state.to_h).to_h | ||
Check failure on line 407 in test/json/json_generator_test.rb
|
||
end | ||
|
||
def test_json_generate | ||
|
@@ -481,6 +484,68 @@ | |
assert_equal json, generate(data, script_safe: true) | ||
end | ||
|
||
def test_escape_html_entities | ||
data = [ '/' ] | ||
json = '["\/"]' | ||
assert_equal json, generate(data, :escape_html_entities => true) | ||
Check failure on line 490 in test/json/json_generator_test.rb
|
||
# | ||
data = [ "\u2028\u2029" ] | ||
json = '["\u2028\u2029"]' | ||
assert_equal json, generate(data, :escape_html_entities => true) | ||
# | ||
data = ['&'] | ||
json = '["\\u0026"]' | ||
assert_equal json, generate(data, escape_html_entities: true) | ||
# | ||
data = ['<'] | ||
json = '["\\u003c"]' | ||
assert_equal json, generate(data, escape_html_entities: true) | ||
# | ||
data = ['>'] | ||
json = '["\\u003e"]' | ||
assert_equal json, generate(data, escape_html_entities: true) | ||
# | ||
data = ["倩", "瀨"] | ||
json = '["倩","瀨"]' | ||
assert_equal json, generate(data, escape_html_entities: true) | ||
end | ||
|
||
def test_escape_html_entities_priority_over_script_safe | ||
data = ['&'] | ||
json = '["\\u0026"]' | ||
assert_equal json, generate(data, escape_html_entities: true, script_safe: true) | ||
Check failure on line 516 in test/json/json_generator_test.rb
|
||
# | ||
data = ['<'] | ||
json = '["\\u003c"]' | ||
assert_equal json, generate(data, escape_html_entities: true, script_safe: true) | ||
# | ||
data = ['>'] | ||
json = '["\\u003e"]' | ||
assert_equal json, generate(data, escape_html_entities: true, script_safe: true) | ||
# | ||
data = ['/'] | ||
json = '["\/"]' | ||
assert_equal json, generate(data, escape_html_entities: true, script_safe: true) | ||
# | ||
data = ['&<>/'] | ||
json = '["\\u0026\\u003c\\u003e\/"]' | ||
assert_equal json, generate(data, escape_html_entities: true, script_safe: true) | ||
end | ||
|
||
def test_ascii_only_with_escape_html_entities | ||
data = ['é&<>'] | ||
json = '["\\u00e9\\u0026\\u003c\\u003e"]' | ||
assert_equal json, generate(data, ascii_only: true, escape_html_entities: true) | ||
Check failure on line 538 in test/json/json_generator_test.rb
|
||
# | ||
data = ['abc123'] | ||
json = '["abc123"]' | ||
assert_equal json, generate(data, ascii_only: true, escape_html_entities: true) | ||
# | ||
data = ['倩瀨'] | ||
json = '["\\u5029\\u7028"]' | ||
assert_equal json, generate(data, ascii_only: true, escape_html_entities: true) | ||
end | ||
|
||
def test_string_subclass | ||
s = Class.new(String) do | ||
def to_s; self; end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This table is copy/pasted from
script_safe
with the exception of characters&
,<
,>
.