exml 0.7.2

Pure Rust XML library based on libxml2
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
// Copyright of the original code is the following.
// --------
// Summary: interfaces for tree manipulation
// Description: this module describes the structures found in an tree resulting
//              from an XML or HTML parsing, as well as the API provided for
//              various processing on that tree
//
// Copy: See Copyright for the status of this software.
//
// Author: Daniel Veillard
// --------
// tree.c : implementation of access function for an XML tree.
//
// References:
//   XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
//
// See Copyright for the status of this software.
//
// daniel@veillard.com

use std::{
    any::type_name,
    borrow::Cow,
    collections::HashMap,
    ops::{Deref, DerefMut},
    os::raw::c_void,
    ptr::{NonNull, null_mut},
};

use crate::{
    encoding::XmlCharEncoding,
    error::XmlParserErrors,
    globals::{get_deregister_node_func, get_register_node_func},
    list::XmlList,
};

use super::{
    InvalidNodePointerCastError, NodeCommon, XML_ENT_EXPANDING, XML_ENT_PARSED,
    XML_LOCAL_NAMESPACE, XML_XML_NAMESPACE, XmlDocProperties, XmlDtdPtr, XmlElementType,
    XmlEntityType, XmlGenericNodePtr, XmlID, XmlNodePtr, XmlNs, XmlNsPtr, XmlRef, xml_free_dtd,
    xml_free_node_list, xml_free_ns_list, xml_get_doc_entity, xml_new_doc_text, xml_new_reference,
    xml_ns_in_scope, xml_tree_err, xml_tree_err_memory,
};

#[repr(C)]
pub struct XmlDoc {
    pub _private: *mut c_void,                  /* application data */
    pub(crate) typ: XmlElementType,             /* XML_DOCUMENT_NODE, must be second ! */
    pub(crate) name: Cow<'static, str>,         /* name/filename/URI of the document */
    pub children: Option<XmlGenericNodePtr>,    /* the document tree */
    pub(crate) last: Option<XmlGenericNodePtr>, /* last child link */
    pub(crate) parent: Option<XmlDocPtr>,       /* child->parent link */
    pub(crate) next: Option<XmlDocPtr>,         /* next sibling link  */
    pub(crate) prev: Option<XmlDocPtr>,         /* previous sibling link  */
    pub(crate) doc: Option<XmlDocPtr>,          /* autoreference to itself */

    /* End of common part */
    // standalone document (no external refs)
    //   1 if standalone="yes"
    //   0 if standalone="no"
    //  -1 if there is no XML declaration
    //  -2 if there is an XML declaration, but no
    //  standalone attribute was specified
    pub(crate) standalone: i32,
    // the document internal subset
    pub int_subset: Option<XmlDtdPtr>,
    // the document external subset
    pub(crate) ext_subset: Option<XmlDtdPtr>,
    // Global namespace, the old way
    pub(crate) old_ns: Option<XmlNsPtr>,
    // the XML version string
    pub(crate) version: Option<String>,
    // external initial encoding, if any
    pub(crate) encoding: Option<String>,
    // Hash table for ID attributes if any
    pub(crate) ids: HashMap<String, XmlID>,
    // Hash table for IDREFs attributes if any
    pub(crate) refs: HashMap<String, XmlList<Box<XmlRef>>>,
    // The URI for that document
    pub(crate) url: Option<String>,
    // Internal flag for charset handling, actually an xmlCharEncoding
    pub(crate) charset: XmlCharEncoding,
    // `dict` confuses me very much about the lifetime of the string...
    // I believe it is incompatible with the lifetime of Rust objects, so I removed it.
    // pub dict: *mut XmlDict,       /* dict used to allocate names or NULL */
    pub(crate) psvi: *mut c_void, /* for type/PSVI information */
    // set of xmlParserOption used to parse the document
    pub(crate) parse_flags: i32,
    // set of xmlDocProperties for this document
    // set at the end of parsing
    pub properties: i32,
}

