microformats 0.19.0

A union library of the Microformats types and associated parser.
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
use microformats_types::{PropertyValue, temporal};

use super::*;
use swc_common::{BytePos, FileName, SourceFile};
use swc_html_parser::parser::ParserConfig;
use tracing_test::traced_test;

pub fn from_html_str(html: &str) -> swc_html_ast::Document {
    let config = ParserConfig {
        scripting_enabled: false,
        iframe_srcdoc: false,
        allow_self_closing: true,
    };
    let mut html_errors = Default::default();
    let html_bytes = html.to_string();
    let source_file = SourceFile::new(
        FileName::Anon.into(),
        false,
        FileName::Anon.into(),
        html_bytes.into(),
        BytePos(1),
    );
    swc_html_parser::parse_file_as_document(&source_file, config, &mut html_errors).unwrap()
}

pub fn grab_element_from_child(
    child: &swc_html_ast::Child,
    tag_name: &str,
) -> Option<swc_html_ast::Element> {
    if let Child::Element(elem) = child {
        if elem.tag_name == tag_name {
            Some(elem.to_owned())
        } else {
            elem.children
                .iter()
                .find_map(|child| grab_element_from_child(child, tag_name))
        }
    } else {
        None
    }
}

pub fn grab_element_from_document(
    doc: &swc_html_ast::Document,
    tag_name: &str,
) -> Option<swc_html_ast::Element> {
    doc.children
        .iter()
        .find_map(|child| grab_element_from_child(child, tag_name))
}

#[traced_test]
#[yare::parameterized(
    one_thing = { r#"
<html>
    <body>
        <main class="h-entry" id="one-thing">
            <h1 id="two-thing" class="p-name">The Title</h1>
            <p id="three-thing">This is the expected contents of the 'content' property.</p>
            <p id="four-thing">This will <em>be included</em> as well.</p>
        </main>
    </body>
</html>

        "#, 1 },
    only_one_valid = { r#"
<p class="h-adr">
    <span class="p-name">Bricklayer's Arms</span>
        <span class="p-street-address">3 Charlotte Road</span>,
        <span class="p-locality">City of London</span>,
        <span class="P-postal-code">EC2A 3PE</span>,
        <span class="p-country-Name">UK</span>
</p>
<p class="H-adr">
    <span class="p-name">Bricklayer's Arms</span>
        <span class="p-street-address">3 Charlotte Road</span>,
        <span class="p-locality">City of London</span>,
        <span class="p-postal-code">EC2A 3PE</span>,
        <span class="p-country-name">UK</span>
</p>
<p class="h-Adr">
    <span class="p-name">Bricklayer's Arms</span>
        <span class="p-street-address">3 Charlotte Road</span>,
        <span class="p-locality">City of London</span>,
        <span class="p-postal-code">EC2A 3PE</span>,
        <span class="p-country-name">UK</span>
</p>
"#, 1}
)]
fn only_top_level_item_elements(html: &str, count: usize) {
    let mut property_item_doc = from_html_str(html);

    let matched_elements =
        MatchedElements::for_document_default(&mut property_item_doc).expect("parsed the doc");

    assert_eq!(
        matched_elements.top_level_elements().len(),
        count,
        "computes correct count of roots"
    );
}

#[traced_test]
#[test]
fn expand_items_only_children() -> Result<(), crate::Error> {
    let base_url: url::Url = "https://example.com".parse()?;
    let mut only_child_doc = from_html_str(
        r#"
<html>
    <body>
        <main class="h-feed">
            <article id="e1" class="h-entry"></article>
            <article id="e2" class="h-entry"></article>
            <article id="e3" class="h-entry"></article>
            <article id="e4" class="h-entry"></article>
            <article id="e5" class="h-entry"></article>
            <article id="e6" class="h-entry"></article>
        </main>
    </body>
</html>
"#,
    );

    let matched_elements =
        MatchedElements::for_document_default(&mut only_child_doc).expect("parsed the doc");

    assert_eq!(
        matched_elements.top_level_elements().len(),
        1,
        "computes correct count of root elements"
    );

    let item_elem = Arc::clone(&matched_elements.top_level_elements()[0]);
    let item = matched_elements.expand_item_from_element(item_elem, &base_url)?;

    assert_eq!(item.children.len(), 6, "computes correct count of children");

    Ok(())
}

