xmloxide 0.4.2

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
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
//! HTML serializer.
//!
//! Serializes a `Document` tree into an HTML string, following libxml2's
//! `htmlSaveFile` behavior. Key differences from XML serialization:
//!
//! - No XML declaration (`<?xml ...?>`)
//! - Void elements use `<br>` syntax (no `/>`)
//! - Non-void empty elements use `<p></p>` (no `<p/>`)
//! - Raw text elements (script, style) are not escaped
//! - Non-ASCII characters are re-encoded as HTML named entities where possible
//! - Formatting newlines around block-level elements

use crate::html::entities::reverse_lookup_entity;
use crate::html::{is_raw_text_element, is_void_element};
use crate::tree::{Document, NodeId, NodeKind};

/// Serializes a document to an HTML string.
///
/// Produces output compatible with libxml2's HTML serialization:
/// - DOCTYPE declaration (if present, or default HTML 4.0 Transitional)
/// - HTML void elements serialized without self-closing slash
/// - Script/style content preserved without escaping
/// - Non-ASCII characters re-encoded as named HTML entities
///
/// # Examples
///
/// ```
/// use xmloxide::html::parse_html;
/// use xmloxide::serial::html::serialize_html;
///
/// let doc = parse_html("<p>Hello</p>").unwrap();
/// let html = serialize_html(&doc);
/// assert!(html.contains("<p>"));
/// ```
#[must_use]
pub fn serialize_html(doc: &Document) -> String {
    let mut output = String::new();

    // Detect whether the document declares UTF-8 charset.
    // If so, non-ASCII characters are preserved as raw UTF-8.
    // Otherwise (default ISO-8859-1), they are re-encoded as named entities.
    let reencode = !detect_utf8_charset(doc);

    // Serialize children of the document root (DOCTYPE, elements, etc.)
    for child in doc.children(doc.root()) {
        serialize_html_node(doc, child, &mut output, reencode);
    }

    // Trailing newline (matches libxml2 output convention)
    if !output.ends_with('\n') {
        output.push('\n');
    }

    output
}

/// Serializes a document produced by the HTML5 parser to an HTML string.
///
/// Unlike [`serialize_html`] (which targets libxml2's HTML 4.01 output),
/// this function always preserves non-ASCII characters as raw UTF-8 and
/// uses self-closing syntax for foreign content elements (SVG, `MathML`).
///
/// # Examples
///
/// ```
/// use xmloxide::html5::parse_html5;
/// use xmloxide::serial::html::serialize_html5;
///
/// let doc = parse_html5("<p>Hello</p>").unwrap();
/// let html = serialize_html5(&doc);
/// assert!(html.contains("<p>Hello</p>"));
/// ```
#[must_use]
pub fn serialize_html5(doc: &Document) -> String {
    let mut output = String::new();

    for child in doc.children(doc.root()) {
        serialize_html5_node(doc, child, &mut output);
    }

    if !output.ends_with('\n') {
        output.push('\n');
    }

    output
}

/// Detects whether the document declares a UTF-8 charset via `<meta>` tags.
///
/// Checks for:
/// - `<meta charset="utf-8">`
/// - `<meta http-equiv="Content-Type" content="...charset=utf-8...">`
///
/// When the charset is UTF-8, non-ASCII characters are preserved as raw
/// UTF-8 in the output. Otherwise (default ISO-8859-1 for HTML), they
/// are re-encoded as named HTML entities.
fn detect_utf8_charset(doc: &Document) -> bool {
    let root = doc.root();
    for id in doc.children(root) {
        if check_meta_charset(doc, id) {
            return true;
        }
    }
    false
}

/// Recursively checks an element subtree for meta charset declarations.
fn check_meta_charset(doc: &Document, id: NodeId) -> bool {
    if let NodeKind::Element {
        name, attributes, ..
    } = &doc.node(id).kind
    {
        if name == "meta" {
            // Check <meta charset="utf-8">
            for attr in attributes {
                if attr.name == "charset" && attr.value.eq_ignore_ascii_case("utf-8") {
                    return true;
                }
            }
            // Check <meta http-equiv="Content-Type" content="...charset=utf-8...">
            let is_content_type = attributes
                .iter()
                .any(|a| a.name == "http-equiv" && a.value.eq_ignore_ascii_case("content-type"));
            if is_content_type {
                for attr in attributes {
                    if attr.name == "content" {
                        let lower = attr.value.to_ascii_lowercase();
                        if lower.contains("charset=utf-8") {
                            return true;
                        }
                    }
                }
            }
        }
        // Recurse into children
        for child in doc.children(id) {
            if check_meta_charset(doc, child) {
                return true;
            }
        }
    }
    false
}

