From c96d64e2759ab28faf3e72a1f0dbfa160f9eb97e Mon Sep 17 00:00:00 2001 From: Cameron Date: Mon, 27 Dec 2021 15:21:18 -0800 Subject: [PATCH] Closes #618, #2599 Added simple options for including 'async' or 'defer' when using e107::js('header') and/or e107::js('footer'). Usage example: e107::js('footer', 'https://www.google.com/recaptcha/api.js?hl=en', ['defer','async']); --- e107_handlers/js_manager.php | 20 +++++++++++++++++--- e107_tests/tests/unit/e_jsmanagerTest.php | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/e107_handlers/js_manager.php b/e107_handlers/js_manager.php index 75b26ea48..2fcffca1a 100644 --- a/e107_handlers/js_manager.php +++ b/e107_handlers/js_manager.php @@ -951,6 +951,10 @@ class e_jsmanager // e107 Core Minimum should function independently of framework. // ie. e107 Core Minimum: JS similar to e107 v1.0 should be loaded "e_js.php" (no framwork dependency) // with basic functions like SyncWithServerTime() and expandit(), externalLinks() etc. + if(empty($opts)) + { + $opts = []; + } $pre = !empty($opts['pre']) ? $opts['pre'] : ''; $post = !empty($opts['post']) ? $opts['post'] : ''; @@ -1105,11 +1109,16 @@ class e_jsmanager $post ]; - if(!empty($opts['defer'])) + if(isset($opts['defer']) || in_array('defer', $opts, true)) { $info[] = 'defer'; } + if(!empty($opts['async']) || in_array('async', $opts, true)) + { + $info[] = 'async'; + } + $file_path = implode($this->_sep, $info); $zone = (int) $runtime_location; @@ -1134,11 +1143,16 @@ class e_jsmanager $post ]; - if(!empty($opts['defer'])) + if(isset($opts['defer']) || in_array('defer', $opts, true)) { $info[] = 'defer'; } + if(!empty($opts['async']) || in_array('async', $opts, true)) + { + $info[] = 'async'; + } + $file_path = implode($this->_sep, $info); $zone = (int) $runtime_location; @@ -1531,7 +1545,7 @@ class e_jsmanager if($pre) $pre .= "\n"; $post = varset($path[2], ''); if($post) $post = "\n".$post; - $inline = isset($path[3]) ? $path[3] : ''; + $inline = isset($path[3]) ? str_replace($this->_sep, ' ',$path[3]) : ''; if($inline) $inline = " ".$inline; $path = $path[0]; diff --git a/e107_tests/tests/unit/e_jsmanagerTest.php b/e107_tests/tests/unit/e_jsmanagerTest.php index 685a89c62..875495e52 100644 --- a/e107_tests/tests/unit/e_jsmanagerTest.php +++ b/e107_tests/tests/unit/e_jsmanagerTest.php @@ -191,6 +191,11 @@ 'zone' => 3, 'opts' => array('defer'=>true) ), + 4 => array( + 'file' => 'https://somewhere/async.js', + 'zone' => 4, + 'opts' => array('defer', 'async') + ), ); @@ -210,6 +215,10 @@ $this->assertStringContainsString('', $result); $this->assertStringContainsString('zone #3', $result); + $result = $this->js->renderJs('header', 4, true, true); + $this->assertStringContainsString('', $result); + $this->assertStringContainsString('zone #4', $result); + } public function testFooterFile() @@ -237,6 +246,12 @@ 'opts' => array('defer'=>true) ), + 4 => array( + 'file' => 'https://somewhere/async.js', + 'zone' => 4, + 'opts' => array('defer', 'async') + ), + ); @@ -255,6 +270,10 @@ $this->assertStringContainsString('', $result); $this->assertStringContainsString('priority #3', $result); + $result = $this->js->renderJs('footer', 4, true, true); + $this->assertStringContainsString('', $result); + $this->assertStringContainsString('priority #4', $result); + } /* public function testSetDependency()