From 3cd628f31ff383114e21349958fd19f45fe2a8c0 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Wed, 27 May 2015 01:19:57 -0300 Subject: content-parser: random fixes, more tests --- src/content-parser.c | 39 ++++++++---- tests/check_content_parser.c | 144 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+), 13 deletions(-) diff --git a/src/content-parser.c b/src/content-parser.c index 7781c92..041e96d 100644 --- a/src/content-parser.c +++ b/src/content-parser.c @@ -588,9 +588,15 @@ hr: parsed = NULL; } else { - state = CONTENT_PARAGRAPH; + state = CONTENT_PARAGRAPH_END; free(tmp); tmp = NULL; + free(prefix); + prefix = NULL; + b_slist_free_full(lines, free); + lines = NULL; + if (is_last) + goto para; break; } free(tmp); @@ -634,7 +640,8 @@ hr: break; prefix_len = current - start; state = CONTENT_ORDERED_LIST_START; - break; + if (c != '\n' && c != '\r' && !is_last) + break; case CONTENT_ORDERED_LIST_START: if (c == '\n' || c == '\r' || is_last) { @@ -646,19 +653,25 @@ hr: free(tmp2); tmp2 = NULL; if (b_strv_length(tmpv) != 2) { - state = CONTENT_PARAGRAPH; - goto err_li; + state = CONTENT_PARAGRAPH_END; + b_strv_free(tmpv); + tmpv = NULL; + free(tmp); + tmp = NULL; + b_slist_free_full(lines, free); + lines = NULL; + goto para; } for (unsigned int i = 0; tmpv[0][i] != '\0'; i++) { if (!(tmpv[0][i] >= '0' && tmpv[0][i] <= '9')) { - state = CONTENT_PARAGRAPH; - goto err_li; - } - } - for (unsigned int i = 0; tmpv[1][i] != '\0'; i++) { - if (!(tmpv[1][i] == ' ' || tmpv[1][i] == '\t')) { - state = CONTENT_PARAGRAPH; - goto err_li; + state = CONTENT_PARAGRAPH_END; + b_strv_free(tmpv); + tmpv = NULL; + free(tmp); + tmp = NULL; + b_slist_free_full(lines, free); + lines = NULL; + goto para; } } tmp3 = b_strdup(tmp + prefix_len); @@ -669,7 +682,6 @@ hr: state = CONTENT_ORDERED_LIST_END; free(parsed); parsed = NULL; -err_li: b_strv_free(tmpv); tmpv = NULL; } @@ -707,6 +719,7 @@ err_li: break; case CONTENT_PARAGRAPH_END: +para: if (c == '\n' || c == '\r' || is_last) { tmp = b_strndup(src + start, end - start); parsed = blogc_content_parse_inline(tmp); diff --git a/tests/check_content_parser.c b/tests/check_content_parser.c index ea8de4b..4f46be8 100644 --- a/tests/check_content_parser.c +++ b/tests/check_content_parser.c @@ -378,6 +378,24 @@ test_content_parse_ordered_list(void **state) "\n" "

fuuuu

\n"); free(html); + html = blogc_content_parse( + "1.\nasd\n" + "2. qwe\n"); + assert_non_null(html); + assert_string_equal(html, + "

1.\n" + "asd

\n" + "
    \n" + "
  1. qwe
  2. \n" + "
\n"); + free(html); + html = blogc_content_parse("1.\n"); + assert_non_null(html); + assert_string_equal(html, + "
    \n" + "
  1. \n" + "
\n"); + free(html); } @@ -462,6 +480,130 @@ test_content_parse_invalid_horizontal_rule(void **state) } +void +test_content_parse_invalid_unordered_list(void **state) +{ + // more invalid html + char *html = blogc_content_parse( + "* asd\n" + "1. qwe"); + assert_non_null(html); + assert_string_equal(html, + "

asd\n" + "1. qwe

\n"); + free(html); + html = blogc_content_parse( + "* asd\n" + "1. qwe\n" + "\n"); + assert_non_null(html); + assert_string_equal(html, + "

asd\n" + "1. qwe

\n"); + free(html); + html = blogc_content_parse( + "* asd\n" + "1. qwe\n"); + assert_non_null(html); + assert_string_equal(html, + "

asd\n" + "1. qwe

\n"); + free(html); + html = blogc_content_parse( + "* asd\n" + "1. qwe\n"); + assert_non_null(html); + assert_string_equal(html, + "

asd\n" + "1. qwe

\n"); + free(html); + html = blogc_content_parse( + "chunda\n" + "\n" + "* asd\n" + "1. qwe\n" + "\n" + "poi\n"); + assert_non_null(html); + assert_string_equal(html, + "

chunda

\n" + "

asd\n" + "1. qwe

\n" + "

poi

\n"); + free(html); +} + + +void +test_content_parse_invalid_ordered_list(void **state) +{ + // more invalid html + char *html = blogc_content_parse( + "1. asd\n" + "* qwe"); + assert_non_null(html); + assert_string_equal(html, + "

1. asd\n" + " qwe

\n"); + free(html); + html = blogc_content_parse( + "1. asd\n" + "* qwe\n" + "\n"); + assert_non_null(html); + assert_string_equal(html, + "

1. asd\n" + " qwe

\n"); + free(html); + html = blogc_content_parse( + "1. asd\n" + "* qwe\n"); + assert_non_null(html); + assert_string_equal(html, + "

1. asd\n" + " qwe

\n"); + free(html); + html = blogc_content_parse( + "1. asd\n" + "* qwe\n"); + assert_non_null(html); + assert_string_equal(html, + "

1. asd\n" + " qwe

\n"); + free(html); + html = blogc_content_parse( + "chunda\n" + "\n" + "1. asd\n" + "* qwe\n" + "\n" + "poi\n"); + assert_non_null(html); + assert_string_equal(html, + "

chunda

\n" + "

1. asd\n" + " qwe

\n" + "

poi

\n"); + free(html); + html = blogc_content_parse( + "1 asd\n" + "* qwe\n"); + assert_non_null(html); + assert_string_equal(html, + "

1 asd\n" + " qwe

\n"); + free(html); + html = blogc_content_parse( + "a. asd\n" + "2. qwe\n"); + assert_non_null(html); + assert_string_equal(html, + "

a. asd\n" + "2. qwe

\n"); + free(html); +} + + void test_content_parse_inline(void **state) { @@ -497,6 +639,8 @@ main(void) unit_test(test_content_parse_invalid_blockquote), unit_test(test_content_parse_invalid_code), unit_test(test_content_parse_invalid_horizontal_rule), + unit_test(test_content_parse_invalid_unordered_list), + unit_test(test_content_parse_invalid_ordered_list), unit_test(test_content_parse_inline), }; return run_tests(tests); -- cgit v1.2.3-18-g5258