metainspector 5.21.2 → 6.0.0
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 +4 -4
- data/CHANGELOG.md +37 -0
- data/README.md +215 -48
- data/lib/meta_inspector/attempt_timeout.rb +40 -0
- data/lib/meta_inspector/document.rb +101 -68
- data/lib/meta_inspector/errors.rb +5 -1
- data/lib/meta_inspector/html_encoding.rb +183 -0
- data/lib/meta_inspector/http_session.rb +74 -45
- data/lib/meta_inspector/image_downloader.rb +37 -24
- data/lib/meta_inspector/parser.rb +144 -34
- data/lib/meta_inspector/parsers/base.rb +7 -2
- data/lib/meta_inspector/parsers/head_links.rb +20 -28
- data/lib/meta_inspector/parsers/iframes.rb +2 -3
- data/lib/meta_inspector/parsers/images.rb +119 -70
- data/lib/meta_inspector/parsers/links.rb +19 -9
- data/lib/meta_inspector/parsers/meta_tags.rb +22 -31
- data/lib/meta_inspector/parsers/texts.rb +43 -33
- data/lib/meta_inspector/request.rb +67 -55
- data/lib/meta_inspector/url.rb +43 -19
- data/lib/meta_inspector/url_policy.rb +18 -11
- data/lib/meta_inspector/version.rb +1 -1
- data/lib/meta_inspector.rb +14 -13
- data/lib/metainspector.rb +1 -1
- data/meta_inspector.gemspec +1 -1
- data/spec/document_spec.rb +154 -56
- data/spec/meta_inspector/content_type_spec.rb +47 -0
- data/spec/meta_inspector/head_links_spec.rb +3 -3
- data/spec/meta_inspector/images_spec.rb +82 -1
- data/spec/meta_inspector/links_spec.rb +98 -2
- data/spec/meta_inspector/meta_inspector_spec.rb +3 -25
- data/spec/meta_inspector/meta_tags_spec.rb +53 -0
- data/spec/meta_inspector/texts_spec.rb +140 -3
- data/spec/meta_inspector/timeouts_spec.rb +107 -0
- data/spec/noscript_spec.rb +179 -0
- data/spec/parser_spec.rb +380 -10
- data/spec/query_string_spec.rb +125 -0
- data/spec/request_errors_spec.rb +91 -0
- data/spec/request_spec.rb +28 -10
- data/spec/response_encoding_spec.rb +487 -0
- data/spec/url_filter_spec.rb +2 -1
- data/spec/url_spec.rb +37 -0
- metadata +10 -2
|
@@ -103,5 +103,58 @@ describe MetaInspector do
|
|
|
103
103
|
|
|
104
104
|
expect(page.charset).to eq(nil)
|
|
105
105
|
end
|
|
106
|
+
|
|
107
|
+
['Content-Type', 'content-type', 'Content-type', 'CONTENT-TYPE'].each do |http_equiv|
|
|
108
|
+
it "gets the charset when http-equiv is #{http_equiv}" do
|
|
109
|
+
html = %(<meta http-equiv="#{http_equiv}" content="text/html; charset=utf-8">)
|
|
110
|
+
page = MetaInspector.new('http://example.com', document: html)
|
|
111
|
+
|
|
112
|
+
expect(page.charset).to eq('utf-8')
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
{
|
|
117
|
+
'text/html; foo=bar; charset=utf-8' => 'utf-8',
|
|
118
|
+
'text/html; charset=utf-8; foo=bar' => 'utf-8',
|
|
119
|
+
'text/html; charset = utf-8 ' => 'utf-8',
|
|
120
|
+
"text/html;\tCHARSET\t=\tUTF-8" => 'UTF-8',
|
|
121
|
+
'text/html; charset="utf-8"' => 'utf-8',
|
|
122
|
+
"text/html; charset='utf-8'" => 'utf-8',
|
|
123
|
+
'text/html; foo=bar' => nil,
|
|
124
|
+
'text/html; othercharset=utf-8' => nil,
|
|
125
|
+
'text/html; charset' => nil,
|
|
126
|
+
'text/html; charset=' => nil,
|
|
127
|
+
'text/html' => nil
|
|
128
|
+
}.each do |content, charset|
|
|
129
|
+
it "gets #{charset.inspect} from the content #{content.inspect}" do
|
|
130
|
+
html = %(<meta http-equiv="Content-Type" content="#{content.gsub('"', '"')}">)
|
|
131
|
+
page = MetaInspector.new('http://example.com', document: html)
|
|
132
|
+
|
|
133
|
+
expect(page.charset).to eq(charset)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
it 'returns nil when the content-type meta tag has no content attribute' do
|
|
138
|
+
page = MetaInspector.new('http://example.com', document: '<meta http-equiv="content-type">')
|
|
139
|
+
|
|
140
|
+
expect(page.charset).to be_nil
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
it 'prefers a meta charset attribute over a content-type declaration' do
|
|
144
|
+
html = '<meta http-equiv="content-type" content="text/html; charset=windows-1252">' \
|
|
145
|
+
'<meta charset="utf-8">'
|
|
146
|
+
page = MetaInspector.new('http://example.com', document: html)
|
|
147
|
+
|
|
148
|
+
expect(page.charset).to eq('utf-8')
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
it 'uses the first content-type declaration and ignores other http-equiv values' do
|
|
152
|
+
html = '<meta http-equiv="refresh" content="0; charset=windows-1252">' \
|
|
153
|
+
'<meta http-equiv="content-type" content="text/html; charset=utf-8">' \
|
|
154
|
+
'<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">'
|
|
155
|
+
page = MetaInspector.new('http://example.com', document: html)
|
|
156
|
+
|
|
157
|
+
expect(page.charset).to eq('utf-8')
|
|
158
|
+
end
|
|
106
159
|
end
|
|
107
160
|
end
|
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe MetaInspector do
|
|
4
|
+
describe 'headings containing paragraphs' do
|
|
5
|
+
(1..6).each do |level|
|
|
6
|
+
{
|
|
7
|
+
'<p>Hello</p>' => 'Hello',
|
|
8
|
+
'Hi <p>there</p>' => 'Hi there',
|
|
9
|
+
'<p>Hello <em>there</em></p> friend' => 'Hello there friend'
|
|
10
|
+
}.each do |content, text|
|
|
11
|
+
it "preserves #{content.inspect} inside h#{level}" do
|
|
12
|
+
html = "<h#{level}>#{content}</h#{level}><p>Outside</p>"
|
|
13
|
+
page = MetaInspector.new('http://example.com', document: html)
|
|
14
|
+
|
|
15
|
+
expect(page.public_send("h#{level}")).to eq([text])
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it 'uses the complete first heading as the best title' do
|
|
21
|
+
page = MetaInspector.new('http://example.com', document: '<h1>Hi <p>there</p></h1><h1>Later</h1>')
|
|
22
|
+
|
|
23
|
+
expect(page.best_title).to eq('Hi there')
|
|
24
|
+
expect(page.to_hash['h1']).to eq(['Hi there', 'Later'])
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it 'preserves paragraph content in downloaded headings' do
|
|
28
|
+
stub_request(:get, 'http://example.com/').to_return(
|
|
29
|
+
body: '<h1><p>Hello</p></h1>', headers: { 'Content-Type' => 'text/html' }
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
expect(MetaInspector.new('http://example.com').h1).to eq(['Hello'])
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
4
36
|
describe '#language' do
|
|
5
37
|
['en', 'en-US', 'zh-Hant-TW'].each do |language|
|
|
6
38
|
it "should return the declared language #{language.inspect}" do
|
|
@@ -50,6 +82,74 @@ describe MetaInspector do
|
|
|
50
82
|
expect(page.title).to eq('An example page')
|
|
51
83
|
end
|
|
52
84
|
|
|
85
|
+
describe '#title' do
|
|
86
|
+
let(:page) do
|
|
87
|
+
MetaInspector.new('http://example.com/', document: html)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
context 'when the head contains multiple titles' do
|
|
91
|
+
let(:html) { '<html><head><title>One</title><title>Two</title></head></html>' }
|
|
92
|
+
|
|
93
|
+
it 'should return only the first title' do
|
|
94
|
+
expect(page.title).to eq('One')
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it 'should include only the first title in to_hash' do
|
|
98
|
+
expect(page.to_hash['title']).to eq('One')
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
context 'when the first title is empty' do
|
|
103
|
+
let(:html) { '<html><head><title></title><title>Two</title></head></html>' }
|
|
104
|
+
|
|
105
|
+
it 'should return nil without falling back to a later title' do
|
|
106
|
+
expect(page.title).to be_nil
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
context 'when the first title is blank' do
|
|
111
|
+
let(:html) { "<html><head><title> \n\t </title><title>Two</title></head></html>" }
|
|
112
|
+
|
|
113
|
+
it 'should return nil without falling back to a later title' do
|
|
114
|
+
expect(page.title).to be_nil
|
|
115
|
+
expect(page.to_hash['title']).to be_nil
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
context 'when the title contains whitespace and entities' do
|
|
120
|
+
let(:html) { "<html><head><title>\n One & Two \n</title></head></html>" }
|
|
121
|
+
|
|
122
|
+
it 'should strip surrounding whitespace and decode entities' do
|
|
123
|
+
expect(page.title).to eq('One & Two')
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
context 'when the title has whitespace inside' do
|
|
128
|
+
let(:html) { "<html><head><title>One\n Two</title></head></html>" }
|
|
129
|
+
|
|
130
|
+
it 'should keep the inner whitespace as written' do
|
|
131
|
+
expect(page.title).to eq("One\n Two")
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
context 'when there is no title' do
|
|
136
|
+
let(:html) { '<html><head></head><body></body></html>' }
|
|
137
|
+
|
|
138
|
+
it 'should return nil, so a fallback can be chained' do
|
|
139
|
+
expect(page.title).to be_nil
|
|
140
|
+
expect(page.title || 'fallback').to eq('fallback')
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
context 'when the title is only in the body' do
|
|
145
|
+
let(:html) { '<html><head></head><body><title>Body title</title></body></html>' }
|
|
146
|
+
|
|
147
|
+
it 'should return nil' do
|
|
148
|
+
expect(page.title).to be_nil
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
53
153
|
describe "#h1" do
|
|
54
154
|
it "should find h1 content" do
|
|
55
155
|
page = MetaInspector.new('http://example.com/headings')
|
|
@@ -266,6 +366,28 @@ describe MetaInspector do
|
|
|
266
366
|
|
|
267
367
|
expect(page.description).to be(nil)
|
|
268
368
|
end
|
|
369
|
+
|
|
370
|
+
it "should strip surrounding whitespace" do
|
|
371
|
+
html = %(<meta name="description" content="\n The description \n">)
|
|
372
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
373
|
+
|
|
374
|
+
expect(page.description).to eq('The description')
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
it "should keep the inner whitespace as written" do
|
|
378
|
+
html = %(<meta name="description" content="One\n Two">)
|
|
379
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
380
|
+
|
|
381
|
+
expect(page.description).to eq("One\n Two")
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
['', ' ', "\n\t "].each do |blank|
|
|
385
|
+
it "should be nil for a blank meta description (#{blank.inspect})" do
|
|
386
|
+
page = MetaInspector.new('http://example.com/', document: %(<meta name="description" content="#{blank}">))
|
|
387
|
+
|
|
388
|
+
expect(page.description).to be_nil
|
|
389
|
+
end
|
|
390
|
+
end
|
|
269
391
|
end
|
|
270
392
|
|
|
271
393
|
describe "#best_description" do
|
|
@@ -287,13 +409,28 @@ describe MetaInspector do
|
|
|
287
409
|
expect(page.best_description).to eq("the twitter description")
|
|
288
410
|
end
|
|
289
411
|
|
|
412
|
+
it "should strip surrounding whitespace, like description" do
|
|
413
|
+
html = %(<meta name="description" content="\n One\n Two \n">)
|
|
414
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
415
|
+
|
|
416
|
+
expect(page.best_description).to eq("One\n Two")
|
|
417
|
+
expect(page.best_description).to eq(page.description)
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
it "should strip surrounding whitespace from the paragraph fallback" do
|
|
421
|
+
paragraph = 'This paragraph is long enough to be used as the description when the page has no description in its metadata at all.'
|
|
422
|
+
page = MetaInspector.new('http://example.com/', document: "<p>\n #{paragraph}\n </p>")
|
|
423
|
+
|
|
424
|
+
expect(page.best_description).to eq(paragraph)
|
|
425
|
+
end
|
|
426
|
+
|
|
290
427
|
['', ' ', "\n\t "].each do |blank|
|
|
291
428
|
it "should skip a blank standard description (#{blank.inspect})" do
|
|
292
429
|
html = %(<html><head><meta name="description" content="#{blank}"><meta property="og:description" content="Useful Open Graph description"></head></html>)
|
|
293
430
|
page = MetaInspector.new('http://example.com/', document: html)
|
|
294
431
|
|
|
295
432
|
expect(page.best_description).to eq('Useful Open Graph description')
|
|
296
|
-
expect(page.description).to
|
|
433
|
+
expect(page.description).to be_nil
|
|
297
434
|
end
|
|
298
435
|
|
|
299
436
|
it "should skip a blank Open Graph description (#{blank.inspect})" do
|
|
@@ -328,12 +465,12 @@ describe MetaInspector do
|
|
|
328
465
|
end
|
|
329
466
|
|
|
330
467
|
['description', 'og:description', 'twitter:description'].each do |name|
|
|
331
|
-
it "should
|
|
468
|
+
it "should strip surrounding whitespace from a useful #{name} value" do
|
|
332
469
|
description = " \n Useful description\t "
|
|
333
470
|
html = %(<html><head><meta name="#{name}" content="#{description}"></head></html>)
|
|
334
471
|
page = MetaInspector.new('http://example.com/', document: html)
|
|
335
472
|
|
|
336
|
-
expect(page.best_description).to eq(description)
|
|
473
|
+
expect(page.best_description).to eq('Useful description')
|
|
337
474
|
end
|
|
338
475
|
end
|
|
339
476
|
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Timeouts and retries' do
|
|
4
|
+
let(:page_url) { 'http://timeouts.example.com/' }
|
|
5
|
+
let(:html) { { status: 200, headers: { 'Content-Type' => 'text/html' }, body: '<title>Hi</title>' } }
|
|
6
|
+
|
|
7
|
+
describe 'connection timeouts' do
|
|
8
|
+
it 'raises MetaInspector::TimeoutError, keeping the original error as the cause' do
|
|
9
|
+
stub_request(:get, page_url).to_raise(Net::OpenTimeout.new('Failed to open TCP connection'))
|
|
10
|
+
|
|
11
|
+
expect do
|
|
12
|
+
MetaInspector.new(page_url, retries: 0)
|
|
13
|
+
end.to raise_error(MetaInspector::TimeoutError, /Failed to open TCP connection/) { |error|
|
|
14
|
+
expect(error.cause).to be_a(Faraday::TimeoutError)
|
|
15
|
+
expect(error.cause.wrapped_exception).to be_a(Net::OpenTimeout)
|
|
16
|
+
}
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'retries them' do
|
|
20
|
+
request = stub_request(:get, page_url).to_raise(Net::OpenTimeout).then.to_return(html)
|
|
21
|
+
|
|
22
|
+
page = MetaInspector.new(page_url, retries: 1)
|
|
23
|
+
|
|
24
|
+
expect(page.title).to eq('Hi')
|
|
25
|
+
expect(request).to have_been_requested.twice
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'raises MetaInspector::TimeoutError after exhausting the retries' do
|
|
29
|
+
request = stub_request(:get, page_url).to_raise(Net::OpenTimeout)
|
|
30
|
+
|
|
31
|
+
expect do
|
|
32
|
+
MetaInspector.new(page_url, retries: 2)
|
|
33
|
+
end.to raise_error(MetaInspector::TimeoutError)
|
|
34
|
+
|
|
35
|
+
expect(request).to have_been_requested.times(3)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'applies to HEAD requests too' do
|
|
39
|
+
head_request = stub_request(:head, page_url).to_raise(Net::OpenTimeout)
|
|
40
|
+
get_request = stub_request(:get, page_url).to_return(html)
|
|
41
|
+
|
|
42
|
+
expect do
|
|
43
|
+
MetaInspector.new(page_url, head_first: true, retries: 1)
|
|
44
|
+
end.to raise_error(MetaInspector::TimeoutError)
|
|
45
|
+
|
|
46
|
+
expect(head_request).to have_been_requested.twice
|
|
47
|
+
expect(get_request).not_to have_been_requested
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
describe 'other connection failures' do
|
|
52
|
+
it 'still raise MetaInspector::RequestError without retrying' do
|
|
53
|
+
request = stub_request(:get, page_url).to_raise(Errno::ECONNREFUSED)
|
|
54
|
+
|
|
55
|
+
expect do
|
|
56
|
+
MetaInspector.new(page_url, retries: 2)
|
|
57
|
+
end.to raise_error(MetaInspector::RequestError)
|
|
58
|
+
|
|
59
|
+
expect(request).to have_been_requested.once
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
describe 'deadline for each attempt' do
|
|
64
|
+
# The deadline is connection_timeout + read_timeout + 1 seconds.
|
|
65
|
+
let(:options) { { connection_timeout: 0.05, read_timeout: 0.05 } }
|
|
66
|
+
|
|
67
|
+
it 'gives every retry its own deadline' do
|
|
68
|
+
calls = 0
|
|
69
|
+
request = stub_request(:get, page_url).to_return do
|
|
70
|
+
calls += 1
|
|
71
|
+
# Ends with a read timeout after more than half the deadline.
|
|
72
|
+
sleep 0.7
|
|
73
|
+
raise Net::ReadTimeout if calls == 1
|
|
74
|
+
html
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
page = MetaInspector.new(page_url, **options, retries: 1)
|
|
78
|
+
|
|
79
|
+
expect(page.title).to eq('Hi')
|
|
80
|
+
expect(request).to have_been_requested.twice
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# A server that keeps an attempt busy until the deadline, such as one that
|
|
84
|
+
# streams forever, would hold the fetch for every retry (#188).
|
|
85
|
+
it 'raises MetaInspector::TimeoutError without retrying when an attempt runs out of time' do
|
|
86
|
+
request = stub_request(:get, page_url).to_return { sleep 1.5; html }
|
|
87
|
+
|
|
88
|
+
expect do
|
|
89
|
+
MetaInspector.new(page_url, **options, retries: 2)
|
|
90
|
+
end.to raise_error(MetaInspector::TimeoutError, /execution expired/)
|
|
91
|
+
|
|
92
|
+
expect(request).to have_been_requested.once
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it 'applies to HEAD requests too' do
|
|
96
|
+
head_request = stub_request(:head, page_url).to_return { sleep 1.5; html }
|
|
97
|
+
get_request = stub_request(:get, page_url).to_return(html)
|
|
98
|
+
|
|
99
|
+
expect do
|
|
100
|
+
MetaInspector.new(page_url, **options, head_first: true, retries: 2)
|
|
101
|
+
end.to raise_error(MetaInspector::TimeoutError)
|
|
102
|
+
|
|
103
|
+
expect(head_request).to have_been_requested.once
|
|
104
|
+
expect(get_request).not_to have_been_requested
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'noscript content' do
|
|
4
|
+
let(:page) { MetaInspector.new('https://example.com/', document: html, download_images: false) }
|
|
5
|
+
|
|
6
|
+
describe 'in the head' do
|
|
7
|
+
[
|
|
8
|
+
'<img src="/pixel.gif">',
|
|
9
|
+
'<iframe src="/tracking"></iframe>',
|
|
10
|
+
'<a href="/fallback">Fallback</a>',
|
|
11
|
+
'<div>Enable JavaScript</div>',
|
|
12
|
+
'Enable JavaScript'
|
|
13
|
+
].each do |content|
|
|
14
|
+
context "containing #{content}" do
|
|
15
|
+
let(:html) do
|
|
16
|
+
<<~HTML
|
|
17
|
+
<html><head>
|
|
18
|
+
<noscript>#{content}</noscript>
|
|
19
|
+
<title>My page</title>
|
|
20
|
+
<link rel="canonical" href="https://example.com/">
|
|
21
|
+
<link rel="stylesheet" href="/style.css">
|
|
22
|
+
</head><body><h1>Heading</h1></body></html>
|
|
23
|
+
HTML
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it 'preserves the title and head links after the fallback' do
|
|
27
|
+
expect(page.title).to eq('My page')
|
|
28
|
+
expect(page.canonicals).to eq([{ rel: 'canonical', href: 'https://example.com/' }])
|
|
29
|
+
expect(page.stylesheets).to eq([{ rel: 'stylesheet', href: 'https://example.com/style.css' }])
|
|
30
|
+
expect(page.head_links.size).to eq(2)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
describe 'with an unclosed iframe' do
|
|
37
|
+
['<iframe src="/tracking">', '<iframe src="/tracking" />'].each do |iframe|
|
|
38
|
+
['head', 'body'].each do |location|
|
|
39
|
+
context "in the #{location}, written as #{iframe}" do
|
|
40
|
+
let(:html) do
|
|
41
|
+
fallback = "<noscript>#{iframe}</noscript>"
|
|
42
|
+
<<~HTML
|
|
43
|
+
<html><head>
|
|
44
|
+
#{fallback if location == 'head'}
|
|
45
|
+
<title>My page</title>
|
|
46
|
+
</head><body>
|
|
47
|
+
#{fallback if location == 'body'}
|
|
48
|
+
<h1>Heading</h1>
|
|
49
|
+
<a href="/about">About</a>
|
|
50
|
+
<img src="/photo.jpg">
|
|
51
|
+
</body></html>
|
|
52
|
+
HTML
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'preserves the rest of the page and the fallback iframe' do
|
|
56
|
+
expect(page.title).to eq('My page')
|
|
57
|
+
expect(page.h1).to eq(['Heading'])
|
|
58
|
+
expect(page.links.raw).to eq(['/about'])
|
|
59
|
+
expect(page.images.to_a).to eq(['https://example.com/photo.jpg'])
|
|
60
|
+
expect(page.iframes).to eq([{ src: '/tracking' }])
|
|
61
|
+
expect(page.iframe_sources).to eq(['https://example.com/tracking'])
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
describe 'fallback extraction' do
|
|
69
|
+
let(:html) do
|
|
70
|
+
<<~HTML
|
|
71
|
+
<html><head>
|
|
72
|
+
<base href="https://example.com/assets/">
|
|
73
|
+
<link rel="preload" href="style.css" as="style">
|
|
74
|
+
<noscript>
|
|
75
|
+
<link rel="stylesheet" href="style.css">
|
|
76
|
+
<link rel="alternate" type="application/rss+xml" href="feed.xml">
|
|
77
|
+
<link rel="icon" href="icon.png">
|
|
78
|
+
<img src="pixel.gif" width="1" height="1">
|
|
79
|
+
</noscript>
|
|
80
|
+
<title>Café & news</title>
|
|
81
|
+
</head><body>
|
|
82
|
+
<a href="before">Before</a>
|
|
83
|
+
<noscript>
|
|
84
|
+
<h1>Café & news</h1>
|
|
85
|
+
<a href="fallback?x=1&y=2">Fallback</a>
|
|
86
|
+
<img src="photo.jpg" srcset="photo-2x.jpg 2x" width="640" height="480">
|
|
87
|
+
<iframe src="video?x=1&y=2" title="Café & news"></iframe>
|
|
88
|
+
<link rel="stylesheet" href="body.css">
|
|
89
|
+
</noscript>
|
|
90
|
+
<a href="after">After</a>
|
|
91
|
+
<img src="after.jpg" width="100" height="100">
|
|
92
|
+
<iframe src="after-video"></iframe>
|
|
93
|
+
</body></html>
|
|
94
|
+
HTML
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it 'preserves head fallback stylesheets without including body stylesheets' do
|
|
98
|
+
expect(page.stylesheets).to eq([{ rel: 'stylesheet', href: 'https://example.com/assets/style.css' }])
|
|
99
|
+
expect(page.head_links.size).to eq(4)
|
|
100
|
+
expect(page.feeds).to eq([
|
|
101
|
+
{ title: nil, href: 'https://example.com/assets/feed.xml', type: 'application/rss+xml' }
|
|
102
|
+
])
|
|
103
|
+
expect(page.images.favicon).to eq('https://example.com/assets/icon.png')
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
it 'preserves fallback links and their order, resolving against the document base' do
|
|
107
|
+
expect(page.links.raw).to eq(['before', 'fallback?x=1&y=2', 'after'])
|
|
108
|
+
expect(page.links.all).to eq([
|
|
109
|
+
'https://example.com/assets/before', 'https://example.com/assets/fallback?x=1&y=2',
|
|
110
|
+
'https://example.com/assets/after'
|
|
111
|
+
])
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
it 'preserves fallback images, responsive candidates and dimensions' do
|
|
115
|
+
expect(page.images.to_a).to eq(%w[pixel.gif photo.jpg photo-2x.jpg after.jpg].map do |name|
|
|
116
|
+
"https://example.com/assets/#{name}"
|
|
117
|
+
end)
|
|
118
|
+
expect(page.images.with_size.first).to eq(['https://example.com/assets/photo.jpg', 640, 480])
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
it 'preserves fallback iframes and decodes entities once' do
|
|
122
|
+
expect(page.iframes).to eq([
|
|
123
|
+
{ src: 'video?x=1&y=2', title: 'Café & news' }, { src: 'after-video' }
|
|
124
|
+
])
|
|
125
|
+
expect(page.iframe_sources).to eq([
|
|
126
|
+
'https://example.com/assets/video?x=1&y=2', 'https://example.com/assets/after-video'
|
|
127
|
+
])
|
|
128
|
+
expect(page.title).to eq('Café & news')
|
|
129
|
+
expect(page.h1).to eq(['Café & news'])
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
it 'keeps fallback nodes in the public DOM and preserves the frozen source' do
|
|
133
|
+
original = html.dup
|
|
134
|
+
html.freeze
|
|
135
|
+
|
|
136
|
+
expect(page.parsed.css('head noscript img').size).to eq(1)
|
|
137
|
+
expect(page.parsed.css('body noscript a').size).to eq(1)
|
|
138
|
+
expect(page.to_s).to equal(html)
|
|
139
|
+
expect(html).to eq(original)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
it 'applies the same parsing to a downloaded document' do
|
|
143
|
+
stub_request(:get, 'https://example.com/').to_return(body: html, headers: { 'Content-Type' => 'text/html' })
|
|
144
|
+
downloaded = MetaInspector.new('https://example.com/', download_images: false)
|
|
145
|
+
|
|
146
|
+
expect(downloaded.title).to eq(page.title)
|
|
147
|
+
expect(downloaded.head_links).to eq(page.head_links)
|
|
148
|
+
expect(downloaded.to_hash['images']).to eq(page.images.to_a)
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
it 'does not treat noscript-like text in scripts or comments as fallback markup' do
|
|
153
|
+
html = <<~HTML
|
|
154
|
+
<head><script>var fallback = '<noscript><img src="/fake.jpg"></noscript>';</script>
|
|
155
|
+
<!-- <noscript><img src="/comment.jpg"></noscript> -->
|
|
156
|
+
<title>My page</title></head><body><img src="/photo.jpg"></body>
|
|
157
|
+
HTML
|
|
158
|
+
page = MetaInspector.new('https://example.com/', document: html, download_images: false)
|
|
159
|
+
|
|
160
|
+
expect(page.title).to eq('My page')
|
|
161
|
+
expect(page.images.to_a).to eq(['https://example.com/photo.jpg'])
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
it 'does not introduce an attribute count limit for fallback content' do
|
|
165
|
+
attributes = (1..500).map { |i| "data-#{i}=\"#{i}\"" }.join(' ')
|
|
166
|
+
html = "<noscript><a href=\"/fallback\" #{attributes}>Fallback</a></noscript>"
|
|
167
|
+
page = MetaInspector.new('https://example.com/', document: html)
|
|
168
|
+
|
|
169
|
+
expect(page.links.raw).to eq(['/fallback'])
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
it 'wraps HTML5 depth-limit errors in fallback fragments' do
|
|
173
|
+
html = '<noscript>' + '<div><h1><p>' * 220 + 'deep</noscript><h1>After</h1>'
|
|
174
|
+
|
|
175
|
+
expect do
|
|
176
|
+
MetaInspector.new('https://example.com/', document: html)
|
|
177
|
+
end.to raise_error(MetaInspector::ParserError, /tree depth limit/)
|
|
178
|
+
end
|
|
179
|
+
end
|