From 282c12c6790e49a14b0dfaa733fd44c5de79e5dc Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 19 Nov 2014 16:16:45 +0100 Subject: Add cmark_node_insert_after to cmark.h Fix copy/paste error. --- src/cmark.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmark.h b/src/cmark.h index 522e77e..18081db 100644 --- a/src/cmark.h +++ b/src/cmark.h @@ -110,7 +110,7 @@ CMARK_EXPORT int cmark_node_insert_before(cmark_node *node, cmark_node *sibling); CMARK_EXPORT int -cmark_node_insert_before(cmark_node *node, cmark_node *sibling); +cmark_node_insert_after(cmark_node *node, cmark_node *sibling); CMARK_EXPORT int cmark_node_prepend_child(cmark_node *node, cmark_node *child); -- cgit v1.2.3 From a595a84f691009808d58226ba0ada52e995ff224 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 19 Nov 2014 16:17:11 +0100 Subject: Add tests for insert_{before|after} functions --- api_test/main.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/api_test/main.c b/api_test/main.c index 1ff606b..1f4e298 100644 --- a/api_test/main.c +++ b/api_test/main.c @@ -10,6 +10,7 @@ static void create_tree(test_batch_runner *runner) { + char *html; cmark_node *doc = cmark_node_new(CMARK_NODE_DOCUMENT); cmark_node *p = cmark_node_new(CMARK_NODE_PARAGRAPH); @@ -35,11 +36,36 @@ create_tree(test_batch_runner *runner) OK(runner, cmark_node_append_child(emph, str2), "append3"); INT_EQ(runner, cmark_node_check(doc), 0, "append3 consistent"); - char *html = (char *)cmark_render_html(doc); + html = (char *)cmark_render_html(doc); STR_EQ(runner, html, "

Hello, world!

\n", "render_html"); free(html); + OK(runner, cmark_node_insert_before(str1, str3), "ins before1"); + INT_EQ(runner, cmark_node_check(doc), 0, "ins before1 consistent"); + // 31e + OK(runner, cmark_node_first_child(p) == str3, "ins before1 works"); + + OK(runner, cmark_node_insert_before(str1, emph), "ins before2"); + INT_EQ(runner, cmark_node_check(doc), 0, "ins before2 consistent"); + // 3e1 + OK(runner, cmark_node_last_child(p) == str1, "ins before2 works"); + + OK(runner, cmark_node_insert_after(str1, str3), "ins after1"); + INT_EQ(runner, cmark_node_check(doc), 0, "ins after1 consistent"); + // e13 + OK(runner, cmark_node_last_child(p) == str3, "ins after1 works"); + + OK(runner, cmark_node_insert_after(str1, emph), "ins after2"); + INT_EQ(runner, cmark_node_check(doc), 0, "ins after2 consistent"); + // 1e3 + OK(runner, cmark_node_first_child(p) == str1, "ins after2 works"); + + html = (char *)cmark_render_html(doc); + STR_EQ(runner, html, "

Hello, world!

\n", + "render_html after shuffling"); + free(html); + cmark_node_destroy(doc); } -- cgit v1.2.3 From 18169a8174cd0c49d64d95a3b9509a66d359da27 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 19 Nov 2014 16:18:44 +0100 Subject: Rename {get|set}_content to {get|set}_string_content --- api_test/main.c | 6 +++--- src/cmark.h | 4 ++-- src/node.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/api_test/main.c b/api_test/main.c index 1f4e298..9f1059f 100644 --- a/api_test/main.c +++ b/api_test/main.c @@ -22,17 +22,17 @@ create_tree(test_batch_runner *runner) INT_EQ(runner, cmark_node_check(doc), 0, "prepend1 consistent"); cmark_node *str1 = cmark_node_new(CMARK_NODE_STRING); - cmark_node_set_content(str1, "Hello, "); + cmark_node_set_string_content(str1, "Hello, "); OK(runner, cmark_node_prepend_child(p, str1), "prepend2"); INT_EQ(runner, cmark_node_check(doc), 0, "prepend2 consistent"); cmark_node *str3 = cmark_node_new(CMARK_NODE_STRING); - cmark_node_set_content(str3, "!"); + cmark_node_set_string_content(str3, "!"); OK(runner, cmark_node_append_child(p, str3), "append2"); INT_EQ(runner, cmark_node_check(doc), 0, "append2 consistent"); cmark_node *str2 = cmark_node_new(CMARK_NODE_STRING); - cmark_node_set_content(str2, "world"); + cmark_node_set_string_content(str2, "world"); OK(runner, cmark_node_append_child(emph, str2), "append3"); INT_EQ(runner, cmark_node_check(doc), 0, "append3 consistent"); diff --git a/src/cmark.h b/src/cmark.h index 18081db..e677ed0 100644 --- a/src/cmark.h +++ b/src/cmark.h @@ -90,10 +90,10 @@ CMARK_EXPORT cmark_node_type cmark_node_get_type(cmark_node *node); CMARK_EXPORT const char* -cmark_node_get_content(cmark_node *node); +cmark_node_get_string_content(cmark_node *node); CMARK_EXPORT int -cmark_node_set_content(cmark_node *node, const char *content); +cmark_node_set_string_content(cmark_node *node, const char *content); CMARK_EXPORT const char* cmark_node_get_url(cmark_node *node); diff --git a/src/node.c b/src/node.c index c5ce642..9119c63 100644 --- a/src/node.c +++ b/src/node.c @@ -96,7 +96,7 @@ S_strdup(const char *str) { } const char* -cmark_node_get_content(cmark_node *node) { +cmark_node_get_string_content(cmark_node *node) { switch (node->type) { case NODE_STRING: case NODE_INLINE_HTML: @@ -110,7 +110,7 @@ cmark_node_get_content(cmark_node *node) { } int -cmark_node_set_content(cmark_node *node, const char *content) { +cmark_node_set_string_content(cmark_node *node, const char *content) { switch (node->type) { case NODE_STRING: case NODE_INLINE_HTML: -- cgit v1.2.3 From b93d0b3a479bfc05dfbba0ad84172302df7f48cc Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 19 Nov 2014 16:28:48 +0100 Subject: Accessors for string content of code and HTML blocks --- src/node.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/node.c b/src/node.c index 9119c63..1b767ae 100644 --- a/src/node.c +++ b/src/node.c @@ -98,6 +98,11 @@ S_strdup(const char *str) { const char* cmark_node_get_string_content(cmark_node *node) { switch (node->type) { + case NODE_INDENTED_CODE: + case NODE_FENCED_CODE: + case NODE_HTML: + return cmark_strbuf_cstr(&node->string_content); + case NODE_STRING: case NODE_INLINE_HTML: case NODE_INLINE_CODE: @@ -112,6 +117,11 @@ cmark_node_get_string_content(cmark_node *node) { int cmark_node_set_string_content(cmark_node *node, const char *content) { switch (node->type) { + case NODE_INDENTED_CODE: + case NODE_FENCED_CODE: + case NODE_HTML: + cmark_strbuf_sets(&node->string_content, content); + case NODE_STRING: case NODE_INLINE_HTML: case NODE_INLINE_CODE: -- cgit v1.2.3 From 3776d23d40b8f638a2a97d7e54f2660c7c435090 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 19 Nov 2014 16:38:22 +0100 Subject: Accessors for header level --- src/cmark.h | 6 ++++++ src/node.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/cmark.h b/src/cmark.h index e677ed0..a23b587 100644 --- a/src/cmark.h +++ b/src/cmark.h @@ -95,6 +95,12 @@ cmark_node_get_string_content(cmark_node *node); CMARK_EXPORT int cmark_node_set_string_content(cmark_node *node, const char *content); +CMARK_EXPORT int +cmark_node_get_header_level(cmark_node *node); + +CMARK_EXPORT int +cmark_node_set_header_level(cmark_node *node, int level); + CMARK_EXPORT const char* cmark_node_get_url(cmark_node *node); diff --git a/src/node.c b/src/node.c index 1b767ae..56f3937 100644 --- a/src/node.c +++ b/src/node.c @@ -134,6 +134,35 @@ cmark_node_set_string_content(cmark_node *node, const char *content) { return 0; } +int +cmark_node_get_header_level(cmark_node *node) { + switch (node->type) { + case CMARK_NODE_ATX_HEADER: + case CMARK_NODE_SETEXT_HEADER: + return node->as.header.level; + + default: + break; + } + + return 0; +} + +int +cmark_node_set_header_level(cmark_node *node, int level) { + switch (node->type) { + case CMARK_NODE_ATX_HEADER: + case CMARK_NODE_SETEXT_HEADER: + node->as.header.level = level; + return 1; + + default: + break; + } + + return 0; +} + const char* cmark_node_get_url(cmark_node *node) { switch (node->type) { -- cgit v1.2.3 From 285879585db2f284a1ce0896c4775be8a260d6db Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 19 Nov 2014 16:53:53 +0100 Subject: Accessors for list data Only 'list_type', 'start', and 'tight' should be relevant for rendering. Accessors for other list data could be added for completeness but they don't seem very useful. --- src/cmark.h | 19 +++++++++++++++++++ src/node.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/src/cmark.h b/src/cmark.h index a23b587..5305dfb 100644 --- a/src/cmark.h +++ b/src/cmark.h @@ -44,6 +44,7 @@ typedef enum { } cmark_node_type; typedef enum { + CMARK_NO_LIST, CMARK_BULLET_LIST, CMARK_ORDERED_LIST } cmark_list_type; @@ -101,6 +102,24 @@ cmark_node_get_header_level(cmark_node *node); CMARK_EXPORT int cmark_node_set_header_level(cmark_node *node, int level); +CMARK_EXPORT cmark_list_type +cmark_node_get_list_type(cmark_node *node); + +CMARK_EXPORT int +cmark_node_set_list_type(cmark_node *node, cmark_list_type type); + +CMARK_EXPORT int +cmark_node_get_list_start(cmark_node *node); + +CMARK_EXPORT int +cmark_node_set_list_start(cmark_node *node, int start); + +CMARK_EXPORT int +cmark_node_get_list_tight(cmark_node *node); + +CMARK_EXPORT int +cmark_node_set_list_tight(cmark_node *node, int tight); + CMARK_EXPORT const char* cmark_node_get_url(cmark_node *node); diff --git a/src/node.c b/src/node.c index 56f3937..c1f29d3 100644 --- a/src/node.c +++ b/src/node.c @@ -163,6 +163,69 @@ cmark_node_set_header_level(cmark_node *node, int level) { return 0; } +cmark_list_type +cmark_node_get_list_type(cmark_node *node) { + if (node->type == CMARK_NODE_LIST) { + return node->as.list.list_type; + } + else { + return CMARK_NO_LIST; + } +} + +int +cmark_node_set_list_type(cmark_node *node, cmark_list_type type) { + if (node->type == CMARK_NODE_LIST) { + node->as.list.list_type = type; + return 1; + } + else { + return 0; + } +} + +int +cmark_node_get_list_start(cmark_node *node) { + if (node->type == CMARK_NODE_LIST) { + return node->as.list.start; + } + else { + return 0; + } +} + +int +cmark_node_set_list_start(cmark_node *node, int start) { + if (node->type == CMARK_NODE_LIST) { + node->as.list.start = start; + return 1; + } + else { + return 0; + } +} + +int +cmark_node_get_list_tight(cmark_node *node) { + if (node->type == CMARK_NODE_LIST) { + return node->as.list.tight; + } + else { + return 0; + } +} + +int +cmark_node_set_list_tight(cmark_node *node, int tight) { + if (node->type == CMARK_NODE_LIST) { + node->as.list.tight = tight; + return 1; + } + else { + return 0; + } +} + const char* cmark_node_get_url(cmark_node *node) { switch (node->type) { -- cgit v1.2.3 From 14588ca3cd25982bdd34e5fc20e69c611e1c8817 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 19 Nov 2014 17:02:04 +0100 Subject: Accessors for fence info Only fence info should be relevant for rendering. Accessors for other fenced code data could be added for completeness but they don't seem very useful. --- src/cmark.h | 6 ++++++ src/node.c | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/cmark.h b/src/cmark.h index 5305dfb..d9daccd 100644 --- a/src/cmark.h +++ b/src/cmark.h @@ -120,6 +120,12 @@ cmark_node_get_list_tight(cmark_node *node); CMARK_EXPORT int cmark_node_set_list_tight(cmark_node *node, int tight); +CMARK_EXPORT const char* +cmark_node_get_fence_info(cmark_node *node); + +CMARK_EXPORT int +cmark_node_set_fence_info(cmark_node *node, const char *info); + CMARK_EXPORT const char* cmark_node_get_url(cmark_node *node); diff --git a/src/node.c b/src/node.c index c1f29d3..814e0e1 100644 --- a/src/node.c +++ b/src/node.c @@ -226,6 +226,27 @@ cmark_node_set_list_tight(cmark_node *node, int tight) { } } +const char* +cmark_node_get_fence_info(cmark_node *node) { + if (node->type == NODE_FENCED_CODE) { + return cmark_strbuf_cstr(&node->as.code.info); + } + else { + return NULL; + } +} + +int +cmark_node_set_fence_info(cmark_node *node, const char *info) { + if (node->type == NODE_FENCED_CODE) { + cmark_strbuf_sets(&node->as.code.info, info); + return 1; + } + else { + return 0; + } +} + const char* cmark_node_get_url(cmark_node *node) { switch (node->type) { -- cgit v1.2.3 From fd13d0211f33a134a4c73882f1f2eaa423f1794a Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 19 Nov 2014 17:05:04 +0100 Subject: Accessors for link titles --- src/cmark.h | 6 ++++++ src/node.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/cmark.h b/src/cmark.h index d9daccd..2b42486 100644 --- a/src/cmark.h +++ b/src/cmark.h @@ -132,6 +132,12 @@ cmark_node_get_url(cmark_node *node); CMARK_EXPORT int cmark_node_set_url(cmark_node *node, const char *url); +CMARK_EXPORT const char* +cmark_node_get_title(cmark_node *node); + +CMARK_EXPORT int +cmark_node_set_title(cmark_node *node, const char *title); + // Tree manipulation CMARK_EXPORT void diff --git a/src/node.c b/src/node.c index 814e0e1..f2649e9 100644 --- a/src/node.c +++ b/src/node.c @@ -275,6 +275,34 @@ cmark_node_set_url(cmark_node *node, const char *url) { return 0; } +const char* +cmark_node_get_title(cmark_node *node) { + switch (node->type) { + case NODE_LINK: + case NODE_IMAGE: + return (char *)node->as.link.title; + default: + break; + } + + return NULL; +} + +int +cmark_node_set_title(cmark_node *node, const char *title) { + switch (node->type) { + case NODE_LINK: + case NODE_IMAGE: + free(node->as.link.title); + node->as.link.title = (unsigned char *)S_strdup(title); + return 1; + default: + break; + } + + return 0; +} + static inline bool S_is_block(cmark_node *node) { return node->type >= CMARK_NODE_FIRST_BLOCK -- cgit v1.2.3 From 7f8a0d96dd3ab34b4f8ec233fd243f53a01a4e6a Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 19 Nov 2014 17:08:03 +0100 Subject: Accessors for start_line, start_column, end_line These are read-only as they're only metadata returned by the parser. --- src/cmark.h | 9 +++++++++ src/node.c | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/cmark.h b/src/cmark.h index 2b42486..a74fe93 100644 --- a/src/cmark.h +++ b/src/cmark.h @@ -138,6 +138,15 @@ cmark_node_get_title(cmark_node *node); CMARK_EXPORT int cmark_node_set_title(cmark_node *node, const char *title); +CMARK_EXPORT int +cmark_node_get_start_line(cmark_node *node); + +CMARK_EXPORT int +cmark_node_get_start_column(cmark_node *node); + +CMARK_EXPORT int +cmark_node_get_end_line(cmark_node *node); + // Tree manipulation CMARK_EXPORT void diff --git a/src/node.c b/src/node.c index f2649e9..8b8b988 100644 --- a/src/node.c +++ b/src/node.c @@ -303,6 +303,21 @@ cmark_node_set_title(cmark_node *node, const char *title) { return 0; } +int +cmark_node_get_start_line(cmark_node *node) { + return node->start_line; +} + +int +cmark_node_get_start_column(cmark_node *node) { + return node->start_column; +} + +int +cmark_node_get_end_line(cmark_node *node) { + return node->end_line; +} + static inline bool S_is_block(cmark_node *node) { return node->type >= CMARK_NODE_FIRST_BLOCK -- cgit v1.2.3 From ce4b717d6a05d707ea19eee68a74dd3621de3513 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 19 Nov 2014 18:06:03 +0100 Subject: Fix {get|set}_string_content --- src/node.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/node.c b/src/node.c index 8b8b988..190deb9 100644 --- a/src/node.c +++ b/src/node.c @@ -107,6 +107,7 @@ cmark_node_get_string_content(cmark_node *node) { case NODE_INLINE_HTML: case NODE_INLINE_CODE: return cmark_chunk_to_cstr(&node->as.literal); + default: break; } @@ -121,12 +122,14 @@ cmark_node_set_string_content(cmark_node *node, const char *content) { case NODE_FENCED_CODE: case NODE_HTML: cmark_strbuf_sets(&node->string_content, content); + return 1; case NODE_STRING: case NODE_INLINE_HTML: case NODE_INLINE_CODE: cmark_chunk_set_cstr(&node->as.literal, content); return 1; + default: break; } -- cgit v1.2.3 From 5e06c76057e87474967a1b55677c71dd89694cf2 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 19 Nov 2014 18:11:37 +0100 Subject: Fix api_test summary --- api_test/harness.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api_test/harness.c b/api_test/harness.c index 6b38d41..e8f320c 100644 --- a/api_test/harness.c +++ b/api_test/harness.c @@ -90,7 +90,7 @@ test_print_summary(test_batch_runner *runner) int num_failed = runner->num_failed; fprintf(stderr, "%d tests passed, %d failed, %d skipped\n", - num_passed, num_skipped, num_failed); + num_passed, num_failed, num_skipped); if (test_ok(runner)) { fprintf(stderr, "PASS\n"); -- cgit v1.2.3 From 2c210483fab6b163a120894f07e4f10622be1f40 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 19 Nov 2014 18:30:49 +0100 Subject: Accessor tests --- api_test/main.c | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) diff --git a/api_test/main.c b/api_test/main.c index 9f1059f..82a0dfb 100644 --- a/api_test/main.c +++ b/api_test/main.c @@ -7,6 +7,203 @@ #include "harness.h" +static void +accessors(test_batch_runner *runner) +{ + static const unsigned char markdown[] = + "## Header\n" + "\n" + "* Item 1\n" + "* Item 2\n" + "\n" + "2. Item 1\n" + "\n" + "3. Item 2\n" + "\n" + "\n" + " code\n" + "\n" + "``` lang\n" + "fenced\n" + "```\n" + "\n" + "
html
\n" + "\n" + "[link](url 'title')\n"; + + cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1); + + // Getters + + cmark_node *header = cmark_node_first_child(doc); + INT_EQ(runner, cmark_node_get_type(header), CMARK_NODE_ATX_HEADER, + "get_type header"); + INT_EQ(runner, cmark_node_get_header_level(header), 2, + "get_header_level"); + + cmark_node *bullet_list = cmark_node_next(header); + INT_EQ(runner, cmark_node_get_type(bullet_list), CMARK_NODE_LIST, + "get_type bullet list"); + INT_EQ(runner, cmark_node_get_list_type(bullet_list), + CMARK_BULLET_LIST, "get_list_type bullet"); + INT_EQ(runner, cmark_node_get_list_tight(bullet_list), 1, + "get_list_tight tight"); + + cmark_node *ordered_list = cmark_node_next(bullet_list); + INT_EQ(runner, cmark_node_get_type(ordered_list), CMARK_NODE_LIST, + "get_type ordered list"); + INT_EQ(runner, cmark_node_get_list_type(ordered_list), + CMARK_ORDERED_LIST, "get_list_type ordered"); + INT_EQ(runner, cmark_node_get_list_start(ordered_list), 2, + "get_list_start"); + INT_EQ(runner, cmark_node_get_list_tight(ordered_list), 0, + "get_list_tight loose"); + + cmark_node *code = cmark_node_next(ordered_list); + INT_EQ(runner, cmark_node_get_type(code), CMARK_NODE_INDENTED_CODE, + "get_type indented code"); + STR_EQ(runner, cmark_node_get_string_content(code), "code\n", + "get_string_content indented code"); + + cmark_node *fenced = cmark_node_next(code); + INT_EQ(runner, cmark_node_get_type(fenced), CMARK_NODE_FENCED_CODE, + "get_type fenced code"); + STR_EQ(runner, cmark_node_get_string_content(fenced), "fenced\n", + "get_string_content fenced code"); + STR_EQ(runner, cmark_node_get_fence_info(fenced), "lang", + "get_fence_info"); + + cmark_node *html = cmark_node_next(fenced); + INT_EQ(runner, cmark_node_get_type(html), CMARK_NODE_HTML, + "get_type html"); + STR_EQ(runner, cmark_node_get_string_content(html), + "
html
\n", "get_string_content html"); + + cmark_node *paragraph = cmark_node_next(html); + INT_EQ(runner, cmark_node_get_type(paragraph), CMARK_NODE_PARAGRAPH, + "get_type paragraph"); + INT_EQ(runner, cmark_node_get_start_line(paragraph), 19, + "get_start_line"); + INT_EQ(runner, cmark_node_get_start_column(paragraph), 1, + "get_start_column"); + INT_EQ(runner, cmark_node_get_end_line(paragraph), 19, + "get_end_line"); + + cmark_node *link = cmark_node_first_child(paragraph); + INT_EQ(runner, cmark_node_get_type(link), CMARK_NODE_LINK, + "get_type link"); + STR_EQ(runner, cmark_node_get_url(link), "url", + "get_url"); + STR_EQ(runner, cmark_node_get_title(link), "title", + "get_title"); + + cmark_node *string = cmark_node_first_child(link); + INT_EQ(runner, cmark_node_get_type(string), CMARK_NODE_STRING, + "get_type string"); + STR_EQ(runner, cmark_node_get_string_content(string), "link", + "get_string_content string"); + + // Setters + + OK(runner, cmark_node_set_header_level(header, 3), + "set_header_level"); + + OK(runner, cmark_node_set_list_type(bullet_list, CMARK_ORDERED_LIST), + "set_list_type ordered"); + OK(runner, cmark_node_set_list_start(bullet_list, 3), + "set_list_start"); + OK(runner, cmark_node_set_list_tight(bullet_list, 0), + "set_list_tight loose"); + + OK(runner, cmark_node_set_list_type(ordered_list, CMARK_BULLET_LIST), + "set_list_type bullet"); + OK(runner, cmark_node_set_list_tight(ordered_list, 1), + "set_list_tight tight"); + + OK(runner, cmark_node_set_string_content(code, "CODE\n"), + "set_string_content indented code"); + + OK(runner, cmark_node_set_string_content(fenced, "FENCED\n"), + "set_string_content fenced code"); + OK(runner, cmark_node_set_fence_info(fenced, "LANG"), + "set_fence_info"); + + OK(runner, cmark_node_set_string_content(html, "
HTML
\n"), + "set_string_content html"); + + OK(runner, cmark_node_set_url(link, "URL"), + "set_url"); + OK(runner, cmark_node_set_title(link, "TITLE"), + "set_title"); + + OK(runner, cmark_node_set_string_content(string, "LINK"), + "set_string_content string"); + + char *rendered_html = (char *)cmark_render_html(doc); + static const char expected_html[] = + "

Header

\n" + "
    \n" + "
  1. \n" + "

    Item 1

    \n" + "
  2. \n" + "
  3. \n" + "

    Item 2

    \n" + "
  4. \n" + "
\n" + "
    \n" + "
  • Item 1
  • \n" + "
  • Item 2
  • \n" + "
\n" + "
CODE\n"
+		"
\n" + "
FENCED\n"
+		"
\n" + "
HTML
\n" + "

LINK

\n"; + STR_EQ(runner, rendered_html, expected_html, "setters work"); + free(rendered_html); + + // Getter errors + + INT_EQ(runner, cmark_node_get_header_level(bullet_list), 0, + "get_header_level error"); + INT_EQ(runner, cmark_node_get_list_type(header), CMARK_NO_LIST, + "get_list_type error"); + INT_EQ(runner, cmark_node_get_list_start(code), 0, + "get_list_start error"); + INT_EQ(runner, cmark_node_get_list_tight(fenced), 0, + "get_list_tight error"); + OK(runner, cmark_node_get_string_content(ordered_list) == NULL, + "get_string_content error"); + OK(runner, cmark_node_get_fence_info(paragraph) == NULL, + "get_fence_info error"); + OK(runner, cmark_node_get_url(html) == NULL, + "get_url error"); + OK(runner, cmark_node_get_title(header) == NULL, + "get_title error"); + + // Setter errors + + OK(runner, !cmark_node_set_header_level(bullet_list, 3), + "set_header_level error"); + OK(runner, !cmark_node_set_list_type(header, CMARK_ORDERED_LIST), + "set_list_type error"); + OK(runner, !cmark_node_set_list_start(code, 3), + "set_list_start error"); + OK(runner, !cmark_node_set_list_tight(fenced, 0), + "set_list_tight error"); + OK(runner, !cmark_node_set_string_content(ordered_list, "content\n"), + "set_string_content error"); + OK(runner, !cmark_node_set_fence_info(paragraph, "lang"), + "set_fence_info error"); + OK(runner, !cmark_node_set_url(html, "url"), + "set_url error"); + OK(runner, !cmark_node_set_title(header, "title"), + "set_title error"); + + cmark_node_destroy(doc); +} + static void create_tree(test_batch_runner *runner) { @@ -73,6 +270,7 @@ int main() { int retval; test_batch_runner *runner = test_batch_runner_new(); + accessors(runner); create_tree(runner); test_print_summary(runner); -- cgit v1.2.3 From f77dcb217261148de1c57c8b042873efbad81d20 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 19 Nov 2014 18:37:52 +0100 Subject: Input validation for setters --- api_test/main.c | 9 +++++++++ src/node.c | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/api_test/main.c b/api_test/main.c index 82a0dfb..f74dee2 100644 --- a/api_test/main.c +++ b/api_test/main.c @@ -201,6 +201,15 @@ accessors(test_batch_runner *runner) OK(runner, !cmark_node_set_title(header, "title"), "set_title error"); + OK(runner, !cmark_node_set_header_level(header, 0), + "set_header_level too small"); + OK(runner, !cmark_node_set_header_level(header, 7), + "set_header_level too large"); + OK(runner, !cmark_node_set_list_type(bullet_list, CMARK_NO_LIST), + "set_list_type invalid"); + OK(runner, !cmark_node_set_list_start(bullet_list, -1), + "set_list_start negative"); + cmark_node_destroy(doc); } diff --git a/src/node.c b/src/node.c index 190deb9..624baad 100644 --- a/src/node.c +++ b/src/node.c @@ -153,6 +153,10 @@ cmark_node_get_header_level(cmark_node *node) { int cmark_node_set_header_level(cmark_node *node, int level) { + if (level < 1 || level > 6) { + return 0; + } + switch (node->type) { case CMARK_NODE_ATX_HEADER: case CMARK_NODE_SETEXT_HEADER: @@ -178,6 +182,10 @@ cmark_node_get_list_type(cmark_node *node) { int cmark_node_set_list_type(cmark_node *node, cmark_list_type type) { + if (!(type == CMARK_BULLET_LIST || type == CMARK_ORDERED_LIST)) { + return 0; + } + if (node->type == CMARK_NODE_LIST) { node->as.list.list_type = type; return 1; @@ -199,6 +207,10 @@ cmark_node_get_list_start(cmark_node *node) { int cmark_node_set_list_start(cmark_node *node, int start) { + if (start < 0) { + return 0; + } + if (node->type == CMARK_NODE_LIST) { node->as.list.start = start; return 1; -- cgit v1.2.3