impl XmlDoc {
    /// Search a Ns aliasing a given URI.
    /// Recurse on the parents until it finds the defined namespace or return NULL otherwise.
    ///
    /// Returns the namespace pointer or NULL.
    #[doc(alias = "xmlSearchNsByHref")]
    pub fn search_ns_by_href(&mut self, doc: Option<XmlDocPtr>, href: &str) -> Option<XmlNsPtr> {
        let orig = XmlGenericNodePtr::from_raw(self).unwrap();

        if href == XML_XML_NAMESPACE {
            let mut doc = doc.or(self.document())?;
            // Return the XML namespace declaration held by the doc.
            if doc.old_ns.is_none() {
                return doc.ensure_xmldecl();
            } else {
                return doc.old_ns;
            }
        }
        let mut node = Some(orig);
        while let Some(cur_node) = node {
            if matches!(
                cur_node.element_type(),
                XmlElementType::XmlEntityRefNode
                    | XmlElementType::XmlEntityNode
                    | XmlElementType::XmlEntityDecl
            ) {
                return None;
            }
            if matches!(cur_node.element_type(), XmlElementType::XmlElementNode) {
                let cur_node = XmlNodePtr::try_from(cur_node).unwrap();
                let mut cur = cur_node.ns_def;
                while let Some(now) = cur {
                    if now.href.as_deref().is_some_and(|h| h == href)
                        && now.prefix().is_some()
                        && xml_ns_in_scope(doc, Some(orig), node, now.prefix.as_deref()) == 1
                    {
                        return Some(now);
                    }
                    cur = now.next;
                }
                if orig != XmlGenericNodePtr::from(cur_node) {
                    let cur = cur_node.ns;
                    if let Some(cur) = cur.filter(|cur| {
                        cur.href.as_deref().is_some_and(|h| h == href)
                            && cur.prefix().is_some()
                            && xml_ns_in_scope(doc, Some(orig), node, cur.prefix.as_deref()) == 1
                    }) {
                        return Some(cur);
                    }
                }
            }
            node = cur_node.parent();
        }
        None
    }

    /// Get the internal subset of a document
    /// Returns a pointer to the DTD structure or null_mut() if not found
    #[doc(alias = "xmlGetIntSubset")]
    pub fn get_int_subset(&self) -> Option<XmlDtdPtr> {
        let mut cur = self.children();
        while let Some(now) = cur {
            if matches!(now.element_type(), XmlElementType::XmlDTDNode) {
                return Some(XmlDtdPtr::try_from(now).unwrap());
            }
            cur = now.next();
        }
        self.int_subset
    }

    /// Get the root element of the document  
    /// (self.children is a list containing possibly comments, PIs, etc ...).
    ///
    /// Returns the `XmlNodePtr` for the root or NULL
    #[doc(alias = "xmlDocGetRootElement")]
    pub fn get_root_element(&self) -> Option<XmlNodePtr> {
        let mut ret = self.children();
        while let Some(now) = ret {
            if matches!(now.element_type(), XmlElementType::XmlElementNode) {
                return Some(XmlNodePtr::try_from(now).unwrap());
            }
            ret = now.next();
        }
        None
    }

    /// Read the value of a node, this can be either the text carried
    /// directly by this node if it's a TEXT node or the aggregate string
    /// of the values carried by this node child's (TEXT and ENTITY_REF).  
    ///
    /// Entity references are substituted.
    ///
    /// Returns a new #XmlChar * or null_mut() if no content is available.  
    /// It's up to the caller to free the memory with xml_free().
    #[doc(alias = "xmlNodeGetContent")]
    pub fn get_content(&self) -> Option<String> {
        assert!(matches!(
            self.element_type(),
            XmlElementType::XmlDocumentNode | XmlElementType::XmlHTMLDocumentNode
        ));
        let mut buf = String::new();
        self.get_content_to(&mut buf);
        Some(buf)
    }

