feedparser-rs 0.6.0

High-performance RSS/Atom/JSON Feed 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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
//! RSS 1.0 (RDF) parser implementation
//!
//! RSS 1.0 differs significantly from RSS 2.0:
//! - Uses RDF (Resource Description Framework) as the container
//! - Root element is `<rdf:RDF>` instead of `<rss>`
//! - Items are siblings of channel, not children
//! - Items have `rdf:about` attributes for identification
//! - Supports Dublin Core and other RDF vocabularies

use std::collections::HashMap;

use crate::{
    ParserLimits,
    error::{FeedError, Result},
    namespace::{content, dublin_core, georss, syndication, threading},
    types::{Entry, FeedVersion, Image, ParsedFeed, TextConstruct, TextType},
    util::base_url::BaseUrlContext,
};
use quick_xml::{
    Reader,
    events::{BytesStart, Event},
};

use super::common::{
    EVENT_BUFFER_CAPACITY, LimitedCollectionExt, check_depth, extract_namespaces, extract_xml_base,
    extract_xml_lang, init_feed, is_content_tag, is_dc_tag, is_geo_tag, is_georss_tag, is_syn_tag,
    is_thr_tag, parse_georss_where, read_text, read_text_str, skip_element,
};
use super::context::{EntryCtx, XmlCtx};

/// Per-RDF-root parse context: XML plumbing plus the mutable xml:base/xml:lang
/// state that `handle_rdf_root` populates from the `<rdf:RDF>` root element.
struct RdfCtx<'r, 'd> {
    /// XML event-loop plumbing (reader, buffer, limits).
    xml: XmlCtx<'r, 'd>,
    /// xml:base resolution context, updated from the RDF root element.
    base: BaseUrlContext,
    /// xml:lang inherited by items that don't declare their own.
    lang: Option<String>,
}

/// Parse RSS 1.0 (RDF) feed from raw bytes
///
/// Parses an RSS 1.0 feed in tolerant mode, setting the bozo flag
/// on errors but continuing to extract as much data as possible.
///
/// # Arguments
///
/// * `data` - Raw RSS 1.0 XML data
///
/// # Returns
///
/// * `Ok(ParsedFeed)` - Successfully parsed feed (may have bozo flag set)
/// * `Err(FeedError)` - Fatal error that prevented any parsing
///
/// # Examples
///
/// ```ignore
/// let xml = br#"
///     <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
///              xmlns="http://purl.org/rss/1.0/">
///         <channel rdf:about="http://example.com/">
///             <title>Example</title>
///             <link>http://example.com/</link>
///             <description>Example RSS 1.0 feed</description>
///         </channel>
///         <item rdf:about="http://example.com/item1">
///             <title>First Item</title>
///             <link>http://example.com/item1</link>
///         </item>
///     </rdf:RDF>
/// "#;
///
/// let feed = parse_rss10(xml).unwrap();
/// assert_eq!(feed.feed.title.as_deref(), Some("Example"));
/// ```
#[allow(dead_code)]
pub fn parse_rss10(data: &[u8]) -> Result<ParsedFeed> {
    parse_rss10_with_limits(data, ParserLimits::default())
}

/// Parse RSS 1.0 with custom parser limits
///
/// Relative URI resolution is always enabled; use [`parse_rss10_with_options`] to
/// control it.
pub fn parse_rss10_with_limits(data: &[u8], limits: ParserLimits) -> Result<ParsedFeed> {
    parse_rss10_with_options(data, limits, true)
}

/// Parse RSS 1.0 with custom parser limits and relative URI resolution control
pub fn parse_rss10_with_options(
    data: &[u8],
    limits: ParserLimits,
    resolve_relative_uris: bool,
) -> Result<ParsedFeed> {
    limits
        .check_feed_size(data.len())
        .map_err(|e| FeedError::InvalidFormat(e.to_string()))?;

    let mut reader = Reader::from_reader(data);

    let mut feed = init_feed(FeedVersion::Rss10, limits.max_entries);
    let mut buf = Vec::with_capacity(EVENT_BUFFER_CAPACITY);
    let mut depth: usize = 1;
    let mut ctx = RdfCtx {
        xml: XmlCtx {
            reader: &mut reader,
            buf: &mut buf,
            limits: &limits,
        },
        base: BaseUrlContext::new().with_resolve(resolve_relative_uris),
        lang: None,
    };

    loop {
        match ctx.xml.reader.read_event_into(ctx.xml.buf) {
            Ok(event @ (Event::Start(_) | Event::Empty(_))) => {
                let is_empty = matches!(event, Event::Empty(_));
                let (Event::Start(e) | Event::Empty(e)) = &event else {
                    unreachable!()
                };
                // NOTE: Allocation here is necessary due to borrow checker constraints.
                // We need an owned element to pass &mut buf to helper functions simultaneously.
                let element = e.to_owned();

                depth += 1;

                dispatch_rdf_element(&mut ctx, &element, is_empty, &mut feed, &mut depth)?;
            }
            Ok(Event::End(_)) => {
                depth = depth.saturating_sub(1);
            }
            Ok(Event::Eof) => {
                if depth > 1 {
                    feed.bozo = true;
                    feed.bozo_exception =
                        Some("Feed is truncated or has unclosed XML elements".to_string());
                }
                break;
            }
            Err(e) => {
                feed.bozo = true;
                feed.bozo_exception = Some(format!("XML parsing error: {e}"));
                break;
            }
            _ => {}
        }
        ctx.xml.buf.clear();
    }

    Ok(feed)
}

