xmloxide 0.4.3

A pure Rust reimplementation of libxml2 — memory-safe XML/HTML parsing
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
//! XML serializer.
//!
//! Serializes a `Document` tree into a well-formed XML string.

use crate::tree::{Document, NodeId, NodeKind};

/// Options controlling XML serialization output.
///
/// # Examples
///
/// ```
/// use xmloxide::Document;
/// use xmloxide::serial::{serialize_with_options, SerializeOptions};
///
/// let doc = Document::parse_str("<root><child>Hello</child></root>").unwrap();
/// let xml = serialize_with_options(&doc, &SerializeOptions::default().indent(true));
/// assert!(xml.contains("  <child>"));
/// ```
#[derive(Debug, Clone)]
pub struct SerializeOptions {
    /// Whether to produce indented (pretty-printed) output.
    /// Defaults to `false`.
    pub indent: bool,
    /// The indentation string used for each level when `indent` is `true`.
    /// Defaults to two spaces.
    pub indent_str: String,
}

impl Default for SerializeOptions {
    fn default() -> Self {
        Self {
            indent: false,
            indent_str: "  ".to_string(),
        }
    }
}

impl SerializeOptions {
    /// Enables or disables indented (pretty-printed) output.
    ///
    /// When enabled, child elements are placed on their own lines with
    /// indentation (two spaces per level by default). Mixed-content elements
    /// (those containing both text and element children) are not indented.
    /// Use [`indent_str`](Self::indent_str) to customize the indentation
    /// string. Disabled by default.
    #[must_use]
    pub fn indent(mut self, indent: bool) -> Self {
        self.indent = indent;
        self
    }

    /// Sets the indentation string used for each nesting level.
    ///
    /// The default is two spaces (`"  "`). Common alternatives include a tab
    /// (`"\t"`) or four spaces (`"    "`). This only takes effect when
    /// [`indent`](Self::indent) is enabled.
    #[must_use]
    pub fn indent_str(mut self, s: &str) -> Self {
        self.indent_str = s.to_string();
        self
    }
}

/// Serializes a document to an XML string.
///
/// # Examples
///
/// ```
/// use xmloxide::Document;
/// use xmloxide::serial::serialize;
///
/// let doc = Document::parse_str("<root><child>Hello</child></root>").unwrap();
/// let xml = serialize(&doc);
/// assert!(xml.contains("<root>"));
/// ```
#[must_use]
pub fn serialize(doc: &Document) -> String {
    serialize_with_options(doc, &SerializeOptions::default())
}

/// Serializes a document to an XML string with the given options.
///
/// When `options.indent` is `true`, produces pretty-printed output with
/// newlines and indentation between elements.
///
/// # Examples
///
/// ```
/// use xmloxide::Document;
/// use xmloxide::serial::{serialize_with_options, SerializeOptions};
///
/// let doc = Document::parse_str("<root><child>Hello</child></root>").unwrap();
/// let xml = serialize_with_options(&doc, &SerializeOptions::default().indent(true));
/// assert!(xml.contains("  <child>"));
/// ```
#[must_use]
pub fn serialize_with_options(doc: &Document, options: &SerializeOptions) -> String {
    let mut output = String::new();

    // XML declaration — always emit, defaulting to version 1.0 (matches libxml2)
    let version = doc.version.as_deref().unwrap_or("1.0");
    output.push_str("<?xml version=\"");
    output.push_str(version);
    output.push('"');
    if let Some(ref encoding) = doc.encoding {
        output.push_str(" encoding=\"");
        output.push_str(encoding);
        output.push('"');
    }
    if let Some(standalone) = doc.standalone {
        output.push_str(" standalone=\"");
        output.push_str(if standalone { "yes" } else { "no" });
        output.push('"');
    }
    output.push_str("?>\n");

    // When no encoding is declared, non-ASCII chars in attributes are
    // re-encoded as hex character references (matches libxml2 behavior).
    let reencode_non_ascii = doc.encoding.is_none();

    // Serialize children of the document root
    for child in doc.children(doc.root()) {
        serialize_node(
            doc,
            child,
            &mut output,
            reencode_non_ascii,
            options,
            0,
            false,
        );
    }

    // Trailing newline (matches libxml2 output convention)
    output.push('\n');

    output
}

