exml 0.7.3-deprecated

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
// 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,
    ops::{Deref, DerefMut},
    os::raw::c_void,
    ptr::{NonNull, null_mut},
};

use crate::{
    globals::{get_deregister_node_func, get_register_node_func},
    valid::{xml_add_id, xml_is_id, xml_remove_id},
};

use super::{
    InvalidNodePointerCastError, NodeCommon, XML_XML_NAMESPACE, XmlAttributeType, XmlDocPtr,
    XmlElementType, XmlGenericNodePtr, XmlNodePtr, XmlNsPtr, xml_free_node_list, xml_new_doc_text,
    xml_new_ns, xml_new_reconciled_ns, xml_ns_in_scope, xml_static_copy_node_list,
    xml_tree_err_memory,
};

#[repr(C)]
pub struct XmlAttr {
    pub _private: *mut c_void,                  /* application data */
    pub(crate) typ: XmlElementType,             /* XML_ATTRIBUTE_NODE, must be second ! */
    pub name: Box<str>,                         /* the name of the property */
    pub(crate) children: Option<XmlNodePtr>,    /* the value of the property */
    pub(crate) last: Option<XmlNodePtr>,        /* NULL */
    pub(crate) parent: Option<XmlNodePtr>,      /* child->parent link */
    pub next: Option<XmlAttrPtr>,               /* next sibling link  */
    pub(crate) prev: Option<XmlAttrPtr>,        /* previous sibling link  */
    pub(crate) doc: Option<XmlDocPtr>,          /* the containing document */
    pub ns: Option<XmlNsPtr>,                   /* pointer to the associated namespace */
    pub(crate) atype: Option<XmlAttributeType>, /* the attribute type if validating */
    pub(crate) psvi: *mut c_void,               /* for type/PSVI information */
}

impl XmlAttr {
    /// 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> {
        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 = self.parent.map(XmlGenericNodePtr::from);
        while let Some(now) = node {
            if matches!(
                now.element_type(),
                XmlElementType::XmlEntityRefNode
                    | XmlElementType::XmlEntityNode
                    | XmlElementType::XmlEntityDecl
            ) {
                return None;
            }
            if let Some(now) = XmlNodePtr::try_from(now)
                .ok()
                .filter(|now| now.element_type() == XmlElementType::XmlElementNode)
            {
                // let href = CString::new(href).unwrap();
                let mut cur = now.ns_def;
                while let Some(cur_ns) = cur {
                    if cur_ns.href.is_some()
                        && cur_ns.href().as_deref() == Some(href)
                        && cur_ns.prefix().is_some()
                        && xml_ns_in_scope(
                            doc,
                            XmlGenericNodePtr::from_raw(self as *mut Self),
                            Some(now.into()),
                            cur_ns.prefix.as_deref(),
                        ) == 1
                    {
                        return Some(cur_ns);
                    }
                    cur = cur_ns.next;
                }
                let cur = now.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,
                            XmlGenericNodePtr::from_raw(self as *mut Self),
                            Some(now.into()),
                            cur.prefix.as_deref(),
                        ) == 1
                }) {
                    return Some(cur);
                }
            }
            node = now.parent();
        }
        None
    }

    pub(super) fn get_prop_node_value_internal(&self) -> Option<String> {
        // Note that we return at least the empty string.
        // TODO: Do we really always want that?
        if let Some(children) = self.children() {
            if children.next().is_none()
                && matches!(
                    children.element_type(),
                    XmlElementType::XmlTextNode | XmlElementType::XmlCDATASectionNode
                )
            {
                let children = XmlNodePtr::try_from(children).unwrap();
                // Optimization for the common case: only 1 text node.
                return children.content.clone();
            } else if let Some(ret) = children.get_string(self.document(), 1) {
                return Some(ret);
            }
        }
        Some("".to_owned())
    }

    /// 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> {
        self.get_prop_node_value_internal()
    }

    /// 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::XmlAttributeNode
        ));
        let mut tmp = self.children();

        while let Some(now) = tmp {
            if matches!(now.element_type(), XmlElementType::XmlTextNode) {
                let now = XmlNodePtr::try_from(now).unwrap();
                buf.push_str(now.content.as_deref().unwrap());
            } else {
                now.get_content_to(buf);
            }
            tmp = now.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::tree::XML_XML_NAMESPACE;

        self.search_ns_by_href(self.document(), XML_XML_NAMESPACE);
    }

    /// update all nodes under the tree to point to the right document
    #[doc(alias = "xmlSetTreeDoc")]
    pub fn set_doc(&mut self, doc: Option<XmlDocPtr>) {
        if self.document() != doc {
            if let Some(children) = self.children() {
                children.set_doc_all_sibling(doc);
            }

            // FIXME: self.ns should be updated as in xmlStaticCopyNode().
            self.set_document(doc);
        }
    }
}

