skyscraper 0.7.0

XPath for HTML web scraping
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use skyscraper::html::{self, grammar::document_builder::DocumentBuilder};

use crate::test_framework;

/// Basic <select> with <option> children.
#[test]
fn select_basic_options() {
    let text =
        r#"<html><head></head><body><select><option>A</option><option>B</option></select></body></html>"#;

    let document = html::parse(text).unwrap();

    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select
                            .add_element("option", |opt| opt.add_text("A"))
                            .add_element("option", |opt| opt.add_text("B"))
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// Select with optgroup containing options.
#[test]
fn select_optgroup_with_options() {
    let text = r#"<html><head></head><body><select><optgroup label="fruits"><option>Apple</option><option>Banana</option></optgroup></select></body></html>"#;

    let document = html::parse(text).unwrap();

    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select.add_element("optgroup", |og| {
                            og.add_attribute_str("label", "fruits")
                                .add_element("option", |opt| opt.add_text("Apple"))
                                .add_element("option", |opt| opt.add_text("Banana"))
                        })
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// Multiple optgroups in select.
#[test]
fn select_multiple_optgroups() {
    let text = r#"<html><head></head><body><select><optgroup label="a"><option>1</option></optgroup><optgroup label="b"><option>2</option></optgroup></select></body></html>"#;

    let document = html::parse(text).unwrap();

    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select
                            .add_element("optgroup", |og| {
                                og.add_attribute_str("label", "a")
                                    .add_element("option", |opt| opt.add_text("1"))
                            })
                            .add_element("optgroup", |og| {
                                og.add_attribute_str("label", "b")
                                    .add_element("option", |opt| opt.add_text("2"))
                            })
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// <option> start tag implicitly closes a previous <option>.
#[test]
fn select_option_closes_previous_option() {
    let text = r#"<html><head></head><body><select><option>A<option>B</select></body></html>"#;

    let document = html::parse(text).unwrap();

    // Per the spec, a new <option> pops the current <option> if open.
    // Both options become direct children of select.
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select
                            .add_element("option", |opt| opt.add_text("A"))
                            .add_element("option", |opt| opt.add_text("B"))
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// <optgroup> start tag implicitly closes a previous <option> and <optgroup>.
#[test]
fn select_optgroup_closes_option_and_optgroup() {
    let text = r#"<html><head></head><body><select><optgroup><option>A<optgroup><option>B</optgroup></select></body></html>"#;

    let document = html::parse(text).unwrap();

    // Per the spec:
    // 1. First <optgroup> is opened.
    // 2. <option>A is opened inside the first optgroup.
    // 3. Second <optgroup> pops <option>A (current is option), then pops the first <optgroup>.
    // 4. <option>B is opened inside the second optgroup.
    // 5. </optgroup> closes the second optgroup.
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select
                            .add_element("optgroup", |og| {
                                og.add_element("option", |opt| opt.add_text("A"))
                            })
                            .add_element("optgroup", |og| {
                                og.add_element("option", |opt| opt.add_text("B"))
                            })
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// </optgroup> end tag when current node is option and previous is optgroup:
/// pops both option and optgroup.
#[test]
fn select_end_optgroup_pops_option_then_optgroup() {
    let text = r#"<html><head></head><body><select><optgroup><option>A</optgroup></select></body></html>"#;

    let document = html::parse(text).unwrap();

    // Per the spec for </optgroup>:
    // 1. Current node is <option>, node before is <optgroup> → pop option.
    // 2. Now current node is <optgroup> → pop it.
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select.add_element("optgroup", |og| {
                            og.add_element("option", |opt| opt.add_text("A"))
                        })
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// Nested <select> start tag closes the current select (parse error).
/// Per the spec, <select> in InSelect closes the select and resets the mode,
/// but does NOT reprocess the token (unlike input/textarea).
#[test]
fn select_nested_select_closes_first() {
    let text = r#"<html><head></head><body><select><option>A</option><select><option>B</option></select></body></html>"#;

    let document = html::parse(text).unwrap();

    // The second <select> closes the first select (parse error).
    // It does NOT open a new select — the token is consumed.
    // Then <option>B is processed in InBody, which creates an option child of body.
    // </option> and </select> are ignored (parse error: nothing to close).
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select.add_element("option", |opt| opt.add_text("A"))
                    })
                    .add_element("option", |opt| opt.add_text("B"))
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// Character text inside select is preserved.
#[test]
fn select_text_content() {
    let text = r#"<html><head></head><body><select>some text</select></body></html>"#;

    let document = html::parse(text).unwrap();

    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| select.add_text("some text"))
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// Comment inside select is preserved.
#[test]
fn select_comment() {
    let text =
        r#"<html><head></head><body><select><!-- comment --></select></body></html>"#;

    let document = html::parse(text).unwrap();

    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| select.add_comment(" comment "))
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// <input> inside select closes the select and reprocesses the token.
#[test]
fn select_input_closes_select() {
    let text = r#"<html><head></head><body><select><option>A</option><input type="text"></select></body></html>"#;

    let document = html::parse(text).unwrap();

    // <input> in InSelect pops until select is popped, resets insertion mode,
    // then reprocesses <input> in InBody which inserts it as a void element.
    // The trailing </select> is ignored (no select in scope).
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select.add_element("option", |opt| opt.add_text("A"))
                    })
                    .add_element("input", |input| input.add_attribute_str("type", "text"))
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// Empty select element.
#[test]
fn select_empty() {
    let text = r#"<html><head></head><body><select></select></body></html>"#;

    let document = html::parse(text).unwrap();

    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| body.add_element("select", |select| select))
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// Select with <hr> element — hr is a void element that gets popped immediately.
#[test]
fn select_hr_element() {
    let text = r#"<html><head></head><body><select><option>A</option><hr><option>B</option></select></body></html>"#;

    let document = html::parse(text).unwrap();

    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select
                            .add_element("option", |opt| opt.add_text("A"))
                            .add_element("hr", |hr| hr)
                            .add_element("option", |opt| opt.add_text("B"))
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// EOF inside select delegates to InBody for EOF handling.
#[test]
fn select_eof() {
    let text = r#"<html><head></head><body><select><option>A"#;

    let document = html::parse(text).unwrap();

    // EOF in select is handled by InBody's EOF, which calls stop_parsing.
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select.add_element("option", |opt| opt.add_text("A"))
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// <textarea> inside select closes the select and reprocesses.
#[test]
fn select_textarea_closes_select() {
    let text = r#"<html><head></head><body><select><option>A</option><textarea>text</textarea></select></body></html>"#;

    let document = html::parse(text).unwrap();

    // <textarea> in InSelect: pop until select, reset mode, reprocess.
    // <textarea> in InBody creates a textarea element.
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select.add_element("option", |opt| opt.add_text("A"))
                    })
                    .add_element("textarea", |ta| ta.add_text("text"))
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// Select inside a table cell enters InSelectInTable mode.
#[test]
fn select_in_table_cell() {
    let text = r#"<html><head></head><body><table><tr><td><select><option>A</option></select></td></tr></table></body></html>"#;

    let document = html::parse(text).unwrap();

    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("table", |table| {
                        table.add_element("tbody", |tbody| {
                            tbody.add_element("tr", |tr| {
                                tr.add_element("td", |td| {
                                    td.add_element("select", |select| {
                                        select
                                            .add_element("option", |opt| opt.add_text("A"))
                                    })
                                })
                            })
                        })
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// InSelectInTable: table start tag inside select closes the select.
#[test]
fn select_in_table_table_start_closes_select() {
    let text = r#"<html><head></head><body><table><tr><td><select><option>A</option><table></table></select></td></tr></table></body></html>"#;

    let document = html::parse(text).unwrap();

    // <table> in InSelectInTable: pop until select, reset mode, reprocess <table>.
    // This creates a nested table structure.
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("table", |table| {
                        table.add_element("tbody", |tbody| {
                            tbody.add_element("tr", |tr| {
                                tr.add_element("td", |td| {
                                    td.add_element("select", |select| {
                                        select
                                            .add_element("option", |opt| opt.add_text("A"))
                                    })
                                    .add_element("table", |t| t)
                                })
                            })
                        })
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// Select with attributes.
#[test]
fn select_with_attributes() {
    let text = r#"<html><head></head><body><select name="color" id="sel"><option value="r">Red</option></select></body></html>"#;

    let document = html::parse(text).unwrap();

    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select
                            .add_attribute_str("name", "color")
                            .add_attribute_str("id", "sel")
                            .add_element("option", |opt| {
                                opt.add_attribute_str("value", "r").add_text("Red")
                            })
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// Anything else in InSelect is a parse error and ignored.
/// E.g. <div> inside select is ignored.
#[test]
fn select_ignores_unexpected_tags() {
    let text = r#"<html><head></head><body><select><div>text</div><option>A</option></select></body></html>"#;

    let document = html::parse(text).unwrap();

    // <div> is an "anything else" case in InSelect — parse error, token ignored.
    // But the text "text" inside will be inserted as character tokens.
    // </div> is also ignored.
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select
                            .add_text("text")
                            .add_element("option", |opt| opt.add_text("A"))
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// Option and optgroup in body (outside select) — InBody handles these.
#[test]
fn option_in_body_outside_select() {
    let text = r#"<html><head></head><body><option>A</option><option>B</option></body></html>"#;

    let document = html::parse(text).unwrap();

    // InBody's optgroup/option handler: if current node is option, pop it first.
    // Then reconstruct formatting elements and insert.
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("option", |opt| opt.add_text("A"))
                        .add_element("option", |opt| opt.add_text("B"))
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// </optgroup> when current node is not option or optgroup — parse error, ignored.
#[test]
fn select_end_optgroup_parse_error_when_not_optgroup() {
    let text = r#"<html><head></head><body><select></optgroup><option>A</option></select></body></html>"#;

    let document = html::parse(text).unwrap();

    // </optgroup> right after <select> — current node is select, not optgroup.
    // Parse error, token ignored. The option is still inserted normally.
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select.add_element("option", |opt| opt.add_text("A"))
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// </option> when current node is not option — parse error, ignored.
#[test]
fn select_end_option_parse_error_when_not_option() {
    let text = r#"<html><head></head><body><select></option><option>A</option></select></body></html>"#;

    let document = html::parse(text).unwrap();

    // </option> right after <select> — current node is select, not option.
    // Parse error, token ignored.
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select.add_element("option", |opt| opt.add_text("A"))
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// NULL character inside select — parse error, ignored.
#[test]
fn select_null_character_ignored() {
    let text = "<html><head></head><body><select>\0<option>A</option></select></body></html>";

    let document = html::parse(text).unwrap();

    // NULL character is a parse error and ignored — not inserted as text.
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select.add_element("option", |opt| opt.add_text("A"))
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// DOCTYPE inside select — parse error, ignored.
#[test]
fn select_doctype_ignored() {
    let text =
        r#"<html><head></head><body><select><!DOCTYPE html><option>A</option></select></body></html>"#;

    let document = html::parse(text).unwrap();

    // DOCTYPE in select is a parse error, ignored.
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("select", |select| {
                        select.add_element("option", |opt| opt.add_text("A"))
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}

/// InSelectInTable: end tag when element not in table scope — ignored.
#[test]
fn select_in_table_end_tag_not_in_scope_ignored() {
    let text = r#"<html><head></head><body><table><tr><td><select></caption><option>A</option></select></td></tr></table></body></html>"#;

    let document = html::parse(text).unwrap();

    // </caption> in InSelectInTable — parse error.
    // No caption in table scope, so token is ignored. Select continues normally.
    let expected = DocumentBuilder::new()
        .add_element("html", |html| {
            html.add_element("head", |head| head)
                .add_element("body", |body| {
                    body.add_element("table", |table| {
                        table.add_element("tbody", |tbody| {
                            tbody.add_element("tr", |tr| {
                                tr.add_element("td", |td| {
                                    td.add_element("select", |select| {
                                        select
                                            .add_element("option", |opt| opt.add_text("A"))
                                    })
                                })
                            })
                        })
                    })
                })
        })
        .build()
        .unwrap();

    assert!(test_framework::compare_documents(expected, document, true));
}