/// Returns true if the element is an HTML inline element.
///
/// libxml2 categorizes elements as inline or block-level. Block-level
/// elements get formatting newlines around them in the serialized output.
fn is_inline_element(tag: &str) -> bool {
    matches!(
        tag,
        "a" | "abbr"
            | "acronym"
            | "b"
            | "bdo"
            | "big"
            | "br"
            | "cite"
            | "code"
            | "dfn"
            | "em"
            | "font"
            | "i"
            | "img"
            | "input"
            | "kbd"
            | "label"
            | "q"
            | "s"
            | "samp"
            | "select"
            | "small"
            | "span"
            | "strike"
            | "strong"
            | "sub"
            | "sup"
            | "textarea"
            | "tt"
            | "u"
            | "var"
    )
}

/// Returns true if the node kind is text-like (`Text`, `CData`, or `EntityRef`).
///
/// libxml2 suppresses formatting newlines when adjacent to text-like nodes.
fn is_text_like(kind: &NodeKind) -> bool {
    matches!(
        kind,
        NodeKind::Text { .. } | NodeKind::CData { .. } | NodeKind::EntityRef { .. }
    )
}

/// Checks whether a formatting newline should be added after a block-level
/// element's opening tag (libxml2 behavior).
///
/// Adds `\n` when:
/// - Element is not inline
/// - Element name does not start with 'p' (p, pre, param)
/// - First child is not a text-like node
/// - Element has more than one child
fn maybe_newline_after_open(doc: &Document, id: NodeId, tag: &str, out: &mut String) {
    if is_inline_element(tag) || tag.starts_with('p') {
        return;
    }
    let Some(first) = doc.first_child(id) else {
        return;
    };
    if is_text_like(&doc.node(first).kind) {
        return;
    }
    // Check that the element has more than one child
    if doc.next_sibling(first).is_none() {
        return;
    }
    out.push('\n');
}

/// Checks whether a formatting newline should be added before a block-level
/// element's closing tag (libxml2 behavior).
///
/// Adds `\n` when:
/// - Element is not inline
/// - Element name does not start with 'p' (p, pre, param)
/// - Last child is not a text-like node
/// - Element has more than one child
fn maybe_newline_before_close(doc: &Document, id: NodeId, tag: &str, out: &mut String) {
    if is_inline_element(tag) || tag.starts_with('p') {
        return;
    }
    let Some(first) = doc.first_child(id) else {
        return;
    };
    let Some(last) = doc.last_child(id) else {
        return;
    };
    if is_text_like(&doc.node(last).kind) {
        return;
    }
    // More than one child
    if doc.next_sibling(first).is_none() {
        return;
    }
    out.push('\n');
}

/// Checks whether a formatting newline should be added after a block-level
/// element's closing tag (libxml2 behavior).
///
/// Adds `\n` when:
/// - Element is not inline
/// - Next sibling exists and is not a text-like node
/// - Parent element name does not start with 'p'
fn maybe_newline_after_close(doc: &Document, id: NodeId, tag: &str, out: &mut String) {
    if is_inline_element(tag) {
        return;
    }
    let Some(next) = doc.next_sibling(id) else {
        return;
    };
    if is_text_like(&doc.node(next).kind) {
        return;
    }
    if let Some(parent) = doc.parent(id) {
        let parent_name = doc.node_name(parent).unwrap_or("");
        if parent_name.starts_with('p') {
            return;
        }
    }
    out.push('\n');
}