/// Dispatch a single RDF root child element (`channel`, `item`, `image`, `textinput`,
/// or the RDF root itself) to its handler.
///
/// Depth accounting: `depth` is already incremented by the caller for this event.
/// `rdf:RDF` nets `+1` with no matching `-1` here (deferred to its `Event::End`);
/// `item` decrements internally in [`parse_rdf_item`] on every exit path; every
/// other branch decrements exactly once here.
fn dispatch_rdf_element(
    ctx: &mut RdfCtx,
    element: &BytesStart<'_>,
    is_empty: bool,
    feed: &mut ParsedFeed,
    depth: &mut usize,
) -> Result<()> {
    let name = element.local_name();
    let full_name = element.name();

    if name.as_ref() == b"RDF" || full_name.as_ref() == b"rdf:RDF" {
        handle_rdf_root(ctx, element, feed);
    } else if name.as_ref() == b"channel" {
        handle_rdf_channel(ctx, element, feed, depth);
        *depth = depth.saturating_sub(1);
    } else if name.as_ref() == b"item" {
        parse_rdf_item(ctx, element, is_empty, feed, depth)?;
    } else if name.as_ref() == b"image" {
        if !is_empty
            && let Ok(image) = parse_image(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits, depth)
        {
            feed.feed.image = Some(image);
        }
        *depth = depth.saturating_sub(1);
    } else if name.as_ref() == b"textinput" || name.as_ref() == b"textInput" {
        // Skip textinput element (rarely used); self-closing refs need no skip
        if !is_empty {
            skip_element(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits, *depth)?;
        }
        *depth = depth.saturating_sub(1);
    } else {
        // Skip unknown elements at RDF level; self-closing tags need no skip
        if !is_empty {
            skip_element(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits, *depth)?;
        }
        *depth = depth.saturating_sub(1);
    }
    Ok(())
}

/// Handle the `<rdf:RDF>` root element: namespaces, xml:lang, xml:base.
///
/// Does not decrement `depth` — the root element's matching decrement happens
/// later at its `Event::End`.
fn handle_rdf_root(ctx: &mut RdfCtx, element: &BytesStart<'_>, feed: &mut ParsedFeed) {
    extract_namespaces(element, feed, ctx.xml.limits);
    // Extract xml:lang and xml:base from <rdf:RDF> root
    ctx.lang =
        extract_xml_lang(element, ctx.xml.limits.max_attribute_length).filter(|s| !s.is_empty());
    if let Some(xml_base) = extract_xml_base(element, ctx.xml.limits.max_attribute_length) {
        ctx.base.update_base(&xml_base);
    }
}

/// Handle the `<channel>` element: extract `rdf:about` as feed ID, then parse.
///
/// Converts a `parse_channel` error into a feed-level bozo internally — this is
/// the only RDF-root branch that does so; all others propagate via `?`.
fn handle_rdf_channel(
    ctx: &mut RdfCtx,
    element: &BytesStart<'_>,
    feed: &mut ParsedFeed,
    depth: &mut usize,
) {
    for attr in element.attributes().flatten() {
        if (attr.key.as_ref() == b"rdf:about" || attr.key.local_name().as_ref() == b"about")
            && let Ok(value) = attr.normalized_value(quick_xml::XmlVersion::Implicit1_0)
        {
            feed.feed.id = Some(value.as_ref().into());
        }
    }
    if let Err(e) = parse_channel(ctx.xml.reader, feed, ctx.xml.limits, depth) {
        feed.bozo = true;
        feed.bozo_exception = Some(e.to_string());
    }
}

/// Parse a top-level `<item>` element (RSS 1.0 items are siblings of `<channel>`).
///
/// Decrements `depth` exactly once on every exit path (`is_empty`, depth-exceeded,
/// entry-limit, normal) — this is the only branch of [`dispatch_rdf_element`] that
/// owns its own decrement, since the caller passes `depth` through unchanged.
fn parse_rdf_item(
    ctx: &mut RdfCtx,
    element: &BytesStart<'_>,
    is_empty: bool,
    feed: &mut ParsedFeed,
    depth: &mut usize,
) -> Result<()> {
    if is_empty {
        *depth = depth.saturating_sub(1);
        return Ok(());
    }

    if *depth > ctx.xml.limits.max_nesting_depth {
        feed.bozo = true;
        feed.bozo_exception = Some(format!(
            "XML nesting depth {} exceeds maximum {}",
            depth, ctx.xml.limits.max_nesting_depth
        ));
        skip_element(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits, *depth)?;
        *depth = depth.saturating_sub(1);
        return Ok(());
    }

    // Extract rdf:about as item ID first (before releasing borrow on buf)
    let item_id = element.attributes().flatten().find_map(|attr| {
        if attr.key.as_ref() == b"rdf:about" || attr.key.local_name().as_ref() == b"about" {
            attr.normalized_value(quick_xml::XmlVersion::Implicit1_0)
                .ok()
                .map(|v| v.to_string())
        } else {
            None
        }
    });

    // Extract item-level xml:lang (falls back to rdf_lang) and xml:base
    let item_lang_owned =
        extract_xml_lang(element, ctx.xml.limits.max_attribute_length).filter(|s| !s.is_empty());
    let effective_item_lang = item_lang_owned.as_deref().or(ctx.lang.as_deref());
    let item_base_owned = extract_xml_base(element, ctx.xml.limits.max_attribute_length);
    let item_base_ctx = item_base_owned
        .as_deref()
        .map_or_else(|| ctx.base.child(), |b| ctx.base.child_with_base(b));

    // Check entry limit (inline to avoid borrow issues)
    if feed.entries.is_at_limit(ctx.xml.limits.max_entries) {
        feed.bozo = true;
        feed.bozo_exception = Some(format!(
            "Entry limit exceeded: {}",
            ctx.xml.limits.max_entries
        ));
        skip_element(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits, *depth)?;
        *depth = depth.saturating_sub(1);
        return Ok(());
    }

    match parse_item(
        &mut ctx.xml,
        depth,
        item_id,
        effective_item_lang,
        &item_base_ctx,
        &feed.namespaces,
    ) {
        Ok((entry, item_bozo, bozo_reason)) => {
            if item_bozo && !feed.bozo {
                feed.bozo = true;
                feed.bozo_exception = Some(
                    bozo_reason
                        .unwrap_or("Unresolvable entity in entry field")
                        .to_string(),
                );
            }
            feed.entries.push(entry);
        }
        Err(err) => {
            feed.bozo = true;
            feed.bozo_exception = Some(err.to_string());
        }
    }
    *depth = depth.saturating_sub(1);
    Ok(())
}