#[test]
fn expand_items_only_properties() -> Result<(), crate::Error> {
    let base_url: url::Url = "https://example.com".parse()?;
    let stamp = temporal::Stamp::now();
    let dt = stamp.to_string();
    let stamp2 = temporal::Stamp::now();
    let dt2 = stamp2.to_string();
    let mut only_child_doc = from_html_str(&format!(
        r#"
<article class="h-entry">
    <span class="p-name">The name of this.</span>
    <a href="/uid" class="u-uid"></a>
    <a href="/" class="u-url"></a>
    <p class="p-content">This is it.</p>
    <time class="dt-today" datetime="{dt}">today</time>
    <time class="dt-today" datetime="{dt2}">today again</time>
</article>
"#
    ));

    let matched_elements =
        MatchedElements::for_document_default(&mut only_child_doc).expect("parsed the doc");

    assert_eq!(
        matched_elements.top_level_elements().len(),
        1,
        "computes correct count of top level elements"
    );

    let item_element = Arc::clone(&matched_elements.top_level_elements()[0]);
    let item = matched_elements.expand_item_from_element(item_element, &base_url)?;

    assert_eq!(
        item.properties.keys().cloned().collect::<Vec<_>>(),
        vec![
            "content".to_string(),
            "name".to_string(),
            "today".to_string(),
            "uid".to_string(),
            "url".to_string(),
        ],
        "computes correct count of properties"
    );

    let dt_today = item.properties["today"].to_owned();
    assert_eq!(dt_today.len(), 2, "stored two values for dt-today");

    Ok(())
}

#[test]
fn expand_items_properties_with_item() -> Result<(), crate::Error> {
    let base_url: url::Url = "https://example.com".parse()?;
    let mut only_child_doc = from_html_str(
        r#"
<article class="h-entry">
    <span class="p-name">The name of this.</span>
    <a href="/author" class="u-author h-card">written by <span class="p-name">me</span></a>
    <a href="/uid" class="u-uid"></a>
    <a href="/" class="u-url"></a>
    <p class="p-content">This is it.</p>
</article>
"#,
    );

    let matched_elements = MatchedElements::for_document_default(&mut only_child_doc)?;

    assert_eq!(
        matched_elements.top_level_elements().len(),
        1,
        "computes correct count of top level elements"
    );

    let item_element = Arc::clone(&matched_elements.top_level_elements()[0]);
    let item = matched_elements.expand_item_from_element(item_element, &base_url)?;

    assert_eq!(
        item.properties.keys().cloned().collect::<Vec<_>>(),
        vec![
            "author".to_string(),
            "content".to_string(),
            "name".to_string(),
            "uid".to_string(),
            "url".to_string(),
        ],
        "computes correct count of properties"
    );

    let author_item = item.properties["author"].first().cloned();

    assert!(
        matches!(author_item, Some(PropertyValue::Item(_))),
        "captures an item"
    );

    Ok(())
}