    /// Read the value of a node `cur`, this can be either the text carried
    /// directly by this node if it's a TEXT node or the aggregate string
    /// of the values carried by this node child's (TEXT and ENTITY_REF).
    ///
    /// Entity references are substituted. Fills up the buffer `buf` with this value.
    ///
    /// Returns 0 in case of success and -1 in case of error.
    #[doc(alias = "xmlBufGetNodeContent")]
    pub fn get_content_to(&self, buf: &mut String) -> i32 {
        assert!(matches!(
            self.element_type(),
            XmlElementType::XmlDocumentNode | XmlElementType::XmlHTMLDocumentNode
        ));
        let mut next = self.children();
        while let Some(cur) = next {
            if matches!(
                cur.element_type(),
                XmlElementType::XmlElementNode
                    | XmlElementType::XmlTextNode
                    | XmlElementType::XmlCDATASectionNode
            ) {
                cur.get_content_to(buf);
            }
            next = cur.next();
        }
        0
    }

    /// Set (or reset) the base URI of a node, i.e. the value of the xml:base attribute.
    #[doc(alias = "xmlNodeSetBase")]
    #[cfg(any(feature = "libxml_tree", feature = "xinclude"))]
    pub fn set_base(&mut self, uri: Option<&str>) {
        use crate::uri::path_to_uri;

        let url = uri.map(path_to_uri);
        self.url = url.map(|url| url.into_owned());
    }

    /// Set the root element of the document.
    /// (self.children is a list containing possibly comments, PIs, etc ...).
    ///
    /// Returns the old root element if any was found, NULL if root was NULL
    #[doc(alias = "xmlDocSetRootElement")]
    #[cfg(any(feature = "libxml_tree", feature = "libxml_writer"))]
    pub unsafe fn set_root_element(&mut self, mut root: XmlNodePtr) -> Option<XmlNodePtr> {
        unsafe {
            use crate::tree::{NodeCommon, xml_replace_node};

            root.unlink();
            root.set_doc(XmlDocPtr::from_raw(self).unwrap());
            root.set_parent(XmlGenericNodePtr::from_raw(self));
            let mut old = self.children();
            while let Some(now) = old {
                if matches!(now.element_type(), XmlElementType::XmlElementNode) {
                    break;
                }
                old = now.next();
            }
            if let Some(old) = old {
                xml_replace_node(old, Some(XmlGenericNodePtr::from(root)));
            } else if let Some(children) = self.children() {
                children.add_sibling(XmlGenericNodePtr::from(root));
            } else {
                self.set_children(Some(root.into()));
                self.set_last(Some(root.into()));
            }
            old.map(|o| XmlNodePtr::try_from(o).unwrap())
        }
    }

    /// Ensures that there is an XML namespace declaration on the doc.
    ///
    /// Returns the XML ns-struct or null_mut() on API and internal errors.
    #[doc(alias = "xmlTreeEnsureXMLDecl")]
    pub(super) fn ensure_xmldecl(&mut self) -> Option<XmlNsPtr> {
        if let Some(old_ns) = self.old_ns {
            return Some(old_ns);
        }
        let Some(ns) = XmlNsPtr::new(XmlNs {
            typ: XML_LOCAL_NAMESPACE,
            href: Some(XML_XML_NAMESPACE.into()),
            prefix: Some("xml".into()),
            ..Default::default()
        }) else {
            xml_tree_err_memory("allocating the XML namespace");
            return None;
        };
        self.old_ns = Some(ns);
        Some(ns)
    }
}

