Skip to content

Commit cc8ab66

Browse files
committed
Merge remote-tracking branch 'origin/fix-append-child'
2 parents 8668bc0 + 9151773 commit cc8ab66

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/inlines.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,24 @@ static cmark_node *make_str_with_entities(subject *subj,
115115
}
116116
}
117117

118+
// Like cmark_node_append_child but without costly sanity checks.
119+
// Assumes that child was newly created.
120+
static void append_child(cmark_node *node, cmark_node *child) {
121+
cmark_node *old_last_child = node->last_child;
122+
123+
child->next = NULL;
124+
child->prev = old_last_child;
125+
child->parent = node;
126+
node->last_child = child;
127+
128+
if (old_last_child) {
129+
old_last_child->next = child;
130+
} else {
131+
// Also set first_child if node previously had no children.
132+
node->first_child = child;
133+
}
134+
}
135+
118136
// Duplicate a chunk by creating a copy of the buffer not by reusing the
119137
// buffer like cmark_chunk_dup does.
120138
static cmark_chunk chunk_clone(cmark_mem *mem, cmark_chunk *src) {
@@ -158,7 +176,7 @@ static CMARK_INLINE cmark_node *make_autolink(subject *subj,
158176
link->start_line = link->end_line = subj->line;
159177
link->start_column = start_column + 1;
160178
link->end_column = end_column + 1;
161-
cmark_node_append_child(link, make_str_with_entities(subj, start_column + 1, end_column - 1, &url));
179+
append_child(link, make_str_with_entities(subj, start_column + 1, end_column - 1, &url));
162180
return link;
163181
}
164182

@@ -777,7 +795,8 @@ static delimiter *S_insert_emph(subject *subj, delimiter *opener,
777795
tmp = opener_inl->next;
778796
while (tmp && tmp != closer_inl) {
779797
tmpnext = tmp->next;
780-
cmark_node_append_child(emph, tmp);
798+
cmark_node_unlink(tmp);
799+
append_child(emph, tmp);
781800
tmp = tmpnext;
782801
}
783802
cmark_node_insert_after(opener_inl, emph);
@@ -1299,7 +1318,8 @@ static cmark_node *handle_close_bracket(cmark_parser *parser, subject *subj) {
12991318
tmp = opener->inl_text->next;
13001319
while (tmp) {
13011320
tmpnext = tmp->next;
1302-
cmark_node_append_child(inl, tmp);
1321+
cmark_node_unlink(tmp);
1322+
append_child(inl, tmp);
13031323
tmp = tmpnext;
13041324
}
13051325

@@ -1512,7 +1532,7 @@ static int parse_inline(cmark_parser *parser, subject *subj, cmark_node *parent,
15121532
new_inl = make_str(subj, startpos, endpos - 1, contents);
15131533
}
15141534
if (new_inl != NULL) {
1515-
cmark_node_append_child(parent, new_inl);
1535+
append_child(parent, new_inl);
15161536
}
15171537

15181538
return 1;

0 commit comments

Comments
 (0)