/// Returns `true` if the element contains only other elements (and optional
/// whitespace text), meaning it's safe to add indentation.
fn is_element_only(doc: &Document, id: NodeId) -> bool {
    let mut has_element_child = false;
    for child in doc.children(id) {
        match &doc.node(child).kind {
            NodeKind::Element { .. } => has_element_child = true,
            NodeKind::Text { content } if !content.trim().is_empty() => {
                return false;
            }
            NodeKind::CData { .. } | NodeKind::EntityRef { .. } => return false,
            _ => {}
        }
    }
    has_element_child
}

#[allow(clippy::too_many_lines)]
fn serialize_node(
    doc: &Document,
    id: NodeId,
    out: &mut String,
    reencode_non_ascii: bool,
    options: &SerializeOptions,
    depth: usize,
    parent_is_element_only: bool,
) {
    let indent = options.indent;
    match &doc.node(id).kind {
        NodeKind::Element {
            name,
            prefix,
            attributes,
            ..
        } => {
            if indent && parent_is_element_only {
                for _ in 0..depth {
                    out.push_str(&options.indent_str);
                }
            }
            out.push('<');
            if let Some(pfx) = prefix {
                out.push_str(pfx);
                out.push(':');
            }
            out.push_str(name);

            for attr in attributes {
                out.push(' ');
                if let Some(pfx) = &attr.prefix {
                    out.push_str(pfx);
                    out.push(':');
                }
                out.push_str(&attr.name);
                out.push_str("=\"");
                let is_ns_decl = attr.name == "xmlns" || attr.prefix.as_deref() == Some("xmlns");
                if let Some(raw) = attr.raw_value.as_ref().filter(|_| !is_ns_decl) {
                    write_escaped_attr_preserve_refs(out, raw, reencode_non_ascii);
                } else {
                    write_escaped_attr(out, &attr.value, reencode_non_ascii);
                }
                out.push('"');
            }

            if doc.first_child(id).is_none() {
                out.push_str("/>");
                if indent && parent_is_element_only {
                    out.push('\n');
                }
            } else {
                out.push('>');
                let element_only = indent && is_element_only(doc, id);
                if element_only {
                    out.push('\n');
                }
                for child in doc.children(id) {
                    if element_only {
                        if let NodeKind::Text { content } = &doc.node(child).kind {
                            if content.trim().is_empty() {
                                continue;
                            }
                        }
                    }
                    serialize_node(
                        doc,
                        child,
                        out,
                        reencode_non_ascii,
                        options,
                        depth + 1,
                        element_only,
                    );
                }
                if element_only {
                    for _ in 0..depth {
                        out.push_str(&options.indent_str);
                    }
                }
                out.push_str("</");
                if let Some(pfx) = prefix {
                    out.push_str(pfx);
                    out.push(':');
                }
                out.push_str(name);
                out.push('>');
                if indent && parent_is_element_only {
                    out.push('\n');
                }
            }
        }
        NodeKind::Text { content } => {
            write_escaped_text(out, content, reencode_non_ascii);
        }
        NodeKind::CData { content } => {
            out.push_str("<![CDATA[");
            out.push_str(content);
            out.push_str("]]>");
        }
        NodeKind::Comment { content } => {
            if indent && parent_is_element_only {
                for _ in 0..depth {
                    out.push_str(&options.indent_str);
                }
            }
            out.push_str("<!--");
            out.push_str(content);
            out.push_str("-->");
            if indent && parent_is_element_only {
                out.push('\n');
            }
        }
        NodeKind::ProcessingInstruction { target, data } => {
            if indent && parent_is_element_only {
                for _ in 0..depth {
                    out.push_str(&options.indent_str);
                }
            }
            out.push_str("<?");
            out.push_str(target);
            if let Some(d) = data {
                out.push(' ');
                out.push_str(d);
            }
            out.push_str("?>");
            if indent && parent_is_element_only {
                out.push('\n');
            }
        }
        NodeKind::EntityRef { name, .. } => {
            out.push('&');
            out.push_str(name);
            out.push(';');
        }
        NodeKind::DocumentType {
            name,
            system_id,
            public_id,
            internal_subset,
        } => {
            out.push_str("<!DOCTYPE ");
            out.push_str(name);
            match (public_id, system_id) {
                (Some(pub_id), Some(sys_id)) => {
                    out.push_str(" PUBLIC \"");
                    out.push_str(pub_id);
                    out.push_str("\" \"");
                    out.push_str(sys_id);
                    out.push('"');
                }
                (None, Some(sys_id)) => {
                    out.push_str(" SYSTEM \"");
                    out.push_str(sys_id);
                    out.push('"');
                }
                _ => {}
            }
            if let Some(ref subset) = internal_subset {
                out.push_str(" [");
                out.push_str(subset);
                out.push_str("]>");
            } else {
                out.push('>');
            }
        }
        NodeKind::Document => {
            // Should not appear as a child node
        }
    }
}

