xref: /php-src/ext/dom/lexbor/lexbor/dom/interface.c (revision f0934090)
1 /*
2  * Copyright (C) 2018-2021 Alexander Borisov
3  *
4  * Author: Alexander Borisov <borisov@lexbor.com>
5  */
6 
7 #include "lexbor/dom/interface.h"
8 #include "lexbor/dom/interfaces/cdata_section.h"
9 #include "lexbor/dom/interfaces/character_data.h"
10 #include "lexbor/dom/interfaces/comment.h"
11 #include "lexbor/dom/interfaces/document.h"
12 #include "lexbor/dom/interfaces/document_fragment.h"
13 #include "lexbor/dom/interfaces/document_type.h"
14 #include "lexbor/dom/interfaces/element.h"
15 #include "lexbor/dom/interfaces/event_target.h"
16 #include "lexbor/dom/interfaces/node.h"
17 #include "lexbor/dom/interfaces/processing_instruction.h"
18 #include "lexbor/dom/interfaces/shadow_root.h"
19 #include "lexbor/dom/interfaces/text.h"
20 
21 
22 lxb_dom_interface_t *
lxb_dom_interface_create(lxb_dom_document_t * document,lxb_tag_id_t tag_id,lxb_ns_id_t ns)23 lxb_dom_interface_create(lxb_dom_document_t *document, lxb_tag_id_t tag_id,
24                          lxb_ns_id_t ns)
25 {
26     lxb_dom_element_t *domel;
27 
28     domel = lxb_dom_element_interface_create(document);
29     if (domel == NULL) {
30         return NULL;
31     }
32 
33     domel->node.local_name = tag_id;
34     domel->node.ns = ns;
35 
36     return domel;
37 }
38 
39 lxb_dom_interface_t *
lxb_dom_interface_clone(lxb_dom_document_t * document,const lxb_dom_interface_t * intrfc)40 lxb_dom_interface_clone(lxb_dom_document_t *document,
41                         const lxb_dom_interface_t *intrfc)
42 {
43     const lxb_dom_node_t *node = intrfc;
44 
45     if (document == NULL) {
46         document = node->owner_document;
47     }
48 
49     switch (node->type) {
50         case LXB_DOM_NODE_TYPE_ELEMENT:
51             return lxb_dom_element_interface_clone(document, intrfc);
52 
53         case LXB_DOM_NODE_TYPE_TEXT:
54             return lxb_dom_text_interface_clone(document, intrfc);
55 
56         case LXB_DOM_NODE_TYPE_PROCESSING_INSTRUCTION:
57             return lxb_dom_processing_instruction_interface_clone(document,
58                                                                   intrfc);
59         case LXB_DOM_NODE_TYPE_COMMENT:
60             return lxb_dom_comment_interface_clone(document, intrfc);
61 
62         case LXB_DOM_NODE_TYPE_DOCUMENT:
63             return lxb_dom_document_interface_clone(document, intrfc);
64 
65         case LXB_DOM_NODE_TYPE_DOCUMENT_TYPE:
66             return lxb_dom_document_type_interface_clone(document, intrfc);
67 
68         default:
69             return lxb_dom_node_interface_clone(document, node, false);
70     }
71 }
72 
73 lxb_dom_interface_t *
lxb_dom_interface_destroy(lxb_dom_interface_t * intrfc)74 lxb_dom_interface_destroy(lxb_dom_interface_t *intrfc)
75 {
76     if (intrfc == NULL) {
77         return NULL;
78     }
79 
80     lxb_dom_node_t *node = intrfc;
81 
82     switch (node->type) {
83         case LXB_DOM_NODE_TYPE_ELEMENT:
84             return lxb_dom_element_interface_destroy(intrfc);
85 
86         case LXB_DOM_NODE_TYPE_TEXT:
87             return lxb_dom_text_interface_destroy(intrfc);
88 
89         case LXB_DOM_NODE_TYPE_CDATA_SECTION:
90             return lxb_dom_cdata_section_interface_destroy(intrfc);
91 
92         case LXB_DOM_NODE_TYPE_PROCESSING_INSTRUCTION:
93             return lxb_dom_processing_instruction_interface_destroy(intrfc);
94 
95         case LXB_DOM_NODE_TYPE_COMMENT:
96             return lxb_dom_comment_interface_destroy(intrfc);
97 
98         case LXB_DOM_NODE_TYPE_DOCUMENT:
99             return lxb_dom_document_interface_destroy(intrfc);
100 
101         case LXB_DOM_NODE_TYPE_DOCUMENT_TYPE:
102             return lxb_dom_document_type_interface_destroy(intrfc);
103 
104         case LXB_DOM_NODE_TYPE_DOCUMENT_FRAGMENT:
105             return lxb_dom_document_fragment_interface_destroy(intrfc);
106 
107         default:
108             return lexbor_mraw_free(node->owner_document->mraw, intrfc);
109     }
110 }
111