Skip to content

Commit

Permalink
Add support for <span class="tg-spoiler">
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed May 9, 2024
1 parent c8dbbe7 commit 5202723
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/Entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ private static function parseNode(DOMNode|DOMText $node, int $offset, string &$m
'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')),
Expand Down Expand Up @@ -384,7 +387,7 @@ public function toHTML(bool $allowTelegramTags = false): string
"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 => '',
Expand All @@ -400,7 +403,7 @@ public function toHTML(bool $allowTelegramTags = false): string
"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 => '',
Expand Down
15 changes: 14 additions & 1 deletion tests/EntitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public static function provideHtmlEntities(): iterable
yield [
'test',
'<tg-spoiler>test</tg-spoiler>',
'test',
'<span class="tg-spoiler">test</span>',
[[
'type' => 'spoiler',
'offset' => 0,
Expand Down Expand Up @@ -618,6 +618,19 @@ private function provideEntities(): array
],
'<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>',
Expand Down

0 comments on commit 5202723

Please sign in to comment.