impl Default for XmlAttr {
    fn default() -> Self {
        Self {
            _private: null_mut(),
            typ: XmlElementType::XmlAttributeNode,
            name: "".into(),
            children: None,
            last: None,
            parent: None,
            next: None,
            prev: None,
            doc: None,
            ns: None,
            atype: None,
            psvi: null_mut(),
        }
    }
}

impl NodeCommon for XmlAttr {
    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))
    }
    fn children(&self) -> Option<XmlGenericNodePtr> {
        self.children.map(|children| children.into())
    }
    fn set_children(&mut self, children: Option<XmlGenericNodePtr>) {
        self.children = children.map(|children| XmlNodePtr::try_from(children).unwrap());
    }
    fn last(&self) -> Option<XmlGenericNodePtr> {
        self.last.map(|last| last.into())
    }
    fn set_last(&mut self, last: Option<XmlGenericNodePtr>) {
        self.last = last.map(|children| XmlNodePtr::try_from(children).unwrap());
    }
    fn next(&self) -> Option<XmlGenericNodePtr> {
        self.next.map(|next| next.into())
    }
    fn set_next(&mut self, next: Option<XmlGenericNodePtr>) {
        self.next = next.map(|next| XmlAttrPtr::try_from(next).unwrap())
    }
    fn prev(&self) -> Option<XmlGenericNodePtr> {
        self.prev.map(|prev| prev.into())
    }
    fn set_prev(&mut self, prev: Option<XmlGenericNodePtr>) {
        self.prev = prev.map(|prev| XmlAttrPtr::try_from(prev).unwrap())
    }
    fn parent(&self) -> Option<XmlGenericNodePtr> {
        self.parent.map(|parent| parent.into())
    }
    fn set_parent(&mut self, parent: Option<XmlGenericNodePtr>) {
        self.parent = parent.map(|children| XmlNodePtr::try_from(children).unwrap());
    }

    fn unlink(&mut self) {
        if let Some(mut parent) = self.parent {
            let attr = unsafe {
                // # Safety
                // Please see the document of `XmlAttrPtr::from_raw`.
                // In addition, this pointer is not leaked to the out of this function.
                XmlAttrPtr::from_raw(self).unwrap()
            };
            if parent.properties == attr {
                parent.properties = attr.and_then(|attr| attr.next);
            }
            self.set_parent(None);
        }
        if let Some(mut next) = self.next() {
            next.set_prev(self.prev());
        }
        if let Some(mut prev) = self.prev() {
            prev.set_next(self.next());
        }
        self.set_next(None);
        self.set_prev(None);
    }
}

