From c2c0544d7dfee4fdb82d56efecdb70a11e25b617 Mon Sep 17 00:00:00 2001 From: Johnny Huynh <27847622+johnnyhuy@users.noreply.github.com> Date: Wed, 3 Jun 2020 23:38:27 +1000 Subject: [PATCH] Added regex match check --- src/Inline/Parser/CodepenParser.php | 6 +++++- src/Inline/Parser/GistParser.php | 6 +++++- src/Inline/Parser/SoundCloudParser.php | 6 +++++- src/Inline/Parser/YouTubeParser.php | 15 ++++++++++----- tests/Elements/Inline/YouTubeTest.php | 1 + 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Inline/Parser/CodepenParser.php b/src/Inline/Parser/CodepenParser.php index ffa4c51..1ffb5c0 100755 --- a/src/Inline/Parser/CodepenParser.php +++ b/src/Inline/Parser/CodepenParser.php @@ -33,7 +33,11 @@ public function parse(InlineParserContext $inlineContext): bool } $matches = []; - preg_match($regex, $validate, $matches); + $exists = preg_match($regex, $validate, $matches); + + if (!$exists) { + return false; + } // Return the given codepen url to the renderer class $inlineContext->getContainer()->appendChild(new Codepen($matches[1])); diff --git a/src/Inline/Parser/GistParser.php b/src/Inline/Parser/GistParser.php index 61152d8..ddf77d1 100755 --- a/src/Inline/Parser/GistParser.php +++ b/src/Inline/Parser/GistParser.php @@ -34,7 +34,11 @@ public function parse(InlineParserContext $inlineContext): bool } $matches = []; - preg_match($regex, $validate, $matches); + $exists = preg_match($regex, $validate, $matches); + + if (!$exists) { + return false; + } // Return the given gist url to the renderer class $inlineContext->getContainer()->appendChild(new Gist($matches[1])); diff --git a/src/Inline/Parser/SoundCloudParser.php b/src/Inline/Parser/SoundCloudParser.php index c15b3ac..458642d 100755 --- a/src/Inline/Parser/SoundCloudParser.php +++ b/src/Inline/Parser/SoundCloudParser.php @@ -34,7 +34,11 @@ public function parse(InlineParserContext $inlineContext): bool } $matches = []; - preg_match($regex, $validate, $matches); + $exists = preg_match($regex, $validate, $matches); + + if (!$exists) { + return false; + } $inlineContext->getContainer()->appendChild(new SoundCloud($matches[1])); diff --git a/src/Inline/Parser/YouTubeParser.php b/src/Inline/Parser/YouTubeParser.php index 9ff1ccf..fdfd235 100755 --- a/src/Inline/Parser/YouTubeParser.php +++ b/src/Inline/Parser/YouTubeParser.php @@ -38,15 +38,20 @@ public function parse(InlineParserContext $inlineContext): bool $videoMatches = []; $timeMatches = []; $timeContinueMatches = []; - preg_match($videoRegex, $validate, $videoMatches); - preg_match($timeRegex, $validate, $timeMatches); - preg_match($timeContinueRegex, $validate, $timeContinueMatches); + $videoExists = preg_match($videoRegex, $validate, $videoMatches); + $timeMatchesExist = preg_match($timeRegex, $validate, $timeMatches); + $timeContinueMatchesExist = preg_match($timeContinueRegex, $validate, $timeContinueMatches); + + if (!$videoExists) { + return false; + } + $videoId = $videoMatches[1]; $startTime = ''; - if ($timeMatches) { + if ($timeMatchesExist) { $startTime = "?start={$timeMatches[1]}"; - } elseif ($timeContinueMatches) { + } elseif ($timeContinueMatchesExist) { $startTime = "?start={$timeContinueMatches[1]}"; } diff --git a/tests/Elements/Inline/YouTubeTest.php b/tests/Elements/Inline/YouTubeTest.php index aeb7a17..73d7db4 100644 --- a/tests/Elements/Inline/YouTubeTest.php +++ b/tests/Elements/Inline/YouTubeTest.php @@ -52,6 +52,7 @@ public function failedStrings() [':youtube .youtube.com/watch?v=USL6P8haroY', '
:youtube .youtube.com/watch?v=USL6P8haroY
'], [':youtube .com/watch?v=USL6P8haroY', ':youtube .com/watch?v=USL6P8haroY
'], [':youtube poop.com/watch?v=USL6P8haroY', ':youtube poop.com/watch?v=USL6P8haroY
'], + [':youtube poop.com/watch?v=', ':youtube poop.com/watch?v=
'], ]; }