kawat-xpath 0.1.2

XPath evaluation on HTML documents for kawat
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
use kawat_xpath::{CompiledXpaths, XpathEngine};

/// Test BODY_XPATH expressions against real-world HTML patterns
#[test]
fn test_body_xpath_article_tag() {
    let html = r#"
        <html>
            <body>
                <article>
                    <h1>Article Title</h1>
                    <p>Article content here</p>
                </article>
            </body>
        </html>
    "#;

    // Expression 2: generic <article>
    let result = XpathEngine::eval_text(html, CompiledXpaths::BODY[1]);
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
}

#[test]
fn test_body_xpath_post_class() {
    let html = r#"
        <html>
            <body>
                <div class="post">
                    <h1>Post Title</h1>
                    <div class="post-content">
                        <p>Post content here</p>
                    </div>
                </div>
            </body>
        </html>
    "#;

    // Expression 1: post class patterns
    let result = XpathEngine::eval_text(html, CompiledXpaths::BODY[0]);
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
}

#[test]
fn test_body_xpath_content_main() {
    let html = r#"
        <html>
            <body>
                <div id="content-main">
                    <h1>Main Content</h1>
                    <p>This is the main content area</p>
                </div>
            </body>
        </html>
    "#;

    // Expression 4: content-main / content-body
    let result = XpathEngine::eval_text(html, CompiledXpaths::BODY[3]);
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
}

#[test]
fn test_body_xpath_main_element() {
    let html = r#"
        <html>
            <body>
                <main>
                    <h1>Main Content</h1>
                    <p>Content in main element</p>
                </main>
            </body>
        </html>
    "#;

    // Expression 5: main element fallback
    let result = XpathEngine::eval_text(html, CompiledXpaths::BODY[4]);
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
}

#[test]
fn test_body_xpath_first_match_wins() {
    let html = r#"
        <html>
            <body>
                <article>
                    <h1>First Match</h1>
                    <p>This should be matched first</p>
                </article>
                <div class="post">
                    <h1>Second Match</h1>
                    <p>This should not be matched</p>
                </div>
            </body>
        </html>
    "#;

    // Test that first matching expression wins
    let mut found = false;
    for xpath in CompiledXpaths::BODY {
        let result = XpathEngine::eval_text(html, xpath);
        if let Ok(matches) = result {
            if !matches.is_empty() {
                found = true;
                break;
            }
        }
    }
    assert!(found);
}

/// Test OVERALL_DISCARD_XPATH expressions
#[test]
fn test_overall_discard_xpath_footer() {
    let html = r#"
        <html>
            <body>
                <div id="footer">
                    <p>Footer content</p>
                </div>
            </body>
        </html>
    "#;

    let result = XpathEngine::eval_text(html, CompiledXpaths::OVERALL_DISCARD[0]);
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
}

#[test]
fn test_overall_discard_xpath_sidebar() {
    let html = r#"
        <html>
            <body>
                <div class="sidebar">
                    <h3>Related Posts</h3>
                    <ul>
                        <li><a href="/post1">Post 1</a></li>
                    </ul>
                </div>
            </body>
        </html>
    "#;

    let result = XpathEngine::eval_text(html, CompiledXpaths::OVERALL_DISCARD[0]);
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
}

#[test]
fn test_overall_discard_xpath_navigation() {
    let html = r#"
        <html>
            <body>
                <div class="navbar">
                    <ul>
                        <li><a href="/">Home</a></li>
                        <li><a href="/about">About</a></li>
                    </ul>
                </div>
            </body>
        </html>
    "#;

    let result = XpathEngine::eval_text(html, CompiledXpaths::OVERALL_DISCARD[0]);
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
}

#[test]
fn test_overall_discard_xpath_comments() {
    let html = r#"
        <html>
            <body>
                <div class="comments-title">
                    <h3>Comments</h3>
                </div>
                <div class="comment">
                    <p>User comment</p>
                </div>
            </body>
        </html>
    "#;

    let result = XpathEngine::eval_text(html, CompiledXpaths::OVERALL_DISCARD[1]);
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
}

#[test]
fn test_overall_discard_xpath_social_sharing() {
    let html = r#"
        <html>
            <body>
                <div class="share-buttons">
                    <button class="share-facebook">Share</button>
                    <button class="share-twitter">Tweet</button>
                </div>
            </body>
        </html>
    "#;

    let result = XpathEngine::eval_text(html, CompiledXpaths::OVERALL_DISCARD[0]);
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
}

