Inproved HTMLParserTag's to_string a bit.

This commit is contained in:
Relintai 2021-11-18 12:21:47 +01:00
parent 7ce989d12b
commit 2dac93662c
2 changed files with 28 additions and 11 deletions

View File

@ -186,21 +186,35 @@ void HTMLParserTag::parse_args(const String &args) {
} }
} }
String HTMLParserTag::to_string() { String HTMLParserTag::to_string(const int level) {
String s; String s;
for (int i = 0; i < level; ++i) {
s += " ";
}
if (type == HTML_PARSER_TAG_TYPE_CONTENT) { if (type == HTML_PARSER_TAG_TYPE_CONTENT) {
s = data; s = data;
} else if (type == HTML_PARSER_TAG_TYPE_OPENING_TAG) { } else if (type == HTML_PARSER_TAG_TYPE_OPENING_TAG) {
int ln = level + 1;
s = "<" + tag; s = "<" + tag;
for (int i = 0; i < attributes.size(); ++i) { for (int i = 0; i < attributes.size(); ++i) {
s += " " + attributes[i]->to_string(); s += " " + attributes[i]->to_string();
} }
s += ">"; s += ">\n";
for (int i = 0; i < tags.size(); ++i) {
s += tags[i]->to_string(ln);
}
s += "</" + tag + ">\n";
} else if (type == HTML_PARSER_TAG_TYPE_CLOSING_TAG) { } else if (type == HTML_PARSER_TAG_TYPE_CLOSING_TAG) {
s = "</" + tag + ">"; //HTMLParserTag should handle this automatically
//it's here for debugging purposes though
s = "</" + tag + "(!)>";
} else if (type == HTML_PARSER_TAG_TYPE_SELF_CLOSING_TAG) { } else if (type == HTML_PARSER_TAG_TYPE_SELF_CLOSING_TAG) {
s = "<" + tag; s = "<" + tag;
@ -208,15 +222,11 @@ String HTMLParserTag::to_string() {
s += " " + attributes[i]->to_string(); s += " " + attributes[i]->to_string();
} }
s += "/>"; s += "/>\n";
} else if (type == HTML_PARSER_TAG_TYPE_COMMENT) { } else if (type == HTML_PARSER_TAG_TYPE_COMMENT) {
s = "<!-- " + data + " -->"; s = "<!-- " + data + " -->\n";
} else if (type == HTML_PARSER_TAG_TYPE_DOCTYPE) { } else if (type == HTML_PARSER_TAG_TYPE_DOCTYPE) {
s = "<!doctype " + data + ">"; s = data + "\n";
}
for (int i = 0; i < tags.size(); ++i) {
s += tags[i]->to_string();
} }
return s; return s;
@ -275,9 +285,16 @@ void HTMLParser::parse(const String &data) {
} }
} }
if (root) {
delete root;
}
root = new HTMLParserTag();
//process tags into hierarchical order //process tags into hierarchical order
Vector<HTMLParserTag *> tag_stack; Vector<HTMLParserTag *> tag_stack;
for (int i = 0; i < tags.size(); ++i) { for (int i = 0; i < tags.size(); ++i) {
tags[i]->print();
} }
} }

View File

@ -40,7 +40,7 @@ public:
void process(); void process();
void parse_args(const String &args); void parse_args(const String &args);
String to_string(); String to_string(const int level = 0);
void print(); void print();
HTMLParserTag(); HTMLParserTag();