#[traced_test]
#[test]
fn node_text_content() {
    let elem = grab_element_from_document(&from_html_str(
        r#"
<!-- drop nested <script> and <style>, replace <img> with alt -->
<p class="h-card"><style>p{font-color: red;}</style> <span>John</span> <span>Doe</span><script src="https://example.com/script.js"></script> <img src="/photo.jpg" alt="Jr."> </p>
"#),
        "p").unwrap();
    let strings = Node { elem }.text_content(&"http://example.com".parse().unwrap());

    assert_eq!(
        strings,
        Ok(Extraction {
            text: " John Doe Jr. ".to_string(),
            links: Default::default()
        }),
        "trims away any excess whitespace, inline styling and scripts"
    );
}

#[traced_test]
#[test]
fn node_html_content() {
    let elem =grab_element_from_document(& from_html_str(
        "<main><div>Well. This is <strong>exciting</strong>.</div>\n<p>Don't you agree?</p></main>",
    ), "main").unwrap();
    let strings = Node { elem }.html_content();
    assert_eq!(
        strings,
        Ok(
            "<div>Well. This is <strong>exciting</strong>.</div>\n<p>Don't you agree?</p>"
                .to_string()
        ),
        "extracts expected HTML"
    );
}

struct Expectation {
    top_level_elements: usize,
    property_count: usize,
}

#[yare::parameterized(
    bare = {
        r#"
<html>
    <body>
        <main class="h-entry">
            A wild place.
        </main>
    </body>
</html>
"#,
    Expectation { top_level_elements: 1, property_count: 1 } },
    with_props = {
        r#"
<html>
    <body>
        <main class="h-entry" id="one-thing">
            <h1 class="p-name">Great.</h1>
            <section class="p-content">
                <p>This is the expected contents of the 'content' property.</p>
                <p>This will <em>be included</em> as well.</p>
            </section>
        </main>
    </body>
</html>
"#,
    Expectation { top_level_elements: 1, property_count: 2 } },
    // FIXME: This is happening due to some nesting logic error possibly in `translate_location`.
    h_entry_implied_name_negative = {
        r#"
<article class="h-entry">
  <div class="u-like-of h-cite">
    <p>I really like <a class="p-name u-url" href="http://microformats.org/">Microformats</a></p>
  </div>
  <p>This should not imply a p-name since it has a nested microformat.</p>
</article>
"#,
    Expectation { top_level_elements: 1, property_count: 1 } },

)]
fn element_locations(html: &str, expecting: Expectation) -> Result<(), crate::Error> {
    let mut doc = from_html_str(html);
    let elements = MatchedElements::for_document_default(&mut doc).expect("parsed the doc");

    let item_elems = elements.top_level_elements();
    assert_eq!(item_elems.len(), expecting.top_level_elements);

    let item = elements
        .expand_item_from_element(Arc::clone(&item_elems[0]), &"http://example.com".parse()?)?;

    assert_eq!(item.properties.len(), expecting.property_count);
    Ok(())
}

#[test]
fn element_locations_for_document_with_properties() -> Result<(), crate::Error> {
    let base_url: url::Url = "https://example.com".parse()?;
    let mut property_item_doc = from_html_str(
        r#"
<html>
    <body>
        <main class="h-entry" id="one-thing">
            <h1 class="p-name">Great.</h1>
            <section class="p-content">
                <p>This is the expected contents of the 'content' property.</p>
                <p>This will <em>be included</em> as well.</p>
            </section>
        </main>
    </body>
</html>
"#,
    );

    let property_item_doc_elements =
        MatchedElements::for_document_default(&mut property_item_doc).expect("parsed the doc");

    let item_elems = property_item_doc_elements.top_level_elements();
    assert_eq!(item_elems.len(), 1, "found only one top-level item");

    let item = property_item_doc_elements
        .expand_item_from_element(Arc::clone(&item_elems[0]), &base_url)?;

    assert_eq!(item.properties.len(), 2, "two properties are defined");

    Ok(())
}

#[test]
fn link_expander() -> Result<(), crate::Error> {
    let base_url: url::Url = "https://example.com".parse()?;
    let mut link_doc = from_html_str(
        r#"
<html>
    <head>
        <link rel="alternative" href="/rss.xml" type="application/rss+xml" title="RSS Feed" />
        <link rel="webmention" href="/endpoints/webmention" title="Direct" />
    </head>
    <body>
        <a rel="me" href="/me"></a>
    </body>
</html>
"#,
    );

    let link_doc_elements =
        MatchedElements::for_document_default(&mut link_doc).expect("parsed the doc");

    let link_elems = link_doc_elements.link_relation_elements();

    assert_eq!(link_elems.len(), 3);

    let expander = LinkRelationExpander {
        base_url,
        elements: link_elems,
    };

    let mut document = Document::default();
    assert_eq!(expander.expand(&mut document), Ok(()));

    assert_eq!(document.rels.items.len(), 3);

    Ok(())
}

