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
631
632
633
use skyscraper::html;
use skyscraper::xpath::grammar::XpathItemTreeNode;

// ---------------------------------------------------------------------------
// Character tokens in foreign content
// ---------------------------------------------------------------------------

/// A NULL character (U+0000) inside a foreign element should be replaced with
/// U+FFFD REPLACEMENT CHARACTER (WHATWG 13.2.6.5).
#[test]
fn null_character_in_foreign_content_replaced_with_replacement_character() {
    let text = "<html><body><svg><text>\0</text></svg></body></html>";
    let document = html::parse(text).unwrap();
    let output = document.to_string();
    assert!(
        output.contains('\u{FFFD}'),
        "NULL should be replaced with U+FFFD in foreign content: {output:?}"
    );
}

/// Regular text inside a foreign element should be preserved.
#[test]
fn text_in_foreign_content_is_preserved() {
    let text = "<html><body><svg><text>hello</text></svg></body></html>";
    let document = html::parse(text).unwrap();
    let output = document.to_string();
    assert!(
        output.contains("hello"),
        "text in foreign content should be preserved: {output:?}"
    );
}

/// Whitespace characters in foreign content should be inserted normally.
#[test]
fn whitespace_in_foreign_content_is_preserved() {
    let text = "<html><body><svg> <rect/> </svg></body></html>";
    let document = html::parse(text).unwrap();
    let output = document.to_string();
    assert!(
        output.contains("<svg>"),
        "svg element should be present: {output:?}"
    );
}

// ---------------------------------------------------------------------------
// Comment and DOCTYPE in foreign content
// ---------------------------------------------------------------------------

/// A comment token in foreign content should be inserted as a comment node.
#[test]
fn comment_in_foreign_content_is_inserted() {
    let text = "<html><body><svg><!-- svg comment --></svg></body></html>";
    let document = html::parse(text).unwrap();
    let output = document.to_string();
    assert!(
        output.contains("<!-- svg comment -->"),
        "comment in foreign content should be preserved: {output:?}"
    );
}

// ---------------------------------------------------------------------------
// Start tags — breakout behavior
// ---------------------------------------------------------------------------

/// An HTML-like start tag (e.g. <div>) inside SVG foreign content should
/// break out of foreign content, popping SVG elements until the adjusted
/// current node is in the HTML namespace, then reprocess using "in body" rules.
#[test]
fn html_start_tag_breaks_out_of_svg_foreign_content() {
    let text = "<html><body><svg><div>hello</div></svg></body></html>";
    let document = html::parse(text).unwrap();
    let output = document.to_string();
    // The <div> should NOT be a child of <svg>; it should be a sibling.
    // The parser should pop <svg> when it sees <div>.
    assert!(
        output.contains("<div>hello</div>"),
        "div should be present after breaking out of SVG: {output:?}"
    );
}

/// A <p> tag inside MathML foreign content should break out of foreign content.
#[test]
fn p_tag_breaks_out_of_mathml_foreign_content() {
    let text = "<html><body><math><mi>x</mi><p>text</p></math></body></html>";
    let document = html::parse(text).unwrap();
    let output = document.to_string();
    assert!(
        output.contains("<p>text</p>"),
        "p element should be present after breaking out of MathML: {output:?}"
    );
}

/// A <font> tag with a "color" attribute should break out of foreign content.
#[test]
fn font_with_color_breaks_out_of_foreign_content() {
    let text = r#"<html><body><svg><font color="red">text</font></svg></body></html>"#;
    let document = html::parse(text).unwrap();
    let output = document.to_string();
    assert!(
        output.contains("<font"),
        "font element should be present after breaking out: {output:?}"
    );
}

/// A <font> tag WITHOUT color/face/size attributes should NOT break out of
/// foreign content — it should be treated as a generic foreign element.
#[test]
fn font_without_special_attrs_stays_in_foreign_content() {
    let text = "<html><body><svg><font>text</font></svg></body></html>";
    let document = html::parse(text).unwrap();
    // The font element should be in SVG namespace since it didn't break out
    let font_element = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::ElementNode(e) if e.name == "font" => Some(e),
            _ => None,
        })
        .expect("font element should be present");
    assert_eq!(
        font_element.namespace.as_deref(),
        Some("http://www.w3.org/2000/svg"),
        "font without color/face/size should stay in SVG namespace"
    );
}

