anydoc 0.1.5

Convert documents (doc, docx, odt, rtf, epub, pdf, presentations, spreadsheets, csv) to GitHub-Flavored Markdown
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
//! Namespace-aware DOM built on quick-xml.
//!
//! Parses from bytes so BOMs and encoding declarations are honored. Element
//! and attribute names carry their resolved namespace URI (interned). Depth
//! and node caps are enforced during parsing; recoverable malformations
//! (unclosed or mismatched tags) are repaired deliberately and logged, never
//! silently.

use crate::error::ConvertError;
use crate::package::limits;
use quick_xml::NsReader;
use quick_xml::events::{BytesStart, Event};
use quick_xml::name::{QName, ResolveResult};
use std::collections::HashMap;
use std::rc::Rc;

/// Well-known namespace URIs.
pub mod ns {
    pub const W: &str = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
    pub const R: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
    pub const A: &str = "http://schemas.openxmlformats.org/drawingml/2006/main";
    pub const PIC: &str = "http://schemas.openxmlformats.org/drawingml/2006/picture";
    pub const WP: &str = "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing";
    pub const MC: &str = "http://schemas.openxmlformats.org/markup-compatibility/2006";
    pub const CHART: &str = "http://schemas.openxmlformats.org/drawingml/2006/chart";
    pub const DGM: &str = "http://schemas.openxmlformats.org/drawingml/2006/diagram";
    pub const P: &str = "http://schemas.openxmlformats.org/presentationml/2006/main";
    pub const PKG_RELS: &str = "http://schemas.openxmlformats.org/package/2006/relationships";

    pub const OFFICE: &str = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
    pub const TEXT: &str = "urn:oasis:names:tc:opendocument:xmlns:text:1.0";
    pub const TABLE: &str = "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
    pub const DRAW: &str = "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0";
    pub const STYLE: &str = "urn:oasis:names:tc:opendocument:xmlns:style:1.0";
    pub const FO: &str = "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0";
    pub const PRESENTATION: &str = "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0";
    pub const MANIFEST: &str = "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0";
    pub const XLINK: &str = "http://www.w3.org/1999/xlink";
    pub const XML: &str = "http://www.w3.org/XML/1998/namespace";
    pub const SVG_COMPAT: &str = "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0";

    pub const VML: &str = "urn:schemas-microsoft-com:vml";
    pub const O_VML: &str = "urn:schemas-microsoft-com:office:office";
    pub const WPS: &str = "http://schemas.microsoft.com/office/word/2010/wordprocessingShape";
    pub const WPG: &str = "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup";
}

#[derive(Debug)]
pub enum Node {
    Elem(Element),
    Text(String),
}

#[derive(Debug)]
pub struct Element {
    /// Resolved namespace URI (interned); `None` for unqualified names.
    pub ns: Option<Rc<str>>,
    pub local: String,
    pub attrs: Vec<Attr>,
    pub children: Vec<Node>,
}

#[derive(Debug)]
pub struct Attr {
    pub ns: Option<Rc<str>>,
    pub local: String,
    pub value: String,
}

impl Element {
    pub fn is(&self, ns: &str, local: &str) -> bool {
        self.local == local && self.ns.as_deref() == Some(ns)
    }

    /// Same-vocabulary attribute lookup: the qualified attribute wins, and
    /// an explicitly unqualified one with the same local name is accepted
    /// as a deliberate leniency - schemas often leave their own attributes
    /// unqualified (DrawingML `gridSpan`) and producers vary. Lookups in a
    /// *different* vocabulary than the element's (`r:id`, `xml:id`) must use
    /// [`Element::attr_qualified`], where that fallback would misattribute.
    pub fn attr(&self, ns: &str, local: &str) -> Option<&str> {
        self.attr_qualified(ns, local).or_else(|| self.attr_unqualified(local))
    }

    /// Strictly namespace-qualified attribute lookup, no fallback.
    pub fn attr_qualified(&self, ns: &str, local: &str) -> Option<&str> {
        self.attrs
            .iter()
            .find(|a| a.local == local && matches!(&a.ns, Some(n) if **n == *ns))
            .map(|a| a.value.as_str())
    }

