Custom non-internal links should go to entities.

This commit is contained in:
John Preston 2020-10-30 18:13:50 +03:00
parent 54a8c62bf5
commit 9c9a4bc4d8

View file

@ -535,6 +535,20 @@ bool Parser::checkEntities() {
const auto entityLength = _waitingEntity->length(); const auto entityLength = _waitingEntity->length();
const auto entityBegin = _start + _waitingEntity->offset(); const auto entityBegin = _start + _waitingEntity->offset();
const auto entityEnd = entityBegin + entityLength; const auto entityEnd = entityBegin + entityLength;
const auto pushSimpleUrl = [&](EntityType type) {
link.type = type;
link.data = QString(entityBegin, entityLength);
if (type == EntityType::Url) {
computeLinkText(link.data, &link.text, &link.shown);
} else {
link.text = link.data;
}
};
const auto pushComplexUrl = [&] {
link.type = entityType;
link.data = _waitingEntity->data();
link.text = QString(entityBegin, entityLength);
};
if (entityType == EntityType::Bold) { if (entityType == EntityType::Bold) {
flags = TextBlockFBold; flags = TextBlockFBold;
} else if (entityType == EntityType::Semibold) { } else if (entityType == EntityType::Semibold) {
@ -559,18 +573,17 @@ bool Parser::checkEntities() {
|| entityType == EntityType::Hashtag || entityType == EntityType::Hashtag
|| entityType == EntityType::Cashtag || entityType == EntityType::Cashtag
|| entityType == EntityType::BotCommand) { || entityType == EntityType::BotCommand) {
link.type = entityType; pushSimpleUrl(entityType);
link.data = QString(entityBegin, entityLength); } else if (entityType == EntityType::CustomUrl) {
if (link.type == EntityType::Url) { const auto url = _waitingEntity->data();
computeLinkText(link.data, &link.text, &link.shown); const auto text = QString(entityBegin, entityLength);
if (url == text) {
pushSimpleUrl(EntityType::Url);
} else { } else {
link.text = link.data; pushComplexUrl();
} }
} else if (entityType == EntityType::CustomUrl } else if (entityType == EntityType::MentionName) {
|| entityType == EntityType::MentionName) { pushComplexUrl();
link.type = entityType;
link.data = _waitingEntity->data();
link.text = QString(_start + _waitingEntity->offset(), _waitingEntity->length());
} }
if (link.type != EntityType::Invalid) { if (link.type != EntityType::Invalid) {
@ -3269,18 +3282,20 @@ TextForMimeData String::toText(
if (!composeExpanded && !composeEntities) { if (!composeExpanded && !composeEntities) {
return; return;
} }
const auto skipLink = (entity.type == EntityType::CustomUrl) const auto customTextLink = (entity.type == EntityType::CustomUrl);
&& (entity.data.startsWith(qstr("internal:")) const auto internalLink = customTextLink
|| (entity.data && entity.data.startsWith(qstr("internal:"));
== UrlClickHandler::EncodeForOpening(full.toString())));
if (composeExpanded) { if (composeExpanded) {
result.expanded.append(full); result.expanded.append(full);
if (entity.type == EntityType::CustomUrl && !skipLink) { const auto sameAsTextLink = customTextLink
&& (entity.data
== UrlClickHandler::EncodeForOpening(full.toString()));
if (customTextLink && !internalLink && !sameAsTextLink) {
const auto &url = entity.data; const auto &url = entity.data;
result.expanded.append(qstr(" (")).append(url).append(')'); result.expanded.append(qstr(" (")).append(url).append(')');
} }
} }
if (composeEntities && !skipLink) { if (composeEntities && !internalLink) {
insertEntity({ insertEntity({
entity.type, entity.type,
linkStart, linkStart,