/// Parse <channel> element (feed metadata)
fn parse_channel(
    reader: &mut Reader<&[u8]>,
    feed: &mut ParsedFeed,
    limits: &ParserLimits,
    depth: &mut usize,
) -> Result<()> {
    let mut buf = Vec::with_capacity(EVENT_BUFFER_CAPACITY);

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(event @ (Event::Start(_) | Event::Empty(_))) => {
                let is_empty = matches!(event, Event::Empty(_));
                let (Event::Start(e) | Event::Empty(e)) = &event else {
                    unreachable!()
                };

                *depth += 1;
                check_depth(*depth, limits.max_nesting_depth)?;

                let name = e.local_name();
                let full_name = e.name();

                match name.as_ref() {
                    b"title" if !is_empty => {
                        let text = read_text_str(reader, &mut buf, limits)?;
                        feed.feed.title = Some(text.clone());
                        // RSS 1.0 has no per-element type attribute; treated as
                        // potentially unsafe HTML by default (fail-closed, #438).
                        feed.feed.title_detail = Some(TextConstruct {
                            value: text,
                            content_type: TextType::Html,
                            language: None,
                            base: None,
                        });
                    }
                    b"link" if !is_empty => {
                        let link_text = read_text_str(reader, &mut buf, limits)?;
                        feed.feed
                            .set_alternate_link(link_text, limits.max_links_per_feed);
                    }
                    b"description" if !is_empty => {
                        let text = read_text_str(reader, &mut buf, limits)?;
                        feed.feed.subtitle = Some(text.clone());
                        feed.feed.subtitle_detail = Some(TextConstruct {
                            value: text,
                            content_type: TextType::Html,
                            language: None,
                            base: None,
                        });
                    }
                    b"items" => {
                        // RSS 1.0 has an <items> element containing rdf:Seq with rdf:li references
                        // We skip this as items are parsed at the RDF root level
                        if !is_empty {
                            skip_element(reader, &mut buf, limits, *depth)?;
                        }
                    }
                    b"image" | b"textinput" | b"textInput" => {
                        // Self-closing refs (e.g. <image rdf:resource="..."/>) need no skip
                        if !is_empty {
                            skip_element(reader, &mut buf, limits, *depth)?;
                        }
                    }
                    _ => {
                        if !is_empty {
                            // NOTE: Allocation here is necessary due to borrow checker
                            // constraints — full_name borrows from buf, which the
                            // namespace dispatcher also needs mutably.
                            let full_name_owned = full_name.as_ref().to_vec();
                            parse_rss10_channel_namespace(
                                &mut XmlCtx {
                                    reader: &mut *reader,
                                    buf: &mut buf,
                                    limits,
                                },
                                &full_name_owned,
                                feed,
                                *depth,
                            )?;
                        }
                        // Self-closing elements: no text content to read
                    }
                }
                *depth = depth.saturating_sub(1);
            }
            Ok(Event::End(e)) if e.local_name().as_ref() == b"channel" => {
                break;
            }
            Ok(Event::Eof) => break,
            Err(e) => return Err(e.into()),
            _ => {}
        }
        buf.clear();
    }

    Ok(())
}

