wovnrb 0.2.28 → 0.2.29

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cbefcf5370f91d37b96f311755e68fce4666118a
4
- data.tar.gz: df2d38a8b611b2cbc38c92f4d2036c4de8c0a660
3
+ metadata.gz: f15715b5f8a8d0d3bb58c2d8a86a915353d9cb7c
4
+ data.tar.gz: 0bb121940678dbc50aa0beb7f9b5839f35935844
5
5
  SHA512:
6
- metadata.gz: fa17224c7706eda6e7fa399295c09900c8ed4e6a10ffd3a69e6e249952411b7081f08b6abd53c8b8896e65f2044c842087ae7ead72e42d6512d113a1dbb43c29
7
- data.tar.gz: 66e899fe8363e92533ae207228a68601aff56d9cb8931b3d029d8f67cd2f85a35bbeecc895e99e89d2d358e0c80052a1fa8b675632c1216b9d16bb37634e4ec5
6
+ metadata.gz: c51823510261c6a2d97ec201285f9eda6f51927e883f017815b48ddaf5c989b7776f670bc4c23599bbdfa5be2a46d33b60e72717d274072725898965b5062645
7
+ data.tar.gz: 01e7f5d664b053574da8a78a86c363ad23556f4e7e443ffc05998ffb2ae1fc7feea1f706cb8b376920522aaff9b44085a08316757e1c2ff41510a0295c9cd958
data/lib/wovnrb.rb CHANGED
@@ -95,16 +95,13 @@ module Wovnrb
95
95
  if ignore_all || !d.xpath('//html[@wovn-ignore]').empty? || is_amp_page?(d)
96
96
  ignore_all = true
97
97
  output = d.to_html.gsub(/href="([^"]*)"/) { |m| "href=\"#{URI.decode($1)}\"" }
98
+ put_back_noscripts!(output, noscripts)
98
99
  new_body.push(output)
99
100
  next
100
101
  end
101
102
 
102
103
  output = lang.switch_dom_lang(d, @store, values, url, headers)
103
- # put back noscripts
104
- noscripts.each_with_index do |noscript, index|
105
- noscript_identifier = "<noscript wovn-id=\"#{index}\"></noscript>"
106
- output.sub!(noscript_identifier, noscript)
107
- end
104
+ put_back_noscripts!(output, noscripts)
108
105
  new_body.push(output)
109
106
  end
110
107
  body.close if body.respond_to?(:close)
@@ -113,6 +110,13 @@ module Wovnrb
113
110
 
114
111
  private
115
112
 
113
+ def put_back_noscripts!(output, noscripts)
114
+ noscripts.each_with_index do |noscript, index|
115
+ noscript_identifier = "<noscript wovn-id=\"#{index}\"></noscript>"
116
+ output.sub!(noscript_identifier, noscript)
117
+ end
118
+ end
119
+
116
120
  # Checks if a given HTML body is an Accelerated Mobile Page (AMP).
117
121
  # To do so, it looks at the required attributes for the HTML tag:
118
122
  # https://www.ampproject.org/docs/tutorials/create/basic_markup.
data/lib/wovnrb/lang.rb CHANGED
@@ -89,7 +89,7 @@ module Wovnrb
89
89
  # in the future, perhaps validate url rather than using begin rescue
90
90
  # "#{url =~ /\// ? 'http:' : ''}#{url}" =~ URI::regexp
91
91
  begin
92
- uri = URI(href)
92
+ uri = Addressable::URI.parse(href)
93
93
  rescue
94
94
  return new_href
95
95
  end
@@ -164,10 +164,25 @@ module Wovnrb
164
164
  (dom.at_css('html') || dom.at_css('HTML')).set_attribute('lang', Lang::iso_639_1_normalization(@lang_code))
165
165
  end
166
166
 
167
- dom.to_html.gsub(/href="([^"]*)"/) { |m| "href=\"#{URI.decode($1)}\"" }
167
+ index_href = index_href_for_encoding_and_decoding(dom)
168
+ # NOTE: when we use `#to_html` with nokogiri, nokogiri encode all href.
169
+ # but we want to keep original href as much as possible.
170
+ # That's why we replace href with original href which added lang info by wovnrb like this after we used `to_html`
171
+ dom.to_html.gsub(/href="([^"]*)"/) { |m| "href=\"#{index_href[$1] || $1}\"" }
168
172
  end
169
173
 
170
174
  private