    /// Explicitly unqualified attribute lookup (no namespace), no fallback.
    pub fn attr_unqualified(&self, local: &str) -> Option<&str> {
        self.attrs.iter().find(|a| a.local == local && a.ns.is_none()).map(|a| a.value.as_str())
    }

    /// Attribute by local name regardless of namespace. For elements whose
    /// vocabulary is unambiguous in context (relationships, container files).
    pub fn attr_any(&self, local: &str) -> Option<&str> {
        self.attrs.iter().find(|a| a.local == local).map(|a| a.value.as_str())
    }

    pub fn child_elems(&self) -> impl Iterator<Item = &Element> {
        self.children.iter().filter_map(|n| match n {
            Node::Elem(e) => Some(e),
            Node::Text(_) => None,
        })
    }

    pub fn find(&self, ns: &str, local: &str) -> Option<&Element> {
        self.child_elems().find(|e| e.is(ns, local))
    }

    pub fn find_all<'a>(
        &'a self,
        ns: &'a str,
        local: &'a str,
    ) -> impl Iterator<Item = &'a Element> {
        self.child_elems().filter(move |e| e.is(ns, local))
    }

    /// All descendant nodes (elements and text), depth-first in document
    /// order, iteratively (deep DOMs must not overflow the call stack).
    pub fn descendant_nodes(&self) -> DescendantNodes<'_> {
        let mut stack: Vec<&Node> = self.children.iter().collect();
        stack.reverse();
        DescendantNodes { stack }
    }

    /// All descendant elements, depth-first in document order, lazily.
    pub fn descendant_elems(&self) -> impl Iterator<Item = &Element> {
        self.descendant_nodes().filter_map(|node| match node {
            Node::Elem(e) => Some(e),
            Node::Text(_) => None,
        })
    }

    /// First descendant with the given name, depth-first.
    pub fn first_descendant(&self, ns: &str, local: &str) -> Option<&Element> {
        self.descendant_elems().find(|e| e.is(ns, local))
    }

    /// All descendants with the given name, in document order, lazily.
    pub fn descendants<'a>(
        &'a self,
        ns: &'a str,
        local: &'a str,
    ) -> impl Iterator<Item = &'a Element> {
        self.descendant_elems().filter(move |e| e.is(ns, local))
    }

    /// All descendants matching a local name regardless of namespace, in
    /// document order. For vocabularies where the namespace varies across
    /// producers (EPUB container/OPF metadata).
    pub fn descendants_any<'a>(&'a self, local: &'a str) -> impl Iterator<Item = &'a Element> {
        self.descendant_elems().filter(move |e| e.local == local)
    }

    /// Concatenated text of all descendant text nodes.
    pub fn text(&self) -> String {
        let mut out = String::new();
        for node in self.descendant_nodes() {
            if let Node::Text(t) = node {
                out.push_str(t);
            }
        }
        out
    }
}

/// Iterative depth-first traversal over a subtree's nodes; the shared core
/// of every descendant lookup.
pub struct DescendantNodes<'a> {
    stack: Vec<&'a Node>,
}

impl<'a> Iterator for DescendantNodes<'a> {
    type Item = &'a Node;

    fn next(&mut self) -> Option<&'a Node> {
        let node = self.stack.pop()?;
        if let Node::Elem(e) = node {
            let start = self.stack.len();
            self.stack.extend(e.children.iter());
            self.stack[start..].reverse();
        }
        Some(node)
    }
}

