From ed6f4a1e09ad598ebdb9c63a5fed8c4be1e0411a Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 4 May 2023 22:36:08 -0700 Subject: [PATCH] CRM_Core_Region::add() - Allow JS resources to be flaged as ES5 modules (`esm=>TRUE`) --- CRM/Core/Region.php | 10 ++++++++-- CRM/Core/Resources/CollectionInterface.php | 1 + tests/phpunit/CRM/Core/RegionTest.php | 10 ++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CRM/Core/Region.php b/CRM/Core/Region.php index 4d9f733068..08338cc426 100644 --- a/CRM/Core/Region.php +++ b/CRM/Core/Region.php @@ -88,7 +88,10 @@ class CRM_Core_Region implements CRM_Core_Resources_CollectionInterface, CRM_Cor break; case 'scriptUrl': - if (!$allowCmsOverride || !$cms->addScriptUrl($snippet['scriptUrl'], $this->_name)) { + if (!empty($snippet['esm'])) { + $html .= sprintf("\n", $snippet['scriptUrl']); + } + elseif (!$allowCmsOverride || !$cms->addScriptUrl($snippet['scriptUrl'], $this->_name)) { $html .= sprintf("\n", $snippet['scriptUrl']); } break; @@ -107,7 +110,10 @@ class CRM_Core_Region implements CRM_Core_Resources_CollectionInterface, CRM_Cor break; case 'script': - if (!$allowCmsOverride || !$cms->addScript($snippet['script'], $this->_name)) { + if (!empty($snippet['esm'])) { + $html .= sprintf("\n", $snippet['script']); + } + elseif (!$allowCmsOverride || !$cms->addScript($snippet['script'], $this->_name)) { $html .= sprintf("\n", $snippet['script']); } break; diff --git a/CRM/Core/Resources/CollectionInterface.php b/CRM/Core/Resources/CollectionInterface.php index a81db1cfac..50a7aae347 100644 --- a/CRM/Core/Resources/CollectionInterface.php +++ b/CRM/Core/Resources/CollectionInterface.php @@ -57,6 +57,7 @@ * not guaranteed among versions/implementations.) * - disabled: int, default=0 * - region: string + * - esm: bool; for "script","scriptFile","scriptUrl", the "esm" flag enables Ecmascript 5 Module support * - translate: bool|string, Autoload translations. (Only applies to 'scriptFile') * - FALSE: Do not load translated strings. * - TRUE: Load translated strings. Use the $ext's default domain. diff --git a/tests/phpunit/CRM/Core/RegionTest.php b/tests/phpunit/CRM/Core/RegionTest.php index d009bb4b79..032d1d7172 100644 --- a/tests/phpunit/CRM/Core/RegionTest.php +++ b/tests/phpunit/CRM/Core/RegionTest.php @@ -113,6 +113,14 @@ class CRM_Core_RegionTest extends CiviUnitTestCase { CRM_Core_Region::instance('testAllTypes')->add([ 'jquery' => '$("div");', ]); + CRM_Core_Region::instance('testAllTypes')->add([ + 'scriptUrl' => '/my%20module.mjs', + 'esm' => TRUE, + ]); + CRM_Core_Region::instance('testAllTypes')->add([ + 'script' => 'import foo from "./foobar.mjs";', + 'esm' => TRUE, + ]); CRM_Core_Region::instance('testAllTypes')->add([ 'styleUrl' => '/foo%20bar.css', ]); @@ -131,6 +139,8 @@ class CRM_Core_RegionTest extends CiviUnitTestCase { . "\n" . "\n" . "\n" + . "\n" + . "\n" . "\n" . "\n"; $this->assertEquals($expected, $actual); -- 2.25.1