From 759e03748a368be8b209dfd113441c05e1a5dcc0 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Thu, 20 Nov 2014 13:02:53 +0100 Subject: More tree hierarchy checks and tests --- src/node.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/node.c') diff --git a/src/node.c b/src/node.c index 624baad..4317dd4 100644 --- a/src/node.c +++ b/src/node.c @@ -348,6 +348,17 @@ S_is_inline(cmark_node *node) { static bool S_can_contain(cmark_node *node, cmark_node *child) { + cmark_node *cur; + + // Verify that child is not an ancestor of node or equal to node. + cur = node; + do { + if (cur == child) { + return false; + } + cur = cur->parent; + } while (cur != NULL); + if (child->type == CMARK_NODE_DOCUMENT) { return false; } -- cgit v1.2.3 From 32be14c39f67eedf356adc4f673b66479b717c7c Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Thu, 20 Nov 2014 15:17:33 +0100 Subject: Set defaults for new headers and lists --- src/node.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/node.c') diff --git a/src/node.c b/src/node.c index 4317dd4..bb9cacc 100644 --- a/src/node.c +++ b/src/node.c @@ -11,6 +11,25 @@ cmark_node* cmark_node_new(cmark_node_type type) { cmark_node *node = (cmark_node *)calloc(1, sizeof(*node)); node->type = type; + + switch (node->type) { + case CMARK_NODE_ATX_HEADER: + case CMARK_NODE_SETEXT_HEADER: + node->as.header.level = 1; + break; + + case CMARK_NODE_LIST: { + cmark_list *list = &node->as.list; + list->list_type = CMARK_BULLET_LIST; + list->start = 1; + list->tight = false; + break; + } + + default: + break; + } + return node; } -- cgit v1.2.3 From 92a4d4cb4eb4a6103f68646a38f8caa29d6df3e3 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Sat, 22 Nov 2014 15:41:40 +0100 Subject: Fix and test node_check --- src/node.c | 55 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 25 deletions(-) (limited to 'src/node.c') diff --git a/src/node.c b/src/node.c index bb9cacc..a79dd82 100644 --- a/src/node.c +++ b/src/node.c @@ -558,58 +558,63 @@ cmark_node_append_child(cmark_node *node, cmark_node *child) } static void -S_print_error(cmark_node *node, const char *elem) +S_print_error(FILE *out, cmark_node *node, const char *elem) { - fprintf(stderr, "Invalid '%s' in node type %s at %d:%d\n", elem, + if (out == NULL) { + return; + } + fprintf(out, "Invalid '%s' in node type %s at %d:%d\n", elem, S_type_string(node), node->start_line, node->start_column); } int -cmark_node_check(cmark_node *node) +cmark_node_check(cmark_node *node, FILE *out) { - cmark_node *cur = node; + cmark_node *cur; int errors = 0; - while (cur) { + if (!node) { + return 0; + } + + cur = node; + while (true) { if (cur->first_child) { if (cur->first_child->parent != cur) { - S_print_error(cur->first_child, "parent"); + S_print_error(out, cur->first_child, "parent"); cur->first_child->parent = cur; ++errors; } cur = cur->first_child; + continue; + } + + next_sibling: + if (cur == node) { + break; } - else if (cur->next) { + if (cur->next) { if (cur->next->prev != cur) { - S_print_error(cur->next, "prev"); + S_print_error(out, cur->next, "prev"); cur->next->prev = cur; ++errors; } if (cur->next->parent != cur->parent) { - S_print_error(cur->next, "parent"); + S_print_error(out, cur->next, "parent"); cur->next->parent = cur->parent; ++errors; } cur = cur->next; + continue; } - else { - if (cur->parent->last_child != cur) { - S_print_error(cur->parent, "last_child"); - cur->parent->last_child = cur; - ++errors; - } - cmark_node *ancestor = cur->parent; - cur = NULL; - - while (ancestor != node->parent) { - if (ancestor->next) { - cur = ancestor->next; - break; - } - ancestor = ancestor->parent; - } + if (cur->parent->last_child != cur) { + S_print_error(out, cur->parent, "last_child"); + cur->parent->last_child = cur; + ++errors; } + cur = cur->parent; + goto next_sibling; } return errors; -- cgit v1.2.3