diff options
-rw-r--r-- | include/HtmlTree.h | 2 | ||||
-rw-r--r-- | src/HtmlTree.c | 57 | ||||
-rw-r--r-- | tests/main.c | 2 |
3 files changed, 27 insertions, 34 deletions
diff --git a/include/HtmlTree.h b/include/HtmlTree.h index 6bb3c5f..4378152 100644 --- a/include/HtmlTree.h +++ b/include/HtmlTree.h @@ -29,7 +29,7 @@ struct HtmlNode { }; struct HtmlNodeChild { - HtmlNode *node; + HtmlNode node; HtmlNodeChild *next; }; diff --git a/src/HtmlTree.c b/src/HtmlTree.c index f4deb88..faef08a 100644 --- a/src/HtmlTree.c +++ b/src/HtmlTree.c @@ -1,5 +1,6 @@ #include "../include/HtmlTree.h" #include <stdlib.h> +#include <assert.h> static void html_node_deinit(HtmlNode *self); @@ -13,45 +14,35 @@ static void html_node_init(HtmlNode *self) { self->node_type = HTML_NODE_NODE; } +/* |parent| can't be NULL */ static HtmlNode* html_node_create(HtmlStringView name_or_value, HtmlNode *parent, HtmlNodeType node_type) { - HtmlNode *new_node = malloc(sizeof(HtmlNode)); - if(!new_node) + HtmlNodeChild *node_child = malloc(sizeof(HtmlNodeChild)); + assert(parent); + if(!node_child) return NULL; - new_node->name_or_value = name_or_value; - new_node->parent = parent; - new_node->first_child = NULL; - new_node->last_child = NULL; - new_node->first_attr = NULL; - new_node->node_type = node_type; - - if(parent) { - HtmlNodeChild *node_child = malloc(sizeof(HtmlNodeChild)); - if(!node_child) { - free(new_node); - return NULL; - } - - node_child->node = new_node; - node_child->next = NULL; - - if(!parent->first_child) { - parent->first_child = node_child; - parent->last_child = node_child; - } else { - parent->last_child->next = node_child; - parent->last_child = node_child; - } + node_child->node.name_or_value = name_or_value; + node_child->node.parent = parent; + node_child->node.first_child = NULL; + node_child->node.last_child = NULL; + node_child->node.first_attr = NULL; + node_child->node.node_type = node_type; + + node_child->next = NULL; + + if(!parent->first_child) { + parent->first_child = node_child; + parent->last_child = node_child; + } else { + parent->last_child->next = node_child; + parent->last_child = node_child; } - return new_node; + return &node_child->node; } static void html_node_child_deinit(HtmlNodeChild *self) { - html_node_deinit(self->node); - free(self->node); - self->node = NULL; - + html_node_deinit(&self->node); if(self->next) { html_node_child_deinit(self->next); free(self->next); @@ -87,6 +78,7 @@ void html_node_deinit(HtmlNode *self) { } typedef struct { + HtmlNode *root_node; HtmlNode *current_node; HtmlAttribute *current_node_last_attribute; } ParseUserdata; @@ -106,7 +98,7 @@ static int parse_callback(HtmlParser *html_parser, HtmlParseType parse_type, voi } case HTML_PARSE_TAG_END: { HtmlNode *parent_node = parse_userdata->current_node->parent; - if(parent_node) { + if(parent_node && parent_node != parse_userdata->root_node) { parse_userdata->current_node = parent_node; parse_userdata->current_node_last_attribute = NULL; } @@ -148,6 +140,7 @@ int html_parse_to_tree(HtmlTree *self, const char *html_source, size_t len) { ParseUserdata parse_userdata; html_node_init(&self->root_node); + parse_userdata.root_node = &self->root_node; parse_userdata.current_node = &self->root_node; parse_userdata.current_node_last_attribute = NULL; result = html_parser_parse(html_source, len, parse_callback, &parse_userdata); diff --git a/tests/main.c b/tests/main.c index 3b22f8a..4c9e93c 100644 --- a/tests/main.c +++ b/tests/main.c @@ -30,7 +30,7 @@ static void html_attributes_print(HtmlAttribute *attr) { 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); + html_node_print(&node_child->node); node_child = node_child->next; } } |