blob: 02554794f78682d27b5195aa564e508e8bb8860d (
plain)
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
|
#include <stddef.h>
#include "config.h"
#include "node.h"
CMARK_EXPORT cmark_node_type
cmark_node_get_type(cmark_node *node)
{
return node->type;
}
cmark_node*
cmark_node_next(cmark_node *node)
{
return node->next;
}
cmark_node*
cmark_node_previous(cmark_node *node)
{
return node->prev;
}
cmark_node*
cmark_node_parent(cmark_node *node)
{
return node->parent;
}
cmark_node*
cmark_node_first_child(cmark_node *node)
{
return node->first_child;
}
cmark_node*
cmark_node_last_child(cmark_node *node)
{
return node->last_child;
}
static inline bool
S_is_block(cmark_node *node) {
return node->type >= CMARK_NODE_FIRST_BLOCK
&& node->type <= CMARK_NODE_LAST_BLOCK;
}
static inline bool
S_is_inline(cmark_node *node) {
return node->type >= CMARK_NODE_FIRST_INLINE
&& node->type <= CMARK_NODE_LAST_INLINE;
}
static bool
S_can_contain(cmark_node *node, cmark_node *child)
{
if (child->type == CMARK_NODE_DOCUMENT) {
return false;
}
switch (node->type) {
case CMARK_NODE_DOCUMENT:
case CMARK_NODE_BQUOTE:
case CMARK_NODE_LIST_ITEM:
return S_is_block(child)
&& child->type != CMARK_NODE_LIST_ITEM;
case CMARK_NODE_LIST:
return child->type == CMARK_NODE_LIST_ITEM;
case CMARK_NODE_PARAGRAPH:
case CMARK_NODE_ATX_HEADER:
case CMARK_NODE_SETEXT_HEADER:
case CMARK_NODE_EMPH:
case CMARK_NODE_STRONG:
case CMARK_NODE_LINK:
case CMARK_NODE_IMAGE:
case CMARK_NODE_LINK_LABEL:
return S_is_inline(child);
default:
break;
}
return false;
}
// Unlink a node without adjusting its next, prev, and parent pointers.
static void
S_node_unlink(cmark_node *node)
{
if (node->prev) {
node->prev->next = node->next;
}
if (node->next) {
node->next->prev = node->prev;
}
// Adjust first_child and last_child of parent.
cmark_node *parent = node->parent;
if (parent) {
if (parent->first_child == node) {
parent->first_child = node->next;
}
if (parent->last_child == node) {
parent->last_child = node->prev;
}
}
}
void
cmark_node_unlink(cmark_node *node) {
S_node_unlink(node);
node->next = NULL;
node->prev = NULL;
node->parent = NULL;
}
int
cmark_node_insert_before(cmark_node *node, cmark_node *sibling)
{
if (!S_can_contain(node->parent, sibling)) {
return 0;
}
S_node_unlink(sibling);
cmark_node *old_prev = node->prev;
// Insert 'sibling' between 'old_prev' and 'node'.
if (old_prev) {
old_prev->next = sibling;
}
sibling->prev = old_prev;
sibling->next = node;
node->prev = sibling;
// Set new parent.
cmark_node *parent = node->parent;
sibling->parent = parent;
// Adjust first_child of parent if inserted as first child.
if (parent && !old_prev) {
parent->first_child = sibling;
}
return 1;
}
int
cmark_node_insert_after(cmark_node *node, cmark_node *sibling)
{
if (!S_can_contain(node->parent, sibling)) {
return 0;
}
S_node_unlink(sibling);
cmark_node *old_next = node->next;
// Insert 'sibling' between 'node' and 'old_next'.
if (old_next) {
old_next->prev = sibling;
}
sibling->next = old_next;
sibling->prev = node;
node->next = sibling;
// Set new parent.
cmark_node *parent = node->parent;
sibling->parent = parent;
// Adjust last_child of parent if inserted as last child.
if (parent && !old_next) {
parent->last_child = sibling;
}
return 1;
}
int
cmark_node_prepend_child(cmark_node *node, cmark_node *child)
{
if (!S_can_contain(node, child)) {
return 0;
}
S_node_unlink(child);
cmark_node *old_first_child = node->first_child;
child->next = old_first_child;
child->prev = NULL;
child->parent = node;
node->first_child = child;
if (old_first_child) {
old_first_child->prev = child;
}
else {
// Also set last_child if node previously had no children.
node->last_child = child;
}
return 1;
}
int
cmark_node_append_child(cmark_node *node, cmark_node *child)
{
if (!S_can_contain(node, child)) {
return 0;
}
S_node_unlink(child);
cmark_node *old_last_child = node->last_child;
child->next = NULL;
child->prev = old_last_child;
child->parent = node;
node->last_child = child;
if (old_last_child) {
old_last_child->next = child;
}
else {
// Also set first_child if node previously had no children.
node->first_child = child;
}
return 1;
}
|