#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct XmlAttrPtr(NonNull<XmlAttr>);

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

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

    // pub(crate) fn as_ptr(self) -> *mut XmlAttr {
    //     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<XmlAttr> {
    //     unsafe { Box::from_raw(self.0.as_ptr()) }
    // }

    /// Unlink and free one attribute, all the content is freed too.
    ///
    /// Note this doesn't work for namespace definition attributes.
    ///
    /// Returns 0 if success and -1 in case of error.
    #[doc(alias = "xmlRemoveProp")]
    pub unsafe fn remove_prop(self) -> i32 {
        unsafe {
            let Some(mut parent) = self
                .parent()
                .map(|parent| XmlNodePtr::try_from(parent).unwrap())
            else {
                return -1;
            };
            let mut tmp = parent.properties;
            if tmp == Some(self) {
                parent.properties = self.next;
                if let Some(mut next) = self.next {
                    next.prev = None;
                }
                xml_free_prop(self);
                return 0;
            }
            while let Some(mut now) = tmp {
                if now.next == Some(self) {
                    now.next = self.next;
                    if let Some(mut next) = now.next {
                        next.prev = Some(now);
                    }
                    xml_free_prop(self);
                    return 0;
                }
                tmp = now.next;
            }
            -1
        }
    }
}

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

impl Copy for XmlAttrPtr {}

impl Deref for XmlAttrPtr {
    type Target = XmlAttr;
    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 XmlAttr`.
        // Therefore, as long as the constructor is correctly implemented,
        // the pointer dereference is valid.
        unsafe { self.0.as_ref() }
    }
}

impl DerefMut for XmlAttrPtr {
    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 XmlAttr`.
        // Therefore, as long as the constructor is correctly implemented,
        // the pointer dereference is valid.
        unsafe { self.0.as_mut() }
    }
}

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

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

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

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

/// Create a new property carried by a document.  
/// Returns a pointer to the attribute
///
/// # NOTE
/// `value` is supposed to be a piece of XML CDATA, so it allows entity references,
/// but XML special chars need to be escaped first by using.  
/// xmlEncodeEntitiesReentrant(). Use xmlNewProp() if you don't need entities support.
#[doc(alias = "xmlNewDocProp")]
pub fn xml_new_doc_prop(
    doc: Option<XmlDocPtr>,
    name: &str,
    value: Option<&str>,
) -> Option<XmlAttrPtr> {
    // Allocate a new property and fill the fields.
    let Some(mut cur) = XmlAttrPtr::new(XmlAttr {
        typ: XmlElementType::XmlAttributeNode,
        name: name.into(),
        doc,
        ..Default::default()
    }) else {
        xml_tree_err_memory("building attribute");
        return None;
    };
    if let Some(value) = value {
        cur.children = doc.and_then(|doc| doc.get_node_list(value));
        cur.last = None;

        let mut tmp = cur.children();
        while let Some(mut now) = tmp {
            now.set_parent(Some(cur.into()));
            if now.next().is_none() {
                cur.set_last(Some(now));
            }
            tmp = now.next();
        }
    }

    if let Some(register) = get_register_node_func() {
        register(cur.into());
    }

    Some(cur)
}

pub(super) fn xml_new_prop_internal(
    node: Option<XmlNodePtr>,
    ns: Option<XmlNsPtr>,
    name: &str,
    value: Option<&str>,
) -> Option<XmlAttrPtr> {
    if node.is_some_and(|node| !matches!(node.element_type(), XmlElementType::XmlElementNode)) {
        return None;
    }

    // Allocate a new property and fill the fields.
    let Some(mut cur) = XmlAttrPtr::new(XmlAttr {
        typ: XmlElementType::XmlAttributeNode,
        parent: node,
        ns,
        name: name.into(),
        ..Default::default()
    }) else {
        xml_tree_err_memory("building attribute");
        return None;
    };

    let mut doc = None;
    if let Some(node) = node {
        doc = node.doc;
        cur.doc = doc;
    }

    if let Some(value) = value {
        cur.set_children(xml_new_doc_text(doc, Some(value)).map(|node| node.into()));
        cur.set_last(None);
        let mut tmp = cur.children();
        while let Some(mut now) = tmp {
            now.set_parent(Some(cur.into()));
            if now.next().is_none() {
                cur.set_last(Some(now));
            }
            tmp = now.next();
        }
    }

    // Add it at the end to preserve parsing order ...
    if let Some(mut node) = node {
        if let Some(mut prev) = node.properties {
            while let Some(next) = prev.next {
                prev = next;
            }
            prev.next = Some(cur);
            cur.prev = Some(prev);
        } else {
            node.properties = Some(cur);
        }
    }

    if let Some(value) = value {
        if let Some(node) = node {
            if xml_is_id(node.doc, Some(node), Some(cur)) == 1 {
                xml_add_id(None, node.doc.unwrap(), value, cur);
            }
        }
    }

    if let Some(register) = get_register_node_func() {
        register(cur.into());
    }

    Some(cur)
}

