aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFerdinand Thiessen <opensource@fthiessen.de>2023-07-18 13:09:30 +0200
committerFerdinand Thiessen <opensource@fthiessen.de>2023-07-18 13:10:37 +0200
commit3b057600d09b8013e14b0c5ea4696fb7eadc6022 (patch)
tree043c9d5d794512f6424d54447d1d97a1960ceef2
parent0e69a6a846dd7dbff6e5fc625d55a125e0684077 (diff)
downloadnextcloud-server-3b057600d09b8013e14b0c5ea4696fb7eadc6022.tar.gz
nextcloud-server-3b057600d09b8013e14b0c5ea4696fb7eadc6022.zip
fix: Correctly add `module` content type to script tags when scripts with versions are used
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
-rw-r--r--lib/private/legacy/template/functions.php3
-rw-r--r--tests/lib/TemplateFunctionsTest.php12
2 files changed, 14 insertions, 1 deletions
diff --git a/lib/private/legacy/template/functions.php b/lib/private/legacy/template/functions.php
index 57c3a834128..bcc0906dcdf 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 b2b25ab654c..aa9ba32610d 100644
--- a/tests/lib/TemplateFunctionsTest.php
+++ b/tests/lib/TemplateFunctionsTest.php
@@ -86,6 +86,18 @@ class TemplateFunctionsTest extends \Test\TestCase {
]);
}
+ public function testEmitScriptLoadingTagsWithVersion() {
+ // Test mjs js and inline content
+ $pattern = '/src="some\.mjs\?v=ab123cd"[^>]+type="module"[^>]*>.+\n'; // some.mjs with type = module
+ $pattern .= '<script[^>]+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
// ---------------------------------------------------------------------------