Skip to content

Commit

Permalink
Support tg-spoiler HTML tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Dec 31, 2021
1 parent 62d4e33 commit 19f1f66
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 2 additions & 3 deletions td/telegram/MessageEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2938,7 +2938,7 @@ static Result<vector<MessageEntity>> do_parse_html(CSlice text, string &result)
string tag_name = to_lower(text.substr(begin_pos + 1, i - begin_pos - 1));
if (tag_name != "a" && tag_name != "b" && tag_name != "strong" && tag_name != "i" && tag_name != "em" &&
tag_name != "s" && tag_name != "strike" && tag_name != "del" && tag_name != "u" && tag_name != "ins" &&
tag_name != "span" && tag_name != "pre" && tag_name != "code") {
tag_name != "tg-spoiler" && tag_name != "span" && tag_name != "pre" && tag_name != "code") {
return Status::Error(400, PSLICE()
<< "Unsupported start tag \"" << tag_name << "\" at byte offset " << begin_pos);
}
Expand Down Expand Up @@ -3059,8 +3059,7 @@ static Result<vector<MessageEntity>> do_parse_html(CSlice text, string &result)
entities.emplace_back(MessageEntity::Type::Strikethrough, entity_offset, entity_length);
} else if (tag_name == "u" || tag_name == "ins") {
entities.emplace_back(MessageEntity::Type::Underline, entity_offset, entity_length);
} else if (tag_name == "span") {
CHECK(nested_entities.back().argument == "spoiler");
} else if (tag_name == "tg-spoiler" || (tag_name == "span" && nested_entities.back().argument == "spoiler")) {
entities.emplace_back(MessageEntity::Type::Spoiler, entity_offset, entity_length);
} else if (tag_name == "a") {
auto url = std::move(nested_entities.back().argument);
Expand Down
7 changes: 7 additions & 0 deletions test/message_entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,13 @@ TEST(MessageEntities, parse_html) {
check_parse_html("🏟 🏟<span class=\"tg-spoiler\">🏟 &gt;<b aba = caba>&lt🏟</b></span>",
"🏟 🏟🏟 ><🏟",
{{td::MessageEntity::Type::Spoiler, 5, 7}, {td::MessageEntity::Type::Bold, 9, 3}});
check_parse_html("➡️ ➡️<tg-spoiler>➡️ ➡️</tg-spoiler><b>➡️ ➡️</b>",
"➡️ ➡️➡️ ➡️➡️ ➡️",
{{td::MessageEntity::Type::Spoiler, 5, 5}, {td::MessageEntity::Type::Bold, 10, 5}});
check_parse_html("🏟 🏟<tg-spoiler>🏟 &lt🏟</tg-spoiler>", "🏟 🏟🏟 <🏟",
{{td::MessageEntity::Type::Spoiler, 5, 6}});
check_parse_html("🏟 🏟<tg-spoiler>🏟 &gt;<b aba = caba>&lt🏟</b></tg-spoiler>", "🏟 🏟🏟 ><🏟",
{{td::MessageEntity::Type::Spoiler, 5, 7}, {td::MessageEntity::Type::Bold, 9, 3}});
check_parse_html("<a href=telegram.org>\t</a>", "\t",
{{td::MessageEntity::Type::TextUrl, 0, 1, "http://telegram.org/"}});
check_parse_html("<a href=telegram.org>\r</a>", "\r",
Expand Down

0 comments on commit 19f1f66

Please sign in to comment.