/// Create a new property carried by a node.  
/// Returns a pointer to the attribute
#[doc(alias = "xmlNewProp")]
#[cfg(any(feature = "libxml_tree", feature = "html", feature = "schema"))]
pub fn xml_new_prop(
    node: Option<XmlNodePtr>,
    name: &str,
    value: Option<&str>,
) -> Option<XmlAttrPtr> {
    xml_new_prop_internal(node, None, name, value)
}

/// Create a new property tagged with a namespace and carried by a node.  
/// Returns a pointer to the attribute
#[doc(alias = "xmlNewNsProp")]
pub fn xml_new_ns_prop(
    node: Option<XmlNodePtr>,
    ns: Option<XmlNsPtr>,
    name: &str,
    value: Option<&str>,
) -> Option<XmlAttrPtr> {
    xml_new_prop_internal(node, ns, name, value)
}

pub(super) unsafe fn xml_copy_prop_internal(
    doc: Option<XmlDocPtr>,
    target: Option<XmlNodePtr>,
    cur: XmlAttrPtr,
) -> Option<XmlAttrPtr> {
    unsafe {
        if target
            .is_some_and(|target| !matches!(target.element_type(), XmlElementType::XmlElementNode))
        {
            return None;
        }
        let mut ret = if let Some(target) = target {
            xml_new_doc_prop(target.doc, &cur.name, None)
        } else if let Some(doc) = doc {
            xml_new_doc_prop(Some(doc), &cur.name, None)
        } else if let Some(parent) = cur.parent() {
            xml_new_doc_prop(parent.document(), &cur.name, None)
        } else if let Some(children) = cur.children() {
            xml_new_doc_prop(children.document(), &cur.name, None)
        } else {
            xml_new_doc_prop(None, &cur.name, None)
        }?;
        ret.parent = target;

        if let Some((cur_ns, mut target)) = cur.ns.zip(target) {
            let prefix = cur_ns.prefix();
            let target_doc = target.doc;
            if let Some(ns) = target.search_ns(target_doc, prefix.as_deref()) {
                // we have to find something appropriate here since
                // we can't be sure, that the namespace we found is identified
                // by the prefix
                if ns.href == cur_ns.href {
                    // this is the nice case
                    ret.ns = Some(ns);
                } else {
                    // we are in trouble: we need a new reconciled namespace.
                    // This is expensive
                    ret.ns = xml_new_reconciled_ns(target.doc, target, cur_ns);
                }
            } else {
                // Humm, we are copying an element whose namespace is defined
                // out of the new tree scope. Search it in the original tree
                // and add it at the top of the new tree
                if let Some(ns) = cur.parent().unwrap().search_ns(cur.doc, prefix.as_deref()) {
                    let mut root = XmlGenericNodePtr::from(target);
                    let mut pred = None;

                    while let Some(parent) = root.parent() {
                        pred = Some(root);
                        root = parent;
                    }
                    ret.ns = if Some(root) == target.doc.map(|doc| doc.into()) {
                        // correct possibly cycling above the document elt
                        xml_new_ns(
                            pred.map(|p| XmlNodePtr::try_from(p).unwrap()),
                            ns.href.as_deref(),
                            ns.prefix().as_deref(),
                        )
                    } else {
                        xml_new_ns(
                            Some(XmlNodePtr::try_from(root).unwrap()),
                            ns.href.as_deref(),
                            ns.prefix().as_deref(),
                        )
                    };
                }
            }
        } else {
            ret.ns = None;
        }

        if let Some(children) = cur.children() {
            let doc = ret.doc;
            let parent = Some(ret.into());
            ret.set_children(xml_static_copy_node_list(Some(children), doc, parent));
            ret.set_last(None);
            let mut tmp = ret.children();
            while let Some(now) = tmp {
                // (*tmp).parent = ret;
                if now.next().is_none() {
                    ret.set_last(Some(now));
                }
                tmp = now.next();
            }
        }
        // Try to handle IDs
        if let Some(target_doc) = target.and_then(|target| target.doc).filter(|_| {
            cur.doc.is_some()
                && cur
                    .parent
                    .filter(|&p| xml_is_id(cur.doc, Some(p), Some(cur)) != 0)
                    .is_some()
        }) {
            let children = cur.children();
            if let Some(id) = children.and_then(|c| c.get_string(cur.doc, 1)) {
                xml_add_id(None, target_doc, &id, ret);
            }
        }
        Some(ret)
    }
}