/// Test XPath engine robustness
#[test]
fn test_xpath_empty_html() {
    let html = "<html><body></body></html>";
    let result = XpathEngine::eval_text(html, CompiledXpaths::BODY[0]);
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(matches.is_empty());
}

#[test]
fn test_xpath_malformed_html() {
    let html = "<html><body><div class='post'><p>Unclosed paragraph";
    let result = XpathEngine::eval_text(html, CompiledXpaths::BODY[0]);
    // Should handle malformed HTML gracefully
    assert!(result.is_ok() || result.is_err());
}

/// Test CSS selector fallback functionality
#[test]
fn test_css_fallback_simple_article() {
    let html = r#"
        <html>
            <body>
                <article>
                    <h1>Test Article</h1>
                    <p>Content here</p>
                </article>
            </body>
        </html>
    "#;

    // This should work with CSS fallback for simple expressions
    let result = XpathEngine::eval_text(html, "(.//article)[1]");
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
}

#[test]
fn test_css_fallback_main_element() {
    let html = r#"
        <html>
            <body>
                <main>
                    <h1>Main Content</h1>
                    <p>Content in main</p>
                </main>
            </body>
        </html>
    "#;

    let result = XpathEngine::eval_text(html, CompiledXpaths::BODY[4]);
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
}

#[test]
fn test_css_fallback_post_class() {
    let html = r#"
        <html>
            <body>
                <div class="post">
                    <h1>Post Title</h1>
                    <p>Post content</p>
                </div>
            </body>
        </html>
    "#;

    let result = XpathEngine::eval_text(html, ".//*[@class=\"post\"]");
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
}

#[test]
fn test_custom_filters_translate() {
    let html = r#"
        <html>
            <body>
                <div class="articlebody">
                    <h1>Article Title</h1>
                    <p>Article content</p>
                </div>
                <div class="othercontent">
                    <p>Other content</p>
                </div>
            </body>
        </html>
    "#;

    // Test translate() expression fallback
    let xpath = r#".//*[self::div][contains(translate(@class, "B", "b"), "articlebody")]"#;
    let result = XpathEngine::eval_text(html, xpath);
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
    // Should only match the articlebody div
}

#[test]
fn test_fallback_robustness() {
    let malformed_html =
        "<html><body><div class='post'><p>Unclosed paragraph<div class='sidebar'>Sidebar</div>";

    // Should handle malformed HTML without crashing
    let result = XpathEngine::eval_text(malformed_html, "(.//article)[1]");
    // Either succeeds (with sxd_html) or falls back gracefully
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_fallback_performance() {
    let html = r#"
        <html>
            <body>
                <article>
                    <h1>Performance Test</h1>
                    <p>Testing fallback performance</p>
                </article>
            </body>
        </html>
    "#;

    // Test that fallback doesn't significantly impact performance
    let start = std::time::Instant::now();
    for _ in 0..100 {
        let _ = XpathEngine::eval_text(html, "(.//article)[1]");
    }
    let duration = start.elapsed();

    // Should complete 100 evaluations in reasonable time (< 1 second)
    assert!(duration.as_secs() < 1);
}

#[test]
fn test_xpath_has_match() {
    let html = r#"
        <html>
            <body>
                <article>
                    <p>Content</p>
                </article>
            </body>
        </html>
    "#;

    assert!(XpathEngine::has_match(html, CompiledXpaths::BODY[1]));
    assert!(!XpathEngine::has_match(html, ".//nonexistent"));
}

/// Test complex nested structures
#[test]
fn test_body_xpath_nested_article() {
    let html = r#"
        <html>
            <body>
                <div class="wrapper">
                    <article>
                        <div class="article-header">
                            <h1>Title</h1>
                        </div>
                        <div class="article-body">
                            <p>Paragraph 1</p>
                            <p>Paragraph 2</p>
                        </div>
                    </article>
                </div>
            </body>
        </html>
    "#;

    let result = XpathEngine::eval_text(html, CompiledXpaths::BODY[1]);
    assert!(result.is_ok());
    let matches = result.unwrap();
    assert!(!matches.is_empty());
}

#[test]
fn test_overall_discard_multiple_matches() {
    let html = r#"
        <html>
            <body>
                <nav class="navbar">Navigation</nav>
                <div id="footer">Footer</div>
                <div class="sidebar">Sidebar</div>
                <article>Main content</article>
            </body>
        </html>
    "#;

    // Should match multiple elements to discard
    let result = XpathEngine::eval_text(html, CompiledXpaths::OVERALL_DISCARD[0]);
    assert!(result.is_ok());
    let matches = result.unwrap();
    // May have multiple matches or combined text
    assert!(!matches.is_empty());
}