#[allow(clippy::too_many_lines)]
fn serialize_html_node(doc: &Document, id: NodeId, out: &mut String, reencode: bool) {
    match &doc.node(id).kind {
        NodeKind::Element {
            name,
            prefix,
            attributes,
            ..
        } => {
            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);
                // Boolean attributes: output without value when value == name
                if attr.value != attr.name {
                    // Use single quotes when value contains double quotes
                    if attr.value.contains('"') && !attr.value.contains('\'') {
                        out.push_str("='");
                        write_html_escaped_attr_sq(out, &attr.value, reencode);
                        out.push('\'');
                    } else {
                        out.push_str("=\"");
                        if is_uri_attribute(&attr.name) {
                            write_html_uri_attr(out, &attr.value, reencode);
                        } else {
                            write_html_escaped_attr(out, &attr.value, reencode);
                        }
                        out.push('"');
                    }
                }
            }
            out.push('>');

            let lower = name.to_ascii_lowercase();

            // Void elements: no closing tag
            if is_void_element(&lower) {
                maybe_newline_after_close(doc, id, &lower, out);
                return;
            }

            // Formatting newline after opening tag for block elements
            maybe_newline_after_open(doc, id, &lower, out);

            // Raw text elements: output content without escaping
            if is_raw_text_element(&lower) {
                for child in doc.children(id) {
                    if let NodeKind::Text { content } = &doc.node(child).kind {
                        out.push_str(content);
                    } else {
                        serialize_html_node(doc, child, out, reencode);
                    }
                }
            } else {
                for child in doc.children(id) {
                    serialize_html_node(doc, child, out, reencode);
                }
            }

            // Formatting newline before closing tag for block elements
            maybe_newline_before_close(doc, id, &lower, out);

            // Closing tag
            out.push_str("</");
            if let Some(pfx) = prefix {
                out.push_str(pfx);
                out.push(':');
            }
            out.push_str(name);
            out.push('>');

            // Formatting newline after closing tag for block elements
            maybe_newline_after_close(doc, id, &lower, out);
        }
        NodeKind::Text { content } => {
            write_html_escaped_text(out, content, reencode);
        }
        NodeKind::CData { content } => {
            // In HTML, CDATA is not standard — output as text
            write_html_escaped_text(out, content, reencode);
        }
        NodeKind::Comment { content } => {
            out.push_str("<!--");
            out.push_str(content);
            out.push_str("-->");
        }
        NodeKind::ProcessingInstruction { target, data } => {
            // HTML PIs use '>' as terminator, not '?>' (XML style)
            out.push_str("<?");
            out.push_str(target);
            if let Some(d) = data {
                out.push(' ');
                out.push_str(d);
            }
            out.push('>');
        }
        NodeKind::EntityRef { name, .. } => {
            out.push('&');
            out.push_str(name);
            out.push(';');
        }
        NodeKind::DocumentType {
            name,
            system_id,
            public_id,
            ..
        } => {
            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('"');
                    if !sys_id.is_empty() {
                        out.push_str(" \"");
                        out.push_str(sys_id);
                        out.push('"');
                    }
                }
                (Some(pub_id), None) => {
                    out.push_str(" PUBLIC \"");
                    out.push_str(pub_id);
                    out.push('"');
                }
                (None, Some(sys_id)) => {
                    out.push_str(" SYSTEM \"");
                    out.push_str(sys_id);
                    out.push('"');
                }
                _ => {}
            }
            out.push_str(">\n");
        }
        NodeKind::Document => {
            // Should not appear as a child node
        }
    }
}

/// Escapes text content for HTML output.
///
/// - `&` → `&amp;`
/// - `<` → `&lt;`
/// - `>` → `&gt;`
/// - Non-ASCII characters with known HTML entities → `&name;` (when `reencode` is true)
fn write_html_escaped_text(out: &mut String, text: &str, reencode: bool) {
    for ch in text.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            c if reencode && (c as u32) >= 0x80 => {
                if let Some(name) = reverse_lookup_entity(c) {
                    out.push('&');
                    out.push_str(name);
                    out.push(';');
                } else {
                    out.push(c);
                }
            }
            _ => out.push(ch),
        }
    }
}

/// Returns true if the attribute name is a URI-type attribute that should
/// have its value URL-encoded (spaces → `%20`, etc.).
fn is_uri_attribute(name: &str) -> bool {
    matches!(
        name,
        "href"
            | "src"
            | "action"
            | "background"
            | "cite"
            | "classid"
            | "codebase"
            | "data"
            | "longdesc"
            | "profile"
            | "usemap"
    )
}

