From e4e2bea570488a0569696e31841672ee51b54e11 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 19 Jul 2022 13:58:26 +0200 Subject: [PATCH] HTMLParser and it's helper classes inherit from Reference now. --- modules/web/html/html_parser.cpp | 156 ++++++++++++++----------------- modules/web/html/html_parser.h | 40 ++++---- 2 files changed, 95 insertions(+), 101 deletions(-) diff --git a/modules/web/html/html_parser.cpp b/modules/web/html/html_parser.cpp index ef626037c..509de84c4 100644 --- a/modules/web/html/html_parser.cpp +++ b/modules/web/html/html_parser.cpp @@ -1,6 +1,7 @@ #include "html_parser.h" #include "core/error_macros.h" +#include "core/log/logger.h" #include "core/print_string.h" bool HTMLParserAttribute::match_attrib(const String &attrib) { @@ -17,7 +18,7 @@ bool HTMLParserAttribute::contains_data(const String &d) { return data.find(d) != -1; } -String HTMLParserAttribute::to_string() { +String HTMLParserAttribute::convert_to_string() const { if (single) { return attribute; } @@ -29,8 +30,8 @@ String HTMLParserAttribute::to_string() { } } -void HTMLParserAttribute::print() { - print_verbose(to_string()); +void HTMLParserAttribute::print() const { + PLOG_MSG(convert_to_string()); } HTMLParserAttribute::HTMLParserAttribute() { @@ -40,53 +41,53 @@ HTMLParserAttribute::HTMLParserAttribute() { HTMLParserAttribute::~HTMLParserAttribute() { } -HTMLParserTag *HTMLParserTag::get_first(const String &t) { +Ref HTMLParserTag::get_first(const String &t) { if (tag == t) { - return this; + return Ref(this); } for (int i = 0; i < tags.size(); ++i) { - HTMLParserTag *ht = tags[i]->get_first(t); + Ref ht = tags.write[i]->get_first(t); - if (ht) { + if (ht.is_valid()) { return ht; } } - return nullptr; + return Ref(); } -HTMLParserTag *HTMLParserTag::get_first(const String &t, const String &attrib, const String &val) { +Ref HTMLParserTag::get_first(const String &t, const String &attrib, const String &val) { if (tag == t) { if (has_attribute(attrib, val)) { - return this; + return Ref(this); } } for (int i = 0; i < tags.size(); ++i) { - HTMLParserTag *ht = tags[i]->get_first(t, attrib, val); + Ref ht = tags.write[i]->get_first(t, attrib, val); - if (ht) { + if (ht.is_valid()) { return ht; } } - return nullptr; + return Ref(); } String HTMLParserTag::get_attribute_value(const String &attrib) { - HTMLParserAttribute *a = get_attribute(attrib); + Ref a = get_attribute(attrib); - if (a) { + if (a.is_valid()) { return a->data; } return ""; } -HTMLParserAttribute *HTMLParserTag::get_attribute(const String &attrib) { +Ref HTMLParserTag::get_attribute(const String &attrib) { for (int i = 0; i < attributes.size(); ++i) { - HTMLParserAttribute *a = attributes[i]; + Ref a = attributes[i]; if (a->match_attrib(attrib)) { return a; @@ -98,7 +99,7 @@ HTMLParserAttribute *HTMLParserTag::get_attribute(const String &attrib) { bool HTMLParserTag::has_attribute(const String &attrib) { for (int i = 0; i < attributes.size(); ++i) { - HTMLParserAttribute *a = attributes[i]; + Ref a = attributes[i]; if (a->match_attrib(attrib)) { return true; @@ -108,9 +109,9 @@ bool HTMLParserTag::has_attribute(const String &attrib) { return false; } -HTMLParserAttribute *HTMLParserTag::get_attribute(const String &attrib, const String &contains_val) { +Ref HTMLParserTag::get_attribute(const String &attrib, const String &contains_val) { for (int i = 0; i < attributes.size(); ++i) { - HTMLParserAttribute *a = attributes[i]; + Ref a = attributes[i]; if (a->match_attrib(attrib) && a->contains_data(contains_val)) { return a; @@ -122,7 +123,7 @@ HTMLParserAttribute *HTMLParserTag::get_attribute(const String &attrib, const St bool HTMLParserTag::has_attribute(const String &attrib, const String &contains_val) { for (int i = 0; i < attributes.size(); ++i) { - HTMLParserAttribute *a = attributes[i]; + Ref a = attributes[i]; if (a->match_attrib(attrib) && a->contains_data(contains_val)) { return true; @@ -240,7 +241,8 @@ void HTMLParserTag::parse_args(const String &args) { int equals_index = args.find_char('=', i); - HTMLParserAttribute *a = memnew(HTMLParserAttribute); + Ref a; + a.instance(); if (equals_index == -1) { a->attribute = args.substr(i, args.size() - i); @@ -300,7 +302,7 @@ void HTMLParserTag::parse_args(const String &args) { } } -String HTMLParserTag::to_string(const int level) { +String HTMLParserTag::convert_to_string(const int level) const { String s; s += String(" ").repeat(level); @@ -313,7 +315,7 @@ String HTMLParserTag::to_string(const int level) { s += "(!CONTENT TAG HAS TAGS!)\n"; for (int i = 0; i < tags.size(); ++i) { - s += tags[i]->to_string(level + 1) + "\n"; + s += tags[i]->convert_to_string(level + 1) + "\n"; } } } else if (type == HTML_PARSER_TAG_TYPE_OPENING_TAG) { @@ -322,13 +324,13 @@ String HTMLParserTag::to_string(const int level) { s += "<" + tag; for (int i = 0; i < attributes.size(); ++i) { - s += " " + attributes[i]->to_string(); + s += " " + attributes[i]->convert_to_string(); } s += ">\n"; for (int i = 0; i < tags.size(); ++i) { - s += tags[i]->to_string(ln); + s += tags[i]->convert_to_string(ln); } s += String(" ").repeat(level); @@ -344,14 +346,14 @@ String HTMLParserTag::to_string(const int level) { s += "(!CLOSING TAG HAS TAGS!)\n"; for (int i = 0; i < tags.size(); ++i) { - s += tags[i]->to_string(level + 1) + "\n"; + s += tags[i]->convert_to_string(level + 1) + "\n"; } } } else if (type == HTML_PARSER_TAG_TYPE_SELF_CLOSING_TAG) { s += "<" + tag; for (int i = 0; i < attributes.size(); ++i) { - s += " " + attributes[i]->to_string(); + s += " " + attributes[i]->convert_to_string(); } s += "/>\n"; @@ -361,7 +363,7 @@ String HTMLParserTag::to_string(const int level) { s += "(!SELF CLOSING TAG HAS TAGS!)\n"; for (int i = 0; i < tags.size(); ++i) { - s += tags[i]->to_string(level + 1) + "\n"; + s += tags[i]->convert_to_string(level + 1) + "\n"; } } } else if (type == HTML_PARSER_TAG_TYPE_COMMENT) { @@ -372,7 +374,7 @@ String HTMLParserTag::to_string(const int level) { s += "(!COMMENT TAG HAS TAGS!)\n"; for (int i = 0; i < tags.size(); ++i) { - s += tags[i]->to_string(level + 1) + "\n"; + s += tags[i]->convert_to_string(level + 1) + "\n"; } } } else if (type == HTML_PARSER_TAG_TYPE_DOCTYPE) { @@ -383,20 +385,20 @@ String HTMLParserTag::to_string(const int level) { s += "(!DOCTYPE TAG HAS TAGS!)\n"; for (int i = 0; i < tags.size(); ++i) { - s += tags[i]->to_string(level + 1) + "\n"; + s += tags[i]->convert_to_string(level + 1) + "\n"; } } } else if (type == HTML_PARSER_TAG_TYPE_NONE) { for (int i = 0; i < tags.size(); ++i) { - s += tags[i]->to_string(level) + "\n"; + s += tags[i]->convert_to_string(level) + "\n"; s += String(" ").repeat(level); } } return s; } -void HTMLParserTag::print() { - print_verbose(to_string()); +void HTMLParserTag::print() const { + PLOG_MSG(convert_to_string()); } HTMLParserTag::HTMLParserTag() { @@ -404,17 +406,12 @@ HTMLParserTag::HTMLParserTag() { } HTMLParserTag::~HTMLParserTag() { - for (int i = 0; i < tags.size(); ++i) { - memdelete(tags[i]); - } - - for (int i = 0; i < attributes.size(); ++i) { - memdelete(attributes[i]); - } + tags.clear(); + attributes.clear(); } void HTMLParser::parse(const String &data) { - Vector tags; + Vector> tags; //