From c63942cff44c788d280ee99655bc2521a8f18e14 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Tue, 18 Jul 2023 13:09:30 +0200 Subject: [PATCH] fix: Correctly add `module` content type to script tags when scripts with versions are used Signed-off-by: Ferdinand Thiessen --- lib/private/legacy/template/functions.php | 3 ++- tests/lib/TemplateFunctionsTest.php | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/private/legacy/template/functions.php b/lib/private/legacy/template/functions.php index 7081bd4f74371..0d386625ab324 100644 --- a/lib/private/legacy/template/functions.php +++ b/lib/private/legacy/template/functions.php @@ -101,7 +101,8 @@ function emit_script_tag(string $src, string $script_content = '', string $conte */ function emit_script_loading_tags($obj) { foreach ($obj['jsfiles'] as $jsfile) { - $type = str_ends_with($jsfile, '.mjs') ? 'module' : ''; + $fileName = explode('?', $jsfile, 2)[0]; + $type = str_ends_with($fileName, '.mjs') ? 'module' : ''; emit_script_tag($jsfile, '', $type); } if (!empty($obj['inline_ocjs'])) { diff --git a/tests/lib/TemplateFunctionsTest.php b/tests/lib/TemplateFunctionsTest.php index b2b25ab654ce7..aa9ba32610d8b 100644 --- a/tests/lib/TemplateFunctionsTest.php +++ b/tests/lib/TemplateFunctionsTest.php @@ -86,6 +86,18 @@ public function testEmitScriptLoadingTags() { ]); } + public function testEmitScriptLoadingTagsWithVersion() { + // Test mjs js and inline content + $pattern = '/src="some\.mjs\?v=ab123cd"[^>]+type="module"[^>]*>.+\n'; // some.mjs with type = module + $pattern .= ']+src="other\.js\?v=12abc34"[^>]*>.+\n'; // other.js as plain javascript + $pattern .= '/'; // no flags + + $this->expectOutputRegex($pattern); + emit_script_loading_tags([ + 'jsfiles' => ['some.mjs?v=ab123cd', 'other.js?v=12abc34'], + ]); + } + // --------------------------------------------------------------------------- // Test relative_modified_date with dates only // ---------------------------------------------------------------------------