summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2016-01-10 23:35:33 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2016-01-10 23:35:33 -0800
commit93843bd2fe8c91fe9e659c5b801f1a7d672dc223 (patch)
tree1b3af3c5fb8c4e7abc8491efd3570620eb4d8d91 /src
parente384149e683fd6ea950ba9318525563175ca9f47 (diff)
Added cmark_node_replace(oldnode, newnode).
API change. I've found in using the API that this is very often wanted.
Diffstat (limited to 'src')
-rw-r--r--src/cmark.h5
-rw-r--r--src/node.c9
2 files changed, 14 insertions, 0 deletions
diff --git a/src/cmark.h b/src/cmark.h
index 5f9f294..6b5c385 100644
--- a/src/cmark.h
+++ b/src/cmark.h
@@ -387,6 +387,11 @@ CMARK_EXPORT int cmark_node_insert_before(cmark_node *node,
*/
CMARK_EXPORT int cmark_node_insert_after(cmark_node *node, cmark_node *sibling);
+/** Replaces 'oldnode' with 'newnode' and frees the memory used by 'oldnode'.
+ * Returns 1 on success, 0 on failure.
+ */
+CMARK_EXPORT int cmark_node_replace(cmark_node *oldnode, cmark_node *newnode);
+
/** Adds 'child' to the beginning of the children of 'node'.
* Returns 1 on success, 0 on failure.
*/
diff --git a/src/node.c b/src/node.c
index ce7f80c..0fd5573 100644
--- a/src/node.c
+++ b/src/node.c
@@ -730,6 +730,15 @@ int cmark_node_insert_after(cmark_node *node, cmark_node *sibling) {
return 1;
}
+int cmark_node_replace(cmark_node *oldnode, cmark_node *newnode) {
+ if (!cmark_node_insert_before(oldnode, newnode)) {
+ return 0;
+ }
+ cmark_node_unlink(oldnode);
+ cmark_node_free(oldnode);
+ return 1;
+}
+
int cmark_node_prepend_child(cmark_node *node, cmark_node *child) {
if (!S_can_contain(node, child)) {
return 0;