impl NodeCommon for XmlDoc {
    fn document(&self) -> Option<XmlDocPtr> {
        self.doc
    }
    fn set_document(&mut self, doc: Option<XmlDocPtr>) {
        self.doc = doc;
    }
    fn element_type(&self) -> XmlElementType {
        self.typ
    }
    fn name(&self) -> Option<Cow<'_, str>> {
        Some(Cow::Borrowed(self.name.as_ref()))
    }
    fn children(&self) -> Option<XmlGenericNodePtr> {
        self.children
    }
    fn set_children(&mut self, children: Option<XmlGenericNodePtr>) {
        self.children = children;
    }
    fn last(&self) -> Option<XmlGenericNodePtr> {
        self.last
    }
    fn set_last(&mut self, last: Option<XmlGenericNodePtr>) {
        self.last = last;
    }
    fn next(&self) -> Option<XmlGenericNodePtr> {
        self.next.map(|next| next.into())
    }
    fn set_next(&mut self, next: Option<XmlGenericNodePtr>) {
        self.next = next.map(|node| XmlDocPtr::try_from(node).unwrap())
    }
    fn prev(&self) -> Option<XmlGenericNodePtr> {
        self.prev.map(|prev| prev.into())
    }
    fn set_prev(&mut self, prev: Option<XmlGenericNodePtr>) {
        self.prev = prev.map(|node| XmlDocPtr::try_from(node).unwrap())
    }
    fn parent(&self) -> Option<XmlGenericNodePtr> {
        self.parent.map(|parent| parent.into())
    }
    fn set_parent(&mut self, parent: Option<XmlGenericNodePtr>) {
        self.parent = parent.map(|node| XmlDocPtr::try_from(node).unwrap());
    }
}

impl Default for XmlDoc {
    fn default() -> Self {
        Self {
            _private: null_mut(),
            typ: XmlElementType::XmlDocumentNode,
            name: "".into(),
            children: None,
            last: None,
            parent: None,
            next: None,
            prev: None,
            doc: None,
            standalone: 0,
            int_subset: None,
            ext_subset: None,
            old_ns: None,
            version: None,
            encoding: None,
            ids: HashMap::new(),
            refs: HashMap::new(),
            url: None,
            charset: XmlCharEncoding::None,
            psvi: null_mut(),
            parse_flags: 0,
            properties: 0,
        }
    }
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct XmlDocPtr(NonNull<XmlDoc>);

impl XmlDocPtr {
    /// Allocate new memory and create new `XmlDocPtr` from an owned xml node.
    ///
    /// This method leaks allocated memory.  
    /// Users can use `free` method for deallocating memory.
    pub(crate) fn new(node: XmlDoc) -> Option<Self> {
        let boxed = Box::new(node);
        NonNull::new(Box::leak(boxed)).map(Self)
    }

    /// Create `XmlDocPtr` from a raw pointer.  
    ///
    /// If `ptr` is a NULL pointer, return `Ok(None)`.  
    /// If `ptr` is a valid pointer of `XmlDoc`, return `Ok(Some(Self))`.  
    /// Otherwise, return `Err`.
    ///
    /// # Safety
    /// - `ptr` must be a pointer of types that is implemented `NodeCommon` at least.
    ///
    /// # TODO
    /// - fix to private mathod
    pub unsafe fn from_raw(ptr: *mut XmlDoc) -> Result<Option<Self>, InvalidNodePointerCastError> {
        unsafe {
            if ptr.is_null() {
                return Ok(None);
            }
            match (*ptr).element_type() {
                XmlElementType::XmlDocumentNode | XmlElementType::XmlHTMLDocumentNode => {
                    Ok(Some(Self(NonNull::new_unchecked(ptr))))
                }
                _ => Err(InvalidNodePointerCastError {
                    from: (*ptr).element_type(),
                    to: type_name::<Self>(),
                }),
            }
        }
    }

    // pub(crate) fn as_ptr(self) -> *mut XmlDoc {
    //     self.0.as_ptr()
    // }

    /// Deallocate memory.
    ///
    /// # Safety
    /// This method should be called only once.  
    /// If called more than twice, the behavior is undefined.
    pub(crate) unsafe fn free(self) {
        unsafe {
            let _ = *Box::from_raw(self.0.as_ptr());
        }
    }

    // /// Acquire the ownership of the inner value.
    // /// As a result, `self` will be invalid. `self` must not be used after performs this method.
    // ///
    // /// # Safety
    // /// This method should be called only once.
    // /// If called more than twice, the behavior is undefined.
    // pub(crate) unsafe fn into_inner(self) -> Box<XmlDoc> {
    //     unsafe { Box::from_raw(self.0.as_ptr()) }
    // }