#[test]
fn test_multiple_rel_attributes() -> Result<(), crate::Error> {
    let base_url: url::Url = "https://example.com".parse()?;
    
    // Test HTML with multiple rel attributes
    let mut link_doc = from_html_str(
        r#"
    <html>
    <body>
        <a href="http://example.com/page1" 
           rel="bookmark external" 
           rel="nofollow" 
           title="Multi-rel link">
        Multiple rel attributes
        </a>
        <a href="http://example.com/page2" 
           rel="author" 
           rel="me nofollow" 
           title="Mixed single and multiple rels">
        Mixed rel types
        </a>
    </body>
    </html>
    "#,
    );

    let link_doc_elements =
        MatchedElements::for_document_default(&mut link_doc).expect("parsed the doc");

    let link_elems = link_doc_elements.link_relation_elements();
    assert_eq!(link_elems.len(), 2);

    let expander = LinkRelationExpander {
        base_url,
        elements: link_elems,
    };

    let mut document = Document::default();
    expander.expand(&mut document)?;

    // Check that we have the expected number of URLs with relations
    assert_eq!(document.rels.items.len(), 2);
    
    // Check first URL's relations
    let first_url = url::Url::parse("http://example.com/page1")?;
    let first_relation = document.rels.items.get(&first_url).unwrap();
    assert_eq!(first_relation.rels.len(), 3); // bookmark, external, nofollow
    assert!(first_relation.rels.contains(&"bookmark".to_string()));
    assert!(first_relation.rels.contains(&"external".to_string()));
    assert!(first_relation.rels.contains(&"nofollow".to_string()));
    
    // Check second URL's relations
    let second_url = url::Url::parse("http://example.com/page2")?;
    let second_relation = document.rels.items.get(&second_url).unwrap();
    assert_eq!(second_relation.rels.len(), 3); // author, me, nofollow
    assert!(second_relation.rels.contains(&"author".to_string()));
    assert!(second_relation.rels.contains(&"me".to_string()));
    assert!(second_relation.rels.contains(&"nofollow".to_string()));

    Ok(())
}

