Skip to content

Commit 78773a4

Browse files
committed
Fix toHTML
1 parent 3ea8555 commit 78773a4

File tree

2 files changed

+238
-0
lines changed

2 files changed

+238
-0
lines changed

src/Entities.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ public function toHTML(bool $allowTelegramTags = false): string
387387
"spoiler" => $allowTelegramTags ? '<tg-spoiler>' : '',
388388
"custom_emoji" => $allowTelegramTags ? '<tg-emoji emoji-id="'.$entity['custom_emoji_id'].'">' : '',
389389
"text_mention" => $allowTelegramTags ? '<a href="tg://user?id='.$entity['user']['id'].'">' : '',
390+
default => '',
390391
};
391392
$offset += $length;
392393
$insertions[$offset] = match ($entity['type']) {
@@ -401,6 +402,7 @@ public function toHTML(bool $allowTelegramTags = false): string
401402
"spoiler" => $allowTelegramTags ? '</tg-spoiler>' : '',
402403
"custom_emoji" => $allowTelegramTags ? "</tg-emoji>" : '',
403404
"text_mention" => $allowTelegramTags ? '</a>' : '',
405+
default => '',
404406
} . ($insertions[$offset] ?? '');
405407
}
406408
\ksort($insertions);

tests/EntitiesTest.php

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use AssertionError;
1111
use danog\TelegramEntities\Entities;
1212
use danog\TelegramEntities\EntityTools;
13+
use PHPUnit\Framework\Attributes\DataProvider;
1314
use PHPUnit\Framework\TestCase;
1415