    /// Parse the value string and build the node list associated.  
    /// Should produce a flat tree with only TEXTs and ENTITY_REFs.
    ///
    /// Returns a pointer to the first child.
    #[doc(alias = "xmlStringGetNodeList")]
    pub fn get_node_list(self, value: &str) -> Option<XmlNodePtr> {
        let mut head: Option<XmlNodePtr> = None;
        let mut last: Option<XmlNodePtr> = None;
        let mut buf = String::new();
        let mut cur = value;
        let mut q = cur;
        while !cur.is_empty() {
            if cur.starts_with('&') {
                let mut charval = 0;

                // Save the current text.
                if cur.len() != q.len() {
                    let len = q.len() - cur.len();
                    buf.push_str(&q[..len]);
                }

                if cur[1..].starts_with("#x") {
                    cur = &cur[3..];
                    if let Some((value, rem)) = cur.split_once(';').and_then(|(head, tail)| {
                        u32::from_str_radix(head, 16).ok().map(|val| (val, tail))
                    }) {
                        charval = value;
                        cur = rem;
                    } else {
                        xml_tree_err(XmlParserErrors::XmlTreeInvalidHex, Some(self.into()), None);
                        charval = 0;
                    };
                    q = cur;
                } else if cur[1..].starts_with('#') {
                    cur = &cur[2..];
                    if let Some((value, rem)) = cur
                        .split_once(';')
                        .and_then(|(head, tail)| head.parse::<u32>().ok().map(|val| (val, tail)))
                    {
                        charval = value;
                        cur = rem;
                    } else {
                        xml_tree_err(XmlParserErrors::XmlTreeInvalidDec, Some(self.into()), None);
                        charval = 0;
                    };
                    q = cur;
                } else {
                    // Read the entity string
                    cur = &cur[1..];
                    q = cur;
                    let Some((entity, rem)) = cur.split_once(';') else {
                        xml_tree_err(
                            XmlParserErrors::XmlTreeUnterminatedEntity,
                            Some(self.into()),
                            Some(q),
                        );
                        if let Some(head) = head {
                            unsafe {
                                // # Safety
                                // This node list is generated in this function,
                                // and is not still committed in any outer data.
                                // Therefore, this operation is safe.
                                xml_free_node_list(Some(head));
                            }
                        }
                        return None;
                    };
                    if !entity.is_empty() {
                        // Predefined entities don't generate nodes
                        let ent = xml_get_doc_entity(Some(self), entity);
                        if let Some(ent) = ent.filter(|ent| {
                            matches!(ent.etype, XmlEntityType::XmlInternalPredefinedEntity)
                        }) {
                            buf.push_str(ent.content.as_deref().unwrap());
                        } else {
                            // Flush buffer so far
                            if !buf.is_empty() {
                                let Some(mut node) = xml_new_doc_text(Some(self), None) else {
                                    if let Some(head) = head {
                                        unsafe {
                                            // # Safety
                                            // This node list is generated in this function,
                                            // and is not still committed in any outer data.
                                            // Therefore, this operation is safe.
                                            xml_free_node_list(Some(head));
                                        }
                                    }
                                    return None;
                                };
                                node.content.get_or_insert_default().push_str(&buf);
                                buf.clear();

                                if let Some(mut l) = last {
                                    unsafe {
                                        // # Safety
                                        // `node` is no longer used in this scope.
                                        last = l
                                            .add_next_sibling(node.into())
                                            .map(|node| XmlNodePtr::try_from(node).unwrap());
                                    }
                                } else {
                                    last = Some(node);
                                    head = Some(node);
                                }
                            }

                            // Create a new REFERENCE_REF node
                            let Some(node) = xml_new_reference(Some(self), entity) else {
                                if let Some(head) = head {
                                    unsafe {
                                        // # Safety
                                        // This node list is generated in this function,
                                        // and is not still committed in any outer data.
                                        // Therefore, this operation is safe.
                                        xml_free_node_list(Some(head));
                                    }
                                }
                                return None;
                            };
                            if let Some(mut ent) = ent.filter(|ent| {
                                ent.flags & XML_ENT_PARSED as i32 == 0
                                    && ent.flags & XML_ENT_EXPANDING as i32 == 0
                            }) {
                                // The entity should have been checked already,
                                // but set the flag anyway to avoid recursion.
                                ent.flags |= XML_ENT_EXPANDING as i32;
                                let content = node.content.as_deref().unwrap();
                                ent.set_children(
                                    self.get_node_list(content).map(|node| node.into()),
                                );
                                ent.owner = 1;
                                ent.flags &= !XML_ENT_EXPANDING as i32;
                                ent.flags |= XML_ENT_PARSED as i32;
                                let mut temp = ent.children();
                                while let Some(mut now) = temp {
                                    now.set_parent(Some(ent.into()));
                                    ent.set_last(Some(now));
                                    temp = now.next();
                                }
                            }
                            if let Some(mut l) = last {
                                unsafe {
                                    // # Safety
                                    // `node` is no longer used in this scope.
                                    last = l
                                        .add_next_sibling(node.into())
                                        .map(|node| XmlNodePtr::try_from(node).unwrap());
                                }
                            } else {
                                last = Some(node);
                                head = Some(node);
                            }
                        }
                    }
                    cur = rem;
                    q = cur;
                }
                if charval != 0 {
                    if let Some(c) = char::from_u32(charval) {
                        buf.push(c);
                    }
                }
            } else {
                let c = cur.chars().next().unwrap();
                cur = &cur[c.len_utf8()..];
            }
        }
        if cur != q || head.is_none() {
            // Handle the last piece of text.
            let len = q.len() - cur.len();
            buf.push_str(&q[..len]);
        }

        if !buf.is_empty() {
            let Some(mut node) = xml_new_doc_text(Some(self), None) else {
                if let Some(head) = head {
                    unsafe {
                        // # Safety
                        // This node list is generated in this function,
                        // and is not still committed in any outer data.
                        // Therefore, this operation is safe.
                        xml_free_node_list(Some(head));
                    }
                }
                return None;
            };
            node.content = Some(buf);

            if let Some(mut last) = last {
                unsafe {
                    // # Safety
                    // `node` is no longer used in this scope.
                    last.add_next_sibling(node.into());
                }
            } else {
                head = Some(node);
            }
        }

        head
    }
}

impl Clone for XmlDocPtr {
    fn clone(&self) -> Self {
        *self
    }
}

impl Copy for XmlDocPtr {}

impl Deref for XmlDocPtr {
    type Target = XmlDoc;
    fn deref(&self) -> &Self::Target {
        // # Safety
        // I don't implement the pointer casting and addition/subtraction methods
        // and don't expose the inner `NonNull` for `*mut XmlDoc`.
        // Therefore, as long as the constructor is correctly implemented,
        // the pointer dereference is valid.
        unsafe { self.0.as_ref() }
    }
}

impl DerefMut for XmlDocPtr {
    fn deref_mut(&mut self) -> &mut Self::Target {
        // # Safety
        // I don't implement the pointer casting and addition/subtraction methods
        // and don't expose the inner `NonNull` for `*mut XmlDoc`.
        // Therefore, as long as the constructor is correctly implemented,
        // the pointer dereference is valid.
        unsafe { self.0.as_mut() }
    }
}

impl TryFrom<XmlGenericNodePtr> for XmlDocPtr {
    type Error = InvalidNodePointerCastError;

