From: Vsevolod Stakhov Date: Wed, 26 May 2021 20:31:32 +0000 (+0100) Subject: [Rework] Html: Convert to variant X-Git-Tag: 3.0~365 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d734f2119237f0df39c1f2475a36701ffe2c985d;p=rspamd.git [Rework] Html: Convert to variant --- diff --git a/src/libserver/html/html.cxx b/src/libserver/html/html.cxx index b68f40360..2ebf46305 100644 --- a/src/libserver/html/html.cxx +++ b/src/libserver/html/html.cxx @@ -696,7 +696,7 @@ html_process_url_tag(rspamd_mempool_t *pool, auto url = html_process_url(pool, href_value); - if (url && tag->extra == nullptr) { + if (url && std::holds_alternative(tag->extra)) { tag->extra = url.value(); } @@ -1551,7 +1551,7 @@ html_process_block_tag(rspamd_mempool_t *pool, struct html_tag *tag, } g_ptr_array_add(hc->blocks, bl); - tag->extra = bl; + tag->block = bl; } static auto @@ -1578,13 +1578,13 @@ static auto html_propagate_style(struct html_content *hc, struct html_tag *tag, struct html_block *bl, - GQueue *blocks) -> void + std::vector &blocks) -> void { struct html_block *bl_parent; gboolean push_block = FALSE; /* Propagate from the parent if needed */ - bl_parent = static_cast(g_queue_peek_tail(blocks)); + bl_parent = blocks.back(); if (bl_parent) { if (!bl->background_color.valid) { @@ -1647,7 +1647,7 @@ html_propagate_style(struct html_content *hc, } if (push_block && !(tag->flags & FL_CLOSED)) { - g_queue_push_tail(blocks, bl); + blocks.push_back(bl); } } @@ -1680,7 +1680,7 @@ html_process_part_full (rspamd_mempool_t *pool, struct rspamd_url *url = NULL; gint len, href_offset = -1; struct html_tag *cur_tag = NULL, *content_tag = NULL; - GQueue *styles_blocks; + std::vector styles_blocks; struct tag_content_parser_state content_parser_env; tags_vector *all_tags; @@ -1719,7 +1719,7 @@ html_process_part_full (rspamd_mempool_t *pool, hc->bgcolor.valid = TRUE; dest = g_byte_array_sized_new (in->len / 3 * 2); - styles_blocks = g_queue_new (); + styles_blocks.reserve(32); p = in->data; c = p; @@ -2199,8 +2199,8 @@ html_process_part_full (rspamd_mempool_t *pool, if (prev_tag->id == Tag_A && !(prev_tag->flags & (FL_CLOSING)) && - prev_tag->extra) { - prev_url = static_cast(prev_tag->extra); + std::holds_alternative(prev_tag->extra)) { + prev_url = std::get(prev_tag->extra); std::string_view disp_part{ reinterpret_cast(dest->data + href_offset), @@ -2265,13 +2265,13 @@ html_process_part_full (rspamd_mempool_t *pool, if (cur_tag->flags & FL_CLOSING) { /* Just remove block element from the queue if any */ - if (styles_blocks->length > 0) { - g_queue_pop_tail (styles_blocks); + if (!styles_blocks.empty()) { + styles_blocks.pop_back(); } } else { html_process_block_tag(pool, cur_tag, hc); - bl = static_cast(cur_tag->extra); + bl = cur_tag->block; if (bl) { html_propagate_style(hc, cur_tag, @@ -2312,7 +2312,6 @@ html_process_part_full (rspamd_mempool_t *pool, html_propagate_lengths, NULL); } - g_queue_free (styles_blocks); hc->parsed = dest; return dest; diff --git a/src/libserver/html/html_tag.hxx b/src/libserver/html/html_tag.hxx index 01fce796a..3375931d1 100644 --- a/src/libserver/html/html_tag.hxx +++ b/src/libserver/html/html_tag.hxx @@ -20,6 +20,7 @@ #include #include +#include #include namespace rspamd::html { @@ -38,6 +39,8 @@ enum class html_component_type : std::uint8_t { RSPAMD_HTML_COMPONENT_ALT, }; +using html_tag_extra_t = std::variant; + struct html_tag { gint id; gint flags; @@ -47,7 +50,8 @@ struct html_tag { std::string_view name; robin_hood::unordered_flat_map parameters; - gpointer extra; /* TODO: convert to variant */ + html_tag_extra_t extra; + struct html_block *block; /* TODO: temporary, must be handled by css */ GNode *parent; };