/// Do a copy of the attribute.
///
/// Returns: a new #xmlAttrPtr, or null_mut() in case of error.
#[doc(alias = "xmlCopyProp")]
pub unsafe fn xml_copy_prop(target: Option<XmlNodePtr>, cur: XmlAttrPtr) -> Option<XmlAttrPtr> {
    unsafe { xml_copy_prop_internal(None, target, cur) }
}

/// Do a copy of an attribute list.
///
/// Returns: a new #xmlAttrPtr, or null_mut() in case of error.
#[doc(alias = "xmlCopyPropList")]
pub unsafe fn xml_copy_prop_list(
    target: Option<XmlNodePtr>,
    mut cur: Option<XmlAttrPtr>,
) -> Option<XmlAttrPtr> {
    unsafe {
        if target
            .is_some_and(|target| !matches!(target.element_type(), XmlElementType::XmlElementNode))
        {
            return None;
        }
        let mut ret = None;
        let mut p = None::<XmlAttrPtr>;
        while let Some(now) = cur {
            let Some(mut q) = xml_copy_prop(target, now) else {
                xml_free_prop_list(ret);
                return None;
            };
            if let Some(mut np) = p {
                np.next = Some(q);
                q.prev = Some(np);
                p = Some(q);
            } else {
                ret = Some(q);
                p = Some(q);
            }
            cur = now.next;
        }
        ret
    }
}

/// Free one attribute, all the content is freed too
#[doc(alias = "xmlFreeProp")]
pub unsafe fn xml_free_prop(cur: XmlAttrPtr) {
    unsafe {
        if let Some(deregister) = get_deregister_node_func() {
            deregister(cur.into());
        }

        // Check for ID removal -> leading to invalid references !
        if let Some(doc) = cur
            .doc
            .filter(|_| matches!(cur.atype, Some(XmlAttributeType::XmlAttributeID)))
        {
            xml_remove_id(doc, cur);
        }
        if let Some(children) = cur.children() {
            xml_free_node_list(Some(children));
        }
        cur.free();
    }
}

/// Free a property and all its siblings, all the children are freed too.
#[doc(alias = "xmlFreePropList")]
pub unsafe fn xml_free_prop_list(cur: Option<XmlAttrPtr>) {
    unsafe {
        if let Some(cur) = cur {
            let mut next = cur.next;
            xml_free_prop(cur);
            while let Some(now) = next {
                next = now.next;
                xml_free_prop(now);
            }
        }
    }
}