// ---------------------------------------------------------------------------
// Start tags — foreign element insertion
// ---------------------------------------------------------------------------

/// Unknown elements inside SVG should be inserted in the SVG namespace.
#[test]
fn unknown_element_in_svg_gets_svg_namespace() {
    let text = "<html><body><svg><rect></rect></svg></body></html>";
    let document = html::parse(text).unwrap();
    let rect_element = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::ElementNode(e) if e.name == "rect" => Some(e),
            _ => None,
        })
        .expect("rect element should be present");
    assert_eq!(
        rect_element.namespace.as_deref(),
        Some("http://www.w3.org/2000/svg"),
        "rect should be in SVG namespace"
    );
}

/// Unknown elements inside MathML should be inserted in the MathML namespace.
#[test]
fn unknown_element_in_mathml_gets_mathml_namespace() {
    let text = "<html><body><math><mfrac><mn>1</mn><mn>2</mn></mfrac></math></body></html>";
    let document = html::parse(text).unwrap();
    let mfrac_element = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::ElementNode(e) if e.name == "mfrac" => Some(e),
            _ => None,
        })
        .expect("mfrac element should be present");
    assert_eq!(
        mfrac_element.namespace.as_deref(),
        Some("http://www.w3.org/1998/Math/MathML"),
        "mfrac should be in MathML namespace"
    );
}

/// Self-closing elements in foreign content should be popped immediately.
#[test]
fn self_closing_element_in_svg_pops_immediately() {
    let text = "<html><body><svg><circle/><rect/></svg></body></html>";
    let document = html::parse(text).unwrap();
    let output = document.to_string();
    assert!(
        output.contains("<circle>"),
        "self-closing circle should be present: {output:?}"
    );
    assert!(
        output.contains("<rect>"),
        "self-closing rect should be present: {output:?}"
    );
}

/// SVG attribute adjustment should be applied when inserting foreign elements
/// in the "in foreign content" mode (not just in the in_body SVG handler).
#[test]
fn svg_attribute_adjustment_in_foreign_content() {
    let text =
        r#"<html><body><svg><rect viewbox="0 0 100 100"></rect></svg></body></html>"#;
    let document = html::parse(text).unwrap();
    let output = document.to_string();
    assert!(
        output.contains("viewBox="),
        "viewbox should be adjusted to viewBox in foreign content: {output:?}"
    );
}

/// SVG element name case correction should be applied (e.g. foreignobject → foreignObject).
#[test]
fn svg_element_name_correction_in_foreign_content() {
    let text = "<html><body><svg><foreignobject>text</foreignobject></svg></body></html>";
    let document = html::parse(text).unwrap();
    let fo_element = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::ElementNode(e) if e.name == "foreignObject" => Some(e),
            _ => None,
        })
        .expect("foreignObject element should be present");
    assert_eq!(
        fo_element.namespace.as_deref(),
        Some("http://www.w3.org/2000/svg"),
        "foreignObject should be in SVG namespace"
    );
}

// ---------------------------------------------------------------------------
// End tags in foreign content
// ---------------------------------------------------------------------------

/// An end tag that matches the current SVG element should pop it.
#[test]
fn end_tag_pops_matching_foreign_element() {
    let text = "<html><body><svg><g><rect></rect></g></svg></body></html>";
    let document = html::parse(text).unwrap();
    let output = document.to_string();
    assert!(
        output.contains("<g>"),
        "g element should be present: {output:?}"
    );
    assert!(
        output.contains("<rect>"),
        "rect element should be present: {output:?}"
    );
}

/// An end tag in foreign content should match case-insensitively (by lowering
/// the node's tag name).
#[test]
fn end_tag_matches_case_insensitively_in_foreign_content() {
    // foreignobject gets corrected to foreignObject, but </foreignobject>
    // should still match it (lowercased comparison).
    let text = "<html><body><svg><foreignobject>text</foreignobject></svg></body></html>";
    let document = html::parse(text).unwrap();
    let output = document.to_string();
    assert!(
        output.contains("foreignObject"),
        "foreignObject element should be present: {output:?}"
    );
    assert!(
        output.contains("text"),
        "text content should be preserved: {output:?}"
    );
}

// ---------------------------------------------------------------------------
// MathML text integration points
// ---------------------------------------------------------------------------

/// A start tag that is NOT mglyph or malignmark inside a MathML text
/// integration point (e.g. <mtext>) should be processed as HTML, not as
/// foreign content (the tree construction dispatcher sends it to "in body").
#[test]
fn html_start_tag_in_mathml_text_integration_point() {
    let text = "<html><body><math><mtext><span>hello</span></mtext></math></body></html>";
    let document = html::parse(text).unwrap();
    let span_element = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::ElementNode(e) if e.name == "span" => Some(e),
            _ => None,
        })
        .expect("span element should be present");
    // span should be in HTML namespace (no namespace = HTML)
    assert_eq!(
        span_element.namespace, None,
        "span inside mtext should be in HTML namespace"
    );
}

/// mglyph inside a MathML text integration point should be processed as
/// foreign content (dispatched to foreign content rules).
#[test]
fn mglyph_in_mathml_text_integration_point_is_foreign() {
    let text = "<html><body><math><mtext><mglyph/></mtext></math></body></html>";
    let document = html::parse(text).unwrap();
    let mglyph_element = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::ElementNode(e) if e.name == "mglyph" => Some(e),
            _ => None,
        })
        .expect("mglyph element should be present");
    assert_eq!(
        mglyph_element.namespace.as_deref(),
        Some("http://www.w3.org/1998/Math/MathML"),
        "mglyph inside mtext should be in MathML namespace"
    );
}

// ---------------------------------------------------------------------------
// HTML integration points
// ---------------------------------------------------------------------------

/// Start tags inside an SVG <foreignObject> should be processed as HTML.
#[test]
fn html_content_inside_svg_foreignobject() {
    let text = "<html><body><svg><foreignobject><div>hello</div></foreignobject></svg></body></html>";
    let document = html::parse(text).unwrap();
    let div_element = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::ElementNode(e) if e.name == "div" => Some(e),
            _ => None,
        })
        .expect("div element should be present inside foreignObject");
    assert_eq!(
        div_element.namespace, None,
        "div inside foreignObject should be in HTML namespace"
    );
}

/// Start tags inside an SVG <desc> should be processed as HTML.
#[test]
fn html_content_inside_svg_desc() {
    let text = "<html><body><svg><desc><span>description</span></desc></svg></body></html>";
    let document = html::parse(text).unwrap();
    let span_element = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::ElementNode(e) if e.name == "span" => Some(e),
            _ => None,
        })
        .expect("span element should be present inside desc");
    assert_eq!(
        span_element.namespace, None,
        "span inside desc should be in HTML namespace"
    );
}

/// Start tags inside an SVG <title> should be processed as HTML.
#[test]
fn html_content_inside_svg_title() {
    let text = "<html><body><svg><title><b>bold</b></title></svg></body></html>";
    let document = html::parse(text).unwrap();
    let b_element = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::ElementNode(e) if e.name == "b" => Some(e),
            _ => None,
        })
        .expect("b element should be present inside title");
    assert_eq!(
        b_element.namespace, None,
        "b inside SVG title should be in HTML namespace"
    );
}

// ---------------------------------------------------------------------------
// Nested SVG/MathML
// ---------------------------------------------------------------------------

/// Nested SVG elements should maintain proper namespacing.
#[test]
fn nested_svg_elements_maintain_namespace() {
    let text = "<html><body><svg><g><circle/></g></svg></body></html>";
    let document = html::parse(text).unwrap();
    let g_element = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::ElementNode(e) if e.name == "g" => Some(e),
            _ => None,
        })
        .expect("g element should be present");
    assert_eq!(
        g_element.namespace.as_deref(),
        Some("http://www.w3.org/2000/svg"),
        "g should be in SVG namespace"
    );

    let circle_element = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::ElementNode(e) if e.name == "circle" => Some(e),
            _ => None,
        })
        .expect("circle element should be present");
    assert_eq!(
        circle_element.namespace.as_deref(),
        Some("http://www.w3.org/2000/svg"),
        "circle should be in SVG namespace"
    );
}

/// An SVG inside a MathML annotation-xml with encoding="text/html" should
/// be processed as HTML (annotation-xml is an HTML integration point).
#[test]
fn svg_in_mathml_annotation_xml_with_html_encoding() {
    let text = r#"<html><body><math><annotation-xml encoding="text/html"><svg></svg></annotation-xml></math></body></html>"#;
    let document = html::parse(text).unwrap();
    let svg_element = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::ElementNode(e) if e.name == "svg" => Some(e),
            _ => None,
        })
        .expect("svg element should be present");
    // When annotation-xml is an HTML integration point, start tags are
    // processed by "in body" rules, so <svg> gets SVG namespace via in_body.
    assert_eq!(
        svg_element.namespace.as_deref(),
        Some("http://www.w3.org/2000/svg"),
        "svg should be in SVG namespace"
    );
}

// ---------------------------------------------------------------------------
// Complex real-world-ish cases
// ---------------------------------------------------------------------------

/// SVG with nested elements followed by HTML content.
#[test]
fn svg_followed_by_html_content() {
    let text = "<html><body><svg><rect/></svg><p>after</p></body></html>";
    let document = html::parse(text).unwrap();
    let output = document.to_string();
    assert!(
        output.contains("<svg>"),
        "svg should be present: {output:?}"
    );
    assert!(
        output.contains("<p>after</p>"),
        "p should follow svg: {output:?}"
    );
}

/// Multiple SVG elements in the same document.
#[test]
fn multiple_svg_elements() {
    let text = "<html><body><svg><rect/></svg><svg><circle/></svg></body></html>";
    let document = html::parse(text).unwrap();
    let svg_count = document
        .iter()
        .filter(|node| matches!(node, XpathItemTreeNode::ElementNode(e) if e.name == "svg"))
        .count();
    assert_eq!(svg_count, 2, "should have two svg elements");
}

/// MathML attributes should be adjusted when processing tokens in foreign content.
#[test]
fn mathml_attribute_adjustment_in_foreign_content() {
    let text = r#"<html><body><math><mrow definitionurl="http://example.com"></mrow></math></body></html>"#;
    let document = html::parse(text).unwrap();
    let output = document.to_string();
    assert!(
        output.contains("definitionURL="),
        "definitionurl should be adjusted to definitionURL in foreign content: {output:?}"
    );
}

// ---------------------------------------------------------------------------
// Foreign attribute namespace assignment (WHATWG 13.2.6.3)
// ---------------------------------------------------------------------------

/// xlink:href in SVG foreign content should get the xlink namespace.
#[test]
fn foreign_attribute_xlink_href_gets_xlink_namespace() {
    let text =
        r##"<html><body><svg><use xlink:href="#icon"></use></svg></body></html>"##;
    let document = html::parse(text).unwrap();
    let attr = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::AttributeNode(a) if a.name == "xlink:href" => Some(a),
            _ => None,
        })
        .expect("xlink:href attribute should be present");
    assert_eq!(
        attr.namespace.as_deref(),
        Some("http://www.w3.org/1999/xlink"),
        "xlink:href should have the xlink namespace"
    );
}

/// All xlink:* attributes should get the xlink namespace.
#[test]
fn foreign_attribute_all_xlink_variants_get_namespace() {
    let text = r##"<html><body><svg><a xlink:actuate="onRequest" xlink:arcrole="http://example.com" xlink:href="#" xlink:role="http://example.com" xlink:show="new" xlink:title="link" xlink:type="simple"></a></svg></body></html>"##;
    let document = html::parse(text).unwrap();
    let xlink_attrs: Vec<_> = document
        .iter()
        .filter_map(|node| match node {
            XpathItemTreeNode::AttributeNode(a) if a.name.starts_with("xlink:") => Some(a),
            _ => None,
        })
        .collect();
    assert_eq!(xlink_attrs.len(), 7, "all 7 xlink:* attributes should be present");
    for attr in &xlink_attrs {
        assert_eq!(
            attr.namespace.as_deref(),
            Some("http://www.w3.org/1999/xlink"),
            "{} should have the xlink namespace",
            attr.name
        );
    }
}

/// xml:lang in SVG foreign content should get the XML namespace.
#[test]
fn foreign_attribute_xml_lang_gets_xml_namespace() {
    let text =
        r#"<html><body><svg xml:lang="en"><text>hello</text></svg></body></html>"#;
    let document = html::parse(text).unwrap();
    let attr = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::AttributeNode(a) if a.name == "xml:lang" => Some(a),
            _ => None,
        })
        .expect("xml:lang attribute should be present");
    assert_eq!(
        attr.namespace.as_deref(),
        Some("http://www.w3.org/XML/1998/namespace"),
        "xml:lang should have the XML namespace"
    );
}

/// xml:space in SVG foreign content should get the XML namespace.
#[test]
fn foreign_attribute_xml_space_gets_xml_namespace() {
    let text =
        r#"<html><body><svg xml:space="preserve"><text>hello</text></svg></body></html>"#;
    let document = html::parse(text).unwrap();
    let attr = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::AttributeNode(a) if a.name == "xml:space" => Some(a),
            _ => None,
        })
        .expect("xml:space attribute should be present");
    assert_eq!(
        attr.namespace.as_deref(),
        Some("http://www.w3.org/XML/1998/namespace"),
        "xml:space should have the XML namespace"
    );
}

/// xmlns on an SVG element should get the xmlns namespace.
#[test]
fn foreign_attribute_xmlns_gets_xmlns_namespace() {
    let text =
        r#"<html><body><svg xmlns="http://www.w3.org/2000/svg"><rect/></svg></body></html>"#;
    let document = html::parse(text).unwrap();
    let attr = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::AttributeNode(a) if a.name == "xmlns" => Some(a),
            _ => None,
        })
        .expect("xmlns attribute should be present");
    assert_eq!(
        attr.namespace.as_deref(),
        Some("http://www.w3.org/2000/xmlns/"),
        "xmlns should have the xmlns namespace"
    );
}

/// xmlns:xlink on an SVG element should get the xmlns namespace.
#[test]
fn foreign_attribute_xmlns_xlink_gets_xmlns_namespace() {
    let text =
        r##"<html><body><svg xmlns:xlink="http://www.w3.org/1999/xlink"><use xlink:href="#icon"/></svg></body></html>"##;
    let document = html::parse(text).unwrap();
    let attr = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::AttributeNode(a) if a.name == "xmlns:xlink" => Some(a),
            _ => None,
        })
        .expect("xmlns:xlink attribute should be present");
    assert_eq!(
        attr.namespace.as_deref(),
        Some("http://www.w3.org/2000/xmlns/"),
        "xmlns:xlink should have the xmlns namespace"
    );
}

/// Regular attributes in foreign content should NOT get a namespace.
#[test]
fn regular_attributes_in_foreign_content_have_no_namespace() {
    let text =
        r#"<html><body><svg><rect width="100" height="50"></rect></svg></body></html>"#;
    let document = html::parse(text).unwrap();
    let attrs: Vec<_> = document
        .iter()
        .filter_map(|node| match node {
            XpathItemTreeNode::AttributeNode(a)
                if a.name == "width" || a.name == "height" =>
            {
                Some(a)
            }
            _ => None,
        })
        .collect();
    assert_eq!(attrs.len(), 2, "width and height should be present");
    for attr in &attrs {
        assert_eq!(
            attr.namespace, None,
            "{} should have no namespace",
            attr.name
        );
    }
}

/// Foreign attribute namespace assignment should work for MathML elements too.
#[test]
fn foreign_attribute_xlink_in_mathml_gets_namespace() {
    let text =
        r##"<html><body><math><mrow xlink:href="#ref"></mrow></math></body></html>"##;
    let document = html::parse(text).unwrap();
    let attr = document
        .iter()
        .find_map(|node| match node {
            XpathItemTreeNode::AttributeNode(a) if a.name == "xlink:href" => Some(a),
            _ => None,
        })
        .expect("xlink:href attribute should be present on MathML element");
    assert_eq!(
        attr.namespace.as_deref(),
        Some("http://www.w3.org/1999/xlink"),
        "xlink:href in MathML should have the xlink namespace"
    );
}