text_entity.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "base/qt/qt_compare.h"
  9. #include "base/basic_types.h"
  10. #include "base/algorithm.h"
  11. #include <QtCore/QList>
  12. #include <QtCore/QVector>
  13. #include <QtGui/QClipboard>
  14. enum class EntityType : uchar {
  15. Invalid = 0,
  16. Url,
  17. CustomUrl,
  18. Email,
  19. Hashtag,
  20. Cashtag,
  21. Mention,
  22. MentionName,
  23. CustomEmoji,
  24. BotCommand,
  25. MediaTimestamp,
  26. Colorized, // Senders in chat list, attachments in chat list, etc.
  27. Phone,
  28. Bold,
  29. Semibold,
  30. Italic,
  31. Underline,
  32. StrikeOut,
  33. Code, // inline
  34. Pre, // block
  35. Blockquote,
  36. Spoiler,
  37. };
  38. enum class EntityLinkShown : uchar {
  39. Full,
  40. Partial,
  41. };
  42. struct EntityLinkData {
  43. QString text;
  44. QString data;
  45. EntityType type = EntityType::Invalid;
  46. EntityLinkShown shown = EntityLinkShown::Full;
  47. friend inline auto operator<=>(
  48. const EntityLinkData &,
  49. const EntityLinkData &) = default;
  50. friend inline bool operator==(
  51. const EntityLinkData &,
  52. const EntityLinkData &) = default;
  53. };
  54. class EntityInText;
  55. using EntitiesInText = QVector<EntityInText>;
  56. class EntityInText {
  57. public:
  58. EntityInText(
  59. EntityType type,
  60. int offset,
  61. int length,
  62. const QString &data = QString());
  63. [[nodiscard]] EntityType type() const {
  64. return _type;
  65. }
  66. [[nodiscard]] int offset() const {
  67. return _offset;
  68. }
  69. [[nodiscard]] int length() const {
  70. return _length;
  71. }
  72. [[nodiscard]] QString data() const {
  73. return _data;
  74. }
  75. void extendToLeft(int extent) {
  76. _offset -= extent;
  77. _length += extent;
  78. }
  79. void shrinkFromRight(int shrink) {
  80. _length -= shrink;
  81. }
  82. void shiftLeft(int shift) {
  83. _offset -= shift;
  84. if (_offset < 0) {
  85. _length += _offset;
  86. _offset = 0;
  87. if (_length < 0) {
  88. _length = 0;
  89. }
  90. }
  91. }
  92. void shiftRight(int shift) {
  93. _offset += shift;
  94. }
  95. void updateTextEnd(int textEnd) {
  96. if (_offset > textEnd) {
  97. _offset = textEnd;
  98. _length = 0;
  99. } else if (_offset + _length > textEnd) {
  100. _length = textEnd - _offset;
  101. }
  102. }
  103. [[nodiscard]] static int FirstMonospaceOffset(
  104. const EntitiesInText &entities,
  105. int textLength);
  106. explicit operator bool() const {
  107. return type() != EntityType::Invalid;
  108. }
  109. friend inline auto operator<=>(
  110. const EntityInText &,
  111. const EntityInText &) = default;
  112. friend inline bool operator==(
  113. const EntityInText &,
  114. const EntityInText &) = default;
  115. private:
  116. EntityType _type = EntityType::Invalid;
  117. int _offset = 0;
  118. int _length = 0;
  119. QString _data;
  120. };
  121. struct TextWithEntities {
  122. QString text;
  123. EntitiesInText entities;
  124. bool empty() const {
  125. return text.isEmpty();
  126. }
  127. void reserve(int size, int entitiesCount = 0) {
  128. text.reserve(size);
  129. entities.reserve(entitiesCount);
  130. }
  131. TextWithEntities &append(TextWithEntities &&other) {
  132. const auto shift = text.size();
  133. for (auto &entity : other.entities) {
  134. entity.shiftRight(shift);
  135. }
  136. text.append(other.text);
  137. entities.append(other.entities);
  138. return *this;
  139. }
  140. TextWithEntities &append(const TextWithEntities &other) {
  141. const auto shift = text.size();
  142. text.append(other.text);
  143. entities.reserve(entities.size() + other.entities.size());
  144. for (auto entity : other.entities) {
  145. entity.shiftRight(shift);
  146. entities.append(entity);
  147. }
  148. return *this;
  149. }
  150. TextWithEntities &append(const QString &other) {
  151. text.append(other);
  152. return *this;
  153. }
  154. TextWithEntities &append(QLatin1String other) {
  155. text.append(other);
  156. return *this;
  157. }
  158. TextWithEntities &append(QChar other) {
  159. text.append(other);
  160. return *this;
  161. }
  162. static TextWithEntities Simple(const QString &simple) {
  163. auto result = TextWithEntities();
  164. result.text = simple;
  165. return result;
  166. }
  167. friend inline auto operator<=>(
  168. const TextWithEntities &,
  169. const TextWithEntities &) = default;
  170. friend inline bool operator==(
  171. const TextWithEntities &,
  172. const TextWithEntities &) = default;
  173. };
  174. struct TextForMimeData {
  175. QString expanded;
  176. TextWithEntities rich;
  177. bool empty() const {
  178. return expanded.isEmpty();
  179. }
  180. void reserve(int size, int entitiesCount = 0) {
  181. expanded.reserve(size);
  182. rich.reserve(size, entitiesCount);
  183. }
  184. TextForMimeData &append(TextForMimeData &&other) {
  185. expanded.append(other.expanded);
  186. rich.append(std::move(other.rich));
  187. return *this;
  188. }
  189. TextForMimeData &append(TextWithEntities &&other) {
  190. expanded.append(other.text);
  191. rich.append(std::move(other));
  192. return *this;
  193. }
  194. TextForMimeData &append(const QString &other) {
  195. expanded.append(other);
  196. rich.append(other);
  197. return *this;
  198. }
  199. TextForMimeData &append(QLatin1String other) {
  200. expanded.append(other);
  201. rich.append(other);
  202. return *this;
  203. }
  204. TextForMimeData &append(QChar other) {
  205. expanded.append(other);
  206. rich.append(other);
  207. return *this;
  208. }
  209. static TextForMimeData WithExpandedLinks(const TextWithEntities &text);
  210. static TextForMimeData Rich(TextWithEntities &&rich) {
  211. auto result = TextForMimeData();
  212. result.expanded = rich.text;
  213. result.rich = std::move(rich);
  214. return result;
  215. }
  216. static TextForMimeData Simple(const QString &simple) {
  217. auto result = TextForMimeData();
  218. result.expanded = result.rich.text = simple;
  219. return result;
  220. }
  221. };
  222. enum {
  223. TextParseMultiline = 0x001,
  224. TextParseLinks = 0x002,
  225. TextParseMentions = 0x004,
  226. TextParseHashtags = 0x008,
  227. TextParseBotCommands = 0x010,
  228. TextParseMarkdown = 0x020,
  229. TextParseColorized = 0x040,
  230. };
  231. struct TextWithTags {
  232. struct Tag {
  233. int offset = 0;
  234. int length = 0;
  235. QString id;
  236. friend inline auto operator<=>(const Tag &, const Tag &) = default;
  237. friend inline bool operator==(const Tag &, const Tag &) = default;
  238. };
  239. using Tags = QVector<Tag>;
  240. QString text;
  241. Tags tags;
  242. [[nodiscard]] bool empty() const {
  243. return text.isEmpty();
  244. }
  245. friend inline auto operator<=>(
  246. const TextWithTags &,
  247. const TextWithTags &) = default;
  248. friend inline bool operator==(
  249. const TextWithTags &,
  250. const TextWithTags &) = default;
  251. };
  252. // Parsing helpers.
  253. namespace TextUtilities {
  254. bool IsValidProtocol(const QString &protocol);
  255. bool IsValidTopDomain(const QString &domain);
  256. const QRegularExpression &RegExpMailNameAtEnd();
  257. const QRegularExpression &RegExpHashtag(bool allowWithMention);
  258. const QRegularExpression &RegExpHashtagExclude();
  259. const QRegularExpression &RegExpMention();
  260. const QRegularExpression &RegExpBotCommand();
  261. const QRegularExpression &RegExpDigitsExclude();
  262. QString MarkdownBoldGoodBefore();
  263. QString MarkdownBoldBadAfter();
  264. QString MarkdownItalicGoodBefore();
  265. QString MarkdownItalicBadAfter();
  266. QString MarkdownStrikeOutGoodBefore();
  267. QString MarkdownStrikeOutBadAfter();
  268. QString MarkdownCodeGoodBefore();
  269. QString MarkdownCodeBadAfter();
  270. QString MarkdownPreGoodBefore();
  271. QString MarkdownPreBadAfter();
  272. QString MarkdownSpoilerGoodBefore();
  273. QString MarkdownSpoilerBadAfter();
  274. // Text preprocess.
  275. QString EscapeForRichParsing(const QString &text);
  276. QString SingleLine(const QString &text);
  277. TextWithEntities SingleLine(const TextWithEntities &text);
  278. QString RemoveAccents(const QString &text);
  279. QString RemoveEmoji(const QString &text);
  280. QString NameSortKey(const QString &text);
  281. QStringList PrepareSearchWords(const QString &query, const QRegularExpression *SplitterOverride = nullptr);
  282. bool CutPart(TextWithEntities &sending, TextWithEntities &left, int limit);
  283. struct MentionNameFields {
  284. uint64 selfId = 0;
  285. uint64 userId = 0;
  286. uint64 accessHash = 0;
  287. };
  288. [[nodiscard]] MentionNameFields MentionNameDataToFields(QStringView data);
  289. [[nodiscard]] QString MentionNameDataFromFields(
  290. const MentionNameFields &fields);
  291. // New entities are added to the ones that are already in result.
  292. // Changes text if (flags & TextParseMarkdown).
  293. TextWithEntities ParseEntities(const QString &text, int32 flags);
  294. void ParseEntities(TextWithEntities &result, int32 flags);
  295. void PrepareForSending(TextWithEntities &result, int32 flags);
  296. void Trim(TextWithEntities &result);
  297. enum class PrepareTextOption {
  298. IgnoreLinks,
  299. CheckLinks,
  300. };
  301. inline QString PrepareForSending(const QString &text, PrepareTextOption option = PrepareTextOption::IgnoreLinks) {
  302. auto result = TextWithEntities { text };
  303. auto prepareFlags = (option == PrepareTextOption::CheckLinks) ? (TextParseLinks | TextParseMentions | TextParseHashtags | TextParseBotCommands) : 0;
  304. PrepareForSending(result, prepareFlags);
  305. return result.text;
  306. }
  307. // Replace bad symbols with space and remove '\r'.
  308. void ApplyServerCleaning(TextWithEntities &result);
  309. [[nodiscard]] int SerializeTagsSize(const TextWithTags::Tags &tags);
  310. [[nodiscard]] QByteArray SerializeTags(const TextWithTags::Tags &tags);
  311. [[nodiscard]] TextWithTags::Tags DeserializeTags(
  312. QByteArray data,
  313. int textLength);
  314. [[nodiscard]] QString TagsMimeType();
  315. [[nodiscard]] QString TagsTextMimeType();
  316. inline const auto kMentionTagStart = qstr("mention://");
  317. [[nodiscard]] bool IsMentionLink(QStringView link);
  318. [[nodiscard]] QString MentionEntityData(QStringView link);
  319. [[nodiscard]] bool IsSeparateTag(QStringView tag);
  320. [[nodiscard]] QString JoinTag(const QList<QStringView> &list);
  321. [[nodiscard]] QList<QStringView> SplitTags(QStringView tag);
  322. [[nodiscard]] QString TagWithRemoved(
  323. const QString &tag,
  324. const QString &removed);
  325. [[nodiscard]] QString TagWithAdded(const QString &tag, const QString &added);
  326. [[nodiscard]] TextWithTags::Tags SimplifyTags(TextWithTags::Tags tags);
  327. EntitiesInText ConvertTextTagsToEntities(const TextWithTags::Tags &tags);
  328. TextWithTags::Tags ConvertEntitiesToTextTags(
  329. const EntitiesInText &entities);
  330. std::unique_ptr<QMimeData> MimeDataFromText(const TextForMimeData &text);
  331. std::unique_ptr<QMimeData> MimeDataFromText(TextWithTags &&text);
  332. void SetClipboardText(
  333. const TextForMimeData &text,
  334. QClipboard::Mode mode = QClipboard::Clipboard);
  335. } // namespace TextUtilities