Add support for <span class="tg-spoiler">

This commit is contained in:
Daniil Gentili 2024-05-09 20:33:35 +02:00
parent c8dbbe7101
commit 5202723d6d
2 changed files with 19 additions and 3 deletions

View File

@ -316,6 +316,9 @@ final class Entities
'pre' => $node->hasAttribute('language')
? ['type' => 'pre', 'language' => $node->getAttribute('language')]
: ['type' => 'pre'],
'span' => $node->hasAttribute('class') && $node->getAttribute('class') === 'tg-spoiler'
? ['type' => 'spoiler']
: null,
'tg-emoji' => ['type' => 'custom_emoji', 'custom_emoji_id' => (int) $node->getAttribute('emoji-id')],
'emoji' => ['type' => 'custom_emoji', 'custom_emoji_id' => (int) $node->getAttribute('id')],
'a' => self::handleLink($node->getAttribute('href')),
@ -384,7 +387,7 @@ final class Entities
"email" => '<a href="mailto:'.EntityTools::htmlEscape(EntityTools::mbSubstr($this->message, $offset, $length)).'">',
"phone" => '<a href="phone:'.EntityTools::htmlEscape(EntityTools::mbSubstr($this->message, $offset, $length)).'">',
"mention" => '<a href="https://t.me/'.EntityTools::htmlEscape(EntityTools::mbSubstr($this->message, $offset+1, $length-1)).'">',
"spoiler" => $allowTelegramTags ? '<tg-spoiler>' : '',
"spoiler" => $allowTelegramTags ? '<tg-spoiler>' : '<span class="tg-spoiler">',
"custom_emoji" => $allowTelegramTags ? '<tg-emoji emoji-id="'.$entity['custom_emoji_id'].'">' : '',
"text_mention" => $allowTelegramTags ? '<a href="tg://user?id='.$entity['user']['id'].'">' : '',
default => '',
@ -400,7 +403,7 @@ final class Entities
"strikethrough" => '</s>',
"underline" => '</u>',
"block_quote" => '</blockquote>',
"spoiler" => $allowTelegramTags ? '</tg-spoiler>' : '',
"spoiler" => $allowTelegramTags ? '</tg-spoiler>' : '</span>',
"custom_emoji" => $allowTelegramTags ? "</tg-emoji>" : '',
"text_mention" => $allowTelegramTags ? '</a>' : '',
default => '',

View File

@ -262,7 +262,7 @@ class EntitiesTest extends TestCase
yield [
'test',
'<tg-spoiler>test</tg-spoiler>',
'test',
'<span class="tg-spoiler">test</span>',
[[
'type' => 'spoiler',
'offset' => 0,
@ -618,6 +618,19 @@ class EntitiesTest extends TestCase
],
'<b>&apos;&quot;</b>',
],
[
'html',
'<span class="tg-spoiler">spoiler</span>',
'spoiler',
[
[
'offset' => 0,
'length' => 7,
'type' => 'spoiler',
],
],
'<tg-spoiler>spoiler</tg-spoiler>',
],
[
'html',
'<a href="mention:101374607">mention1</a> <a href="tg://user?id=101374607">mention2</a>',