/// Writes a URI attribute value with URL encoding for non-URI characters.
///
/// Spaces are encoded as `%20`. HTML-special characters (`&`, `<`, `>`)
/// are entity-escaped. Non-ASCII characters are handled based on the
/// `reencode` flag.
fn write_html_uri_attr(out: &mut String, text: &str, reencode: bool) {
    for ch in text.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '"' => out.push_str("&quot;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            ' ' => out.push_str("%20"),
            c if reencode && (c as u32) >= 0x80 => {
                if let Some(name) = reverse_lookup_entity(c) {
                    out.push('&');
                    out.push_str(name);
                    out.push(';');
                } else {
                    out.push(c);
                }
            }
            _ => out.push(ch),
        }
    }
}

/// Escapes an attribute value for HTML output (single-quote delimited).
///
/// Used when the value contains `"` characters and is delimited by `'`.
/// - `&` → `&amp;`
/// - `'` → `&#39;`
/// - `<` → `&lt;`
/// - `>` → `&gt;`
/// - Non-ASCII characters with known HTML entities → `&name;` (when `reencode` is true)
fn write_html_escaped_attr_sq(out: &mut String, text: &str, reencode: bool) {
    for ch in text.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '\'' => out.push_str("&#39;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            c if reencode && (c as u32) >= 0x80 => {
                if let Some(name) = reverse_lookup_entity(c) {
                    out.push('&');
                    out.push_str(name);
                    out.push(';');
                } else {
                    out.push(c);
                }
            }
            _ => out.push(ch),
        }
    }
}

/// Escapes an attribute value for HTML output.
///
/// - `&` → `&amp;`
/// - `"` → `&quot;`
/// - `<` → `&lt;`
/// - `>` → `&gt;`
/// - Non-ASCII characters with known HTML entities → `&name;` (when `reencode` is true)
fn write_html_escaped_attr(out: &mut String, text: &str, reencode: bool) {
    for ch in text.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '"' => out.push_str("&quot;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            c if reencode && (c as u32) >= 0x80 => {
                if let Some(name) = reverse_lookup_entity(c) {
                    out.push('&');
                    out.push_str(name);
                    out.push(';');
                } else {
                    out.push(c);
                }
            }
            _ => out.push(ch),
        }
    }
}

// ---------------------------------------------------------------------------
// HTML5 serialization
// ---------------------------------------------------------------------------

/// HTML5 void elements (WHATWG §13.1.2).
fn is_html5_void(tag: &str) -> bool {
    matches!(
        tag,
        "area"
            | "base"
            | "br"
            | "col"
            | "embed"
            | "hr"
            | "img"
            | "input"
            | "link"
            | "meta"
            | "source"
            | "track"
            | "wbr"
    )
}

/// HTML5 raw text elements (content is not escaped).
fn is_html5_raw_text(tag: &str) -> bool {
    matches!(tag, "script" | "style")
}

/// Serialize a single node for HTML5 output.
fn serialize_html5_node(doc: &Document, id: NodeId, out: &mut String) {
    match &doc.node(id).kind {
        NodeKind::Element {
            name,
            namespace,
            attributes,
            ..
        } => {
            let is_foreign = namespace.as_deref().is_some_and(|ns| {
                ns == "http://www.w3.org/2000/svg" || ns == "http://www.w3.org/1998/Math/MathML"
            });

            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("=\"");
                write_html5_escaped_attr(out, &attr.value);
                out.push('"');
            }

            let lower = name.to_ascii_lowercase();

            // Void elements: no closing tag
            if !is_foreign && is_html5_void(&lower) {
                out.push('>');
                return;
            }

            // Foreign content with no children: self-closing
            if is_foreign && doc.first_child(id).is_none() {
                out.push_str("/>");
                return;
            }

            out.push('>');

            // Raw text elements: output content without escaping
            if is_html5_raw_text(&lower) {
                for child in doc.children(id) {
                    if let NodeKind::Text { content } = &doc.node(child).kind {
                        out.push_str(content);
                    }
                }
            } else {
                for child in doc.children(id) {
                    serialize_html5_node(doc, child, out);
                }
            }

            out.push_str("</");
            out.push_str(name);
            out.push('>');
        }
        NodeKind::Text { content } => {
            write_html5_escaped_text(out, content);
        }
        NodeKind::Comment { content } => {
            out.push_str("<!--");
            out.push_str(content);
            out.push_str("-->");
        }
        NodeKind::DocumentType {
            name,
            public_id,
            system_id,
            ..
        } => {
            out.push_str("<!DOCTYPE ");
            out.push_str(name);
            if let Some(pub_id) = public_id {
                out.push_str(" PUBLIC \"");
                out.push_str(pub_id);
                out.push('"');
                if let Some(sys_id) = system_id {
                    out.push_str(" \"");
                    out.push_str(sys_id);
                    out.push('"');
                }
            } else if let Some(sys_id) = system_id {
                out.push_str(" SYSTEM \"");
                out.push_str(sys_id);
                out.push('"');
            }
            out.push_str(">\n");
        }
        NodeKind::ProcessingInstruction { target, data } => {
            out.push_str("<?");
            out.push_str(target);
            if let Some(d) = data {
                out.push(' ');
                out.push_str(d);
            }
            out.push('>');
        }
        _ => {
            for child in doc.children(id) {
                serialize_html5_node(doc, child, out);
            }
        }
    }
}

