mirror of
https://git.sekbaer.de/Friendica/friendica.git
synced 2025-06-06 15:24:27 +02:00
Merge pull request #14940 from haheute/support-youtube-without-www
Support YouTube links without www
This commit is contained in:
commit
dc0dee77d0
4 changed files with 123 additions and 31 deletions
|
@ -1210,13 +1210,13 @@ class BBCode
|
|||
*/
|
||||
private static function normalizeVideoLinks(string $text): string
|
||||
{
|
||||
$text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
|
||||
$text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
|
||||
$text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/shorts\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
|
||||
$text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
|
||||
$text = preg_replace("/\[youtube\]https?:\/\/(www\.)?youtube\.com\/watch\?v\=(.*?)\[\/youtube\]/ism", '[youtube]$2[/youtube]', $text);
|
||||
$text = preg_replace("/\[youtube\]https?:\/\/(www\.)?youtube\.com\/embed\/(.*?)\[\/youtube\]/ism", '[youtube]$2[/youtube]', $text);
|
||||
$text = preg_replace("/\[youtube\]https?:\/\/(www\.)?youtube\.com\/shorts\/(.*?)\[\/youtube\]/ism", '[youtube]$2[/youtube]', $text);
|
||||
$text = preg_replace("/\[youtube\]https?:\/\/youtu\.be\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
|
||||
|
||||
$text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $text);
|
||||
$text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $text);
|
||||
$text = preg_replace("/\[vimeo\]https?:\/\/player\.vimeo\.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $text);
|
||||
$text = preg_replace("/\[vimeo\]https?:\/\/vimeo\.com\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $text);
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
|
|
@ -121,11 +121,13 @@ class Markdown
|
|||
$s = str_replace('♲', html_entity_decode('♲', ENT_QUOTES, 'UTF-8'), $s);
|
||||
|
||||
//$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
|
||||
$s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism', '[youtube]$2[/youtube]', 'url', $s);
|
||||
$s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism', '[youtube]$1[/youtube]', 'url', $s);
|
||||
$s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/www.youtube.com\/shorts\/(.*?)\].*?\[\/url\]/ism', '[youtube]$1[/youtube]', 'url', $s);
|
||||
$s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism', '[vimeo]$2[/vimeo]', 'url', $s);
|
||||
$s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism', '[vimeo]$1[/vimeo]', 'url', $s);
|
||||
$s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/www\.youtube\.com\/watch\?v\=(.*?)\[\/url\]/ism', '[youtube]$2[/youtube]', 'url', $s);
|
||||
$s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/(www\.)?youtube\.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism', '[youtube]$2[/youtube]', 'url', $s);
|
||||
$s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/(www\.)?youtube\.com\/embed\/(.*?)\].*?\[\/url\]/ism', '[youtube]$2[/youtube]', 'url', $s);
|
||||
$s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/(www\.)?youtube\.com\/shorts\/(.*?)\].*?\[\/url\]/ism', '[youtube]$2[/youtube]', 'url', $s);
|
||||
$s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/vimeo\.com\/([0-9]+)(.*?)\[\/url\]/ism', '[vimeo]$2[/vimeo]', 'url', $s);
|
||||
$s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/player\.vimeo\.com\/video\/([0-9]+)(.*?)\[\/url\]/ism', '[vimeo]$2[/vimeo]', 'url', $s);
|
||||
$s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/vimeo\.com\/([0-9]+)\](.*?)\[\/url\]/ism', '[vimeo]$1[/vimeo]', 'url', $s);
|
||||
|
||||
// remove duplicate adjacent code tags
|
||||
$s = preg_replace('/(\[code\])+(.*?)(\[\/code\])+/ism', '[code]$2[/code]', $s);
|
||||
|
|
|
@ -73,51 +73,51 @@ class BBCodeTest extends FixtureTestCase
|
|||
],
|
||||
'no-protocol' => [
|
||||
'data' => 'example.com/path',
|
||||
'assertHTML' => false
|
||||
'assertHTML' => false,
|
||||
],
|
||||
'wrong-protocol' => [
|
||||
'data' => 'ftp://example.com',
|
||||
'assertHTML' => false
|
||||
'assertHTML' => false,
|
||||
],
|
||||
'wrong-domain-without-path' => [
|
||||
'data' => 'http://example',
|
||||
'assertHTML' => false
|
||||
'assertHTML' => false,
|
||||
],
|
||||
'wrong-domain-with-path' => [
|
||||
'data' => 'http://example/path',
|
||||
'assertHTML' => false
|
||||
'assertHTML' => false,
|
||||
],
|
||||
'bug-6857-domain-start' => [
|
||||
'data' => "http://\nexample.com",
|
||||
'assertHTML' => false
|
||||
'assertHTML' => false,
|
||||
],
|
||||
'bug-6857-domain-end' => [
|
||||
'data' => "http://example\n.com",
|
||||
'assertHTML' => false
|
||||
'assertHTML' => false,
|
||||
],
|
||||
'bug-6857-tld' => [
|
||||
'data' => "http://example.\ncom",
|
||||
'assertHTML' => false
|
||||
'assertHTML' => false,
|
||||
],
|
||||
'bug-6857-end' => [
|
||||
'data' => "http://example.com\ntest",
|
||||
'assertHTML' => false
|
||||
'assertHTML' => false,
|
||||
],
|
||||
'bug-6901' => [
|
||||
'data' => "http://example.com<ul>",
|
||||
'assertHTML' => false
|
||||
'assertHTML' => false,
|
||||
],
|
||||
'bug-7150' => [
|
||||
'data' => html_entity_decode('http://example.com ', ENT_QUOTES, 'UTF-8'),
|
||||
'assertHTML' => false
|
||||
'assertHTML' => false,
|
||||
],
|
||||
'bug-7271-query-string-brackets' => [
|
||||
'data' => 'https://example.com/search?q=square+brackets+[url]',
|
||||
'assertHTML' => true
|
||||
'assertHTML' => true,
|
||||
],
|
||||
'bug-7271-path-brackets' => [
|
||||
'data' => 'http://example.com/path/to/file[3].html',
|
||||
'assertHTML' => true
|
||||
'assertHTML' => true,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
@ -215,19 +215,19 @@ class BBCodeTest extends FixtureTestCase
|
|||
],
|
||||
'bug-9611-purify-xss-nobb' => [
|
||||
'expectedHTML' => '<span>dare to move your mouse here</span>',
|
||||
'text' => '[nobb]<span onmouseover="alert(0)">dare to move your mouse here</span>[/nobb]'
|
||||
'text' => '[nobb]<span onmouseover="alert(0)">dare to move your mouse here</span>[/nobb]',
|
||||
],
|
||||
'bug-9611-purify-xss-noparse' => [
|
||||
'expectedHTML' => '<span>dare to move your mouse here</span>',
|
||||
'text' => '[noparse]<span onmouseover="alert(0)">dare to move your mouse here</span>[/noparse]'
|
||||
'text' => '[noparse]<span onmouseover="alert(0)">dare to move your mouse here</span>[/noparse]',
|
||||
],
|
||||
'bug-9611-purify-xss-attributes' => [
|
||||
'expectedHTML' => '<span>dare to move your mouse here</span>',
|
||||
'text' => '[color="onmouseover=alert(0) style="]dare to move your mouse here[/color]'
|
||||
'text' => '[color="onmouseover=alert(0) style="]dare to move your mouse here[/color]',
|
||||
],
|
||||
'bug-9611-purify-attributes-correct' => [
|
||||
'expectedHTML' => '<span style="color:#FFFFFF;">dare to move your mouse here</span>',
|
||||
'text' => '[color=FFFFFF]dare to move your mouse here[/color]'
|
||||
'text' => '[color=FFFFFF]dare to move your mouse here[/color]',
|
||||
],
|
||||
'bug-9639-span-classes' => [
|
||||
'expectedHTML' => '<span class="arbitrary classes">Test</span>',
|
||||
|
@ -308,11 +308,11 @@ Karl Marx - Die ursprüngliche Akkumulation
|
|||
],
|
||||
'bug-12701-quotes' => [
|
||||
'expected' => '[](https://domain.tld/photos/user/image/86912721086415cdc8e0a03226831581)',
|
||||
'text' => '[url=https://domain.tld/photos/user/image/86912721086415cdc8e0a03226831581][img=https://domain.tld/photo/86912721086415cdc8e0a03226831581-1.png]abc"fgh[/img][/url]'
|
||||
'text' => '[url=https://domain.tld/photos/user/image/86912721086415cdc8e0a03226831581][img=https://domain.tld/photo/86912721086415cdc8e0a03226831581-1.png]abc"fgh[/img][/url]',
|
||||
],
|
||||
'bug-12701-no-quotes' => [
|
||||
'expected' => '[](https://domain.tld/photos/user/image/86912721086415cdc8e0a03226831581)',
|
||||
'text' => '[url=https://domain.tld/photos/user/image/86912721086415cdc8e0a03226831581][img=https://domain.tld/photo/86912721086415cdc8e0a03226831581-1.png]abcfgh[/img][/url]'
|
||||
'text' => '[url=https://domain.tld/photos/user/image/86912721086415cdc8e0a03226831581][img=https://domain.tld/photo/86912721086415cdc8e0a03226831581-1.png]abcfgh[/img][/url]',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
@ -345,7 +345,7 @@ Karl Marx - Die ursprüngliche Akkumulation
|
|||
'bug-10692-start-line' => [
|
||||
'#[url=https://friendica.local/search?tag=L160]L160[/url]',
|
||||
'#L160',
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -362,6 +362,58 @@ Karl Marx - Die ursprüngliche Akkumulation
|
|||
self::assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
public function dataExpandVideoLinks(): array
|
||||
{
|
||||
return [
|
||||
/** @see https://github.com/friendica/friendica/pull/14940 */
|
||||
'task-14940-youtube-watch-with-www' => [
|
||||
'expectedBBCode' => '[url=https://www.youtube.com/watch?v=hfwbmTzBFT0]https://www.youtube.com/watch?v=hfwbmTzBFT0[/url]',
|
||||
'text' => '[youtube]https://www.youtube.com/watch?v=hfwbmTzBFT0[/youtube]',
|
||||
],
|
||||
'task-14940-youtube-watch-without-www' => [
|
||||
'expectedBBCode' => '[url=https://www.youtube.com/watch?v=hfwbmTzBFT0]https://www.youtube.com/watch?v=hfwbmTzBFT0[/url]',
|
||||
'text' => '[youtube]https://youtube.com/watch?v=hfwbmTzBFT0[/youtube]',
|
||||
],
|
||||
'task-14940-youtube-shorts-with-www' => [
|
||||
'expectedBBCode' => '[url=https://www.youtube.com/watch?v=hfwbmTzBFT0]https://www.youtube.com/watch?v=hfwbmTzBFT0[/url]',
|
||||
'text' => '[youtube]https://www.youtube.com/shorts/hfwbmTzBFT0[/youtube]',
|
||||
],
|
||||
'task-14940-youtube-shorts-without-www' => [
|
||||
'expectedBBCode' => '[url=https://www.youtube.com/watch?v=hfwbmTzBFT0]https://www.youtube.com/watch?v=hfwbmTzBFT0[/url]',
|
||||
'text' => '[youtube]https://youtube.com/shorts/hfwbmTzBFT0[/youtube]',
|
||||
],
|
||||
'task-14940-youtube-embed-with-www' => [
|
||||
'expectedBBCode' => '[url=https://www.youtube.com/watch?v=hfwbmTzBFT0]https://www.youtube.com/watch?v=hfwbmTzBFT0[/url]',
|
||||
'text' => '[youtube]https://www.youtube.com/embed/hfwbmTzBFT0[/youtube]',
|
||||
],
|
||||
'task-14940-youtube-embed-without-www' => [
|
||||
'expectedBBCode' => '[url=https://www.youtube.com/watch?v=hfwbmTzBFT0]https://www.youtube.com/watch?v=hfwbmTzBFT0[/url]',
|
||||
'text' => '[youtube]https://youtube.com/embed/hfwbmTzBFT0[/youtube]',
|
||||
],
|
||||
'task-14940-vimeo' => [
|
||||
'expectedBBCode' => '[url=https://vimeo.com/2345345]https://vimeo.com/2345345[/url]',
|
||||
'text' => '[vimeo]https://vimeo.com/2345345[/vimeo]',
|
||||
],
|
||||
'task-14940-player-vimeo' => [
|
||||
'expectedBBCode' => '[url=https://vimeo.com/2345345]https://vimeo.com/2345345[/url]',
|
||||
'text' => '[vimeo]https://player.vimeo.com/video/2345345[/vimeo]',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataExpandVideoLinks
|
||||
*
|
||||
* @param string $expected Expected BBCode output
|
||||
* @param string $text Input text
|
||||
*/
|
||||
public function testExpandVideoLinks(string $expected, string $text)
|
||||
{
|
||||
$actual = BBCode::expandVideoLinks($text);
|
||||
|
||||
self::assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
public function dataGetAbstract(): array
|
||||
{
|
||||
return [
|
||||
|
|
|
@ -51,7 +51,45 @@ class MarkdownTest extends FixtureTestCase
|
|||
return [
|
||||
'bug-8358-double-decode' => [
|
||||
'expectedBBCode' => 'with the <sup> and </sup> tag',
|
||||
'markdown' => 'with the <sup> and </sup> tag',
|
||||
'markdown' => 'with the <sup> and </sup> tag',
|
||||
],
|
||||
/** @see https://github.com/friendica/friendica/pull/14940 */
|
||||
'task-14940-youtube-watch-with-www' => [
|
||||
'expectedBBCode' => '[youtube]hfwbmTzBFT0[/youtube]',
|
||||
'markdown' => '[url=https://www.youtube.com/watch?v=hfwbmTzBFT0]https://www.youtube.com/watch?v=hfwbmTzBFT0[/url]',
|
||||
],
|
||||
'task-14940-youtube-watch-without-www' => [
|
||||
'expectedBBCode' => '[youtube]hfwbmTzBFT0[/youtube]',
|
||||
'markdown' => '[url=https://youtube.com/watch?v=hfwbmTzBFT0]https://youtube.com/watch?v=hfwbmTzBFT0[/url]',
|
||||
],
|
||||
'task-14940-youtube-shorts-with-www' => [
|
||||
'expectedBBCode' => '[youtube]hfwbmTzBFT0[/youtube]',
|
||||
'markdown' => '[url=https://www.youtube.com/shorts/hfwbmTzBFT0]https://www.youtube.com/shorts/hfwbmTzBFT0[/url]',
|
||||
],
|
||||
'task-14940-youtube-shorts-without-www' => [
|
||||
'expectedBBCode' => '[youtube]hfwbmTzBFT0[/youtube]',
|
||||
'markdown' => '[url=https://youtube.com/shorts/hfwbmTzBFT0]https://youtube.com/shorts/hfwbmTzBFT0[/url]',
|
||||
],
|
||||
'task-14940-youtube-embed-with-www' => [
|
||||
'expectedBBCode' => '[youtube]hfwbmTzBFT0[/youtube]',
|
||||
'markdown' => '[url=https://www.youtube.com/embed/hfwbmTzBFT0]https://www.youtube.com/embed/hfwbmTzBFT0[/url]',
|
||||
],
|
||||
'task-14940-youtube-embed-without-www' => [
|
||||
'expectedBBCode' => '[youtube]hfwbmTzBFT0[/youtube]',
|
||||
'markdown' => '[url=https://youtube.com/embed/hfwbmTzBFT0]https://youtube.com/embed/hfwbmTzBFT0[/url]',
|
||||
],
|
||||
// @todo - should we really ignore the URL content in favor of parsing the link of the body?
|
||||
'task-14940-vimeo-custom-url' => [
|
||||
'expectedBBCode' => '[vimeo]2345345[/vimeo]',
|
||||
'markdown' => '[url=https://no.thing]https://vimeo.com/2345345[/url]',
|
||||
],
|
||||
'task-14940-vimeo-custom-text' => [
|
||||
'expectedBBCode' => '[vimeo]2345345[/vimeo]',
|
||||
'markdown' => '[url=https://vimeo.com/2345345]CustomText[/url]',
|
||||
],
|
||||
'task-14940-player-vimeo' => [
|
||||
'expectedBBCode' => '[vimeo]2345345[/vimeo]',
|
||||
'markdown' => '[url=https://player.vimeo.com/video/2345345]https://player.vimeo.com/video/2345345[/url]',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue