wovnrb 2.2.0 → 2.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@ module Wovnrb
6
6
  converter = prepare_html_converter('<html><body><a class="test">hello</a></body></html>', supported_langs: %w[en vi])
7
7
  converted_html, = converter.build_api_compatible_html
8
8
 
9
- expected_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=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><a class=\"test\">hello</a></body></html>"
9
+ expected_html = "<html lang=\"en\"><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=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><a class=\"test\">hello</a></body></html>"
10
10
  assert_equal(expected_html, converted_html)
11
11
  end
12
12
 
@@ -19,7 +19,7 @@ module Wovnrb
19
19
  converter = prepare_html_converter('<html><body><a class="test">hello</a></body></html>', settings)
20
20
  converted_html, = converter.build_api_compatible_html
21
21
 
22
- expected_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=lang&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=\"vi\" href=\"http://my-site.com/?lang=vi\"></head><body><a class=\"test\">hello</a></body></html>"
22
+ expected_html = "<html lang=\"en\"><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=lang&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=\"vi\" href=\"http://my-site.com/?lang=vi\"></head><body><a class=\"test\">hello</a></body></html>"
23
23
  assert_equal(expected_html, converted_html)
24
24
  end
25
25
 
@@ -28,7 +28,7 @@ module Wovnrb
28
28
  converter = prepare_html_converter('<html><body><p>' + long_string + '</p></body></html>', supported_langs: %w[en vi])
29
29
  converted_html, = converter.build_api_compatible_html
30
30
 
31
- expected_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=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><p>" + long_string + '</p></body></html>'
31
+ expected_html = "<html lang=\"en\"><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=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><p>" + long_string + '</p></body></html>'
32
32
  assert_equal(expected_html, converted_html)
33
33
  end
34
34
 
