From 5f283adc8d1a29f420d466e85b216e9d6f4a9822 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 2 Jul 2021 23:17:06 +0200 Subject: Remove dependency on html tidy --- tests/main.c | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) (limited to 'tests') diff --git a/tests/main.c b/tests/main.c index 7888ea1..2a08ec7 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,34 +1,51 @@ -#include #include "../include/quickmedia/HtmlSearch.h" -#include +#include #include +#include +#include -static char* get_file_content(const char *filepath) { +static char* get_file_content(const char *filepath, size_t *filesize) { FILE *file = fopen(filepath, "rb"); assert(file); fseek(file, 0, SEEK_END); - size_t filesize = ftell(file); + *filesize = ftell(file); fseek(file, 0, SEEK_SET); - char *buffer = malloc(filesize + 1); - buffer[filesize] = '\0'; - fread(buffer, 1, filesize, file); + char *buffer = malloc((*filesize) + 1); + buffer[*filesize] = '\0'; + fread(buffer, 1, *filesize, file); return buffer; } -static void result_callback(QuickMediaHtmlNode *node, void *userdata) { - const char *href = quickmedia_html_node_get_attribute_value(node, "href"); - const char *text = quickmedia_html_node_get_text(node); - printf("a href: %s, node value: %s\n", href, text); +static int result_callback(QuickMediaMatchNode *node, void *userdata) { + QuickMediaStringView href = quickmedia_html_node_get_attribute_value(node, "href"); + QuickMediaStringView text = quickmedia_html_node_get_text(node); + printf("a href: %.*s, node value: %.*s\n", (int)href.size, href.data, (int)text.size, text.data); + return 0; +} + +static int result_callback_nested_text(QuickMediaMatchNode *node, void *userdata) { + QuickMediaStringView text = quickmedia_html_node_get_text(node); + printf("text: %.*s\n", (int)text.size, text.data); + return 0; +} + +static void test_nested_nodes_get_text() { + const char *html_source = "
hello

text

world
"; + QuickMediaHtmlSearch html_search; + quickmedia_html_search_init(&html_search, html_source, strlen(html_source)); + quickmedia_html_find_nodes_xpath(&html_search, "//div[class='item']", result_callback_nested_text, NULL); + quickmedia_html_search_deinit(&html_search); } -int main(int argc, char **argv) { - char *file_content = get_file_content("test_files/test.html"); +int main() { + size_t filesize = 0; + char *file_content = get_file_content("test_files/test.html", &filesize); QuickMediaHtmlSearch html_search; - int result = quickmedia_html_search_init(&html_search, file_content); + int result = quickmedia_html_search_init(&html_search, file_content, filesize); if(result != 0) goto cleanup; result = quickmedia_html_find_nodes_xpath(&html_search, "//h3[class=\"story_name\"]//a", result_callback, NULL); @@ -38,5 +55,6 @@ int main(int argc, char **argv) { cleanup: quickmedia_html_search_deinit(&html_search); free(file_content); + test_nested_nodes_get_text(); return result; } -- cgit v1.2.3