    fn try_from(value: XmlGenericNodePtr) -> Result<Self, Self::Error> {
        match value.element_type() {
            XmlElementType::XmlDocumentNode | XmlElementType::XmlHTMLDocumentNode => {
                Ok(Self(value.0.cast()))
            }
            _ => Err(InvalidNodePointerCastError {
                from: value.element_type(),
                to: type_name::<Self>(),
            }),
        }
    }
}

impl From<XmlDocPtr> for XmlGenericNodePtr {
    fn from(value: XmlDocPtr) -> Self {
        Self(value.0 as NonNull<dyn NodeCommon>)
    }
}

impl From<XmlDocPtr> for *mut XmlDoc {
    fn from(value: XmlDocPtr) -> Self {
        value.0.as_ptr()
    }
}

/// Creates a new XML document
///
/// Returns a new document
#[doc(alias = "xmlNewDoc")]
pub fn xml_new_doc(version: Option<&str>) -> Option<XmlDocPtr> {
    let version = version.unwrap_or("1.0");

    // Allocate a new document and fill the fields.
    let Some(mut cur) = XmlDocPtr::new(XmlDoc {
        typ: XmlElementType::XmlDocumentNode,
        version: Some(version.to_owned()),
        standalone: -1,
        parse_flags: 0,
        properties: XmlDocProperties::XmlDocUserbuilt as i32,
        // The in memory encoding is always UTF8
        // This field will never change and would
        // be obsolete if not for binary compatibility.
        charset: XmlCharEncoding::UTF8,
        ..Default::default()
    }) else {
        xml_tree_err_memory("building doc");
        return None;
    };
    cur.doc = Some(cur);
    if let Some(register) = get_register_node_func() {
        register(cur.into());
    }

    Some(cur)
}

/// Do a copy of the document info. If recursive, the content tree will
/// be copied too as well as DTD, namespaces and entities.
///
/// Returns: a new #xmlDocPtr, or null_mut() in case of error.
#[doc(alias = "xmlCopyDoc")]
#[cfg(any(feature = "libxml_tree", feature = "schema"))]
pub unsafe fn xml_copy_doc(doc: XmlDocPtr, recursive: i32) -> Option<XmlDocPtr> {
    unsafe {
        use crate::tree::{xml_copy_dtd, xml_copy_namespace_list, xml_static_copy_node_list};

        let mut ret = xml_new_doc(doc.version.as_deref())?;
        ret.typ = doc.typ;
        ret.name = doc.name.clone();
        ret.encoding = doc.encoding.clone();
        if let Some(url) = doc.url.as_deref() {
            ret.url = Some(url.to_owned());
        }
        ret.charset = doc.charset;
        ret.standalone = doc.standalone;
        if recursive == 0 {
            return Some(ret);
        }

        ret.last = None;
        ret.children = None;
        #[cfg(feature = "libxml_tree")]
        if let Some(doc_int_subset) = doc.int_subset {
            ret.int_subset = xml_copy_dtd(doc_int_subset);
            let Some(mut ret_int_subset) = ret.int_subset else {
                xml_free_doc(ret);
                return None;
            };
            ret_int_subset.set_doc(Some(ret));
            ret_int_subset.parent = Some(ret);
        }
        if doc.old_ns.is_some() {
            ret.old_ns = xml_copy_namespace_list(doc.old_ns);
        }
        if let Some(children) = doc.children {
            ret.children = xml_static_copy_node_list(Some(children), Some(ret), Some(ret.into()));
            ret.last = None;
            let mut tmp = ret.children();
            while let Some(now) = tmp {
                if now.next().is_none() {
                    ret.set_last(Some(now));
                }
                tmp = now.next();
            }
        }
        Some(ret)
    }
}

/// Free up all the structures used by a document, tree included.
#[doc(alias = "xmlFreeDoc")]
pub unsafe fn xml_free_doc(mut cur: XmlDocPtr) {
    unsafe {
        if let Some(deregister) = get_deregister_node_func() {
            deregister(cur.into());
        }

        // Do this before freeing the children list to avoid ID lookups
        let mut ext_subset = cur.ext_subset.take();
        let int_subset = cur.int_subset.take();
        if int_subset == ext_subset {
            ext_subset = None;
        }
        if let Some(mut ext_subset) = ext_subset {
            ext_subset.unlink();
            xml_free_dtd(ext_subset);
        }
        if let Some(mut int_subset) = int_subset {
            int_subset.unlink();
            xml_free_dtd(int_subset);
        }

        if let Some(children) = cur.children() {
            xml_free_node_list(Some(children));
        }
        if let Some(old_ns) = cur.old_ns.take() {
            xml_free_ns_list(old_ns);
        }

        cur.free();
    }
}