aboutsummaryrefslogtreecommitdiff
path: root/tests/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/main.c')
-rw-r--r--tests/main.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/tests/main.c b/tests/main.c
new file mode 100644
index 0000000..3b22f8a
--- /dev/null
+++ b/tests/main.c
@@ -0,0 +1,80 @@
+#include "../include/HtmlTree.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+char* file_get_content(const char *path, long *filesize) {
+ FILE *file = fopen(path, "rb");
+ if(!file) {
+ perror(path);
+ return NULL;
+ }
+
+ fseek(file, 0, SEEK_END);
+ *filesize = ftell(file);
+ fseek(file, 0, SEEK_SET);
+
+ char *data = malloc(*filesize);
+ fread(data, 1, *filesize, file);
+ fclose(file);
+ return data;
+}
+
+static void html_attributes_print(HtmlAttribute *attr) {
+ while(attr) {
+ printf("%.*s=\"%.*s\" ", (int)attr->key.size, attr->key.data, (int)attr->value.size, attr->value.data);
+ attr = attr->next;
+ }
+}
+
+static void html_node_print(HtmlNode *node);
+static void html_node_child_print(HtmlNodeChild *node_child) {
+ while(node_child) {
+ html_node_print(node_child->node);
+ node_child = node_child->next;
+ }
+}
+
+void html_node_print(HtmlNode *node) {
+ switch(node->node_type) {
+ case HTML_NODE_NODE: {
+ printf("<%.*s ", (int)node->name_or_value.size, node->name_or_value.data);
+ html_attributes_print(node->first_attr);
+ printf(">\n");
+ html_node_child_print(node->first_child);
+ printf("</%.*s>\n", (int)node->name_or_value.size, node->name_or_value.data);
+ break;
+ }
+ case HTML_NODE_TEXT: {
+ printf("%.*s", (int)node->name_or_value.size, node->name_or_value.data);
+ break;
+ }
+ case HTML_NODE_JS: {
+ printf("%.*s", (int)node->name_or_value.size, node->name_or_value.data);
+ break;
+ }
+ }
+}
+
+int main() {
+ int result;
+ HtmlTree html_tree;
+ long filesize;
+ char *file_data;
+
+ file_data = file_get_content("depends/html-parser/tests/hotexamples.html", &filesize);
+ if(!file_data) {
+ fprintf(stderr, "Failed to read from file: depends/html-parser/tests/hotexamples.html\n");
+ return 1;
+ }
+
+ result = html_parse_to_tree(&html_tree, file_data, filesize);
+ if(result != 0)
+ return result;
+
+ html_node_print(&html_tree.root_node);
+
+ html_tree_deinit(&html_tree);
+ free(file_data);
+ return 0;
+}