/// Parse Dublin Core, Syndication, `GeoRSS`, and W3C Geo namespace tags at RSS 1.0
/// channel level. Falls back to `skip_element` for unrecognized tags.
///
/// Not part of the item-tier `EntryCtx` batch — called from [`parse_channel`],
/// which owns a separate local `buf` and has no base/lang.
fn parse_rss10_channel_namespace(
    ctx: &mut XmlCtx,
    full_name: &[u8],
    feed: &mut ParsedFeed,
    depth: usize,
) -> Result<()> {
    if let Some(dc_element) = is_dc_tag(full_name, &feed.namespaces) {
        let dc_elem = dc_element.to_string();
        let text = read_text_str(ctx.reader, ctx.buf, ctx.limits)?;
        dublin_core::handle_feed_element(&dc_elem, &text, &mut feed.feed);
    } else if let Some(syn_element) = is_syn_tag(full_name) {
        let syn_elem = syn_element.to_string();
        let text = read_text_str(ctx.reader, ctx.buf, ctx.limits)?;
        syndication::handle_feed_element(&syn_elem, &text, &mut feed.feed);
    } else if let Some(georss_element) = is_georss_tag(full_name) {
        if georss_element == "where" {
            let (loc, had_bozo, bozo_reason) =
                parse_georss_where(ctx.reader, ctx.buf, ctx.limits, depth)?;
            if had_bozo && !feed.bozo {
                feed.bozo = true;
                feed.bozo_exception = Some(
                    bozo_reason
                        .unwrap_or("Unresolvable entity in feed field")
                        .to_string(),
                );
            }
            if let Some(loc) = loc {
                georss::merge_geometry(&mut feed.feed.r#where, loc);
            }
        } else {
            let georss_elem = georss_element.to_string();
            let text = read_text_str(ctx.reader, ctx.buf, ctx.limits)?;
            georss::handle_feed_element(georss_elem.as_bytes(), &text, &mut feed.feed, ctx.limits);
        }
    } else if let Some(geo_element) = is_geo_tag(full_name) {
        let geo_elem = geo_element.to_string();
        let text = read_text_str(ctx.reader, ctx.buf, ctx.limits)?;
        georss::handle_feed_geo_element(geo_elem.as_bytes(), &text, &mut feed.feed);
    } else {
        skip_element(ctx.reader, ctx.buf, ctx.limits, depth)?;
    }
    Ok(())
}

/// Parse <item> element (entry)
///
/// Returns `(entry, bozo, bozo_reason)`, where `bozo` reflects any
/// unresolved entity reference or GML coordinate/dims mismatch encountered
/// while reading this item's text fields, and `bozo_reason` is a specific
/// description when available.
fn parse_item(
    xml: &mut XmlCtx,
    depth: &mut usize,
    item_id: Option<String>,
    lang: Option<&str>,
    base_ctx: &BaseUrlContext,
    namespaces: &HashMap<String, String>,
) -> Result<(Entry, bool, Option<&'static str>)> {
    let mut entry = Entry::with_capacity();
    entry.id = item_id.map(std::convert::Into::into);

    let mut ctx = EntryCtx {
        xml: xml.reborrow(),
        base: base_ctx,
        lang,
        namespaces,
        bozo: false,
        bozo_reason: None,
    };

    loop {
        match ctx.xml.reader.read_event_into(ctx.xml.buf) {
            Ok(event @ (Event::Start(_) | Event::Empty(_))) => {
                let is_empty = matches!(event, Event::Empty(_));
                let (Event::Start(e) | Event::Empty(e)) = &event else {
                    unreachable!()
                };
                // NOTE: Allocation here is necessary due to borrow checker constraints.
                // We need an owned element to pass &mut buf to helper functions simultaneously.
                let element = e.to_owned();

                *depth += 1;
                check_depth(*depth, ctx.xml.limits.max_nesting_depth)?;

                let name = element.local_name();
                let full_name = element.name();

                // NOTE: the tag list below must stay in sync with the inner `match` in
                // parse_rss10_item_standard — a tag routed there that the handler
                // doesn't also match falls through its silent `_ => {}` and consumes
                // no events, desyncing the event stream.
                match name.as_ref() {
                    b"title" | b"link" | b"description" if !is_empty => {
                        parse_rss10_item_standard(&mut ctx, name.as_ref(), &mut entry)?;
                    }
                    _ => {
                        parse_rss10_item_namespace(
                            &mut ctx,
                            &element,
                            full_name.as_ref(),
                            &mut entry,
                            *depth,
                            is_empty,
                        )?;
                    }
                }
                *depth = depth.saturating_sub(1);
            }
            Ok(Event::End(e)) if e.local_name().as_ref() == b"item" => {
                break;
            }
            Ok(Event::Eof) => break,
            Err(e) => return Err(e.into()),
            _ => {}
        }
        ctx.xml.buf.clear();
    }

    // dc:creator takes precedence over <author>
    if let Some(dc) = &entry.dc_creator {
        entry.author = Some(dc.clone());
    }

    Ok((entry, ctx.bozo, ctx.bozo_reason))
}

/// Parse standard RSS 1.0 item elements: title, link, description.
fn parse_rss10_item_standard(ctx: &mut EntryCtx, name: &[u8], entry: &mut Entry) -> Result<()> {
    // keep tag list in sync with the dispatcher arm in parse_item
    match name {
        b"title" => {
            let (text, had_bozo) = read_text(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits)?;
            ctx.bozo |= had_bozo;
            entry.title = Some(text.clone());
            // See feed-level <title> above: no type attribute, so treated
            // as potentially unsafe HTML by default (fail-closed, #438).
            entry.title_detail = Some(TextConstruct {
                value: text,
                content_type: TextType::Html,
                language: ctx.lang.filter(|s| !s.is_empty()).map(Into::into),
                base: ctx.base.base().map(ToString::to_string),
            });
        }
        b"link" => {
            let link_text = read_text_str(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits)?;
            entry.set_alternate_link(link_text, ctx.xml.limits.max_links_per_entry);
        }
        b"description" => {
            let (desc, had_bozo) = read_text(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits)?;
            ctx.bozo |= had_bozo;
            entry.summary = Some(desc.clone());
            entry.summary_detail = Some(TextConstruct {
                value: desc,
                content_type: TextType::Html,
                language: ctx.lang.filter(|s| !s.is_empty()).map(Into::into),
                base: ctx.base.base().map(ToString::to_string),
            });
        }
        _ => {}
    }
    Ok(())
}

/// Parse namespaced RSS 1.0 item elements (Dublin Core, Content, `GeoRSS`, Geo,
/// Threading), plus the self-closing `thr:in-reply-to` special case.
fn parse_rss10_item_namespace(
    ctx: &mut EntryCtx,
    element: &BytesStart<'_>,
    full_name: &[u8],
    entry: &mut Entry,
    depth: usize,
    is_empty: bool,
) -> Result<()> {
    if is_empty
        && let Some(thr_element) = is_thr_tag(full_name)
        && thr_element == "in-reply-to"
    {
        // thr:in-reply-to may be self-closing and carry attributes
        if let Some(reply) = threading::parse_in_reply_to_from_attrs(
            element.attributes().flatten(),
            ctx.xml.limits.max_attribute_length,
        ) {
            entry
                .in_reply_to
                .try_push_limited(reply, ctx.xml.limits.max_links_per_entry);
        }
        return Ok(());
    } else if is_empty {
        // other self-closing elements: no content to process
        return Ok(());
    }

    if parse_rss10_item_ns_text(ctx, full_name, entry)? {
        return Ok(());
    }
    if parse_rss10_item_ns_geo_thr(ctx, element, full_name, entry, depth)? {
        return Ok(());
    }
    skip_element(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits, depth)
}

/// Parse Dublin Core and Content namespace tags at RSS 1.0 item level.
///
/// Returns `Ok(true)` if the tag was recognized and handled, `Ok(false)` if not recognized.
fn parse_rss10_item_ns_text(
    ctx: &mut EntryCtx,
    full_name: &[u8],
    entry: &mut Entry,
) -> Result<bool> {
    if let Some(dc_element) = is_dc_tag(full_name, ctx.namespaces) {
        let dc_elem = dc_element.to_string();
        let (text, had_bozo) = read_text(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits)?;
        ctx.bozo |= had_bozo;
        // dublin_core::handle_entry_element already handles dc:date -> published
        dublin_core::handle_entry_element(&dc_elem, &text, entry);
        Ok(true)
    } else if let Some(content_element) = is_content_tag(full_name) {
        let content_elem = content_element.to_string();
        let (text, had_bozo) = read_text(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits)?;
        ctx.bozo |= had_bozo;
        content::handle_entry_element(&content_elem, &text, entry, ctx.lang, ctx.base.base());
        Ok(true)
    } else {
        Ok(false)
    }
}

/// Parse `GeoRSS`, W3C Geo, and Threading namespace tags at RSS 1.0 item level.
///
/// Returns `Ok(true)` if the tag was recognized and handled, `Ok(false)` if not recognized.
fn parse_rss10_item_ns_geo_thr(
    ctx: &mut EntryCtx,
    element: &BytesStart<'_>,
    full_name: &[u8],
    entry: &mut Entry,
    depth: usize,
) -> Result<bool> {
    if let Some(georss_element) = is_georss_tag(full_name) {
        if georss_element == "where" {
            let (loc, had_bozo, bozo_reason) =
                parse_georss_where(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits, depth)?;
            ctx.bozo |= had_bozo;
            if let Some(reason) = bozo_reason {
                ctx.bozo_reason.get_or_insert(reason);
            }
            if let Some(loc) = loc {
                georss::merge_geometry(&mut entry.r#where, loc);
            }
        } else {
            let georss_elem = georss_element.to_string();
            let (text, had_bozo) = read_text(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits)?;
            ctx.bozo |= had_bozo;
            georss::handle_entry_element(georss_elem.as_bytes(), &text, entry, ctx.xml.limits);
        }
        Ok(true)
    } else if let Some(geo_element) = is_geo_tag(full_name) {
        let geo_elem = geo_element.to_string();
        let (text, had_bozo) = read_text(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits)?;
        ctx.bozo |= had_bozo;
        georss::handle_entry_geo_element(geo_elem.as_bytes(), &text, entry);
        Ok(true)
    } else if let Some(thr_element) = is_thr_tag(full_name) {
        // Atom Threading Extensions (RFC 4685)
        match thr_element {
            "in-reply-to" => {
                if let Some(reply) = threading::parse_in_reply_to_from_attrs(
                    element.attributes().flatten(),
                    ctx.xml.limits.max_attribute_length,
                ) {
                    // Shares max_links_per_entry limit; split if needed later
                    entry
                        .in_reply_to
                        .try_push_limited(reply, ctx.xml.limits.max_links_per_entry);
                }
                skip_element(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits, depth)?;
            }
            "total" => {
                let (text, had_bozo) = read_text(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits)?;
                ctx.bozo |= had_bozo;
                threading::handle_total(&text, entry);
            }
            _ => {
                skip_element(ctx.xml.reader, ctx.xml.buf, ctx.xml.limits, depth)?;
            }
        }
        Ok(true)
    } else {
        Ok(false)
    }
}

/// Parse <image> element
fn parse_image(
    reader: &mut Reader<&[u8]>,
    buf: &mut Vec<u8>,
    limits: &ParserLimits,
    depth: &mut usize,
) -> Result<Image> {
    let mut url = String::new();
    let mut title = None;
    let mut link = None;

    loop {
        match reader.read_event_into(buf) {
            Ok(event @ (Event::Start(_) | Event::Empty(_))) => {
                let is_empty = matches!(event, Event::Empty(_));
                let (Event::Start(e) | Event::Empty(e)) = &event else {
                    unreachable!()
                };

                *depth += 1;
                check_depth(*depth, limits.max_nesting_depth)?;

                if !is_empty {
                    match e.local_name().as_ref() {
                        b"url" => url = read_text_str(reader, buf, limits)?,
                        b"title" => title = Some(read_text_str(reader, buf, limits)?),
                        b"link" => link = Some(read_text_str(reader, buf, limits)?),
                        _ => skip_element(reader, buf, limits, *depth)?,
                    }
                }
                *depth = depth.saturating_sub(1);
            }
            Ok(Event::End(e)) if e.local_name().as_ref() == b"image" => break,
            Ok(Event::Eof) => break,
            Err(e) => return Err(e.into()),
            _ => {}
        }
        buf.clear();
    }

    if url.is_empty() {
        return Err(FeedError::InvalidFormat("Image missing url".to_string()));
    }

    Ok(Image {
        url: url.into(),
        title,
        link,
        width: None,
        height: None,
        description: None,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Datelike;

    #[test]
    fn test_parse_basic_rss10() {
        let xml = br#"<?xml version="1.0"?>
        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                 xmlns="http://purl.org/rss/1.0/">
            <channel rdf:about="http://example.com/">
                <title>Test Feed</title>
                <link>http://example.com</link>
                <description>Test description</description>
            </channel>
        </rdf:RDF>"#;

        let feed = parse_rss10(xml).unwrap();
        assert_eq!(feed.version, FeedVersion::Rss10);
        assert!(!feed.bozo);
        assert_eq!(feed.feed.title.as_deref(), Some("Test Feed"));
        assert_eq!(feed.feed.link.as_deref(), Some("http://example.com"));
        assert_eq!(feed.feed.subtitle.as_deref(), Some("Test description"));
        assert_eq!(feed.feed.id.as_deref(), Some("http://example.com/"));
    }

    #[test]
    fn test_parse_rss10_with_items() {
        let xml = br#"<?xml version="1.0"?>
        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                 xmlns="http://purl.org/rss/1.0/">
            <channel rdf:about="http://example.com/">
                <title>Test</title>
                <link>http://example.com</link>
                <description>Test</description>
                <items>
                    <rdf:Seq>
                        <rdf:li resource="http://example.com/1"/>
                        <rdf:li resource="http://example.com/2"/>
                    </rdf:Seq>
                </items>
            </channel>
            <item rdf:about="http://example.com/1">
                <title>Item 1</title>
                <link>http://example.com/1</link>
                <description>Description 1</description>
            </item>
            <item rdf:about="http://example.com/2">
                <title>Item 2</title>
                <link>http://example.com/2</link>
            </item>
        </rdf:RDF>"#;

        let feed = parse_rss10(xml).unwrap();
        assert_eq!(feed.entries.len(), 2);
        assert_eq!(feed.entries[0].title.as_deref(), Some("Item 1"));
        assert_eq!(feed.entries[0].id.as_deref(), Some("http://example.com/1"));
        assert_eq!(feed.entries[1].title.as_deref(), Some("Item 2"));
    }

    #[test]
    fn test_parse_rss10_with_dublin_core() {
        let xml = br#"<?xml version="1.0"?>
        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                 xmlns="http://purl.org/rss/1.0/"
                 xmlns:dc="http://purl.org/dc/elements/1.1/">
            <channel rdf:about="http://example.com/">
                <title>Test</title>
                <link>http://example.com</link>
                <description>Test</description>
                <dc:creator>John Doe</dc:creator>
                <dc:rights>Copyright 2024</dc:rights>
            </channel>
            <item rdf:about="http://example.com/1">
                <title>Item 1</title>
                <link>http://example.com/1</link>
                <dc:date>2024-12-15T10:00:00Z</dc:date>
                <dc:creator>Jane Smith</dc:creator>
            </item>
        </rdf:RDF>"#;

        let feed = parse_rss10(xml).unwrap();
        assert_eq!(feed.feed.dc_creator.as_deref(), Some("John Doe"));
        assert_eq!(feed.feed.dc_rights.as_deref(), Some("Copyright 2024"));

        assert_eq!(feed.entries.len(), 1);
        let entry = &feed.entries[0];
        assert!(entry.updated.is_some());
        assert!(entry.published.is_some());
        let dt = entry.updated.unwrap();
        assert_eq!(dt.year(), 2024);
        assert_eq!(dt.month(), 12);
        assert_eq!(dt.day(), 15);
    }

    #[test]
    fn test_parse_rss10_with_image() {
        let xml = br#"<?xml version="1.0"?>
        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                 xmlns="http://purl.org/rss/1.0/">
            <channel rdf:about="http://example.com/">
                <title>Test</title>
                <link>http://example.com</link>
                <description>Test</description>
            </channel>
            <image rdf:about="http://example.com/logo.png">
                <url>http://example.com/logo.png</url>
                <title>Logo</title>
                <link>http://example.com</link>
            </image>
        </rdf:RDF>"#;

        let feed = parse_rss10(xml).unwrap();
        assert!(feed.feed.image.is_some());
        let img = feed.feed.image.as_ref().unwrap();
        assert_eq!(img.url, "http://example.com/logo.png");
        assert_eq!(img.title.as_deref(), Some("Logo"));
    }

    #[test]
    fn test_parse_rss10_without_rdf_prefix() {
        // Some RSS 1.0 feeds don't use the rdf: prefix
        let xml = br#"<?xml version="1.0"?>
        <RDF xmlns="http://purl.org/rss/1.0/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
            <channel>
                <title>Test</title>
                <link>http://example.com</link>
                <description>Test</description>
            </channel>
        </RDF>"#;

        let feed = parse_rss10(xml).unwrap();
        assert_eq!(feed.version, FeedVersion::Rss10);
        assert_eq!(feed.feed.title.as_deref(), Some("Test"));
    }

    #[test]
    fn test_parse_rss10_entry_limit() {
        let xml = br#"<?xml version="1.0"?>
        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                 xmlns="http://purl.org/rss/1.0/">
            <channel rdf:about="http://example.com/">
                <title>Test</title>
                <link>http://example.com</link>
                <description>Test</description>
            </channel>
            <item rdf:about="http://example.com/1"><title>1</title><link>http://example.com/1</link></item>
            <item rdf:about="http://example.com/2"><title>2</title><link>http://example.com/2</link></item>
            <item rdf:about="http://example.com/3"><title>3</title><link>http://example.com/3</link></item>
            <item rdf:about="http://example.com/4"><title>4</title><link>http://example.com/4</link></item>
        </rdf:RDF>"#;

        let limits = ParserLimits {
            max_entries: 2,
            ..Default::default()
        };
        let feed = parse_rss10_with_limits(xml, limits).unwrap();
        assert_eq!(feed.entries.len(), 2);
        assert!(feed.bozo);
    }

    #[test]
    fn test_parse_rss10_malformed_continues() {
        let xml = br#"<?xml version="1.0"?>
        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                 xmlns="http://purl.org/rss/1.0/">
            <channel rdf:about="http://example.com/">
                <title>Test</title>
                <link>http://example.com</link>
                <description>Test</description>
            </channel>
            <item rdf:about="http://example.com/1">
                <title>Item 1</title>
                <!-- Missing close tag but continues -->
        </rdf:RDF>"#;

        let feed = parse_rss10(xml).unwrap();
        // Should still extract some data
        assert_eq!(feed.feed.title.as_deref(), Some("Test"));
    }

    #[test]
    fn test_is_dc_tag_valid() {
        let ns = HashMap::new();
        assert_eq!(is_dc_tag(b"dc:creator", &ns), Some("creator"));
        assert_eq!(is_dc_tag(b"dc:date", &ns), Some("date"));
        assert_eq!(is_dc_tag(b"dc:description", &ns), Some("description"));
        assert_eq!(is_dc_tag(b"dc:subject", &ns), Some("subject"));
        assert_eq!(is_dc_tag(b"dc:content-type", &ns), Some("content-type"));
    }

    #[test]
    fn test_is_dc_tag_rejects_malicious() {
        let ns = HashMap::new();
        // Path traversal attempts
        assert!(is_dc_tag(b"dc:../../etc/passwd", &ns).is_none());
        assert!(is_dc_tag(b"dc:../../../root", &ns).is_none());

        // Special characters
        assert!(is_dc_tag(b"dc:invalid<tag>", &ns).is_none());
        assert!(is_dc_tag(b"dc:tag&name", &ns).is_none());
        assert!(is_dc_tag(b"dc:tag;name", &ns).is_none());
        assert!(is_dc_tag(b"dc:tag/name", &ns).is_none());
        assert!(is_dc_tag(b"dc:tag\\name", &ns).is_none());

        // Empty tag name
        assert!(is_dc_tag(b"dc:", &ns).is_none());
    }

    #[test]
    fn test_is_dc_tag_non_dc() {
        let ns = HashMap::new();
        assert!(is_dc_tag(b"title", &ns).is_none());
        assert!(is_dc_tag(b"link", &ns).is_none());
        assert!(is_dc_tag(b"atom:title", &ns).is_none());
    }

    #[test]
    fn test_dc_date_maps_to_updated_and_published() {
        let xml = include_bytes!("../../../../tests/fixtures/rss10_dc_date.xml");
        let feed = parse_rss10(xml).unwrap();
        assert_eq!(feed.entries.len(), 1);
        let entry = &feed.entries[0];
        assert!(
            entry.updated.is_some(),
            "entry.updated should be set from dc:date"
        );
        assert!(
            entry.published.is_some(),
            "entry.published should be set from dc:date as fallback"
        );
        let dt = entry.updated.unwrap();
        assert_eq!(dt.year(), 2025);
        assert_eq!(dt.month(), 1);
        assert_eq!(dt.day(), 15);
        assert_eq!(entry.updated_str.as_deref(), Some("2025-01-15T10:00:00Z"));
    }

    #[test]
    fn test_parse_rss10_with_content_encoded() {
        let xml = br#"<?xml version="1.0"?>
        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                 xmlns="http://purl.org/rss/1.0/"
                 xmlns:content="http://purl.org/rss/1.0/modules/content/">
            <channel rdf:about="http://example.com/">
                <title>Test</title>
                <link>http://example.com</link>
                <description>Test</description>
            </channel>
            <item rdf:about="http://example.com/1">
                <title>Item 1</title>
                <link>http://example.com/1</link>
                <description>Brief summary</description>
                <content:encoded><![CDATA[<p>Full <strong>HTML</strong> content</p>]]></content:encoded>
            </item>
        </rdf:RDF>"#;

        let feed = parse_rss10(xml).unwrap();
        assert_eq!(feed.entries.len(), 1);

        let entry = &feed.entries[0];
        assert_eq!(entry.summary.as_deref(), Some("Brief summary"));

        // Verify content:encoded is parsed
        assert!(!entry.content.is_empty());
        assert_eq!(entry.content[0].content_type.as_deref(), Some("text/html"));
        assert!(entry.content[0].value.contains("Full"));
        assert!(entry.content[0].value.contains("HTML"));
    }

    #[test]
    fn test_parse_rss10_with_syndication() {
        let xml = br#"<?xml version="1.0"?>
        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                 xmlns="http://purl.org/rss/1.0/"
                 xmlns:syn="http://purl.org/rss/1.0/modules/syndication/">
            <channel rdf:about="http://example.com/">
                <title>Test</title>
                <link>http://example.com</link>
                <description>Test</description>
                <syn:updatePeriod>hourly</syn:updatePeriod>
                <syn:updateFrequency>2</syn:updateFrequency>
                <syn:updateBase>2024-01-01T00:00:00Z</syn:updateBase>
            </channel>
        </rdf:RDF>"#;

        let feed = parse_rss10(xml).unwrap();
        assert!(feed.feed.syndication.is_some());

        let syn = feed.feed.syndication.as_ref().unwrap();
        assert_eq!(
            syn.update_period,
            Some(crate::namespace::syndication::UpdatePeriod::Hourly)
        );
        assert_eq!(syn.update_frequency, Some("2".to_string()));
        assert_eq!(syn.update_base.as_deref(), Some("2024-01-01T00:00:00Z"));
    }

    #[test]
    fn test_parse_rss10_with_syndication_sy_prefix() {
        let xml = br#"<?xml version="1.0"?>
        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                 xmlns="http://purl.org/rss/1.0/"
                 xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
            <channel rdf:about="http://example.com/">
                <title>Test</title>
                <link>http://example.com</link>
                <description>Test</description>
                <sy:updatePeriod>daily</sy:updatePeriod>
                <sy:updateFrequency>1</sy:updateFrequency>
                <sy:updateBase>2024-06-01T00:00:00Z</sy:updateBase>
            </channel>
        </rdf:RDF>"#;

        let feed = parse_rss10(xml).unwrap();
        assert!(feed.feed.syndication.is_some());

        let syn = feed.feed.syndication.as_ref().unwrap();
        assert_eq!(
            syn.update_period,
            Some(crate::namespace::syndication::UpdatePeriod::Daily)
        );
        assert_eq!(syn.update_frequency, Some("1".to_string()));
        assert_eq!(syn.update_base.as_deref(), Some("2024-06-01T00:00:00Z"));
    }

    #[test]
    fn test_rss10_namespaces_on_rdf_root() {
        let xml = br#"<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns="http://purl.org/rss/1.0/"
         xmlns:dc="http://purl.org/dc/elements/1.1/"
         xmlns:syn="http://purl.org/rss/1.0/modules/syndication/">
<channel rdf:about="http://example.com/">
<title>T</title><link>http://example.com/</link><description>D</description>
</channel>
</rdf:RDF>"#;
        let feed = parse_rss10(xml).unwrap();
        assert!(!feed.bozo);
        assert_eq!(
            feed.namespaces.get("rdf").map(String::as_str),
            Some("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
        );
        assert_eq!(
            feed.namespaces.get("").map(String::as_str),
            Some("http://purl.org/rss/1.0/")
        );
        assert_eq!(
            feed.namespaces.get("dc").map(String::as_str),
            Some("http://purl.org/dc/elements/1.1/")
        );
        assert_eq!(
            feed.namespaces.get("syn").map(String::as_str),
            Some("http://purl.org/rss/1.0/modules/syndication/")
        );
    }

    #[test]
    fn test_rss10_no_namespaces() {
        let xml = br#"<?xml version="1.0"?>
<RDF><channel><title>T</title><link>http://x.com</link><description>D</description></channel></RDF>"#;
        let feed = parse_rss10(xml).unwrap();
        assert!(feed.namespaces.is_empty());
    }

    #[test]
    fn test_rss10_xml_lang_from_rdf_root_propagates_to_items() {
        let xml = br#"<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns="http://purl.org/rss/1.0/"
         xml:lang="de">
  <channel rdf:about="http://example.com/">
    <title>Test Feed</title>
    <link>http://example.com/</link>
    <description>Beschreibung</description>
  </channel>
  <item rdf:about="http://example.com/1">
    <title>Artikel</title>
    <link>http://example.com/1</link>
    <description>Inhalt</description>
  </item>
</rdf:RDF>"#;
        let feed = parse_rss10(xml).unwrap();
        assert!(!feed.bozo);
        let entry = &feed.entries[0];
        assert_eq!(
            entry.title_detail.as_ref().unwrap().language.as_deref(),
            Some("de")
        );
        assert_eq!(
            entry.summary_detail.as_ref().unwrap().language.as_deref(),
            Some("de")
        );
    }

    #[test]
    fn test_rss10_item_xml_lang_overrides_rdf_lang() {
        let xml = br#"<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns="http://purl.org/rss/1.0/"
         xml:lang="de">
  <channel rdf:about="http://example.com/"><title>T</title><link>http://example.com/</link><description>D</description></channel>
  <item rdf:about="http://example.com/1" xml:lang="fr">
    <title>Titre</title>
    <link>http://example.com/1</link>
    <description>Contenu</description>
  </item>
</rdf:RDF>"#;
        let feed = parse_rss10(xml).unwrap();
        assert!(!feed.bozo);
        let entry = &feed.entries[0];
        assert_eq!(
            entry.title_detail.as_ref().unwrap().language.as_deref(),
            Some("fr")
        );
    }

    #[test]
    fn test_rss10_no_xml_lang_yields_none() {
        let xml = br#"<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns="http://purl.org/rss/1.0/">
  <channel rdf:about="http://example.com/"><title>T</title><link>http://example.com/</link><description>D</description></channel>
  <item rdf:about="http://example.com/1">
    <title>Title</title>
    <link>http://example.com/1</link>
    <description>Summary</description>
  </item>
</rdf:RDF>"#;
        let feed = parse_rss10(xml).unwrap();
        assert!(!feed.bozo);
        let entry = &feed.entries[0];
        assert!(entry.title_detail.as_ref().unwrap().language.is_none());
        assert!(entry.summary_detail.as_ref().unwrap().language.is_none());
    }
}