/// Parse an XML part into a synthetic root element containing the top-level
/// nodes. Encoding comes from the BOM or XML declaration; the part is
/// transcoded to UTF-8 before parsing so namespace resolution sees one
/// consistent encoding.
pub fn parse_xml(bytes: &[u8]) -> Result<Element, ConvertError> {
    let utf8 = to_utf8(bytes);
    let mut reader = NsReader::from_reader(utf8.as_ref());
    reader.config_mut().check_end_names = false;

    let mut interner: HashMap<Vec<u8>, Rc<str>> = HashMap::new();
    let mut root =
        Element { ns: None, local: String::new(), attrs: Vec::new(), children: Vec::new() };
    let mut stack: Vec<Element> = Vec::new();
    let mut nodes: usize = 0;
    let mut recovered = false;

    loop {
        let event = reader
            .read_event()
            .map_err(|e| ConvertError::malformed(format!("unparseable xml: {e}")))?;
        match event {
            Event::Start(e) => {
                if stack.len() >= limits::MAX_XML_DEPTH {
                    return Err(ConvertError::ResourceLimit {
                        limit: "max_xml_depth",
                        detail: format!("element nesting exceeds {}", limits::MAX_XML_DEPTH),
                    });
                }
                bump_nodes(&mut nodes)?;
                stack.push(start_to_element(&e, &reader, &mut interner));
            }
            Event::Empty(e) => {
                bump_nodes(&mut nodes)?;
                let elem = start_to_element(&e, &reader, &mut interner);
                attach(&mut stack, &mut root, Node::Elem(elem));
            }
            Event::End(end) => match stack.pop() {
                Some(elem) => {
                    if elem.local.as_bytes() != end.local_name().as_ref() {
                        recovered = true;
                    }
                    attach(&mut stack, &mut root, Node::Elem(elem));
                }
                None => recovered = true,
            },
            Event::Text(e) => {
                let text = match e.decode() {
                    Ok(t) => t.into_owned(),
                    Err(_) => String::from_utf8_lossy(e.as_ref()).into_owned(),
                };
                if !text.is_empty() {
                    bump_nodes(&mut nodes)?;
                    push_text(&mut stack, &mut root, text);
                }
            }
            Event::GeneralRef(e) => {
                let name = String::from_utf8_lossy(e.as_ref()).into_owned();
                let resolved = resolve_entity(&name).unwrap_or_else(|| format!("&{name};"));
                bump_nodes(&mut nodes)?;
                push_text(&mut stack, &mut root, resolved);
            }
            Event::CData(e) => {
                let text = String::from_utf8_lossy(e.as_ref()).into_owned();
                bump_nodes(&mut nodes)?;
                push_text(&mut stack, &mut root, text);
            }
            Event::Eof => break,
            _ => {}
        }
    }
    if !stack.is_empty() {
        recovered = true;
        while let Some(elem) = stack.pop() {
            attach(&mut stack, &mut root, Node::Elem(elem));
        }
    }
    if recovered {
        log::warn!("recovered malformed xml (unclosed or mismatched elements)");
    }
    Ok(root)
}

/// Transcode an XML part to UTF-8 based on its BOM or encoding declaration.
fn to_utf8(bytes: &[u8]) -> std::borrow::Cow<'_, [u8]> {
    use std::borrow::Cow;
    match bytes {
        [0xFF, 0xFE, ..] => {
            let (s, _, _) = encoding_rs::UTF_16LE.decode(bytes);
            Cow::Owned(s.into_owned().into_bytes())
        }
        [0xFE, 0xFF, ..] => {
            let (s, _, _) = encoding_rs::UTF_16BE.decode(bytes);
            Cow::Owned(s.into_owned().into_bytes())
        }
        [0xEF, 0xBB, 0xBF, rest @ ..] => Cow::Borrowed(rest),
        _ => {
            // Sniff a non-UTF-8 encoding declaration in the XML prolog.
            let head = &bytes[..bytes.len().min(200)];
            if let Some(label) = declared_encoding(head)
                && let Some(enc) = encoding_rs::Encoding::for_label(label.as_bytes())
                && enc != encoding_rs::UTF_8
            {
                let (s, _, _) = enc.decode(bytes);
                return Cow::Owned(s.into_owned().into_bytes());
            }
            Cow::Borrowed(bytes)
        }
    }
}

fn declared_encoding(head: &[u8]) -> Option<String> {
    let head = std::str::from_utf8(head).ok()?;
    let idx = head.find("encoding")?;
    let rest = &head[idx + "encoding".len()..];
    let rest = rest.trim_start().strip_prefix('=')?.trim_start();
    let quote = rest.chars().next().filter(|c| *c == '"' || *c == '\'')?;
    let rest = &rest[1..];
    let end = rest.find(quote)?;
    Some(rest[..end].to_string())
}

