Skip to content

Commit

Permalink
Merge pull request WOVNio#152 from WOVNio/fix/remained_input_type_hid…
Browse files Browse the repository at this point in the history
…den_marker

### Purpose/goal of Pull Request (Issue #)
WOVNio/equalizer#24431

Before posting html to html-swapper, wovnrb put marker like `<!-- __wovn-backend-ignored-key-0 -->` to value attribute of input tag which type is hidden.
After response backed from html-swapper, this marker is replaced back to original.
But this marker is remained like `&lt;!-- __wovn-backend-ignored-key-0 --&gt;`, because of escaped.

I add `CGI.unescapeHTML` before revert marker.
  • Loading branch information
Soichiro725 authored Nov 7, 2019
2 parents bc7b153 + 25c2fa0 commit 8856d24
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/wovnrb/services/html_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def strip_form(node, marker)
return if original_text.nil?
return if original_text.include?(HtmlReplaceMarker::KEY_PREFIX)

node.set_attribute('value', marker.add_comment_value(original_text))
node.set_attribute('value', marker.add_value(original_text))
end
end

Expand Down
8 changes: 7 additions & 1 deletion lib/wovnrb/services/html_replace_marker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ def add_comment_value(value)
key
end

def add_value(value)
key = generate_key
@mapped_values << [key, value]

key
end

def revert(marked_html)
i = @mapped_values.size
while i > 0
i -= 1
key, value = @mapped_values[i]
marked_html = marked_html.sub(key, value)
end

marked_html
end

Expand Down
2 changes: 1 addition & 1 deletion lib/wovnrb/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Wovnrb
VERSION = '2.2.1'.freeze
VERSION = '2.2.2'.freeze
end
12 changes: 11 additions & 1 deletion test/lib/services/html_converter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,24 @@ def test_build_api_compatible_html_hidden_input_should_not_be_sent
html = [
'<html><body>',
'<input id="user-id" type="hidden" value="secret-id">',
'<input id="password" type="hidden" value="secret-password">',
'<input id="something" type="hidden" value="">',
'<input id="name" type="text" value="wovn.io">',
'</body></html>'
].join

converter = prepare_html_converter(html, ignore_class: [])
converted_html, = converter.build_api_compatible_html

expected_convert_html = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&amp;backend=true&amp;currentLang=en&amp;defaultLang=en&amp;urlPattern=query&amp;langCodeAliases={}&amp;langParamName=wovn&amp;version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"fr\" href=\"http://my-site.com/?wovn=fr\"><link rel=\"alternate\" hreflang=\"ja\" href=\"http://my-site.com/?wovn=ja\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><input id=\"user-id\" type=\"hidden\" value=\"<!-- __wovn-backend-ignored-key-0 -->\"><input id=\"name\" type=\"text\" value=\"wovn.io\"></body></html>"
expected_convert_html = [
'<html><head>',
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&amp;backend=true&amp;currentLang=en&amp;defaultLang=en&amp;urlPattern=query&amp;langCodeAliases={}&amp;langParamName=wovn&amp;version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"fr\" href=\"http://my-site.com/?wovn=fr\"><link rel=\"alternate\" hreflang=\"ja\" href=\"http://my-site.com/?wovn=ja\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body>",
'<input id="user-id" type="hidden" value="__wovn-backend-ignored-key-0">',
'<input id="password" type="hidden" value="__wovn-backend-ignored-key-1">',
'<input id="something" type="hidden" value="__wovn-backend-ignored-key-2">',
'<input id="name" type="text" value="wovn.io">',
'</body></html>'
].join
assert_equal(expected_convert_html, converted_html)
end

Expand Down
86 changes: 81 additions & 5 deletions test/lib/services/html_replace_marker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ def test_add_comment_value
end

