Allow pre, TextUrl and TextMention entities, containing only new lines.

This commit is contained in:
levlam 2021-06-09 20:35:02 +03:00
parent 3614425c8a
commit 8b87e8ed56
2 changed files with 6 additions and 11 deletions

View File

@ -3514,7 +3514,6 @@ static std::pair<size_t, int32> remove_invalid_entities(const string &text, vect
size_t last_non_whitespace_pos = text.size();
int32 utf16_offset = 0;
int32 last_space_utf16_offset = -1;
int32 last_non_whitespace_utf16_offset = -1;
td::remove_if(entities, [](const auto &entity) { return entity.length == 0; });
@ -3527,10 +3526,9 @@ static std::pair<size_t, int32> remove_invalid_entities(const string &text, vect
break;
}
if (last_non_whitespace_utf16_offset >= entity->offset ||
(last_space_utf16_offset >= entity->offset && is_hidden_data_entity(entity->type))) {
// TODO check entity for validness, for example, that mentions, hashtags, cashtags and URLs are valid
if (last_non_whitespace_utf16_offset >= entity->offset || is_hidden_data_entity(entity->type)) {
// keep entity
// TODO check entity for validness, for example, that mentions, hashtags, cashtags and URLs are valid
} else {
entity->length = 0;
}
@ -3566,9 +3564,7 @@ static std::pair<size_t, int32> remove_invalid_entities(const string &text, vect
auto c = static_cast<unsigned char>(text[pos]);
switch (c) {
case '\n':
break;
case 32:
last_space_utf16_offset = utf16_offset;
break;
default:
while (!is_utf8_character_first_code_unit(static_cast<unsigned char>(text[pos + 1]))) {

View File

@ -783,9 +783,8 @@ TEST(MessageEntities, fix_formatted_text) {
td::vector<td::MessageEntity> fixed_entities;
if (fixed_length > 0) {
for (auto i = 0; i < length; i++) {
if (str[offset + i] != '\r' && str[offset + i] != '\n' &&
(str[offset + i] != ' ' || type == td::MessageEntity::Type::TextUrl ||
type == td::MessageEntity::Type::MentionName)) {
if (!td::is_space(str[offset + i]) || type == td::MessageEntity::Type::TextUrl ||
type == td::MessageEntity::Type::MentionName) {
fixed_entities.emplace_back(type, fixed_offset, fixed_length);
break;
}
@ -1413,7 +1412,7 @@ TEST(MessageEntities, parse_markdown_v3) {
check_parse_markdown_v3("` `", " ", {{td::MessageEntity::Type::Code, 0, 1}});
check_parse_markdown_v3("`\n`", "\n", {{td::MessageEntity::Type::Code, 0, 1}});
check_parse_markdown_v3("` `a", " a", {{td::MessageEntity::Type::Code, 0, 1}}, true);
check_parse_markdown_v3("`\n`a", "\na", {}, true);
check_parse_markdown_v3("`\n`a", "\na", {{td::MessageEntity::Type::Code, 0, 1}}, true);
check_parse_markdown_v3("``", "``", {});
check_parse_markdown_v3("`a````b```", "`a````b```", {});
check_parse_markdown_v3("ab", {{td::MessageEntity::Type::Code, 0, 1}, {td::MessageEntity::Type::Pre, 1, 1}}, "ab",
@ -1428,7 +1427,7 @@ TEST(MessageEntities, parse_markdown_v3) {
"[ ](t.me) [ ](t.me)", {{td::MessageEntity::Type::TextUrl, 8, 1, "http://t.me/"}, {10, 1, td::UserId(1)}},
"[ ](t.me) [ ](t.me)", {{td::MessageEntity::Type::TextUrl, 8, 1, "http://t.me/"}, {10, 1, td::UserId(1)}});
check_parse_markdown_v3("[\n](t.me)", "\n", {{td::MessageEntity::Type::TextUrl, 0, 1, "http://t.me/"}});
check_parse_markdown_v3("[\n](t.me)a", "\na", {}, true);
check_parse_markdown_v3("[\n](t.me)a", "\na", {{td::MessageEntity::Type::TextUrl, 0, 1, "http://t.me/"}}, true);
check_parse_markdown_v3("asd[abcd](google.com)", {{td::MessageEntity::Type::Italic, 0, 5}}, "asdabcd",
{{td::MessageEntity::Type::Italic, 0, 3},
{td::MessageEntity::Type::TextUrl, 3, 4, "http://google.com/"},