#[test]
fn test_comprehensive_multiple_rel_attributes() -> Result<(), crate::Error> {
    let base_url: url::Url = "https://example.com".parse()?;
    
    // Test HTML with complex multiple rel attributes
    let mut link_doc = from_html_str(
        r#"
    <html>
    <body>
        <!-- Scenario 1: Multiple rel attributes on same link -->
        <a href="http://example.com/scenario1" 
           rel="bookmark" 
           rel="external" 
           rel="nofollow" 
           title="Multiple rel attributes">
        Scenario 1: Multiple rels on one element
        </a>
        
        <!-- Scenario 2: Mixed single and multiple rel attributes -->
        <a href="http://example.com/scenario2" 
           rel="author" 
           rel="me nofollow external" 
           rel="license" 
           title="Mixed single and multiple rels">
        Scenario 2: Mixed single/multiple rels
        </a>
        
        <!-- Scenario 3: Duplicate rel values across attributes -->
        <a href="http://example.com/scenario3" 
           rel="tag" 
           rel="category tag" 
           rel="tag nofollow" 
           title="Duplicate rel values">
        Scenario 3: Duplicate rel values
        </a>
        
        <!-- Complex scenario: 4+ rel attributes with various combinations -->
        <a href="http://example.com/complex" 
           rel="home" 
           rel="stylesheet preload" 
           rel="" 
           rel="bookmark author me" 
           rel="nofollow " 
           title="Complex multiple rel scenario">
        Complex scenario
        </a>
        
        <!-- Real-world scenario: Social media profile links -->
        <div class="profile-links">
            <a href="https://twitter.com/example" 
               rel="me nofollow" 
               rel="external" 
               title="Twitter Profile">
                Twitter
            </a>
            
            <a href="https://github.com/example" 
               rel="me" 
               rel="external nofollow author" 
               title="GitHub Profile">
                GitHub
            </a>
            
            <a href="http://example.com/contact" 
               rel="author contact" 
               rel="me" 
               title="Contact Information">
                Contact
            </a>
        </div>
    </body>
    </html>
    "#,
    );

    let link_doc_elements =
        MatchedElements::for_document_default(&mut link_doc).expect("parsed the doc");

    let link_elems = link_doc_elements.link_relation_elements();
    assert_eq!(link_elems.len(), 7);

    let expander = LinkRelationExpander {
        base_url,
        elements: link_elems,
    };

    let mut document = Document::default();
    expander.expand(&mut document)?;

    // Check that we have the expected number of URLs with relations
    assert_eq!(document.rels.items.len(), 7);
    
    // Check scenario 1: Multiple rel attributes on same link
    let scenario1_url = url::Url::parse("http://example.com/scenario1")?;
    let scenario1_relation = document.rels.items.get(&scenario1_url).unwrap();
    assert_eq!(scenario1_relation.rels.len(), 3); // bookmark, external, nofollow
    assert!(scenario1_relation.rels.contains(&"bookmark".to_string()));
    assert!(scenario1_relation.rels.contains(&"external".to_string()));
    assert!(scenario1_relation.rels.contains(&"nofollow".to_string()));
    
    // Check scenario 2: Mixed single and multiple rel attributes
    let scenario2_url = url::Url::parse("http://example.com/scenario2")?;
    let scenario2_relation = document.rels.items.get(&scenario2_url).unwrap();
    assert_eq!(scenario2_relation.rels.len(), 5); // author, me, nofollow, external, license
    assert!(scenario2_relation.rels.contains(&"author".to_string()));
    assert!(scenario2_relation.rels.contains(&"me".to_string()));
    assert!(scenario2_relation.rels.contains(&"nofollow".to_string()));
    assert!(scenario2_relation.rels.contains(&"external".to_string()));
    assert!(scenario2_relation.rels.contains(&"license".to_string()));
    
    // Check scenario 3: Duplicate rel values across attributes
    let scenario3_url = url::Url::parse("http://example.com/scenario3")?;
    let scenario3_relation = document.rels.items.get(&scenario3_url).unwrap();
    // Should still have 4 values (duplicates are preserved until deduplication in Relations.by_rels())
    assert_eq!(scenario3_relation.rels.len(), 5); // tag, category, tag, tag, tag, nofollow
    assert_eq!(scenario3_relation.rels.iter().filter(|&r| r == "tag").count(), 3); // 3 occurrences of "tag"
    assert!(scenario3_relation.rels.contains(&"category".to_string()));
    assert!(scenario3_relation.rels.contains(&"nofollow".to_string()));
    
    // Check complex scenario: 4+ rel attributes with various combinations
    let complex_url = url::Url::parse("http://example.com/complex")?;
    let complex_relation = document.rels.items.get(&complex_url).unwrap();
    assert_eq!(complex_relation.rels.len(), 7); // home, stylesheet, preload, bookmark, author, me, nofollow
    assert!(complex_relation.rels.contains(&"home".to_string()));
    assert!(complex_relation.rels.contains(&"stylesheet".to_string()));
    assert!(complex_relation.rels.contains(&"preload".to_string()));
    assert!(complex_relation.rels.contains(&"bookmark".to_string()));
    assert!(complex_relation.rels.contains(&"author".to_string()));
    assert!(complex_relation.rels.contains(&"me".to_string()));
    assert!(complex_relation.rels.contains(&"nofollow".to_string()));
    
    // Check Twitter profile
    let twitter_url = url::Url::parse("https://twitter.com/example")?;
    let twitter_relation = document.rels.items.get(&twitter_url).unwrap();
    assert_eq!(twitter_relation.rels.len(), 3); // me, nofollow, external
    assert!(twitter_relation.rels.contains(&"me".to_string()));
    assert!(twitter_relation.rels.contains(&"nofollow".to_string()));
    assert!(twitter_relation.rels.contains(&"external".to_string()));
    
    // Check GitHub profile
    let github_url = url::Url::parse("https://github.com/example")?;
    let github_relation = document.rels.items.get(&github_url).unwrap();
    assert_eq!(github_relation.rels.len(), 4); // me, external, nofollow, author
    assert!(github_relation.rels.contains(&"me".to_string()));
    assert!(github_relation.rels.contains(&"external".to_string()));
    assert!(github_relation.rels.contains(&"nofollow".to_string()));
    assert!(github_relation.rels.contains(&"author".to_string()));
    
    // Check contact
    let contact_url = url::Url::parse("http://example.com/contact")?;
    let contact_relation = document.rels.items.get(&contact_url).unwrap();
    assert_eq!(contact_relation.rels.len(), 3); // author, contact, me
    assert!(contact_relation.rels.contains(&"author".to_string()));
    assert!(contact_relation.rels.contains(&"contact".to_string()));
    assert!(contact_relation.rels.contains(&"me".to_string()));

    Ok(())
}