@@ -36,6 +36,7 @@ module Wovnrb
36
36
  html = [
37
37
  '<html><body>',
38
38
  '<p>Hello <span wovn-ignore>WOVN</span><p>',
39
+ '<p>Hello <span data-wovn-ignore>WOVN</span><p>',
39
40
  '<div><span class="ignore-me">should be ignored</span></div>',
40
41
  '<span>Have a nice day!</span>',
41
42
  '</body></html>'
@@ -44,7 +45,7 @@ module Wovnrb
44
45
  converter = prepare_html_converter(html, ignore_class: ['ignore-me'])
45
46
  converted_html, = converter.build_api_compatible_html
46
47
 
47
- expected_convert_html = "<html><head><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><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><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><p>Hello <span wovn-ignore=\"\"><!-- __wovn-backend-ignored-key-0 --></span></p><p></p><div><span class=\"ignore-me\"><!-- __wovn-backend-ignored-key-1 --></span></div><span>Have a nice day!</span></body></html>"
48
+ expected_convert_html = "<html lang=\"en\"><head><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><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><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><p>Hello <span wovn-ignore=\"\"><!-- __wovn-backend-ignored-key-0 --></span></p><p></p><p>Hello <span data-wovn-ignore=\"\"><!-- __wovn-backend-ignored-key-1 --></span></p><p></p><div><span class=\"ignore-me\"><!-- __wovn-backend-ignored-key-2 --></span></div><span>Have a nice day!</span></body></html>"
48
49
  assert_equal(expected_convert_html, converted_html)
49
50
  end
50
51
 
@@ -61,7 +62,7 @@ module Wovnrb
61
62
  converter = prepare_html_converter(html, ignore_class: [])
62
63
  converted_html, = converter.build_api_compatible_html
63
64
 
64
- expected_convert_html = "<html><head><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><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><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><form action=\"/test\" method=\"POST\"><!-- __wovn-backend-ignored-key-0 --></form></body></html>"
65
+ expected_convert_html = "<html lang=\"en\"><head><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><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><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><form action=\"/test\" method=\"POST\"><!-- __wovn-backend-ignored-key-0 --></form></body></html>"
65
66
  assert_equal(expected_convert_html, converted_html)
66
67
  end
67
68
 
@@ -69,6 +70,8 @@ module Wovnrb
69
70
  html = [
70
71
  '<html><body>',
71
72
  '<input id="user-id" type="hidden" value="secret-id">',
73
+ '<input id="password" type="hidden" value="secret-password">',
74
+ '<input id="something" type="hidden" value="">',
72
75
  '<input id="name" type="text" value="wovn.io">',
73
76
  '</body></html>'
74
77
  ].join
@@ -76,7 +79,15 @@ module Wovnrb
76
79
  converter = prepare_html_converter(html, ignore_class: [])
77
80
  converted_html, = converter.build_api_compatible_html
78
81
 
79
- 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>"
82
+ expected_convert_html = [
83
+ '<html lang="en"><head>',
84
+ "<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>",
85
+ '<input id="user-id" type="hidden" value="__wovn-backend-ignored-key-0">',
86
+ '<input id="password" type="hidden" value="__wovn-backend-ignored-key-1">',
87
+ '<input id="something" type="hidden" value="__wovn-backend-ignored-key-2">',
88
+ '<input id="name" type="text" value="wovn.io">',
89
+ '</body></html>'
90
+ ].join
80
91
  assert_equal(expected_convert_html, converted_html)
81
92
  end
82
93
 
@@ -84,7 +95,7 @@ module Wovnrb
84
95
  converter = prepare_html_converter('<html><body><a>hello</a></body></html>', supported_langs: %w[en vi])
85
96
  translated_html = converter.build
86
97
 
87
- expected_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=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><a>hello</a></body></html>"
98
+ expected_html = "<html lang=\"en\"><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=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><a>hello</a></body></html>"
88
99
  assert_equal(expected_html, translated_html)
89
100
  end
90
101
 
@@ -92,7 +103,7 @@ module Wovnrb
92
103
  converter = prepare_html_converter('<html><body><a>hello</a></body></html>', supported_langs: [])
93
104
  translated_html = converter.build
94
105
 
95
- expected_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></head><body><a>hello</a></body></html>"
106
+ expected_html = "<html lang=\"en\"><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></head><body><a>hello</a></body></html>"
96
107
  assert_equal(expected_html, translated_html)
97
108
  end
98
109
 
@@ -100,7 +111,7 @@ module Wovnrb
100
111
  converter = prepare_html_converter('<html><head><title>TITLE</title></head><body><a>hello</a></body></html>', supported_langs: %w[en vi])
101
112
  translated_html = converter.build
102
113
 
103
- expected_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><title>TITLE</title><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><a>hello</a></body></html>"
114
+ expected_html = "<html lang=\"en\"><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><title>TITLE</title><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><a>hello</a></body></html>"
104
115
  assert_equal(expected_html, translated_html)
105
116
  end
106
117
 
@@ -108,7 +119,7 @@ module Wovnrb
108
119
  converter = prepare_html_converter('<html>hello<a>world</a></html>', supported_langs: [])
109
120
  translated_html = converter.build
110
121
 
111
- expected_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></head><body>hello<a>world</a></body></html>"
122
+ expected_html = "<html lang=\"en\"><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></head><body>hello<a>world</a></body></html>"
112
123
  assert_equal(expected_html, translated_html)
113
124
  end
114
125
 
@@ -123,7 +134,7 @@ module Wovnrb
123
134
  converter = HtmlConverter.new(dom, store, headers)
124
135
  translated_html = converter.build
125
136
 
126
- expected_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=\"ja\" href=\"http://my-site.com/?wovn=ja\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body>hello<a>world</a></body></html>"
137
+ expected_html = "<html lang=\"en\"><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=\"ja\" href=\"http://my-site.com/?wovn=ja\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body>hello<a>world</a></body></html>"
127
138
  assert_equal(expected_html, translated_html)
128
139
  end
129
140
 
@@ -138,7 +149,7 @@ module Wovnrb
138
149
  converter = HtmlConverter.new(dom, store, headers)
139
150
  translated_html = converter.build
140
151
 
141
- expected_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=path&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=\"ja\" href=\"http://my-site.com/ja/\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/vi/\"></head><body>hello<a>world</a></body></html>"
152
+ expected_html = "<html lang=\"en\"><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=path&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=\"ja\" href=\"http://my-site.com/ja/\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/vi/\"></head><body>hello<a>world</a></body></html>"
142
153
  assert_equal(expected_html, translated_html)
143
154
  end
144
155
 
@@ -158,18 +169,33 @@ module Wovnrb
158
169
  assert_equal(expected_html, converter.send(:html))
159
170
  end
160
171
 
161
- def test_inject_lang_html_tag
172
+ def test_inject_lang_html_tag_with_no_lang_in_html_tag
162
173
  settings = default_store_settings
163
174
  store = Wovnrb::Store.instance
164
175
  store.update_settings(settings)
165
176
 
166
177
  headers = Wovnrb::Headers.new(
167
- Wovnrb.get_env('url' => 'http://my-site.com/?wovn=ja'),
178
+ Wovnrb.get_env('url' => 'http://my-site.com/'),
168
179
  Wovnrb.get_settings(settings)
169
180
  )
170
181
  converter = HtmlConverter.new(get_dom('<html><body>hello</body></html>'), store, headers)
171
182
  converter.send(:inject_lang_html_tag)
172
- expected_html = '<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body>hello</body></html>'
183
+ expected_html = '<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body>hello</body></html>'
184
+ assert_equal(expected_html, converter.send(:html))
185
+ end
186
+
187
+ def test_inject_lang_html_tag_with_lang_in_html_tag
188
+ settings = default_store_settings
189
+ store = Wovnrb::Store.instance
190
+ store.update_settings(settings)
191
+
192
+ headers = Wovnrb::Headers.new(
193
+ Wovnrb.get_env('url' => 'http://my-site.com/'),
194
+ Wovnrb.get_settings(settings)
195
+ )
196
+ converter = HtmlConverter.new(get_dom('<html lang="th"><body>hello</body></html>'), store, headers)
197
+ converter.send(:inject_lang_html_tag)
198
+ expected_html = '<html lang="th"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body>hello</body></html>'
173
199
  assert_equal(expected_html, converter.send(:html))
174
200
  end
175
201
 
@@ -8,11 +8,11 @@ module Wovnrb
8
8
  end
9
9
 
10
10
  def test_add_comment_value_multiple_times
11
- maker = HtmlReplaceMarker.new
12
- assert_equal('<!-- __wovn-backend-ignored-key-0 -->', maker.add_comment_value('hello'))
13
- assert_equal('<!-- __wovn-backend-ignored-key-1 -->', maker.add_comment_value('hello'))
14
- assert_equal('<!-- __wovn-backend-ignored-key-2 -->', maker.add_comment_value('hello'))
15
- assert_equal('<!-- __wovn-backend-ignored-key-3 -->', maker.add_comment_value('hello'))
11
+ marker = HtmlReplaceMarker.new
12
+ assert_equal('<!-- __wovn-backend-ignored-key-0 -->', marker.add_comment_value('hello'))
13
+ assert_equal('<!-- __wovn-backend-ignored-key-1 -->', marker.add_comment_value('hello'))
14
+ assert_equal('<!-- __wovn-backend-ignored-key-2 -->', marker.add_comment_value('hello'))
15
+ assert_equal('<!-- __wovn-backend-ignored-key-3 -->', marker.add_comment_value('hello'))
16
16
  end
17
17
 
18
18
  def test_add_same_comment_value_multiple_times
@@ -23,6 +23,27 @@ module Wovnrb
23
23
  end
24
24
  end
25
25
 
26
+ def test_add_same_value_multiple_times
27
+ marker = HtmlReplaceMarker.new
28
+
29
+ 25.times do |i|
30
+ assert_equal("__wovn-backend-ignored-key-#{i}", marker.add_value('hello'))
31
+ end
32
+ end
33
+
34
+ def test_mixed_add_comment_value_and_add_value
35
+ marker = HtmlReplaceMarker.new
36
+
37
+ assert_equal('<!-- __wovn-backend-ignored-key-0 -->', marker.add_comment_value('hello'))
38
+ assert_equal('__wovn-backend-ignored-key-1', marker.add_value('hello'))
39
+ assert_equal('<!-- __wovn-backend-ignored-key-2 -->', marker.add_comment_value('hello'))
40
+ assert_equal('__wovn-backend-ignored-key-3', marker.add_value('hello'))
41
+ assert_equal('<!-- __wovn-backend-ignored-key-4 -->', marker.add_comment_value('hello'))
42
+ assert_equal('__wovn-backend-ignored-key-5', marker.add_value('hello'))
43
+ assert_equal('<!-- __wovn-backend-ignored-key-6 -->', marker.add_comment_value('hello'))
44
+ assert_equal('__wovn-backend-ignored-key-7', marker.add_value('hello'))
45
+ end
46
+
26
47
  def test_revert
27
48
  marker = HtmlReplaceMarker.new
28
49
  original_html = '<html><body>hello<a> replacement </a>world </body></html>'
@@ -32,6 +53,42 @@ module Wovnrb
32
53
  assert_equal(original_html, marker.revert(new_html))
33
54
  end
34
55
 
56
+ def test_revert_input_value
57
+ marker = HtmlReplaceMarker.new
58
+ original_html = '<html><body><input type="hidden" value="please-revert"></body></html>'
59
+ key = marker.add_value('please-revert')
60
+ new_html = original_html.sub('please-revert', key)
61
+ assert_equal("<html><body><input type=\"hidden\" value=\"#{key}\"></body></html>", new_html)
62
+ assert_equal(original_html, marker.revert(new_html))
63
+ end
64
+
65
+ def test_revert_input_empty_value
66
+ marker = HtmlReplaceMarker.new
67
+ original_html = '<html><body><input type="hidden" value=""></body></html>'
68
+ key = marker.add_value('')
69
+ new_html = original_html.sub('value=""', "value=\"#{key}\"")
70
+ assert_equal("<html><body><input type=\"hidden\" value=\"#{key}\"></body></html>", new_html)
71
+ assert_equal(original_html, marker.revert(new_html))
72
+ end
73
+
74
+ def test_revert_multiple_input
75
+ marker = HtmlReplaceMarker.new
76
+ original_html = [
77
+ '<html><body>',
78
+ '<input type="hidden" value="please_revert1"></body></html>',
79
+ '<input type="hidden" value="please_revert2"></body></html>',
80
+ '<input type="hidden" value=""></body></html>'
81
+ ].join
82
+ new_html = [
83
+ '<html><body>',
84
+ "<input type=\"hidden\" value=\"#{marker.add_value('please_revert1')}\"></body></html>",
85
+ "<input type=\"hidden\" value=\"#{marker.add_value('please_revert2')}\"></body></html>",
86
+ "<input type=\"hidden\" value=\"#{marker.add_value('')}\">",
87
+ '</body></html>'
88
+ ].join
89
+ assert_equal(original_html, marker.revert(new_html))
90
+ end
91
+
35
92
  def test_revert_multiple_values
36
93
  marker = HtmlReplaceMarker.new
37
94
  original_html = '<html><body>hello<a> replacement </a>world </body></html>'
@@ -71,5 +128,24 @@ module Wovnrb
71
128
  new_html = "<html><body>#{key1}<a>#{key2}</a>#{key3}</body></html>"
72
129
  assert_equal(original_html, marker.revert(new_html))
73
130
  end
131
+
132
+ def test_revert_mixed_values
133
+ marker = HtmlReplaceMarker.new
134
+ original_html = [
135
+ '<html><body>',
136
+ '<span>hello</span>',
137
+ '<input type="hidden" value="please_revert">',
138
+ '</body></html>'
139
+ ].join
140
+ key1 = marker.add_comment_value('hello')
141
+ key2 = marker.add_value('please_revert')
142
+ new_html = [
143
+ '<html><body>',
144
+ "<span>#{key1}</span>",
145
+ "<input type=\"hidden\" value=\"#{key2}\">",
146
+ '</body></html>'
147
+ ].join
148
+ assert_equal(original_html, marker.revert(new_html))
149
+ end
74
150
  end
75
151
  end
@@ -132,6 +132,7 @@ module Wovnrb
132
132
  store.update_settings('wovn_dev_mode' => true)
133
133
 
134
134
  assert(store.dev_mode?)
135
+ assert_equal('dev-wovn.io', store.wovn_host)
135
136
  assert_equal('http://dev-wovn.io:3001/v0/', store.settings['api_url'])
136
137
  assert_equal(3, store.settings['api_timeout_seconds'])
137
138
  end
@@ -27,6 +27,34 @@ class WovnrbTest < Minitest::Test
27
27
  assert_switch_lang('en', 'ja', body, expected_body, true)
28
28
  end
29
29
 
30
+ def test_switch_lang_with_input_tags
31
+ body = [
32
+ '<html lang="ja">',
33
+ '<body>',
34
+ '<input type="hidden" value="test1">',
35
+ '<input type="hidden" value="test2">',
36
+ '<input type="hidden" value="">',
37
+ '<input value="test3">',
38
+ '</body></html>'
39
+ ].join
40
+
41
+ expected_body = [
42
+ '<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
43
+ "<script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script>",
44
+ '<link rel="alternate" hreflang="ja" href="http://ja.page.com/">',
45
+ '<link rel="alternate" hreflang="en" href="http://page.com/"></head>',
46
+ '<body>',
47
+ '<input type="hidden" value="test1">',
48
+ '<input type="hidden" value="test2">',
49
+ '<input type="hidden" value="">',
50
+ '<input value="test3">',
51
+ '<p><!--wovn-src:Hello-->こんにちは</p>',
52
+ '</body></html>'
53
+ ].join
54
+
55
+ assert_switch_lang('en', 'ja', body, expected_body, true)
56
+ end
57
+
30
58
  def test_switch_lang_of_html_fragment_with_japanese_translations
31
59
  bodies = ['<span>Hello</span>'].join
32
60
  expected_bodies = ['<span><!--wovn-src:Hello-->こんにちは</span>'].join
@@ -108,8 +136,119 @@ HTML
108
136
  assert_switch_lang('en', 'ja', body, body, false)
109
137
  end
110
138
 
139
+ def test_call_without_path_ignored_should_change_environment
140
+ settings = {
141
+ 'project_token' => '123456',
142
+ 'url_pattern' => 'path',
143
+ 'default_lang' => 'ja',
144
+ 'supported_langs' => %w[ja en],
145
+ 'ignore_paths' => ['/en/ignored']
146
+ }
147
+ env = {
148
+ 'rack.input' => '',
149
+ 'rack.request.query_string' => '',
150
+ 'rack.request.query_hash' => {},
151
+ 'HTTP_HOST' => 'test.com',
152
+ 'REQUEST_URI' => '/en/not_ignored',
153
+ 'PATH_INFO' => '/en/not_ignored'
154
+ }
155
+
156
+ assert_call_affects_env(settings, env, mock_api: true, affected: true)
157
+ end
158
+
159
+ def test_call_with_path_ignored_with_language_code_should_change_environment
160
+ settings = {
161
+ 'project_token' => '123456',
162
+ 'url_pattern' => 'path',
163
+ 'default_lang' => 'ja',
164
+ 'supported_langs' => %w[ja en],
165
+ 'ignore_paths' => ['/en/ignored']
166
+ }
167
+ env = {
168
+ 'rack.input' => '',
169
+ 'rack.request.query_string' => '',
170
+ 'rack.request.query_hash' => {},
171
+ 'HTTP_HOST' => 'test.com',
172
+ 'REQUEST_URI' => '/ignored',
173
+ 'PATH_INFO' => '/ignored'
174
+ }
175
+
176
+ assert_call_affects_env(settings, env, mock_api: false, affected: true)
177
+ end
178
+
179
+ def test_call_with_path_ignored_without_language_code_should_change_environment
180
+ settings = {
181
+ 'project_token' => '123456',
182
+ 'url_pattern' => 'path',
183
+ 'default_lang' => 'ja',
184
+ 'supported_langs' => %w[ja en],
185
+ 'ignore_paths' => ['/ignored']
186
+ }
187
+ env = {
188
+ 'rack.input' => '',
189
+ 'rack.request.query_string' => '',
190
+ 'rack.request.query_hash' => {},
191
+ 'HTTP_HOST' => 'test.com',
192
+ 'REQUEST_URI' => '/en/ignored',
193
+ 'PATH_INFO' => '/en/ignored'
194
+ }
195
+
196
+ assert_call_affects_env(settings, env, mock_api: false, affected: true)
197
+ end
198
+
199
+ def test_call_with_path_ignored_without_language_code_in_original_language_should_change_environment
200
+ settings = {
201
+ 'project_token' => '123456',
202
+ 'url_pattern' => 'path',
203
+ 'default_lang' => 'ja',
204
+ 'supported_langs' => %w[ja en],
205
+ 'ignore_paths' => ['/ignored']
206
+ }
207
+ env = {
208
+ 'rack.input' => '',
209
+ 'rack.request.query_string' => '',
210
+ 'rack.request.query_hash' => {},
211
+ 'HTTP_HOST' => 'test.com',
212
+ 'REQUEST_URI' => '/ignored',
213
+ 'PATH_INFO' => '/ignored'
214
+ }
215
+
216
+ assert_call_affects_env(settings, env, mock_api: false, affected: true)
217
+ end
218
+
219
+ def test_call_with_path_ignored_should_not_change_environment
220
+ settings = {
221
+ 'project_token' => '123456',
222
+ 'url_pattern' => 'path',
223
+ 'default_lang' => 'ja',
224
+ 'supported_langs' => %w[ja en],
225
+ 'ignore_paths' => ['/en/ignored']
226
+ }
227
+ env = {
228
+ 'rack.input' => '',
229
+ 'rack.request.query_string' => '',
230
+ 'rack.request.query_hash' => {},
231
+ 'HTTP_HOST' => 'test.com',
232
+ 'REQUEST_URI' => '/en/ignored',
233
+ 'PATH_INFO' => '/en/ignored'
234
+ }
235
+
236
+ assert_call_affects_env(settings, env, mock_api: false, affected: false)
237
+ end
238
+
111
239
  private
112
240
 
241
+ def assert_call_affects_env(settings, env, mock_api:, affected:)
242
+ app_mock = get_app
243
+ sut = Wovnrb::Interceptor.new(app_mock, settings)
244
+ unaffected_env = env
245
+
246
+ mock_translation_api_response('', '') if mock_api
247
+ sut.call(env.clone)
248
+
249
+ assert_equal(unaffected_env != app_mock.env, affected)
250
+ end
251
+
113
252
  def assert_switch_lang(original_lang, target_lang, body, expected_body, api_expected = true)
114
253
  subdomain = target_lang == original_lang ? '' : "#{target_lang}."
115
254
  interceptor = Wovnrb::Interceptor.new(get_app)
@@ -160,7 +299,7 @@ HTML
160
299
  end
161
300
 
162
301
  class RackMock
163
- attr_accessor :params
302
+ attr_accessor :params, :env
164
303
 
165
304
  def initialize(opts = {})
166
305
  @params = {}
data/test/test_helper.rb CHANGED
@@ -59,7 +59,8 @@ module Wovnrb
59
59
  end
60
60
 
61
61
  def get_settings(options = {})
62
- settings = {}
62
+ settings = Wovnrb::Store.default_settings
63
+
63
64
  settings['project_token'] = 'OHYx9'
64
65
  settings['url_pattern'] = 'path'
65
66
  settings['url_pattern_reg'] = '/(?<lang>[^/.?]+)'
@@ -68,7 +69,8 @@ module Wovnrb
68
69
  settings['api_url'] = 'http://localhost/v0/values'
69
70
  settings['default_lang'] = 'en'
70
71
  settings['supported_langs'] = %w[en ja]
71
- settings.merge(options)
72
+
73
+ Wovnrb::Settings.new.merge(settings.merge(options))
72
74
  end
73
75
 
74
76
  def get_env(options = {})