fn bump_nodes(nodes: &mut usize) -> Result<(), ConvertError> {
    *nodes += 1;
    if *nodes > limits::MAX_XML_NODES {
        return Err(ConvertError::ResourceLimit {
            limit: "max_xml_nodes",
            detail: format!("part exceeds {} xml nodes", limits::MAX_XML_NODES),
        });
    }
    Ok(())
}

fn attach(stack: &mut [Element], root: &mut Element, node: Node) {
    match stack.last_mut() {
        Some(parent) => parent.children.push(node),
        None => root.children.push(node),
    }
}

/// ISO/IEC 29500 *Strict* namespace and relationship-type URIs are the
/// Transitional ones re-rooted under `http://purl.oclc.org/ooxml/` with the
/// `2006` version segment dropped. Normalizing to the Transitional family at
/// parse time lets the rest of the crate match a single set of constants.
pub fn normalize_ooxml_uri(uri: &str) -> Option<String> {
    let rest = uri.strip_prefix("http://purl.oclc.org/ooxml/")?;
    let (family, tail) = rest.split_once('/')?;
    Some(format!("http://schemas.openxmlformats.org/{family}/2006/{tail}"))
}

fn intern(interner: &mut HashMap<Vec<u8>, Rc<str>>, uri: &[u8]) -> Rc<str> {
    if let Some(rc) = interner.get(uri) {
        return rc.clone();
    }
    let raw = String::from_utf8_lossy(uri).into_owned();
    let rc: Rc<str> = Rc::from(normalize_ooxml_uri(&raw).unwrap_or(raw));
    interner.insert(uri.to_vec(), rc.clone());
    rc
}

fn start_to_element(
    e: &BytesStart,
    reader: &NsReader<&[u8]>,
    interner: &mut HashMap<Vec<u8>, Rc<str>>,
) -> Element {
    let (res, local) = reader.resolver().resolve_element(e.name());
    let ns = match res {
        ResolveResult::Bound(namespace) => Some(intern(interner, namespace.as_ref())),
        _ => None,
    };
    let local = String::from_utf8_lossy(local.as_ref()).into_owned();
    let mut attrs = Vec::new();
    for attr in e.attributes() {
        let attr = match attr {
            Ok(a) => a,
            Err(e) => {
                // Malformed attributes degrade to absent, but visibly.
                log::debug!("dropping undecodable attribute: {e}");
                continue;
            }
        };
        // Namespace declarations are consumed by the reader's resolver; the
        // DOM stores only resolved names.
        if attr.key.as_ref().strip_prefix(b"xmlns").is_some_and(|r| matches!(r, [] | [b':', ..])) {
            continue;
        }
        let (res, alocal) = reader.resolver().resolve_attribute(attr.key);
        let ans = match res {
            ResolveResult::Bound(namespace) => Some(intern(interner, namespace.as_ref())),
            _ => None,
        };
        let value = attr
            .decoded_and_normalized_value(quick_xml::XmlVersion::Implicit1_0, reader.decoder())
            .map(|v| v.into_owned())
            .unwrap_or_else(|_| String::from_utf8_lossy(&attr.value).into_owned());
        attrs.push(Attr {
            ns: ans,
            local: String::from_utf8_lossy(alocal.as_ref()).into_owned(),
            value,
        });
    }
    // `mc:Choice/@Requires` holds namespace *prefixes* evaluated in the
    // element's lexical scope (prefixes may be rebound locally). Resolve
    // them to URIs here, where the scope is known, so consumers never need
    // a document-wide prefix map. Unresolvable prefixes stay literal and
    // match no requirement.
    if local == "Choice"
        && ns.as_deref() == Some(ns::MC)
        && let Some(requires) = attrs.iter_mut().find(|a| a.local == "Requires")
    {
        requires.value = requires
            .value
            .split_whitespace()
            .map(|prefix| {
                let probe = format!("{prefix}:x");
                match reader.resolver().resolve_element(QName(probe.as_bytes())).0 {
                    ResolveResult::Bound(namespace) => {
                        let raw = String::from_utf8_lossy(namespace.as_ref()).into_owned();
                        normalize_ooxml_uri(&raw).unwrap_or(raw)
                    }
                    _ => prefix.to_string(),
                }
            })
            .collect::<Vec<_>>()
            .join(" ");
    }
    Element { ns, local, attrs, children: Vec::new() }
}