/// Writes a hexadecimal character reference (`&#xHH;`) for a Unicode code point.
fn write_hex_char_ref(out: &mut String, ch: char) {
    use std::fmt::Write;
    let _ = write!(out, "&#x{:X};", ch as u32);
}

/// Escapes text content for XML output.
///
/// Follows libxml2's `xmlEscapeEntities` behavior:
/// - `<`, `>`, `&` are escaped with named entity references
/// - `\r` is encoded as `&#13;`
/// - `\t` and `\n` are passed through
/// - Control characters below 0x20 (other than `\t`, `\n`, `\r`) are hex-encoded
/// - Non-ASCII characters are passed through as raw UTF-8
fn write_escaped_text(out: &mut String, text: &str, reencode_non_ascii: bool) {
    for ch in text.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '\r' => out.push_str("&#13;"),
            '\t' | '\n' => out.push(ch),
            c if (c as u32) < 0x20 => write_hex_char_ref(out, c),
            c if reencode_non_ascii && (c as u32) >= 0x80 => write_hex_char_ref(out, c),
            _ => out.push(ch),
        }
    }
}

/// Escapes an attribute value that contains preserved entity references.
///
/// Custom entity references (`&name;` where name is not a builtin) are
/// preserved as-is. Character references (`&#...;`) and builtin entity refs
/// (`&amp;`, `&lt;`, `&gt;`, `&apos;`, `&quot;`) are decoded to their
/// actual characters and then re-escaped normally through the attribute
/// escaping logic. This matches libxml2's serialization behavior.
fn write_escaped_attr_preserve_refs(out: &mut String, text: &str, reencode_non_ascii: bool) {
    let bytes = text.as_bytes();
    let len = bytes.len();
    let mut i = 0;

    while i < len {
        let b = bytes[i];
        if b == b'&' {
            // Check if this starts a valid reference
            if let Some(ref_end) = find_attr_reference_end(bytes, i) {
                let ref_str = &text[i..=ref_end];
                if ref_str.starts_with("&#") {
                    // Character reference — decode to char, then escape normally
                    if let Some(ch) = decode_char_ref(ref_str) {
                        write_escaped_attr_char(out, ch, reencode_non_ascii);
                    } else {
                        out.push_str(ref_str);
                    }
                } else if let Some(ch) = decode_builtin_entity_ref(ref_str) {
                    // Builtin entity ref — decode and re-escape normally
                    write_escaped_attr_char(out, ch, reencode_non_ascii);
                } else {
                    // Custom entity reference — preserve as-is
                    out.push_str(ref_str);
                }
                i = ref_end + 1;
                continue;
            }
            out.push_str("&amp;");
            i += 1;
        } else if b == b'<' {
            out.push_str("&lt;");
            i += 1;
        } else if b == b'>' {
            out.push_str("&gt;");
            i += 1;
        } else if b == b'"' {
            out.push_str("&quot;");
            i += 1;
        } else if b == b'\t' {
            out.push_str("&#9;");
            i += 1;
        } else if b == b'\n' {
            out.push_str("&#10;");
            i += 1;
        } else if b == b'\r' {
            out.push_str("&#13;");
            i += 1;
        } else {
            let ch = &text[i..];
            if let Some(c) = ch.chars().next() {
                if (c as u32) < 0x20 || (reencode_non_ascii && (c as u32) >= 0x80) {
                    write_hex_char_ref(out, c);
                } else {
                    out.push(c);
                }
                i += c.len_utf8();
            } else {
                i += 1;
            }
        }
    }
}

