diff --git a/event/formatter_listener.php b/event/formatter_listener.php index eb0c210..89a18bf 100644 --- a/event/formatter_listener.php +++ b/event/formatter_listener.php @@ -121,12 +121,7 @@ public function modify_tag_templates($event) { try { - $tag = $event['configurator']->tags['YOUTUBE']; - $tag->template = str_replace('www.youtube.com', 'www.youtube-nocookie.com', $tag->template); - if (!$this->sites->is_phpbb4()) - { - $tag->template = str_replace(' allowfullscreen', ' referrerpolicy="origin" allowfullscreen', $tag->template); - } + $this->configure_youtube($event['configurator']->tags['YOUTUBE']); } catch (\RuntimeException $e) { @@ -159,4 +154,31 @@ protected function get_site_ids() return $site_ids ? json_decode($site_ids, true) : []; } + + /** + * Apply YouTube privacy, compatibility, and Shorts rendering changes. + * + * @param s9e\TextFormatter\Configurator\Items\Tag $tag Tag to configure + * @return void + */ + public function configure_youtube($tag) + { + $template = str_replace('www.youtube.com', 'www.youtube-nocookie.com', (string) $tag->template); + if (!$this->sites->is_phpbb4()) + { + $template = str_replace(' allowfullscreen', ' referrerpolicy="origin" allowfullscreen', $template); + } + + $shorts_template = str_replace( + ['max-width:640px', 'padding-bottom:56.25%'], + ['max-width:360px', 'padding-bottom:177.777778%'], + $template + ); + + $tag->template = + '' . + '' . $shorts_template . '' . + '' . $template . '' . + ''; + } } diff --git a/tests/event/listener_test.php b/tests/event/listener_test.php index 6cca606..871145e 100644 --- a/tests/event/listener_test.php +++ b/tests/event/listener_test.php @@ -185,6 +185,9 @@ public function test_youtube_template_uses_phpbb3_compatibility_fixes() $template = $configurator->tags['YOUTUBE']->template; $this->assertStringContainsString('www.youtube-nocookie.com', $template); $this->assertStringContainsString('referrerpolicy="origin"', $template); + $this->assertStringContainsString('contains(., \'/shorts/\')', $template); + $this->assertStringContainsString('max-width:360px', $template); + $this->assertStringContainsString('padding-bottom:177.777778%', $template); } public function test_upstream_collection_is_skipped_on_phpbb4() diff --git a/tests/functional/media_embed_test.php b/tests/functional/media_embed_test.php index 4c95c92..47c5690 100644 --- a/tests/functional/media_embed_test.php +++ b/tests/functional/media_embed_test.php @@ -18,6 +18,7 @@ class media_embed_test extends \phpbb_functional_test_case { protected $youtubeId = 'PHzShhtkzEk'; + protected $youtubeShortsId = 'xer3HqUWLkw'; protected $ok_ru_Id = '549000643961'; protected static function setup_extensions() @@ -36,7 +37,7 @@ public function test_posting_media_bbcode() $post = $this->create_topic($forum_id, 'Media Embed Test Topic 1', "[media]https://youtu.be/$this->youtubeId[/media]"); $crawler = self::request('GET', "viewtopic.php?t={$post['topic_id']}&sid=$this->sid"); - self::assertStringContainsString("//www.youtube-nocookie.com/embed/$this->youtubeId", $crawler->filter("#post_content{$post['topic_id']} iframe")->attr('src')); + self::assertStringContainsString("//www.youtube-nocookie.com/embed/$this->youtubeId", $crawler->filter("#post_content{$post['post_id']} iframe")->attr('src')); } public function test_posting_custom_site() @@ -50,7 +51,30 @@ public function test_posting_custom_site() $post = $this->create_topic(2, 'Media Embed Custom Site Test Topic 1', "[media]https://ok.ru/video/$this->ok_ru_Id[/media]"); $crawler = self::request('GET', "viewtopic.php?t={$post['topic_id']}&sid=$this->sid"); - self::assertStringContainsString("//ok.ru/videoembed/$this->ok_ru_Id", $crawler->filter("#post_content{$post['topic_id']} iframe")->attr('src')); + self::assertStringContainsString("//ok.ru/videoembed/$this->ok_ru_Id", $crawler->filter("#post_content{$post['post_id']} iframe")->attr('src')); + } + + public function test_youtube_shorts_template() + { + $this->login(); + + $forum_id = 2; + + $post = $this->create_topic( + $forum_id, + 'YouTube Shorts vertical rendering test', + "https://youtube.com/shorts/$this->youtubeShortsId\nhttps://youtu.be/$this->youtubeId" + ); + $crawler = self::request('GET', "viewtopic.php?t={$post['topic_id']}&sid=$this->sid"); + $selector = '#post_content' . $post['post_id'] . ' span[data-s9e-mediaembed="youtube"]'; + $embeds = $crawler->filter($selector); + $wrappers = $crawler->filter($selector . ' > span'); + self::assertCount(2, $embeds); + self::assertCount(2, $wrappers); + self::assertStringContainsString('max-width:360px', $embeds->eq(0)->attr('style')); + self::assertStringContainsString('padding-bottom:177.777778%', $wrappers->eq(0)->attr('style')); + self::assertStringContainsString('max-width:640px', $embeds->eq(1)->attr('style')); + self::assertStringContainsString('padding-bottom:56.25%', $wrappers->eq(1)->attr('style')); } public function signatures_data()