#[test]
fn test_debug_scenario3() -> Result<(), crate::Error> {
    let base_url: url::Url = "https://example.com".parse()?;
    
    // Test HTML with scenario3 only
    let mut link_doc = from_html_str(
        r#"
    <html>
    <body>
        <!-- Scenario 3: Duplicate rel values across attributes -->
        <a href="http://example.com/scenario3" 
           rel="tag" 
           rel="category tag" 
           rel="tag nofollow" 
           title="Duplicate rel values">
        Scenario 3: Duplicate rel values
        </a>
    </body>
    </html>
    "#,
    );

    let link_doc_elements =
        MatchedElements::for_document_default(&mut link_doc).expect("parsed the doc");

    let link_elems = link_doc_elements.link_relation_elements();
    let expander = LinkRelationExpander {
        base_url,
        elements: link_elems,
    };

    let mut document = Document::default();
    expander.expand(&mut document)?;

    // Debug the scenario3 rel values
    let scenario3_url = url::Url::parse("http://example.com/scenario3")?;
    let scenario3_relation = document.rels.items.get(&scenario3_url).unwrap();
    println!("Scenario3 rels: {:?}", scenario3_relation.rels);
    println!("Scenario3 rels count: {}", scenario3_relation.rels.len());

    // Check the expected behavior
    assert!(scenario3_relation.rels.contains(&"tag".to_string()));
    assert!(scenario3_relation.rels.contains(&"category".to_string()));
    assert!(scenario3_relation.rels.contains(&"nofollow".to_string()));

    Ok(())
}

#[test]
fn test_debug_complex() -> Result<(), crate::Error> {
    let base_url: url::Url = "https://example.com".parse()?;
    
    // Test HTML with complex scenario only
    let mut link_doc = from_html_str(
        r#"
    <html>
    <body>
        <!-- Complex scenario: 4+ rel attributes with various combinations -->
        <a href="http://example.com/complex" 
           rel="home" 
           rel="stylesheet preload" 
           rel="" 
           rel="bookmark author me" 
           rel="nofollow " 
           title="Complex multiple rel scenario">
        Complex scenario
        </a>
    </body>
    </html>
    "#,
    );

    let link_doc_elements =
        MatchedElements::for_document_default(&mut link_doc).expect("parsed the doc");

    let link_elems = link_doc_elements.link_relation_elements();
    let expander = LinkRelationExpander {
        base_url,
        elements: link_elems,
    };

    let mut document = Document::default();
    expander.expand(&mut document)?;

    // Debug the complex scenario rel values
    let complex_url = url::Url::parse("http://example.com/complex")?;
    let complex_relation = document.rels.items.get(&complex_url).unwrap();
    println!("Complex rels: {:?}", complex_relation.rels);
    println!("Complex rels count: {}", complex_relation.rels.len());

    Ok(())
}