/// Escape text content for HTML5 output (always UTF-8).
fn write_html5_escaped_text(out: &mut String, text: &str) {
    for ch in text.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            _ => out.push(ch),
        }
    }
}

/// Escape an attribute value for HTML5 output.
fn write_html5_escaped_attr(out: &mut String, text: &str) {
    for ch in text.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '"' => out.push_str("&quot;"),
            _ => out.push(ch),
        }
    }
}

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

    // -- Void elements -------------------------------------------------------

    #[test]
    fn test_void_element_br() {
        let doc = parse_html("<html><body><br></body></html>").unwrap();
        let html = serialize_html(&doc);
        assert!(html.contains("<br>"), "expected <br>, got: {html}");
        assert!(!html.contains("<br/>"), "should not have <br/>");
        assert!(!html.contains("</br>"), "should not have </br>");
    }

    #[test]
    fn test_void_element_img_with_attr() {
        let doc = parse_html(r#"<html><body><img src="x.png"></body></html>"#).unwrap();
        let html = serialize_html(&doc);
        assert!(
            html.contains(r#"<img src="x.png">"#),
            "expected img with src, got: {html}"
        );
        assert!(!html.contains("</img>"), "void element should not close");
    }

    // -- Non-void elements ---------------------------------------------------

    #[test]
    fn test_non_void_empty_element() {
        let doc = parse_html("<html><body><p></p></body></html>").unwrap();
        let html = serialize_html(&doc);
        assert!(
            html.contains("<p></p>"),
            "expected <p></p>, not self-closing, got: {html}"
        );
    }

    // -- Raw text elements ---------------------------------------------------

    #[test]
    fn test_script_not_escaped() {
        let doc = parse_html("<html><body><script>if (a < b) {}</script></body></html>").unwrap();
        let html = serialize_html(&doc);
        assert!(
            html.contains("if (a < b) {}"),
            "script content should not be escaped, got: {html}"
        );
        assert!(
            !html.contains("&lt;"),
            "script content should not contain &lt;"
        );
    }

    #[test]
    fn test_style_not_escaped() {
        let doc = parse_html("<html><body><style>.a > .b {}</style></body></html>").unwrap();
        let html = serialize_html(&doc);
        assert!(
            html.contains(".a > .b {}"),
            "style content should not be escaped, got: {html}"
        );
        assert!(
            !html.contains("&gt;"),
            "style content should not contain &gt; inside style tag"
        );
    }

    // -- Attributes ----------------------------------------------------------

    #[test]
    fn test_boolean_attribute() {
        let doc = parse_html(r#"<html><body><input disabled="disabled"></body></html>"#).unwrap();
        let html = serialize_html(&doc);
        // Boolean attribute: when value == name, output without value
        assert!(
            html.contains("<input disabled>") || html.contains("<input disabled "),
            "expected boolean attr, got: {html}"
        );
    }

    #[test]
    fn test_regular_attribute_preserved() {
        let doc = parse_html(r#"<html><body><input type="text"></body></html>"#).unwrap();
        let html = serialize_html(&doc);
        assert!(
            html.contains(r#"type="text""#),
            "expected type=\"text\", got: {html}"
        );
    }

    #[test]
    fn test_multiple_attributes() {
        let doc = parse_html(
            r#"<html><body><input type="text" name="field" value="hello"></body></html>"#,
        )
        .unwrap();
        let html = serialize_html(&doc);
        assert!(html.contains(r#"type="text""#), "missing type attr");
        assert!(html.contains(r#"name="field""#), "missing name attr");
        assert!(html.contains(r#"value="hello""#), "missing value attr");
    }

    // -- Text escaping -------------------------------------------------------

    #[test]
    fn test_text_escaping() {
        let doc = parse_html("<html><body><p>a &amp; b &lt; c &gt; d</p></body></html>").unwrap();
        let html = serialize_html(&doc);
        // The serializer should re-escape special characters in text
        assert!(
            html.contains("&amp;") && html.contains("&lt;") && html.contains("&gt;"),
            "expected escaped entities in text, got: {html}"
        );
    }

    // -- Comments ------------------------------------------------------------

    #[test]
    fn test_comment_preserved() {
        let doc = parse_html("<html><body><!-- comment --></body></html>").unwrap();
        let html = serialize_html(&doc);
        assert!(
            html.contains("<!-- comment -->"),
            "comment should be preserved, got: {html}"
        );
    }

    // -- DOCTYPE -------------------------------------------------------------

    #[test]
    fn test_doctype_serialization() {
        let doc = parse_html(
            r#"<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><body></body></html>"#,
        )
        .unwrap();
        let html = serialize_html(&doc);
        assert!(
            html.contains("<!DOCTYPE html"),
            "expected DOCTYPE, got: {html}"
        );
        assert!(
            html.contains("PUBLIC"),
            "expected PUBLIC in DOCTYPE, got: {html}"
        );
    }

    // -- Charset / encoding --------------------------------------------------

    #[test]
    fn test_meta_charset_utf8() {
        let doc =
            parse_html(r#"<html><head><meta charset="utf-8"></head><body>caf&#233;</body></html>"#)
                .unwrap();
        let html = serialize_html(&doc);
        // With UTF-8 charset declared, non-ASCII should be preserved as UTF-8
        assert!(
            html.contains("café") || html.contains("caf"),
            "UTF-8 content should be preserved, got: {html}"
        );
    }

    // -- Block/inline formatting ---------------------------------------------

    #[test]
    fn test_inline_element_no_newlines() {
        let doc = parse_html("<html><body><p>Hello <span>world</span></p></body></html>").unwrap();
        let html = serialize_html(&doc);
        // Inline elements like span should not have extra formatting newlines
        assert!(
            html.contains("<span>world</span>"),
            "inline element should not have extra newlines, got: {html}"
        );
    }

    // -- Trailing newline ----------------------------------------------------

    #[test]
    fn test_trailing_newline() {
        let doc = parse_html("<html><body></body></html>").unwrap();
        let html = serialize_html(&doc);
        assert!(
            html.ends_with('\n'),
            "output should end with newline, got: {html:?}"
        );
    }

    // -- Nested elements -----------------------------------------------------

    #[test]
    fn test_nested_elements() {
        let doc =
            parse_html("<html><body><div><ul><li>one</li><li>two</li></ul></div></body></html>")
                .unwrap();
        let html = serialize_html(&doc);
        assert!(html.contains("<ul>"), "missing <ul>");
        assert!(html.contains("<li>one</li>"), "missing first li");
        assert!(html.contains("<li>two</li>"), "missing second li");
        assert!(html.contains("</ul>"), "missing </ul>");
    }

    // -- Entity references ---------------------------------------------------

    #[test]
    fn test_entity_ref_serialization() {
        // Build a document manually with an entity reference node
        let mut doc = Document::new();
        let root = doc.root();
        let html_id = doc.create_node(NodeKind::Element {
            name: "html".to_string(),
            prefix: None,
            namespace: None,
            attributes: vec![],
        });
        doc.append_child(root, html_id);
        let body_id = doc.create_node(NodeKind::Element {
            name: "body".to_string(),
            prefix: None,
            namespace: None,
            attributes: vec![],
        });
        doc.append_child(html_id, body_id);
        let entity_id = doc.create_node(NodeKind::EntityRef {
            name: "nbsp".to_string(),
            value: None,
        });
        doc.append_child(body_id, entity_id);
        let html = serialize_html(&doc);
        assert!(
            html.contains("&nbsp;"),
            "entity reference should be preserved, got: {html}"
        );
    }

    // -- Full document roundtrip ---------------------------------------------

    #[test]
    fn test_full_html_document() {
        let input =
            "<html><head><title>Test</title></head><body><h1>Hello</h1><p>World</p></body></html>";
        let doc = parse_html(input).unwrap();
        let html = serialize_html(&doc);
        assert!(html.contains("<html>"), "missing <html>");
        assert!(html.contains("<head>"), "missing <head>");
        assert!(html.contains("<title>Test</title>"), "missing title");
        assert!(html.contains("<body>"), "missing <body>");
        assert!(html.contains("Hello"), "missing h1 content");
        assert!(html.contains("World"), "missing p content");
        assert!(html.contains("</html>"), "missing </html>");
    }

    // -- URI attribute encoding ----------------------------------------------

    #[test]
    fn test_uri_attribute_space() {
        let doc = parse_html(r#"<html><body><a href="a b">link</a></body></html>"#).unwrap();
        let html = serialize_html(&doc);
        assert!(
            html.contains("a%20b"),
            "spaces in href should be encoded as %20, got: {html}"
        );
    }

    // -- Attribute with quotes -----------------------------------------------

    #[test]
    fn test_attr_with_quotes() {
        // Build manually to control the attribute value precisely
        let mut doc = Document::new();
        let root = doc.root();
        let html_id = doc.create_node(NodeKind::Element {
            name: "html".to_string(),
            prefix: None,
            namespace: None,
            attributes: vec![],
        });
        doc.append_child(root, html_id);
        let body_id = doc.create_node(NodeKind::Element {
            name: "body".to_string(),
            prefix: None,
            namespace: None,
            attributes: vec![],
        });
        doc.append_child(html_id, body_id);
        let div_id = doc.create_node(NodeKind::Element {
            name: "div".to_string(),
            prefix: None,
            namespace: None,
            attributes: vec![crate::tree::Attribute {
                name: "title".to_string(),
                value: "say \"hello\"".to_string(),
                prefix: None,
                namespace: None,
                raw_value: None,
            }],
        });
        doc.append_child(body_id, div_id);
        let html = serialize_html(&doc);
        // When value contains " and not ', should use single-quote delimiters
        assert!(
            html.contains("title='say \"hello\"'"),
            "expected single-quoted attr, got: {html}"
        );
    }

    // -- HTML5 serializer ----------------------------------------------------

    #[test]
    fn test_html5_basic_roundtrip() {
        let doc = crate::html5::parse_html5("<p>Hello</p>").unwrap();
        let html = serialize_html5(&doc);
        assert!(html.contains("<p>Hello</p>"), "got: {html}");
        assert!(html.contains("<html>"), "got: {html}");
    }

    #[test]
    fn test_html5_void_elements() {
        let doc = crate::html5::parse_html5("<br><hr><img src=\"x.png\">").unwrap();
        let html = serialize_html5(&doc);
        assert!(html.contains("<br>"), "got: {html}");
        assert!(!html.contains("</br>"), "void should not close: {html}");
        assert!(html.contains("<hr>"), "got: {html}");
        assert!(html.contains("<img"), "got: {html}");
    }

    #[test]
    fn test_html5_raw_text() {
        let doc = crate::html5::parse_html5("<script>if (a < b) {}</script>").unwrap();
        let html = serialize_html5(&doc);
        assert!(
            html.contains("if (a < b) {}"),
            "script content should not be escaped: {html}"
        );
    }

    #[test]
    fn test_html5_preserves_utf8() {
        let doc = crate::html5::parse_html5("<p>café</p>").unwrap();
        let html = serialize_html5(&doc);
        assert!(html.contains("café"), "UTF-8 should be preserved: {html}");
    }

    #[test]
    fn test_html5_foreign_self_closing() {
        let doc =
            crate::html5::parse_html5("<svg><circle cx=\"50\" cy=\"50\" r=\"40\"/></svg>").unwrap();
        let html = serialize_html5(&doc);
        assert!(html.contains("<circle"), "got: {html}");
        // Foreign empty elements should use self-closing syntax
        assert!(
            html.contains("/>"),
            "foreign empty element should self-close: {html}"
        );
    }
}