175
+
176
+ def index_href_for_encoding_and_decoding(dom)
177
+ result = {}
178
+ dom.xpath('//*[@href]').each do |a_tag|
179
+ url = a_tag['href']
180
+ encoded_url = Addressable::URI.parse(url).normalize.to_s
181
+ result[encoded_url] = url if encoded_url != url
182
+ end
183
+ result
184
+ end
185
+
171
186
  def replace_dom_values(dom, values, store, url, headers)
172
187
  text_index = values['text_vals'] || {}
173
188
  src_index = values['img_vals'] || {}
@@ -1,3 +1,3 @@
1
1
  module Wovnrb
2
- VERSION = "0.2.28"
2
+ VERSION = "0.2.29"
3
3
  end
@@ -583,5 +583,33 @@ module Wovnrb
583
583
  h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://favy.tips'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
584
584
  assert_equal("http://staging-fr.favy.tips/topics/50", lang.add_lang_code("/topics/50", 'subdomain', h))
585
585
  end
586
+
587
+ def test_switch_dom_lang_for_keeping_originl_encoding_decoding
588
+ lang = Lang.new('ja')
589
+ header = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings)
590
+ html = <<-HTML
591
+ <head>
592
+ <link rel="stylesheet" type="text/css" href="%3Ca%3E%E3%81%B5%E3%82%86%3C%2Fa%3E/theme.css">
593
+ <link rel="stylesheet" type="text/css" href="テスト.css">
594
+ </head>
595
+ <ul>
596
+ <li><a href="http://page.com/%e5%ba%83%e5%b0%be">encoded japanese path</a></li>
597
+ <li><a href="http://page.com/テスト/DxA9J">no encoded japanese path</a></li>
598
+ <li><a href="http://page.com/#DnE897">decoded invalid path</a></li>
599
+ <li><a href="http://page.com/%23DnE897">encoded invalid path</a>
600
+ </ul>
601
+ HTML
602
+
603
+ dom = Wovnrb.to_dom(html)
604
+ url = header.url
605
+
606
+ swapped_body = lang.switch_dom_lang(dom, Store.instance, generate_values, url, header)
607
+ assert(swapped_body.include?('<link rel="stylesheet" type="text/css" href="%3Ca%3E%E3%81%B5%E3%82%86%3C%2Fa%3E/theme.css">'))
608
+ assert(swapped_body.include?('<link rel="stylesheet" type="text/css" href="テスト.css">'))
609
+ assert(swapped_body.include?('<a href="http://page.com/ja/%e5%ba%83%e5%b0%be">encoded japanese path</a>'))
610
+ assert(swapped_body.include?('<a href="http://page.com/ja/テスト/DxA9J">no encoded japanese path</a>'))
611
+ assert(swapped_body.include?('<a href="http://page.com/ja/#DnE897">decoded invalid path</a>'))
612
+ assert(swapped_body.include?('<a href="http://page.com/ja/%23DnE897">encoded invalid path</a>'))
613
+ end
586
614
  end
587
615
  end
@@ -112,6 +112,7 @@ class WovnrbTest < Minitest::Test
112
112
  headers = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
113
113
  body = <<HTML
114
114
  <html amp>
115
+ <head><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript></head>
115
116
  <body>
116
117
  <h1>Mr. Belvedere Fan Club</h1>
117
118
  <div><p>Hello</p></div>
@@ -120,7 +121,10 @@ class WovnrbTest < Minitest::Test
120
121
  HTML
121
122
  expected_body = <<HTML
122
123
  <html amp="">
123
- <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head>
124
+ <head>
125
+ <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
126
+ <noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
127
+ </head>
124
128
  <body>
125
129
  <h1>Mr. Belvedere Fan Club</h1>
126
130
  <div><p>Hello</p></div>
data/test/test_helper.rb CHANGED
@@ -94,7 +94,7 @@ module Wovnrb
94
94
  env['PATH_INFO'] = url.path
95
95
  end
96
96
 
97
- return Rack::MockRequest.env_for(url, env.merge(options))
97
+ return Rack::MockRequest.env_for(url.to_s, env.merge(options))
98
98
  end
99
99
 
100
100
  def to_dom(html)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wovnrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.28
4
+ version: 0.2.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Sandford
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-11-10 00:00:00.000000000 Z
12
+ date: 2017-12-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogumbo
@@ -423,3 +423,4 @@ test_files:
423
423
  - test/lib/wovnrb_test.rb
424
424
  - test/services/url_test.rb
425
425
  - test/test_helper.rb
426
+ has_rdoc: