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
use skyscraper::{
    html,
    html::grammar::document_builder::DocumentBuilder,
    xpath,
    xpath::grammar::data_model::{AnyAtomicType, XpathItem},
};

/// `element()` with no arguments matches any element.
#[test]
fn element_test_no_args_matches_all_elements() {
    let text = r#"<html><body><div>a</div><span>b</span></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath = xpath::parse("//body/element()").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(items.len(), 2, "should match div and span: {items:?}");
}

/// `element(div)` matches only div elements.
#[test]
fn element_test_named_matches_specific_element() {
    let text = r#"<html><body><div>a</div><span>b</span></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath = xpath::parse("//body/element(div)").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(items.len(), 1, "should match only div: {items:?}");
}

/// `element(*)` matches any element (same as no args).
#[test]
fn element_test_wildcard_matches_all() {
    let text = r#"<html><body><div>a</div><span>b</span></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath = xpath::parse("//body/element(*)").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(items.len(), 2, "should match div and span: {items:?}");
}

/// `element(nonexistent)` matches nothing.
#[test]
fn element_test_named_no_match() {
    let text = r#"<html><body><div>a</div></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath = xpath::parse("//body/element(section)").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(items.len(), 0, "should match nothing: {items:?}");
}

/// `comment()` matches comment nodes.
#[test]
fn comment_test_matches_comments() {
    let text = r#"<html><body><!-- hello --><div>a</div></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath = xpath::parse("//body/comment()").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(items.len(), 1, "should match 1 comment: {items:?}");
}

/// `comment()` string value is the comment's content (XPath 3.1 §6.7.6).
#[test]
fn comment_node_string_value() {
    let text = r#"<html><body><!-- hello world --><div>a</div></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath = xpath::parse("string(//body/comment())").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(
        items[0],
        XpathItem::AnyAtomicType(AnyAtomicType::String(" hello world ".to_string())),
        "comment string value should be its content"
    );
}

/// `comment()` parent axis navigates to the containing element.
#[test]
fn comment_node_parent_navigation() {
    let text = r#"<html><body><!-- hello --><div>a</div></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath = xpath::parse("name(//comment()/parent::*)").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(
        items[0],
        XpathItem::AnyAtomicType(AnyAtomicType::String("body".to_string())),
        "comment parent should be 'body'"
    );
}

/// `comment()` ancestor axis navigates up the tree.
#[test]
fn comment_node_ancestor_navigation() {
    let text = r#"<html><body><div><!-- inside div --></div></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath = xpath::parse("count(//comment()/ancestor::*)").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(
        items[0],
        XpathItem::AnyAtomicType(AnyAtomicType::Integer(3)),
        "comment should have 3 ancestors: div, body, html"
    );
}

/// `data()` atomization of a comment node returns its content.
#[test]
fn comment_node_data_atomization() {
    let text = r#"<html><body><!-- atomize me --><div>a</div></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath = xpath::parse("data(//body/comment())").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(
        items[0],
        XpathItem::AnyAtomicType(AnyAtomicType::String(" atomize me ".to_string())),
        "data() on comment should return its content"
    );
}

/// `namespace-node()` returns empty in HTML context.
#[test]
fn namespace_node_test_returns_empty() {
    let text = r#"<html><body><div>a</div></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath = xpath::parse("//div/namespace-node()").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(items.len(), 0, "HTML has no namespace nodes: {items:?}");
}

/// `attribute(id)` matches only the id attribute.
#[test]
fn attribute_test_named_matches_specific() {
    let text = r#"<html><body><div id="foo" class="bar">a</div></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath = xpath::parse("//div/attribute(id)").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(items.len(), 1, "should match only id attribute: {items:?}");
}

/// `attribute(*)` matches all attributes.
#[test]
fn attribute_test_wildcard_matches_all() {
    let text = r#"<html><body><div id="foo" class="bar">a</div></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath = xpath::parse("//div/attribute(*)").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(items.len(), 2, "should match both attributes: {items:?}");
}

/// `processing-instruction()` display formatting.
#[test]
fn pi_test_display() {
    let xpath = xpath::parse("//processing-instruction()").unwrap();
    assert_eq!(xpath.to_string(), "//processing-instruction()");
}

/// `processing-instruction(name)` display formatting.
#[test]
fn pi_test_named_display() {
    let xpath = xpath::parse("//processing-instruction(xml)").unwrap();
    assert_eq!(xpath.to_string(), "//processing-instruction(xml)");
}

/// `schema-attribute(name)` display formatting.
#[test]
fn schema_attribute_test_display() {
    let xpath = xpath::parse("//schema-attribute(price)").unwrap();
    assert_eq!(xpath.to_string(), "//schema-attribute(price)");
}

/// `document-node()` matches the root document node via `instance of`.
#[test]
fn document_node_test_matches_root() {
    let text = r#"<html><body></body></html>"#;

    let document = html::parse(text).unwrap();
    // fn:root() returns the document node; check it's a document-node()
    let xpath = xpath::parse("fn:root(.) instance of document-node()").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(
        items[0],
        XpathItem::AnyAtomicType(AnyAtomicType::Boolean(true)),
        "root should be a document-node()"
    );
}

/// `document-node(element(html))` matches a document whose single element child is `html`.
#[test]
fn document_node_element_test_matches_html() {
    let text = r#"<html><body></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath =
        xpath::parse("fn:root(.) instance of document-node(element(html))").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(
        items[0],
        XpathItem::AnyAtomicType(AnyAtomicType::Boolean(true)),
        "document should match document-node(element(html))"
    );
}

/// `document-node(element(wrong))` does NOT match when element name doesn't match.
#[test]
fn document_node_element_test_wrong_name() {
    let text = r#"<html><body></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath =
        xpath::parse("fn:root(.) instance of document-node(element(wrong))").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(
        items[0],
        XpathItem::AnyAtomicType(AnyAtomicType::Boolean(false)),
        "document should NOT match document-node(element(wrong))"
    );
}

/// `document-node(element(*))` matches any document with a single element child.
#[test]
fn document_node_element_wildcard_matches() {
    let text = r#"<html><body></body></html>"#;

    let document = html::parse(text).unwrap();
    let xpath =
        xpath::parse("fn:root(.) instance of document-node(element(*))").unwrap();

    let items = xpath.apply(&document).unwrap();
    assert_eq!(
        items[0],
        XpathItem::AnyAtomicType(AnyAtomicType::Boolean(true)),
        "document should match document-node(element(*))"
    );
}

/// `processing-instruction()` matches PI nodes.
#[test]
fn pi_test_matches_pi_nodes() {
    let document = DocumentBuilder::new()
        .add_element("root", |e| {
            e.add_processing_instruction("xml-stylesheet", "type=\"text/xsl\"")
                .add_text("hello")
        })
        .build()
        .unwrap();

    let xpath = xpath::parse("//processing-instruction()").unwrap();
    let items = xpath.apply(&document).unwrap();
    assert_eq!(items.len(), 1, "should match 1 PI node: {items:?}");
}

/// `processing-instruction(name)` matches PIs with the specified target.
#[test]
fn pi_test_named_matches_target() {
    let document = DocumentBuilder::new()
        .add_element("root", |e| {
            e.add_processing_instruction("xml-stylesheet", "type=\"text/xsl\"")
                .add_processing_instruction("php", "echo 'hello';")
        })
        .build()
        .unwrap();

    let xpath = xpath::parse("//processing-instruction(php)").unwrap();
    let items = xpath.apply(&document).unwrap();
    assert_eq!(items.len(), 1, "should match only the php PI: {items:?}");
}

/// `processing-instruction(name)` doesn't match PIs with different targets.
#[test]
fn pi_test_named_no_match() {
    let document = DocumentBuilder::new()
        .add_element("root", |e| {
            e.add_processing_instruction("xml-stylesheet", "type=\"text/xsl\"")
        })
        .build()
        .unwrap();

    let xpath = xpath::parse("//processing-instruction(php)").unwrap();
    let items = xpath.apply(&document).unwrap();
    assert_eq!(items.len(), 0, "should match nothing: {items:?}");
}

/// PI node string value is its data content.
#[test]
fn pi_node_string_value() {
    let document = DocumentBuilder::new()
        .add_element("root", |e| {
            e.add_processing_instruction("xml-stylesheet", "type=\"text/xsl\"")
        })
        .build()
        .unwrap();

    let xpath = xpath::parse("string(//processing-instruction())").unwrap();
    let items = xpath.apply(&document).unwrap();
    assert_eq!(
        items[0],
        XpathItem::AnyAtomicType(AnyAtomicType::String(
            "type=\"text/xsl\"".to_string()
        ))
    );
}

/// PI node name returns its target.
#[test]
fn pi_node_name_value() {
    let document = DocumentBuilder::new()
        .add_element("root", |e| {
            e.add_processing_instruction("xml-stylesheet", "type=\"text/xsl\"")
        })
        .build()
        .unwrap();

    let xpath = xpath::parse("name(//processing-instruction())").unwrap();
    let items = xpath.apply(&document).unwrap();
    assert_eq!(
        items[0],
        XpathItem::AnyAtomicType(AnyAtomicType::String(
            "xml-stylesheet".to_string()
        ))
    );
}

/// PI node with empty data returns empty string.
#[test]
fn pi_node_empty_data_string_value() {
    let document = DocumentBuilder::new()
        .add_element("root", |e| {
            e.add_processing_instruction("target", "")
        })
        .build()
        .unwrap();

    let xpath = xpath::parse("string(//processing-instruction())").unwrap();
    let items = xpath.apply(&document).unwrap();
    assert_eq!(
        items[0],
        XpathItem::AnyAtomicType(AnyAtomicType::String(String::new()))
    );
}

/// `node()` should NOT match DoctypeNode (DOCTYPE is not a valid XPath 3.1 node type).
#[test]
fn node_test_excludes_doctype() {
    // Parse HTML with DOCTYPE — the direct path creates a DoctypeNode child of the document.
    let text = "<!DOCTYPE html><html><head></head><body></body></html>";
    let document = html::parse(text).unwrap();

    // /node() selects the document's children that are XPath node types.
    // DOCTYPE should be excluded; only the <html> element should match.
    let xpath = xpath::parse("/node()").unwrap();
    let items = xpath.apply(&document).unwrap();

    assert_eq!(items.len(), 1, "node() should return only the html element, not the doctype");
    let node = items[0].as_node().unwrap();
    let element = node.as_element_node().unwrap();
    assert_eq!(element.name, "html");
}

/// `node()` should not match DoctypeNode even in a DocumentBuilder tree.
#[test]
fn node_test_excludes_doctype_in_builder() {
    let document = DocumentBuilder::new()
        .add_doctype("html")
        .add_element("html", |e| {
            e.add_element("body", |e| e.add_text("hello"))
        })
        .build()
        .unwrap();

    let xpath = xpath::parse("/node()").unwrap();
    let items = xpath.apply(&document).unwrap();

    // Only the html element should match, not the doctype.
    assert_eq!(items.len(), 1, "should match only the html element: {items:?}");
    let node = items[0].as_node().unwrap();
    let element = node.as_element_node().unwrap();
    assert_eq!(element.name, "html");
}

/// DoctypeNode should have a working parent() — it should navigate to the document node.
#[test]
fn doctype_node_has_parent() {
    use skyscraper::xpath::grammar::XpathItemTreeNode;

    let text = "<!DOCTYPE html><html><head></head><body></body></html>";
    let document = html::parse(text).unwrap();

    // Find the DoctypeNode by iterating the tree.
    let doctype = document.iter().find(|node| {
        matches!(node, XpathItemTreeNode::DoctypeNode(_))
    });

    assert!(doctype.is_some(), "DoctypeNode should exist in the tree");
    let doctype = doctype.unwrap();
    let parent = doctype.parent(&document);
    assert!(parent.is_some(), "DoctypeNode should have a parent");
    assert!(
        matches!(parent.unwrap(), XpathItemTreeNode::DocumentNode(_)),
        "DoctypeNode parent should be the document node"
    );
}

/// count(/node()) should not count the DoctypeNode.
#[test]
fn count_node_excludes_doctype() {
    let text = "<!DOCTYPE html><html><head></head><body></body></html>";
    let document = html::parse(text).unwrap();

    let xpath = xpath::parse("count(/node())").unwrap();
    let items = xpath.apply(&document).unwrap();

    assert_eq!(
        items[0],
        XpathItem::AnyAtomicType(AnyAtomicType::Integer(1)),
        "count(/node()) should be 1 (only the html element, not the doctype)"
    );
}