Fix toHTML

This commit is contained in:
Daniil Gentili 2024-04-22 17:29:36 +02:00
parent 3ea855502b
commit 78773a4e60
2 changed files with 238 additions and 0 deletions

View File

@ -387,6 +387,7 @@ final class Entities
"spoiler" => $allowTelegramTags ? '<tg-spoiler>' : '', "spoiler" => $allowTelegramTags ? '<tg-spoiler>' : '',
"custom_emoji" => $allowTelegramTags ? '<tg-emoji emoji-id="'.$entity['custom_emoji_id'].'">' : '', "custom_emoji" => $allowTelegramTags ? '<tg-emoji emoji-id="'.$entity['custom_emoji_id'].'">' : '',
"text_mention" => $allowTelegramTags ? '<a href="tg://user?id='.$entity['user']['id'].'">' : '', "text_mention" => $allowTelegramTags ? '<a href="tg://user?id='.$entity['user']['id'].'">' : '',
default => '',
}; };
$offset += $length; $offset += $length;
$insertions[$offset] = match ($entity['type']) { $insertions[$offset] = match ($entity['type']) {
@ -401,6 +402,7 @@ final class Entities
"spoiler" => $allowTelegramTags ? '</tg-spoiler>' : '', "spoiler" => $allowTelegramTags ? '</tg-spoiler>' : '',
"custom_emoji" => $allowTelegramTags ? "</tg-emoji>" : '', "custom_emoji" => $allowTelegramTags ? "</tg-emoji>" : '',
"text_mention" => $allowTelegramTags ? '</a>' : '', "text_mention" => $allowTelegramTags ? '</a>' : '',
default => '',
} . ($insertions[$offset] ?? ''); } . ($insertions[$offset] ?? '');
} }
\ksort($insertions); \ksort($insertions);

View File

@ -10,6 +10,7 @@ use Amp\Http\Client\Request;
use AssertionError; use AssertionError;
use danog\TelegramEntities\Entities; use danog\TelegramEntities\Entities;
use danog\TelegramEntities\EntityTools; use danog\TelegramEntities\EntityTools;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/** @internal */ /** @internal */
@ -79,6 +80,241 @@ class EntitiesTest extends TestCase
$this->assertEmpty($test->entities); $this->assertEmpty($test->entities);
$this->assertSame('|', $test->message); $this->assertSame('|', $test->message);
} }
#[DataProvider('provideHtmlEntities')]
public function testToHtml(string $message, string $htmlTg, string $htmlNoTg, array $entities): void
{
$e = new Entities($message, $entities);
$this->assertEquals($htmlTg, $e->toHTML(true));
$this->assertEquals($htmlNoTg, $e->toHTML(false));
$this->assertEquals($htmlNoTg, $e->toHTML());
}
public static function provideHtmlEntities(): iterable
{
yield [
'test',
'test',
'test',
[[
'type' => 'bank_card',
'offset' => 0,
'length' => 4,
]]
];
yield [
'test',
'<blockquote>test</blockquote>',
'<blockquote>test</blockquote>',
[[
'type' => 'block_quote',
'offset' => 0,
'length' => 4,
]]
];
yield [
'test',
'<b>test</b>',
'<b>test</b>',
[[
'type' => 'bold',
'offset' => 0,
'length' => 4,
]]
];
yield [
'test',
'<b>t<i>es</i>t</b>',
'<b>t<i>es</i>t</b>',
[[
'type' => 'bold',
'offset' => 0,
'length' => 4,
], [
'type' => 'italic',
'offset' => 1,
'length' => 2,
]]
];
yield [
'test',
'test',
'test',
[[
'type' => 'bot_command',
'offset' => 0,
'length' => 4,
]]
];
yield [
'test',
'test',
'test',
[[
'type' => 'cashtag',
'offset' => 0,
'length' => 4,
]]
];
yield [
'test',
'<code>test</code>',
'<code>test</code>',
[[
'type' => 'code',
'offset' => 0,
'length' => 4,
]]
];
yield [
'test',
'<tg-emoji emoji-id="12345">test</tg-emoji>',
'test',
[[
'type' => 'custom_emoji',
'offset' => 0,
'length' => 4,
'custom_emoji_id' => 12345,
]]
];
yield [
'test',
'<a href="mailto:test">test</a>',
'<a href="mailto:test">test</a>',
[[
'type' => 'email',
'offset' => 0,
'length' => 4,
]]
];
yield [
'test',
'test',
'test',
[[
'type' => 'hashtag',
'offset' => 0,
'length' => 4,
]]
];
yield [
'test',
'<i>test</i>',
'<i>test</i>',
[[
'type' => 'italic',
'offset' => 0,
'length' => 4,
]]
];
yield [
'test',
'<a href="tg://user?id=12345">test</a>',
'test',
[[
'type' => 'text_mention',
'offset' => 0,
'length' => 4,
'user' => ['id' => 12345]
]]
];
yield [
'@test',
'<a href="https://t.me/test">@test</a>',
'<a href="https://t.me/test">@test</a>',
[[
'type' => 'mention',
'offset' => 0,
'length' => 5,
]]
];
yield [
'test',
'test',
'test',
[[
'type' => 'phone_number',
'offset' => 0,
'length' => 4,
]]
];
yield [
'test',
'<pre language="language">test</pre>',
'<pre language="language">test</pre>',
[[
'type' => 'pre',
'offset' => 0,
'length' => 4,
'language' => 'language',
]]
];
yield [
'test',
'<tg-spoiler>test</tg-spoiler>',
'test',
[[
'type' => 'spoiler',
'offset' => 0,
'length' => 4,
]]
];
yield [
'test',
'<s>test</s>',
'<s>test</s>',
[[
'type' => 'strikethrough',
'offset' => 0,
'length' => 4,
]]
];
yield [
'test',
'<a href="https://google.com">test</a>',
'<a href="https://google.com">test</a>',
[[
'type' => 'text_link',
'offset' => 0,
'length' => 4,
'url' => 'https://google.com',
]]
];
yield [
'test',
'<u>test</u>',
'<u>test</u>',
[[
'type' => 'underline',
'offset' => 0,
'length' => 4,
]]
];
yield [
'test',
'<a href="test">test</a>',
'<a href="test">test</a>',
[[
'type' => 'url',
'offset' => 0,
'length' => 4,
]]
];
}
private function testEntitiesInner(string $mode, string $html, string $bare, array $entities, ?string $htmlReverse = null): void private function testEntitiesInner(string $mode, string $html, string $bare, array $entities, ?string $htmlReverse = null): void
{ {
$result = self::render(message: $html, parse_mode: $mode); $result = self::render(message: $html, parse_mode: $mode);