fn push_text(stack: &mut [Element], root: &mut Element, text: String) {
    let target = match stack.last_mut() {
        Some(parent) => &mut parent.children,
        None => &mut root.children,
    };
    if let Some(Node::Text(prev)) = target.last_mut() {
        prev.push_str(&text);
    } else {
        target.push(Node::Text(text));
    }
}

/// Resolve entity references quick-xml surfaces as GeneralRef events.
fn resolve_entity(name: &str) -> Option<String> {
    if let Some(num) = name.strip_prefix('#') {
        let code = if let Some(hex) = num.strip_prefix('x').or_else(|| num.strip_prefix('X')) {
            u32::from_str_radix(hex, 16).ok()?
        } else {
            num.parse().ok()?
        };
        return char::from_u32(code).map(String::from);
    }
    let ch = match name {
        "amp" => '&',
        "lt" => '<',
        "gt" => '>',
        "apos" => '\'',
        "quot" => '"',
        "nbsp" => '\u{a0}',
        "shy" => '\u{ad}',
        "mdash" => '\u{2014}',
        "ndash" => '\u{2013}',
        "lsquo" => '\u{2018}',
        "rsquo" => '\u{2019}',
        "ldquo" => '\u{201c}',
        "rdquo" => '\u{201d}',
        "hellip" => '\u{2026}',
        "copy" => '\u{a9}',
        "reg" => '\u{ae}',
        "trade" => '\u{2122}',
        "deg" => '\u{b0}',
        "middot" => '\u{b7}',
        "bull" => '\u{2022}',
        "sect" => '\u{a7}',
        "para" => '\u{b6}',
        "laquo" => '\u{ab}',
        "raquo" => '\u{bb}',
        "times" => '\u{d7}',
        "divide" => '\u{f7}',
        "plusmn" => '\u{b1}',
        "frac12" => '\u{bd}',
        "frac14" => '\u{bc}',
        "eacute" => '\u{e9}',
        "egrave" => '\u{e8}',
        "agrave" => '\u{e0}',
        "ccedil" => '\u{e7}',
        "uuml" => '\u{fc}',
        "ouml" => '\u{f6}',
        "auml" => '\u{e4}',
        "szlig" => '\u{df}',
        "aring" => '\u{e5}',
        "oslash" => '\u{f8}',
        "aelig" => '\u{e6}',
        "euro" => '\u{20ac}',
        "pound" => '\u{a3}',
        "yen" => '\u{a5}',
        "cent" => '\u{a2}',
        _ => return None,
    };
    Some(ch.to_string())
}

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

    #[test]
    fn resolves_namespaces_regardless_of_prefix() {
        let xml =
            br#"<x:root xmlns:x="urn:a" xmlns:y="urn:b"><y:kid x:id="1" plain="p"/></x:root>"#;
        let root = parse_xml(xml).unwrap();
        let r = root.find("urn:a", "root").unwrap();
        let kid = r.find("urn:b", "kid").unwrap();
        assert_eq!(kid.attr("urn:a", "id"), Some("1"));
        assert_eq!(kid.attr("urn:whatever", "plain"), Some("p"));
        assert!(r.find("urn:b", "root").is_none());
    }

    #[test]
    fn qualified_and_unqualified_lookups_are_separate() {
        let xml =
            br#"<x:root xmlns:x="urn:a" xmlns:r="urn:r"><x:kid r:id="rel" id="plain"/></x:root>"#;
        let root = parse_xml(xml).unwrap();
        let kid = root.first_descendant("urn:a", "kid").unwrap();
        assert_eq!(kid.attr_qualified("urn:r", "id"), Some("rel"));
        assert_eq!(kid.attr_unqualified("id"), Some("plain"));
        // The lenient lookup prefers the qualified match, while a strict
        // cross-namespace lookup never picks up the unqualified one.
        assert_eq!(kid.attr("urn:r", "id"), Some("rel"));
        assert_eq!(kid.attr_qualified("urn:other", "id"), None);
    }

    #[test]
    fn strict_ooxml_namespaces_normalize_to_transitional() {
        assert_eq!(
            normalize_ooxml_uri("http://purl.oclc.org/ooxml/wordprocessingml/main").as_deref(),
            Some(ns::W)
        );
        assert_eq!(
            normalize_ooxml_uri("http://purl.oclc.org/ooxml/officeDocument/relationships")
                .as_deref(),
            Some(ns::R)
        );
        assert_eq!(
            normalize_ooxml_uri(
                "http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument"
            )
            .as_deref(),
            Some(
                "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
            )
        );
        assert_eq!(normalize_ooxml_uri(ns::W), None);
        assert_eq!(normalize_ooxml_uri("urn:schemas-microsoft-com:vml"), None);

        let xml = br#"<w:document xmlns:w="http://purl.oclc.org/ooxml/wordprocessingml/main"><w:body/></w:document>"#;
        let root = parse_xml(xml).unwrap();
        let doc = root.find(ns::W, "document").unwrap();
        assert!(doc.find(ns::W, "body").is_some());
    }

    #[test]
    fn default_namespace_applies_to_elements_not_attributes() {
        let xml = br#"<root xmlns="urn:d"><kid id="1"/></root>"#;
        let root = parse_xml(xml).unwrap();
        let r = root.find("urn:d", "root").unwrap();
        let kid = r.find("urn:d", "kid").unwrap();
        assert_eq!(kid.attr_any("id"), Some("1"));
    }

    #[test]
    fn requires_prefixes_resolve_in_their_lexical_scope() {
        // M4: the same prefix rebound locally must resolve per element, not
        // through a document-wide first-declaration-wins map.
        let xml = br#"<mc:AlternateContent
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
            <mc:Choice xmlns:z="urn:one" Requires="z"/>
            <mc:Choice xmlns:z="urn:two" Requires="z unknownprefix"/>
        </mc:AlternateContent>"#;
        let root = parse_xml(xml).unwrap();
        let alt = root.find(ns::MC, "AlternateContent").unwrap();
        let choices: Vec<&Element> = alt.find_all(ns::MC, "Choice").collect();
        assert_eq!(choices[0].attr_any("Requires"), Some("urn:one"));
        assert_eq!(choices[1].attr_any("Requires"), Some("urn:two unknownprefix"));
    }

    #[test]
    fn utf16_with_bom_decodes() {
        let text = "<r a=\"caf\u{e9}\">\u{4f60}\u{597d}</r>";
        let mut bytes = vec![0xFF, 0xFE];
        for unit in text.encode_utf16() {
            bytes.extend_from_slice(&unit.to_le_bytes());
        }
        let root = parse_xml(&bytes).unwrap();
        let r = &root.child_elems().next().unwrap();
        assert_eq!(r.attr_any("a"), Some("caf\u{e9}"));
        assert_eq!(r.text(), "\u{4f60}\u{597d}");
    }

    #[test]
    fn unclosed_elements_recovered() {
        let root = parse_xml(b"<a><b>text").unwrap();
        let a = root.child_elems().next().unwrap();
        assert_eq!(a.local, "a");
        assert_eq!(a.text(), "text");
    }

    #[test]
    fn depth_limit_is_hard_error() {
        let mut xml = Vec::new();
        for _ in 0..(limits::MAX_XML_DEPTH + 2) {
            xml.extend_from_slice(b"<d>");
        }
        let err = parse_xml(&xml).unwrap_err();
        assert!(matches!(err, ConvertError::ResourceLimit { limit: "max_xml_depth", .. }));
    }

    #[test]
    fn deep_dom_traversal_is_iterative() {
        // Just under the cap: descendants/text must not overflow the stack.
        let depth = limits::MAX_XML_DEPTH - 1;
        let mut xml = Vec::new();
        for _ in 0..depth {
            xml.extend_from_slice(b"<d>");
        }
        xml.extend_from_slice(b"leaf");
        for _ in 0..depth {
            xml.extend_from_slice(b"</d>");
        }
        let root = parse_xml(&xml).unwrap();
        assert_eq!(root.text(), "leaf");
        assert_eq!(root.descendants("", "never").count(), 0);
    }
}