html-to-markdown 2.29.0-arm64-darwin
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 +7 -0
- data/.bundle/config +2 -0
- data/.gitignore +3 -0
- data/.rubocop.yml +29 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +223 -0
- data/README.md +345 -0
- data/Rakefile +32 -0
- data/Steepfile +26 -0
- data/bin/benchmark.rb +232 -0
- data/exe/html-to-markdown +6 -0
- data/html-to-markdown-rb.gemspec +99 -0
- data/lib/html_to_markdown/cli.rb +21 -0
- data/lib/html_to_markdown/cli_proxy.rb +74 -0
- data/lib/html_to_markdown/version.rb +5 -0
- data/lib/html_to_markdown.rb +211 -0
- data/lib/html_to_markdown_rb.bundle +0 -0
- data/sig/html_to_markdown/cli.rbs +24 -0
- data/sig/html_to_markdown/cli_proxy.rbs +48 -0
- data/sig/html_to_markdown.rbs +498 -0
- data/sig/open3.rbs +12 -0
- data/spec/cli_proxy_spec.rb +42 -0
- data/spec/convert_spec.rb +77 -0
- data/spec/convert_with_tables_spec.rb +194 -0
- data/spec/metadata_extraction_spec.rb +437 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/visitor_issue_187_spec.rb +605 -0
- data/spec/visitor_spec.rb +1149 -0
- metadata +80 -0
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# rubocop:disable RSpec/VerifiedDoubles, RSpec/DescribeMethod
|
|
4
|
+
require 'spec_helper'
|
|
5
|
+
|
|
6
|
+
RSpec.describe HtmlToMarkdown, 'Issue #187: Visitor tag_name context validation' do
|
|
7
|
+
describe 'tag_name in visit_element_start context' do
|
|
8
|
+
it 'receives correct tag_name for div element' do
|
|
9
|
+
html = '<div>Content</div>'
|
|
10
|
+
visitor = double(Object)
|
|
11
|
+
tag_names_visited = []
|
|
12
|
+
|
|
13
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
14
|
+
tag_names_visited << ctx[:tag_name]
|
|
15
|
+
{ type: :continue }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
allow(visitor).to receive_messages(
|
|
19
|
+
visit_element_end: { type: :continue },
|
|
20
|
+
visit_text: { type: :continue }
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
24
|
+
|
|
25
|
+
expect(tag_names_visited).to include('div')
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'receives correct tag_name for script element' do
|
|
29
|
+
html = '<div><script>var x = 1;</script></div>'
|
|
30
|
+
visitor = double(Object)
|
|
31
|
+
tag_names_visited = []
|
|
32
|
+
|
|
33
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
34
|
+
tag_names_visited << ctx[:tag_name]
|
|
35
|
+
{ type: :continue }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# NOTE: script and style elements are filtered out by default, so this test verifies
|
|
39
|
+
# that when they do appear in the context, tag_name is correct
|
|
40
|
+
allow(visitor).to receive_messages(
|
|
41
|
+
visit_element_end: { type: :continue },
|
|
42
|
+
visit_text: { type: :continue }
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
46
|
+
|
|
47
|
+
# script tags are often filtered out for security; verify div is there at minimum
|
|
48
|
+
expect(tag_names_visited).to include('div')
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'receives correct tag_name for style element' do
|
|
52
|
+
html = '<style>.cls { color: red; }</style><p>Text</p>'
|
|
53
|
+
visitor = double(Object)
|
|
54
|
+
tag_names_visited = []
|
|
55
|
+
|
|
56
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
57
|
+
tag_names_visited << ctx[:tag_name]
|
|
58
|
+
{ type: :continue }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
allow(visitor).to receive_messages(
|
|
62
|
+
visit_element_end: { type: :continue },
|
|
63
|
+
visit_text: { type: :continue }
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
67
|
+
|
|
68
|
+
# style tags are often filtered out for security; verify p is there at minimum
|
|
69
|
+
expect(tag_names_visited).to include('p')
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it 'receives correct tag_name for p element' do
|
|
73
|
+
html = '<div><p>Paragraph</p></div>'
|
|
74
|
+
visitor = double(Object)
|
|
75
|
+
tag_names_visited = []
|
|
76
|
+
|
|
77
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
78
|
+
tag_names_visited << ctx[:tag_name]
|
|
79
|
+
{ type: :continue }
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
allow(visitor).to receive_messages(
|
|
83
|
+
visit_element_end: { type: :continue },
|
|
84
|
+
visit_text: { type: :continue }
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
88
|
+
|
|
89
|
+
expect(tag_names_visited).to include('p')
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'receives all expected tag names for mixed HTML' do
|
|
93
|
+
html = '<div><p>Text</p><h1>Heading</h1><span>Span</span></div>'
|
|
94
|
+
visitor = double(Object)
|
|
95
|
+
tag_names_visited = []
|
|
96
|
+
|
|
97
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
98
|
+
tag_names_visited << ctx[:tag_name]
|
|
99
|
+
{ type: :continue }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
allow(visitor).to receive_messages(
|
|
103
|
+
visit_element_end: { type: :continue },
|
|
104
|
+
visit_text: { type: :continue },
|
|
105
|
+
visit_heading: { type: :continue }
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
109
|
+
|
|
110
|
+
expect(tag_names_visited).to include('div')
|
|
111
|
+
expect(tag_names_visited).to include('p')
|
|
112
|
+
expect(tag_names_visited).to include('h1')
|
|
113
|
+
expect(tag_names_visited).to include('span')
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
describe 'tag_name type validation' do
|
|
118
|
+
it 'tag_name is always a string' do
|
|
119
|
+
html = '<section id="main"><article>Content</article></section>'
|
|
120
|
+
visitor = double(Object)
|
|
121
|
+
tag_name_types = []
|
|
122
|
+
|
|
123
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
124
|
+
tag_name_types << ctx[:tag_name].class
|
|
125
|
+
{ type: :continue }
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
allow(visitor).to receive_messages(
|
|
129
|
+
visit_element_end: { type: :continue },
|
|
130
|
+
visit_text: { type: :continue }
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
134
|
+
|
|
135
|
+
expect(tag_name_types).not_to be_empty
|
|
136
|
+
expect(tag_name_types).to all(eq(String))
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
it 'tag_name is never nil' do
|
|
140
|
+
html = '<div><span>Text</span></div>'
|
|
141
|
+
visitor = double(Object)
|
|
142
|
+
nil_tag_names = []
|
|
143
|
+
|
|
144
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
145
|
+
nil_tag_names << ctx[:tag_name] if ctx[:tag_name].nil?
|
|
146
|
+
{ type: :continue }
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
allow(visitor).to receive_messages(
|
|
150
|
+
visit_element_end: { type: :continue },
|
|
151
|
+
visit_text: { type: :continue }
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
155
|
+
|
|
156
|
+
expect(nil_tag_names).to be_empty
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
it 'tag_name is never empty string' do
|
|
160
|
+
html = '<div><p>Test</p></div>'
|
|
161
|
+
visitor = double(Object)
|
|
162
|
+
empty_tag_names = []
|
|
163
|
+
|
|
164
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
165
|
+
empty_tag_names << ctx[:tag_name] if ctx[:tag_name].empty?
|
|
166
|
+
{ type: :continue }
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
allow(visitor).to receive_messages(
|
|
170
|
+
visit_element_end: { type: :continue },
|
|
171
|
+
visit_text: { type: :continue }
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
175
|
+
|
|
176
|
+
expect(empty_tag_names).to be_empty
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
describe 'filtering by tag name' do
|
|
181
|
+
it 'filters divs by tag name in context' do
|
|
182
|
+
html = '<div id="d1"><div id="d2">Nested</div></div><p>Paragraph</p>'
|
|
183
|
+
visitor = double(Object)
|
|
184
|
+
divs_found = []
|
|
185
|
+
|
|
186
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
187
|
+
divs_found << ctx[:attributes]['id'] if ctx[:tag_name] == 'div'
|
|
188
|
+
{ type: :continue }
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
allow(visitor).to receive_messages(
|
|
192
|
+
visit_element_end: { type: :continue },
|
|
193
|
+
visit_text: { type: :continue }
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
197
|
+
|
|
198
|
+
expect(divs_found).to include('d1')
|
|
199
|
+
expect(divs_found).to include('d2')
|
|
200
|
+
expect(divs_found.length).to eq(2)
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
it 'filters elements by tag name and applies custom transformation' do
|
|
204
|
+
html = '<div class="remove">Skip me</div><div class="keep">Keep me</div><p>Text</p>'
|
|
205
|
+
visitor = double(Object)
|
|
206
|
+
|
|
207
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
208
|
+
if ctx[:tag_name] == 'div' && ctx[:attributes]['class'] == 'remove'
|
|
209
|
+
{ type: :skip }
|
|
210
|
+
else
|
|
211
|
+
{ type: :continue }
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
allow(visitor).to receive_messages(
|
|
216
|
+
visit_element_end: { type: :continue },
|
|
217
|
+
visit_text: { type: :continue }
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
result = described_class.convert_with_visitor(html, nil, visitor)
|
|
221
|
+
|
|
222
|
+
expect(result).to include('Keep me')
|
|
223
|
+
expect(result).not_to include('Skip me')
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
it 'collects tag names by class attribute' do
|
|
227
|
+
html = '<div class="container"><p class="content">Text</p></div>'
|
|
228
|
+
visitor = double(Object)
|
|
229
|
+
tags_by_class = {}
|
|
230
|
+
|
|
231
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
232
|
+
class_name = ctx[:attributes]['class']
|
|
233
|
+
if class_name
|
|
234
|
+
tags_by_class[class_name] ||= []
|
|
235
|
+
tags_by_class[class_name] << ctx[:tag_name]
|
|
236
|
+
end
|
|
237
|
+
{ type: :continue }
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
allow(visitor).to receive_messages(
|
|
241
|
+
visit_element_end: { type: :continue },
|
|
242
|
+
visit_text: { type: :continue }
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
246
|
+
|
|
247
|
+
expect(tags_by_class['container']).to include('div')
|
|
248
|
+
expect(tags_by_class['content']).to include('p')
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
it 'filters and counts specific tag names' do
|
|
252
|
+
html = '<h1>H1</h1><h2>H2</h2><p>Para</p><h3>H3</h3>'
|
|
253
|
+
visitor = double(Object)
|
|
254
|
+
heading_count = { h1: 0, h2: 0, h3: 0 }
|
|
255
|
+
|
|
256
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
257
|
+
case ctx[:tag_name]
|
|
258
|
+
when 'h1'
|
|
259
|
+
heading_count[:h1] += 1
|
|
260
|
+
when 'h2'
|
|
261
|
+
heading_count[:h2] += 1
|
|
262
|
+
when 'h3'
|
|
263
|
+
heading_count[:h3] += 1
|
|
264
|
+
end
|
|
265
|
+
{ type: :continue }
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
allow(visitor).to receive_messages(
|
|
269
|
+
visit_element_end: { type: :continue },
|
|
270
|
+
visit_text: { type: :continue },
|
|
271
|
+
visit_heading: { type: :continue }
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
275
|
+
|
|
276
|
+
expect(heading_count[:h1]).to eq(1)
|
|
277
|
+
expect(heading_count[:h2]).to eq(1)
|
|
278
|
+
expect(heading_count[:h3]).to eq(1)
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
describe 'filtering divs by class attribute combined with tag_name' do
|
|
283
|
+
it 'identifies and filters divs with specific classes' do
|
|
284
|
+
html = '
|
|
285
|
+
<div class="header">Header</div>
|
|
286
|
+
<div class="content">Content</div>
|
|
287
|
+
<div class="footer">Footer</div>
|
|
288
|
+
<p class="text">Paragraph</p>
|
|
289
|
+
'
|
|
290
|
+
visitor = double(Object)
|
|
291
|
+
content_divs = []
|
|
292
|
+
|
|
293
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
294
|
+
content_divs << ctx if ctx[:tag_name] == 'div' && ctx[:attributes]['class'] == 'content'
|
|
295
|
+
{ type: :continue }
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
allow(visitor).to receive_messages(
|
|
299
|
+
visit_element_end: { type: :continue },
|
|
300
|
+
visit_text: { type: :continue }
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
304
|
+
|
|
305
|
+
expect(content_divs.length).to eq(1)
|
|
306
|
+
expect(content_divs[0][:tag_name]).to eq('div')
|
|
307
|
+
expect(content_divs[0][:attributes]['class']).to eq('content')
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
it 'skips divs matching filter criteria' do
|
|
311
|
+
html = '
|
|
312
|
+
<div class="advertisement">Ad</div>
|
|
313
|
+
<p>Paragraph 1</p>
|
|
314
|
+
<div class="advertisement">Another ad</div>
|
|
315
|
+
<p>Paragraph 2</p>
|
|
316
|
+
'
|
|
317
|
+
visitor = double(Object)
|
|
318
|
+
|
|
319
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
320
|
+
if ctx[:tag_name] == 'div' && ctx[:attributes]['class']&.include?('advertisement')
|
|
321
|
+
{ type: :skip }
|
|
322
|
+
else
|
|
323
|
+
{ type: :continue }
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
allow(visitor).to receive_messages(
|
|
328
|
+
visit_element_end: { type: :continue },
|
|
329
|
+
visit_text: { type: :continue }
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
result = described_class.convert_with_visitor(html, nil, visitor)
|
|
333
|
+
|
|
334
|
+
expect(result).to include('Paragraph 1')
|
|
335
|
+
expect(result).to include('Paragraph 2')
|
|
336
|
+
expect(result).not_to include('Ad')
|
|
337
|
+
expect(result).not_to include('Another ad')
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
it 'transforms divs with specific class' do
|
|
341
|
+
html = '<div class="warning">Important</div><div>Normal</div>'
|
|
342
|
+
visitor = double(Object)
|
|
343
|
+
|
|
344
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
345
|
+
if ctx[:tag_name] == 'div' && ctx[:attributes]['class'] == 'warning'
|
|
346
|
+
{ type: :custom, output: '**WARNING: Important**' }
|
|
347
|
+
else
|
|
348
|
+
{ type: :continue }
|
|
349
|
+
end
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
allow(visitor).to receive_messages(
|
|
353
|
+
visit_element_end: { type: :continue },
|
|
354
|
+
visit_text: { type: :continue }
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
result = described_class.convert_with_visitor(html, nil, visitor)
|
|
358
|
+
|
|
359
|
+
expect(result).to include('WARNING: Important')
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
it 'preserves HTML for certain divs based on class' do
|
|
363
|
+
html = '<div class="custom">Custom HTML</div><div>Normal</div>'
|
|
364
|
+
visitor = double(Object)
|
|
365
|
+
|
|
366
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
367
|
+
if ctx[:tag_name] == 'div' && ctx[:attributes]['class'] == 'custom'
|
|
368
|
+
{ type: :preserve_html }
|
|
369
|
+
else
|
|
370
|
+
{ type: :continue }
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
allow(visitor).to receive_messages(
|
|
375
|
+
visit_element_end: { type: :continue },
|
|
376
|
+
visit_text: { type: :continue }
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
result = described_class.convert_with_visitor(html, nil, visitor)
|
|
380
|
+
|
|
381
|
+
expect(result).to be_a(String)
|
|
382
|
+
end
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
describe 'tag_name consistency across visitor lifecycle' do
|
|
386
|
+
it 'tag_name is consistent in visit_element_start and visit_element_end' do
|
|
387
|
+
html = '<section><article>Content</article></section>'
|
|
388
|
+
visitor = double(Object)
|
|
389
|
+
element_lifecycle = {}
|
|
390
|
+
|
|
391
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
392
|
+
element_lifecycle[ctx[:tag_name]] ||= { start: 0, end: 0 }
|
|
393
|
+
element_lifecycle[ctx[:tag_name]][:start] += 1
|
|
394
|
+
{ type: :continue }
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
allow(visitor).to receive(:visit_element_end) do |ctx, _output|
|
|
398
|
+
element_lifecycle[ctx[:tag_name]][:end] += 1
|
|
399
|
+
{ type: :continue }
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
allow(visitor).to receive(:visit_text).and_return({ type: :continue })
|
|
403
|
+
|
|
404
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
405
|
+
|
|
406
|
+
expect(element_lifecycle['section'][:start]).to eq(element_lifecycle['section'][:end])
|
|
407
|
+
expect(element_lifecycle['article'][:start]).to eq(element_lifecycle['article'][:end])
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
it 'tag_name is consistent for nested elements of same type' do
|
|
411
|
+
html = '<div><div><div>Deep</div></div></div>'
|
|
412
|
+
visitor = double(Object)
|
|
413
|
+
div_count = 0
|
|
414
|
+
|
|
415
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
416
|
+
div_count += 1 if ctx[:tag_name] == 'div'
|
|
417
|
+
{ type: :continue }
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
allow(visitor).to receive_messages(
|
|
421
|
+
visit_element_end: { type: :continue },
|
|
422
|
+
visit_text: { type: :continue }
|
|
423
|
+
)
|
|
424
|
+
|
|
425
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
426
|
+
|
|
427
|
+
expect(div_count).to eq(3)
|
|
428
|
+
end
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
describe 'tag_name with complex HTML structures' do
|
|
432
|
+
it 'maintains tag_name accuracy with complex nested structure' do
|
|
433
|
+
html = '
|
|
434
|
+
<article>
|
|
435
|
+
<header><h1>Title</h1></header>
|
|
436
|
+
<section>
|
|
437
|
+
<div class="content">
|
|
438
|
+
<p>Paragraph <strong>bold</strong></p>
|
|
439
|
+
<ul>
|
|
440
|
+
<li>Item 1</li>
|
|
441
|
+
<li>Item 2</li>
|
|
442
|
+
</ul>
|
|
443
|
+
</div>
|
|
444
|
+
</section>
|
|
445
|
+
<footer>Footer</footer>
|
|
446
|
+
</article>
|
|
447
|
+
'
|
|
448
|
+
visitor = double(Object)
|
|
449
|
+
tag_names = []
|
|
450
|
+
|
|
451
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
452
|
+
tag_names << ctx[:tag_name]
|
|
453
|
+
{ type: :continue }
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
allow(visitor).to receive_messages(
|
|
457
|
+
visit_element_end: { type: :continue },
|
|
458
|
+
visit_text: { type: :continue },
|
|
459
|
+
visit_heading: { type: :continue },
|
|
460
|
+
visit_list_start: { type: :continue },
|
|
461
|
+
visit_list_end: { type: :continue },
|
|
462
|
+
visit_list_item: { type: :continue },
|
|
463
|
+
visit_strong: { type: :continue }
|
|
464
|
+
)
|
|
465
|
+
|
|
466
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
467
|
+
|
|
468
|
+
expect(tag_names).to include('article')
|
|
469
|
+
expect(tag_names).to include('header')
|
|
470
|
+
expect(tag_names).to include('h1')
|
|
471
|
+
expect(tag_names).to include('section')
|
|
472
|
+
expect(tag_names).to include('div')
|
|
473
|
+
expect(tag_names).to include('p')
|
|
474
|
+
expect(tag_names).to include('strong')
|
|
475
|
+
expect(tag_names).to include('ul')
|
|
476
|
+
expect(tag_names).to include('li')
|
|
477
|
+
expect(tag_names).to include('footer')
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
it 'correctly filters table elements by tag_name' do
|
|
481
|
+
html = '
|
|
482
|
+
<table>
|
|
483
|
+
<tr><th>Header</th></tr>
|
|
484
|
+
<tr><td>Data</td></tr>
|
|
485
|
+
</table>
|
|
486
|
+
'
|
|
487
|
+
visitor = double(Object)
|
|
488
|
+
tag_names = []
|
|
489
|
+
|
|
490
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
491
|
+
tag_names << ctx[:tag_name]
|
|
492
|
+
{ type: :continue }
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
allow(visitor).to receive_messages(
|
|
496
|
+
visit_element_end: { type: :continue },
|
|
497
|
+
visit_text: { type: :continue },
|
|
498
|
+
visit_table_start: { type: :continue },
|
|
499
|
+
visit_table_end: { type: :continue },
|
|
500
|
+
visit_table_row: { type: :continue }
|
|
501
|
+
)
|
|
502
|
+
|
|
503
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
504
|
+
|
|
505
|
+
# Tables are handled at higher level, verify core table tag is there
|
|
506
|
+
expect(tag_names).to include('table')
|
|
507
|
+
end
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
describe 'tag_name edge cases' do
|
|
511
|
+
it 'handles self-closing tags correctly' do
|
|
512
|
+
html = '<p>Text<br/>More text<hr/></p>'
|
|
513
|
+
visitor = double(Object)
|
|
514
|
+
tag_names = []
|
|
515
|
+
|
|
516
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
517
|
+
tag_names << ctx[:tag_name]
|
|
518
|
+
{ type: :continue }
|
|
519
|
+
end
|
|
520
|
+
|
|
521
|
+
allow(visitor).to receive_messages(
|
|
522
|
+
visit_element_end: { type: :continue },
|
|
523
|
+
visit_text: { type: :continue }
|
|
524
|
+
)
|
|
525
|
+
|
|
526
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
527
|
+
|
|
528
|
+
expect(tag_names).to include('p')
|
|
529
|
+
expect(tag_names).to include('br')
|
|
530
|
+
expect(tag_names).to include('hr')
|
|
531
|
+
end
|
|
532
|
+
|
|
533
|
+
it 'handles lowercase tag names' do
|
|
534
|
+
html = '<DIV class="Test">Content</DIV>'
|
|
535
|
+
visitor = double(Object)
|
|
536
|
+
tag_names = []
|
|
537
|
+
|
|
538
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
539
|
+
tag_names << ctx[:tag_name]
|
|
540
|
+
{ type: :continue }
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
allow(visitor).to receive_messages(
|
|
544
|
+
visit_element_end: { type: :continue },
|
|
545
|
+
visit_text: { type: :continue }
|
|
546
|
+
)
|
|
547
|
+
|
|
548
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
549
|
+
|
|
550
|
+
# HTML5 normalizes tags to lowercase
|
|
551
|
+
expect(tag_names).to include('div')
|
|
552
|
+
end
|
|
553
|
+
|
|
554
|
+
it 'handles special/semantic elements' do
|
|
555
|
+
html = '<main><nav>Navigation</nav><aside>Sidebar</aside></main>'
|
|
556
|
+
visitor = double(Object)
|
|
557
|
+
tag_names = []
|
|
558
|
+
|
|
559
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
560
|
+
tag_names << ctx[:tag_name]
|
|
561
|
+
{ type: :continue }
|
|
562
|
+
end
|
|
563
|
+
|
|
564
|
+
allow(visitor).to receive_messages(
|
|
565
|
+
visit_element_end: { type: :continue },
|
|
566
|
+
visit_text: { type: :continue }
|
|
567
|
+
)
|
|
568
|
+
|
|
569
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
570
|
+
|
|
571
|
+
expect(tag_names).to include('main')
|
|
572
|
+
expect(tag_names).to include('nav')
|
|
573
|
+
expect(tag_names).to include('aside')
|
|
574
|
+
end
|
|
575
|
+
|
|
576
|
+
it 'handles form elements correctly' do
|
|
577
|
+
html = '
|
|
578
|
+
<form>
|
|
579
|
+
<input type="text" name="username"/>
|
|
580
|
+
<button>Submit</button>
|
|
581
|
+
</form>
|
|
582
|
+
'
|
|
583
|
+
visitor = double(Object)
|
|
584
|
+
tag_names = []
|
|
585
|
+
|
|
586
|
+
allow(visitor).to receive(:visit_element_start) do |ctx|
|
|
587
|
+
tag_names << ctx[:tag_name]
|
|
588
|
+
{ type: :continue }
|
|
589
|
+
end
|
|
590
|
+
|
|
591
|
+
allow(visitor).to receive_messages(
|
|
592
|
+
visit_element_end: { type: :continue },
|
|
593
|
+
visit_text: { type: :continue }
|
|
594
|
+
)
|
|
595
|
+
|
|
596
|
+
described_class.convert_with_visitor(html, nil, visitor)
|
|
597
|
+
|
|
598
|
+
expect(tag_names).to include('form')
|
|
599
|
+
expect(tag_names).to include('input')
|
|
600
|
+
expect(tag_names).to include('button')
|
|
601
|
+
end
|
|
602
|
+
end
|
|
603
|
+
end
|
|
604
|
+
|
|
605
|
+
# rubocop:enable RSpec/VerifiedDoubles, RSpec/DescribeMethod
|