/// Decodes a character reference (`&#NNN;` or `&#xHHH;`) to a `char`.
fn decode_char_ref(s: &str) -> Option<char> {
    let inner = s.strip_prefix("&#")?.strip_suffix(';')?;
    let code_point = if let Some(hex) = inner.strip_prefix('x') {
        u32::from_str_radix(hex, 16).ok()?
    } else {
        inner.parse::<u32>().ok()?
    };
    char::from_u32(code_point)
}

/// Decodes a builtin entity reference to a `char`, if it is one.
/// Returns `None` for custom (non-builtin) entity references.
fn decode_builtin_entity_ref(s: &str) -> Option<char> {
    match s {
        "&amp;" => Some('&'),
        "&lt;" => Some('<'),
        "&gt;" => Some('>'),
        "&apos;" => Some('\''),
        "&quot;" => Some('"'),
        _ => None,
    }
}

/// Writes a single character with attribute escaping rules.
fn write_escaped_attr_char(out: &mut String, ch: char, reencode_non_ascii: bool) {
    match ch {
        '&' => out.push_str("&amp;"),
        '<' => out.push_str("&lt;"),
        '>' => out.push_str("&gt;"),
        '"' => out.push_str("&quot;"),
        '\t' => out.push_str("&#9;"),
        '\n' => out.push_str("&#10;"),
        '\r' => out.push_str("&#13;"),
        c if (c as u32) < 0x20 => write_hex_char_ref(out, c),
        c if reencode_non_ascii && (c as u32) >= 0x80 => write_hex_char_ref(out, c),
        _ => out.push(ch),
    }
}

/// Finds the end of an entity/character reference in attribute raw value.
fn find_attr_reference_end(bytes: &[u8], start: usize) -> Option<usize> {
    if start >= bytes.len() || bytes[start] != b'&' {
        return None;
    }
    let mut i = start + 1;
    if i >= bytes.len() {
        return None;
    }
    if bytes[i] == b'#' {
        i += 1;
        if i >= bytes.len() {
            return None;
        }
        if bytes[i] == b'x' {
            i += 1;
            let d = i;
            while i < bytes.len() && bytes[i].is_ascii_hexdigit() {
                i += 1;
            }
            if i == d || i >= bytes.len() || bytes[i] != b';' {
                return None;
            }
        } else {
            let d = i;
            while i < bytes.len() && bytes[i].is_ascii_digit() {
                i += 1;
            }
            if i == d || i >= bytes.len() || bytes[i] != b';' {
                return None;
            }
        }
        Some(i)
    } else {
        if !bytes[i].is_ascii_alphabetic() && bytes[i] != b'_' && bytes[i] != b':' {
            return None;
        }
        i += 1;
        while i < bytes.len()
            && (bytes[i].is_ascii_alphanumeric()
                || bytes[i] == b'_'
                || bytes[i] == b':'
                || bytes[i] == b'-'
                || bytes[i] == b'.')
        {
            i += 1;
        }
        if i >= bytes.len() || bytes[i] != b';' {
            return None;
        }
        Some(i)
    }
}

