1 /*
2  * Copyright (C) 2018-2021 Alexander Borisov
3  *
4  * Author: Alexander Borisov <borisov@lexbor.com>
5  */
6 
7 #include "lexbor/dom/interfaces/processing_instruction.h"
8 #include "lexbor/dom/interfaces/document.h"
9 
10 
11 lxb_dom_processing_instruction_t *
lxb_dom_processing_instruction_interface_create(lxb_dom_document_t * document)12 lxb_dom_processing_instruction_interface_create(lxb_dom_document_t *document)
13 {
14     lxb_dom_processing_instruction_t *element;
15 
16     element = lexbor_mraw_calloc(document->mraw,
17                                  sizeof(lxb_dom_processing_instruction_t));
18     if (element == NULL) {
19         return NULL;
20     }
21 
22     lxb_dom_node_t *node = lxb_dom_interface_node(element);
23 
24     node->owner_document = lxb_dom_document_owner(document);
25     node->type = LXB_DOM_NODE_TYPE_PROCESSING_INSTRUCTION;
26 
27     return element;
28 }
29 
30 lxb_dom_processing_instruction_t *
lxb_dom_processing_instruction_interface_clone(lxb_dom_document_t * document,const lxb_dom_processing_instruction_t * pinstr)31 lxb_dom_processing_instruction_interface_clone(lxb_dom_document_t *document,
32                                                const lxb_dom_processing_instruction_t *pinstr)
33 {
34     lxb_status_t status;
35     lxb_dom_processing_instruction_t *new;
36 
37     new = lxb_dom_processing_instruction_interface_create(document);
38     if (new == NULL) {
39         return NULL;
40     }
41 
42     status = lxb_dom_processing_instruction_copy(new, pinstr);
43     if (status != LXB_STATUS_OK) {
44         return lxb_dom_processing_instruction_interface_destroy(new);
45     }
46 
47     return new;
48 }
49 
50 lxb_dom_processing_instruction_t *
lxb_dom_processing_instruction_interface_destroy(lxb_dom_processing_instruction_t * processing_instruction)51 lxb_dom_processing_instruction_interface_destroy(lxb_dom_processing_instruction_t *processing_instruction)
52 {
53     lexbor_mraw_t *text;
54     lexbor_str_t target;
55 
56     text = lxb_dom_interface_node(processing_instruction)->owner_document->text;
57     target = processing_instruction->target;
58 
59     (void) lxb_dom_character_data_interface_destroy(
60                       lxb_dom_interface_character_data(processing_instruction));
61 
62     (void) lexbor_str_destroy(&target, text, false);
63 
64     return NULL;
65 }
66 
67 lxb_status_t
lxb_dom_processing_instruction_copy(lxb_dom_processing_instruction_t * dst,const lxb_dom_processing_instruction_t * src)68 lxb_dom_processing_instruction_copy(lxb_dom_processing_instruction_t *dst,
69                                     const lxb_dom_processing_instruction_t *src)
70 {
71     dst->target.length = 0;
72 
73     if (lexbor_str_copy(&dst->target, &src->target,
74                         lxb_dom_interface_node(dst)->owner_document->text) == NULL)
75     {
76         return LXB_STATUS_ERROR_MEMORY_ALLOCATION;
77     }
78 
79     return lxb_dom_character_data_interface_copy(&dst->char_data,
80                                                  &src->char_data);
81 }
82 
83 
84 /*
85  * No inline functions for ABI.
86  */
87 const lxb_char_t *
lxb_dom_processing_instruction_target_noi(lxb_dom_processing_instruction_t * pi,size_t * len)88 lxb_dom_processing_instruction_target_noi(lxb_dom_processing_instruction_t *pi,
89                                           size_t *len)
90 {
91     return lxb_dom_processing_instruction_target(pi, len);
92 }
93