From fab7a00da328debef4fbeba9eb21fc02cf21675d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 19 Aug 2023 13:26:46 +0200 Subject: Update html-parser version to latest, add html_node_find_child --- src/HtmlTree.c | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'src/HtmlTree.c') diff --git a/src/HtmlTree.c b/src/HtmlTree.c index faef08a..4c1628c 100644 --- a/src/HtmlTree.c +++ b/src/HtmlTree.c @@ -1,5 +1,6 @@ #include "../include/HtmlTree.h" #include +#include #include static void html_node_deinit(HtmlNode *self); @@ -123,7 +124,11 @@ static int parse_callback(HtmlParser *html_parser, HtmlParseType parse_type, voi } case HTML_PARSE_TEXT: case HTML_PARSE_JAVASCRIPT_CODE: { - HtmlNode *new_node = html_node_create(html_parser->text_stripped, parse_userdata->current_node, parse_type == HTML_PARSE_TEXT ? HTML_NODE_TEXT : HTML_NODE_JS); + HtmlNode *new_node; + if(html_parser->text_stripped.size == 0) + return 0; + + new_node = html_node_create(html_parser->text_stripped, parse_userdata->current_node, parse_type == HTML_PARSE_TEXT ? HTML_NODE_TEXT : HTML_NODE_JS); if(!new_node) return 1; @@ -162,3 +167,37 @@ HtmlAttribute* html_node_get_attribute_by_name(HtmlNode *self, HtmlStringView na } return NULL; } + +HtmlNode* html_node_find_child(HtmlNode *self, const char *tag_name, const char *attribute_name, const char *attribute_value) { + HtmlStringView tag; + HtmlStringView attr_name; + HtmlStringView attr_value; + HtmlNodeChild *child; + + tag.data = tag_name; + tag.size = strlen(tag_name); + + attr_name.data = attribute_name; + attr_name.size = strlen(attribute_name); + + attr_value.data = attribute_value; + attr_value.size = strlen(attribute_value); + + for(child = self->first_child; child; child = child->next) { + HtmlAttribute *attr; + if(child->node.node_type != HTML_NODE_NODE) + continue; + + if(!html_string_view_equals_case_insensitive(&child->node.name_or_value, &tag)) + continue; + + attr = html_node_get_attribute_by_name(&child->node, attr_name); + if(!attr) + continue; + + if(html_string_view_equals_case_insensitive(&attr->value, &attr_value)) + return &child->node; + } + + return NULL; +} -- cgit v1.2.3