/// Escapes attribute values for XML output.
///
/// Follows libxml2's `xmlAttrSerializeTxtContent` behavior:
/// - `<`, `>`, `&`, `"` are escaped with named entity references
/// - `\t` → `&#9;`, `\n` → `&#10;`, `\r` → `&#13;`
/// - When `reencode_non_ascii` is true (doc has no declared encoding), non-ASCII
///   characters (>= U+0080) are encoded as hex character references
fn write_escaped_attr(out: &mut String, text: &str, reencode_non_ascii: bool) {
    for ch in text.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            '\t' => out.push_str("&#9;"),
            '\n' => out.push_str("&#10;"),
            '\r' => out.push_str("&#13;"),
            c if (c as u32) < 0x20 => write_hex_char_ref(out, c),
            c if reencode_non_ascii && (c as u32) >= 0x80 => write_hex_char_ref(out, c),
            _ => out.push(ch),
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::tree::Attribute;

    #[test]
    fn test_serialize_empty_element() {
        let mut doc = Document::new();
        let root = doc.root();
        let elem = doc.create_node(NodeKind::Element {
            name: "br".to_string(),
            prefix: None,
            namespace: None,
            attributes: vec![],
        });
        doc.append_child(root, elem);
        assert_eq!(serialize(&doc), "<?xml version=\"1.0\"?>\n<br/>\n");
    }

    #[test]
    fn test_serialize_element_with_text() {
        let mut doc = Document::new();
        let root = doc.root();
        let elem = doc.create_node(NodeKind::Element {
            name: "p".to_string(),
            prefix: None,
            namespace: None,
            attributes: vec![],
        });
        let text = doc.create_node(NodeKind::Text {
            content: "Hello".to_string(),
        });
        doc.append_child(root, elem);
        doc.append_child(elem, text);
        assert_eq!(serialize(&doc), "<?xml version=\"1.0\"?>\n<p>Hello</p>\n");
    }

    #[test]
    fn test_serialize_element_with_attributes() {
        let mut doc = Document::new();
        let root = doc.root();
        let elem = doc.create_node(NodeKind::Element {
            name: "div".to_string(),
            prefix: None,
            namespace: None,
            attributes: vec![
                Attribute {
                    name: "id".to_string(),
                    value: "main".to_string(),
                    prefix: None,
                    namespace: None,
                    raw_value: None,
                },
                Attribute {
                    name: "class".to_string(),
                    value: "big".to_string(),
                    prefix: None,
                    namespace: None,
                    raw_value: None,
                },
            ],
        });
        doc.append_child(root, elem);
        assert_eq!(
            serialize(&doc),
            "<?xml version=\"1.0\"?>\n<div id=\"main\" class=\"big\"/>\n"
        );
    }

    #[test]
    fn test_serialize_escaping() {
        let mut doc = Document::new();
        let root = doc.root();
        let elem = doc.create_node(NodeKind::Element {
            name: "p".to_string(),
            prefix: None,
            namespace: None,
            attributes: vec![],
        });
        let text = doc.create_node(NodeKind::Text {
            content: "a < b & c > d".to_string(),
        });
        doc.append_child(root, elem);
        doc.append_child(elem, text);
        assert_eq!(
            serialize(&doc),
            "<?xml version=\"1.0\"?>\n<p>a &lt; b &amp; c &gt; d</p>\n"
        );
    }

    #[test]
    fn test_serialize_comment() {
        let mut doc = Document::new();
        let root = doc.root();
        let comment = doc.create_node(NodeKind::Comment {
            content: " a comment ".to_string(),
        });
        doc.append_child(root, comment);
        assert_eq!(
            serialize(&doc),
            "<?xml version=\"1.0\"?>\n<!-- a comment -->\n"
        );
    }

    #[test]
    fn test_serialize_cdata() {
        let mut doc = Document::new();
        let root = doc.root();
        let elem = doc.create_node(NodeKind::Element {
            name: "script".to_string(),
            prefix: None,
            namespace: None,
            attributes: vec![],
        });
        let cdata = doc.create_node(NodeKind::CData {
            content: "x < 1 && y > 2".to_string(),
        });
        doc.append_child(root, elem);
        doc.append_child(elem, cdata);
        assert_eq!(
            serialize(&doc),
            "<?xml version=\"1.0\"?>\n<script><![CDATA[x < 1 && y > 2]]></script>\n"
        );
    }

    #[test]
    fn test_serialize_processing_instruction() {
        let mut doc = Document::new();
        let root = doc.root();
        let pi = doc.create_node(NodeKind::ProcessingInstruction {
            target: "xml-stylesheet".to_string(),
            data: Some("type=\"text/css\" href=\"style.css\"".to_string()),
        });
        doc.append_child(root, pi);
        assert_eq!(
            serialize(&doc),
            "<?xml version=\"1.0\"?>\n<?xml-stylesheet type=\"text/css\" href=\"style.css\"?>\n"
        );
    }

    #[test]
    fn test_serialize_xml_declaration() {
        let mut doc = Document::new();
        doc.version = Some("1.0".to_string());
        doc.encoding = Some("UTF-8".to_string());
        let root = doc.root();
        let elem = doc.create_node(NodeKind::Element {
            name: "root".to_string(),
            prefix: None,
            namespace: None,
            attributes: vec![],
        });
        doc.append_child(root, elem);
        assert_eq!(
            serialize(&doc),
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root/>\n"
        );
    }

    #[test]
    fn test_serialize_attr_escaping() {
        let mut doc = Document::new();
        let root = doc.root();
        let elem = doc.create_node(NodeKind::Element {
            name: "a".to_string(),
            prefix: None,
            namespace: None,
            attributes: vec![Attribute {
                name: "title".to_string(),
                value: "He said \"hello\" & <bye>".to_string(),
                prefix: None,
                namespace: None,
                raw_value: None,
            }],
        });
        doc.append_child(root, elem);
        assert_eq!(
            serialize(&doc),
            "<?xml version=\"1.0\"?>\n<a title=\"He said &quot;hello&quot; &amp; &lt;bye&gt;\"/>\n"
        );
    }

    #[test]
    fn test_serialize_pretty_print() {
        let doc = Document::parse_str("<root><child><inner>text</inner></child></root>").unwrap();
        let opts = SerializeOptions::default().indent(true);
        let xml = serialize_with_options(&doc, &opts);
        assert_eq!(
            xml,
            "<?xml version=\"1.0\"?>\n<root>\n  <child>\n    <inner>text</inner>\n  </child>\n</root>\n"
        );
    }

    #[test]
    fn test_serialize_pretty_print_mixed_content() {
        // Mixed content (element + non-whitespace text) should not be indented
        let doc = Document::parse_str("<root><p>Hello <b>world</b></p></root>").unwrap();
        let opts = SerializeOptions::default().indent(true);
        let xml = serialize_with_options(&doc, &opts);
        // Mixed content children are not indented
        assert!(xml.contains("  <p>Hello <b>world</b></p>"));
    }

    #[test]
    fn test_serialize_pretty_print_custom_indent() {
        let doc = Document::parse_str("<root><child/></root>").unwrap();
        let opts = SerializeOptions::default().indent(true).indent_str("\t");
        let xml = serialize_with_options(&doc, &opts);
        assert!(xml.contains("\t<child/>"));
    }

    #[test]
    fn test_serialize_no_indent_unchanged() {
        // Default (no indent) should produce same output as serialize()
        let doc = Document::parse_str("<root><child>Hello</child></root>").unwrap();
        let xml1 = serialize(&doc);
        let xml2 = serialize_with_options(&doc, &SerializeOptions::default());
        assert_eq!(xml1, xml2);
    }
}