From e2cfa524d0e5e6f4fd846b3ad70a3713f94b73ac Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Wed, 10 Jan 2024 13:09:01 +0900 Subject: [PATCH] Fix TemplateParserV1 and FrontEndFileHandler not recognizing Google webfonts '.../css2?...' URL as CSS --- classes/frontendfile/FrontEndFileHandler.class.php | 2 +- .../framework/parsers/template/TemplateParser_v1.php | 9 ++++++++- tests/unit/classes/FrontEndFileHandlerTest.php | 11 +++++++++++ tests/unit/framework/parsers/TemplateParserV1Test.php | 10 ++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/classes/frontendfile/FrontEndFileHandler.class.php b/classes/frontendfile/FrontEndFileHandler.class.php index fde276ac7..a90e7c4a1 100644 --- a/classes/frontendfile/FrontEndFileHandler.class.php +++ b/classes/frontendfile/FrontEndFileHandler.class.php @@ -209,7 +209,7 @@ class FrontEndFileHandler extends Handler $file->isExternalURL = preg_match('@^(https?:)?//@i', $file->filePath) ? true : false; if ($file->isExternalURL && !$file->fileExtension) { - $file->fileExtension = preg_match('/[\.\/](css|js)\b/', $fileName, $matches) ? $matches[1] : null; + $file->fileExtension = preg_match('/[\.\/](css|js)[0-9]?\b/', $fileName, $matches) ? $matches[1] : null; } $file->isCachedScript = !$file->isExternalURL && strpos($file->filePath, 'files/cache/') !== false; $file->isCommon = $isCommon; diff --git a/common/framework/parsers/template/TemplateParser_v1.php b/common/framework/parsers/template/TemplateParser_v1.php index e5246998f..5c97346d3 100644 --- a/common/framework/parsers/template/TemplateParser_v1.php +++ b/common/framework/parsers/template/TemplateParser_v1.php @@ -628,7 +628,14 @@ class TemplateParser_v1 $doUnload = ($m[3] === 'unload'); $isRemote = !!preg_match('@^(https?:)?//@i', $attr['target']); - if(!$isRemote) + if($isRemote) + { + if (empty($pathinfo['extension'])) + { + $pathinfo['extension'] = preg_match('/[\.\/](css|js)[0-9]?\b/', $attr['target'], $mx) ? $mx[1] : null; + } + } + else { if (preg_match('!^\\^/(.+)!', $attr['target'], $tmatches)) { diff --git a/tests/unit/classes/FrontEndFileHandlerTest.php b/tests/unit/classes/FrontEndFileHandlerTest.php index 615fe5c36..b3980f8b0 100644 --- a/tests/unit/classes/FrontEndFileHandlerTest.php +++ b/tests/unit/classes/FrontEndFileHandlerTest.php @@ -161,6 +161,17 @@ class FrontEndFileHandlerTest extends \Codeception\Test\Unit $this->assertEquals($expected, $handler->getCssFileList()); } + public function testExternalFile5() + { + $handler = new FrontEndFileHandler(); + $handler->loadFile(array('https://fonts.googleapis.com/css?family=Montserrat&display=swap')); + $handler->loadFile(array('//fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@400;700&display=swap')); + + $expected[] = array('file' => 'https://fonts.googleapis.com/css?family=Montserrat&display=swap', 'media'=>'all'); + $expected[] = array('file' => '//fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@400;700&display=swap', 'media'=>'all'); + $this->assertEquals($expected, $handler->getCssFileList()); + } + public function testPathConversion() { $handler = new FrontEndFileHandler(); diff --git a/tests/unit/framework/parsers/TemplateParserV1Test.php b/tests/unit/framework/parsers/TemplateParserV1Test.php index 519ae9308..768e1ffe7 100644 --- a/tests/unit/framework/parsers/TemplateParserV1Test.php +++ b/tests/unit/framework/parsers/TemplateParserV1Test.php @@ -118,6 +118,16 @@ class TemplateParserV1Test extends \Codeception\Test\Unit '', '?>' ), + // + array( + '', + '?>' + ), + // + array( + '', + '?>' + ), // array( '',