1516
/** @internal */
@@ -79,6 +80,241 @@ public function testStandalone(): void
7980
$this->assertEmpty($test->entities);
8081
$this->assertSame('|', $test->message);
8182
}
83+
84+
#[DataProvider('provideHtmlEntities')]
85+
public function testToHtml(string $message, string $htmlTg, string $htmlNoTg, array $entities): void
86+
{
87+
$e = new Entities($message, $entities);
88+
$this->assertEquals($htmlTg, $e->toHTML(true));
89+
$this->assertEquals($htmlNoTg, $e->toHTML(false));
90+
$this->assertEquals($htmlNoTg, $e->toHTML());
91+
}
92+
public static function provideHtmlEntities(): iterable
93+
{
94+
yield [
95+
'test',
96+
'test',
97+
'test',
98+
[[
99+
'type' => 'bank_card',
100+
'offset' => 0,
101+
'length' => 4,
102+
]]
103+
];
104+
yield [
105+
'test',
106+
'<blockquote>test</blockquote>',
107+
'<blockquote>test</blockquote>',
108+
[[
109+
'type' => 'block_quote',
110+
'offset' => 0,
111+
'length' => 4,
112+
]]
113+
];
114+
yield [
115+
'test',
116+
'<b>test</b>',
117+
'<b>test</b>',
118+
[[
119+
'type' => 'bold',
120+
'offset' => 0,
121+
'length' => 4,
122+
]]
123+
];
124+
yield [
125+
'test',
126+
'<b>t<i>es</i>t</b>',
127+
'<b>t<i>es</i>t</b>',
128+
[[
129+
'type' => 'bold',
130+
'offset' => 0,
131+
'length' => 4,
132+
], [
133+
'type' => 'italic',
134+
'offset' => 1,
135+
'length' => 2,
136+
]]
137+
];
138+
yield [
139+
'test',
140+
'test',
141+
'test',
142+
[[
143+
'type' => 'bot_command',
144+
'offset' => 0,
145+
'length' => 4,
146+
]]
147+
];
148+
149+
yield [
150+
'test',
151+
'test',
152+
'test',
153+
[[
154+
'type' => 'cashtag',
155+
'offset' => 0,
156+
'length' => 4,
157+
]]
158+
];
159+
160+
yield [
161+
'test',
162+
'<code>test</code>',
163+
'<code>test</code>',
164+
[[
165+
'type' => 'code',
166+
'offset' => 0,
167+
'length' => 4,
168+
]]
169+
];
170+
171+
yield [
172+
'test',
173+
'<tg-emoji emoji-id="12345">test</tg-emoji>',
174+
'test',
175+
[[
176+
'type' => 'custom_emoji',
177+
'offset' => 0,
178+
'length' => 4,
179+
'custom_emoji_id' => 12345,
180+
]]
181+
];
182+
183+
yield [
184+
'test',
185+
'<a href="mailto:test">test</a>',
186+
'<a href="mailto:test">test</a>',
187+
[[
188+
'type' => 'email',
189+
'offset' => 0,
190+
'length' => 4,
191+
]]
192+
];
193+
194+
yield [
195+
'test',
196+
'test',
197+
'test',
198+
[[
199+
'type' => 'hashtag',
200+
'offset' => 0,
201+
'length' => 4,
202+
]]
203+
];
204+
205+
yield [
206+
'test',
207+
'<i>test</i>',
208+
'<i>test</i>',
209+
[[
210+
'type' => 'italic',
211+
'offset' => 0,
212+
'length' => 4,
213+
]]
214+
];
215+
216+
yield [
217+
'test',
218+
'<a href="tg://user?id=12345">test</a>',
219+
'test',
220+
[[
221+
'type' => 'text_mention',
222+
'offset' => 0,
223+
'length' => 4,
224+
'user' => ['id' => 12345]
225+
]]
226+
];
227+
228+
yield [
229+
'@test',
230+
'<a href="https://t.me/test">@test</a>',
231+
'<a href="https://t.me/test">@test</a>',
232+
[[
233+
'type' => 'mention',
234+
'offset' => 0,
235+
'length' => 5,
236+
]]
237+
];
238+
239+
yield [
240+
'test',
241+
'test',
242+
'test',
243+
[[
244+
'type' => 'phone_number',
245+
'offset' => 0,
246+
'length' => 4,
247+
]]
248+
];
249+
250+
yield [
251+
'test',
252+
'<pre language="language">test</pre>',
253+
'<pre language="language">test</pre>',
254+
[[
255+
'type' => 'pre',
256+
'offset' => 0,
257+
'length' => 4,
258+
'language' => 'language',
259+
]]
260+
];
261+
262+
yield [
263+
'test',
264+
'<tg-spoiler>test</tg-spoiler>',
265+
'test',
266+
[[
267+
'type' => 'spoiler',
268+
'offset' => 0,
269+
'length' => 4,
270+
]]
271+
];
272+
273+
yield [
274+
'test',
275+
'<s>test</s>',
276+
'<s>test</s>',
277+
[[
278+
'type' => 'strikethrough',
279+
'offset' => 0,
280+
'length' => 4,
281+
]]
282+
];
283+
284+
yield [
285+
'test',
286+
'<a href="https://google.com">test</a>',
287+
'<a href="https://google.com">test</a>',
288+
[[
289+
'type' => 'text_link',
290+
'offset' => 0,
291+
'length' => 4,
292+
'url' => 'https://google.com',
293+
]]
294+
];
295+
296+
yield [
297+
'test',
298+
'<u>test</u>',
299+
'<u>test</u>',
300+
[[
301+
'type' => 'underline',
302+
'offset' => 0,
303+
'length' => 4,
304+
]]
305+
];
306+
307+
yield [
308+
'test',
309+
'<a href="test">test</a>',
310+
'<a href="test">test</a>',
311+
[[
312+
'type' => 'url',
313+
'offset' => 0,
314+
'length' => 4,
315+
]]
316+
];
317+
}
82318
private function testEntitiesInner(string $mode, string $html, string $bare, array $entities, ?string $htmlReverse = null): void
83319
{
84320
$result = self::render(message: $html, parse_mode: $mode);

0 commit comments

Comments
 (0)