def test_add_comment_value_multiple_times
maker = HtmlReplaceMarker.new
assert_equal('<!-- __wovn-backend-ignored-key-0 -->', maker.add_comment_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-1 -->', maker.add_comment_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-2 -->', maker.add_comment_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-3 -->', maker.add_comment_value('hello'))
marker = HtmlReplaceMarker.new
assert_equal('<!-- __wovn-backend-ignored-key-0 -->', marker.add_comment_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-1 -->', marker.add_comment_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-2 -->', marker.add_comment_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-3 -->', marker.add_comment_value('hello'))
end

def test_add_same_comment_value_multiple_times
Expand All @@ -23,6 +23,27 @@ def test_add_same_comment_value_multiple_times
end
end

def test_add_same_value_multiple_times
marker = HtmlReplaceMarker.new

25.times do |i|
assert_equal("__wovn-backend-ignored-key-#{i}", marker.add_value('hello'))
end
end

def test_mixed_add_comment_value_and_add_value
marker = HtmlReplaceMarker.new

assert_equal('<!-- __wovn-backend-ignored-key-0 -->', marker.add_comment_value('hello'))
assert_equal('__wovn-backend-ignored-key-1', marker.add_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-2 -->', marker.add_comment_value('hello'))
assert_equal('__wovn-backend-ignored-key-3', marker.add_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-4 -->', marker.add_comment_value('hello'))
assert_equal('__wovn-backend-ignored-key-5', marker.add_value('hello'))
assert_equal('<!-- __wovn-backend-ignored-key-6 -->', marker.add_comment_value('hello'))
assert_equal('__wovn-backend-ignored-key-7', marker.add_value('hello'))
end

def test_revert
marker = HtmlReplaceMarker.new
original_html = '<html><body>hello<a> replacement </a>world </body></html>'
Expand All @@ -32,6 +53,42 @@ def test_revert
assert_equal(original_html, marker.revert(new_html))
end

def test_revert_input_value
marker = HtmlReplaceMarker.new
original_html = '<html><body><input type="hidden" value="please-revert"></body></html>'
key = marker.add_value('please-revert')
new_html = original_html.sub('please-revert', key)
assert_equal("<html><body><input type=\"hidden\" value=\"#{key}\"></body></html>", new_html)
assert_equal(original_html, marker.revert(new_html))
end

def test_revert_input_empty_value
marker = HtmlReplaceMarker.new
original_html = '<html><body><input type="hidden" value=""></body></html>'
key = marker.add_value('')
new_html = original_html.sub('value=""', "value=\"#{key}\"")
assert_equal("<html><body><input type=\"hidden\" value=\"#{key}\"></body></html>", new_html)
assert_equal(original_html, marker.revert(new_html))
end

def test_revert_multiple_input
marker = HtmlReplaceMarker.new
original_html = [
'<html><body>',
'<input type="hidden" value="please_revert1"></body></html>',
'<input type="hidden" value="please_revert2"></body></html>',
'<input type="hidden" value=""></body></html>'
].join
new_html = [
'<html><body>',
"<input type=\"hidden\" value=\"#{marker.add_value('please_revert1')}\"></body></html>",
"<input type=\"hidden\" value=\"#{marker.add_value('please_revert2')}\"></body></html>",
"<input type=\"hidden\" value=\"#{marker.add_value('')}\">",
'</body></html>'
].join
assert_equal(original_html, marker.revert(new_html))
end

def test_revert_multiple_values
marker = HtmlReplaceMarker.new
original_html = '<html><body>hello<a> replacement </a>world </body></html>'
Expand Down Expand Up @@ -71,5 +128,24 @@ def test_revert_same_value
new_html = "<html><body>#{key1}<a>#{key2}</a>#{key3}</body></html>"
assert_equal(original_html, marker.revert(new_html))
end

def test_revert_mixed_values
marker = HtmlReplaceMarker.new
original_html = [
'<html><body>',
'<span>hello</span>',
'<input type="hidden" value="please_revert">',
'</body></html>'
].join
key1 = marker.add_comment_value('hello')
key2 = marker.add_value('please_revert')
new_html = [
'<html><body>',
"<span>#{key1}</span>",
"<input type=\"hidden\" value=\"#{key2}\">",
'</body></html>'
].join
assert_equal(original_html, marker.revert(new_html))
end
end
end
28 changes: 28 additions & 0 deletions test/lib/wovnrb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ def test_switch_lang
assert_switch_lang('en', 'ja', body, expected_body, true)
end

def test_switch_lang_with_input_tags
body = [
'<html lang="ja">',
'<body>',
'<input type="hidden" value="test1">',
'<input type="hidden" value="test2">',
'<input type="hidden" value="">',
'<input value="test3">',
'</body></html>'
].join

expected_body = [
'<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
"<script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script>",
'<link rel="alternate" hreflang="ja" href="http://ja.page.com/">',
'<link rel="alternate" hreflang="en" href="http://page.com/"></head>',
'<body>',
'<input type="hidden" value="test1">',
'<input type="hidden" value="test2">',
'<input type="hidden" value="">',
'<input value="test3">',
'<p><!--wovn-src:Hello-->こんにちは</p>',
'</body></html>'
].join

assert_switch_lang('en', 'ja', body, expected_body, true)
end

def test_switch_lang_of_html_fragment_with_japanese_translations
bodies = ['<span>Hello</span>'].join
expected_bodies = ['<span><!--wovn-src:Hello-->こんにちは</span>'].join
Expand Down

0 comments on commit 8856d24

Please sign in to comment.