From 41206f68eb257e788a2d038e25f01d9deb7d37af Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 3 Jul 2021 00:18:06 +0200 Subject: Strip whitespace for attribute values as well --- include/quickmedia/HtmlSearch.h | 2 ++ src/HtmlSearch.c | 34 ++++++++++++++++++---------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/include/quickmedia/HtmlSearch.h b/include/quickmedia/HtmlSearch.h index 63f2175..1a7faca 100644 --- a/include/quickmedia/HtmlSearch.h +++ b/include/quickmedia/HtmlSearch.h @@ -53,12 +53,14 @@ typedef struct { /* Returns an empty string view if attribute doesn't exist or if it doesn't have any value. The result is only valid within the callback function scope. + The result is stripped of whitespace on the left and right side. */ QuickMediaStringView quickmedia_html_node_get_attribute_value(QuickMediaMatchNode *self, const char *attribute_name); /* Returns an empty string if the node doesn't have any text or if there was an error creating the text. The result is only valid within the callback function scope. + The result is stripped of whitespace on the left and right side. */ QuickMediaStringView quickmedia_html_node_get_text(QuickMediaMatchNode *self); diff --git a/src/HtmlSearch.c b/src/HtmlSearch.c index 89ee326..03d7e3a 100644 --- a/src/HtmlSearch.c +++ b/src/HtmlSearch.c @@ -329,22 +329,6 @@ static int html_parse_callback(HtmlParser *html_parser, HtmlParseType parse_type return 0; } -QuickMediaStringView quickmedia_html_node_get_attribute_value(QuickMediaMatchNode *self, const char *attribute_name) { - QuickMediaStringView attr_name; - attr_name.data = attribute_name; - attr_name.size = strlen(attribute_name); - - QuickMediaHtmlAttribute *attr = get_attribute_by_name(self->node, attr_name); - if(attr) { - return attr->value; - } else { - QuickMediaStringView attr_value; - attr_value.data = NULL; - attr_value.size = 0; - return attr_value; - } -} - static int is_whitespace(int c) { switch(c) { case ' ': @@ -384,6 +368,24 @@ static void strip(const char *str, size_t size, const char **output_str, size_t rstrip(*output_str, *output_size, output_size, strip_filter_func); } +QuickMediaStringView quickmedia_html_node_get_attribute_value(QuickMediaMatchNode *self, const char *attribute_name) { + QuickMediaStringView attr_name; + attr_name.data = attribute_name; + attr_name.size = strlen(attribute_name); + + QuickMediaHtmlAttribute *attr = get_attribute_by_name(self->node, attr_name); + if(attr) { + QuickMediaStringView attr_value = attr->value; + strip(attr_value.data, attr_value.size, &attr_value.data, &attr_value.size, is_whitespace); + return attr_value; + } else { + QuickMediaStringView attr_value; + attr_value.data = NULL; + attr_value.size = 0; + return attr_value; + } +} + static int merge_inner_text(QuickMediaHtmlNode *node, QuickMediaString *str) { if(node->is_tag) { int newline = 0; -- cgit v1.2.3