Skip to content

Commit

Permalink
Closes #618, #2599 Added simple options for including 'async' or 'def…
Browse files Browse the repository at this point in the history
…er' 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']);
  • Loading branch information
CaMer0n committed Dec 27, 2021
1 parent f587343 commit c96d64e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
20 changes: 17 additions & 3 deletions e107_handlers/js_manager.php
Expand Up @@ -951,6 +951,10 @@ protected function addJs($type, $file_path, $runtime_location = '', $opts = arra
// 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'] : '';
Expand Down Expand Up @@ -1105,11 +1109,16 @@ protected function addJs($type, $file_path, $runtime_location = '', $opts = arra
$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;

Expand All @@ -1134,11 +1143,16 @@ protected function addJs($type, $file_path, $runtime_location = '', $opts = arra
$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;
Expand Down Expand Up @@ -1531,7 +1545,7 @@ public function renderFile($file_path_array, $external = false, $label = '', $mo
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];

Expand Down
19 changes: 19 additions & 0 deletions e107_tests/tests/unit/e_jsmanagerTest.php
Expand Up @@ -191,6 +191,11 @@ public function testHeaderFile()
'zone' => 3,
'opts' => array('defer'=>true)
),
4 => array(
'file' => 'https://somewhere/async.js',
'zone' => 4,
'opts' => array('defer', 'async')
),


);
Expand All @@ -210,6 +215,10 @@ public function testHeaderFile()
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/something.min.js" defer></script>', $result);
$this->assertStringContainsString('zone #3', $result);

$result = $this->js->renderJs('header', 4, true, true);
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/async.js" defer async></script>', $result);
$this->assertStringContainsString('zone #4', $result);

}

public function testFooterFile()
Expand Down Expand Up @@ -237,6 +246,12 @@ public function testFooterFile()
'opts' => array('defer'=>true)
),

4 => array(
'file' => 'https://somewhere/async.js',
'zone' => 4,
'opts' => array('defer', 'async')
),


);

Expand All @@ -255,6 +270,10 @@ public function testFooterFile()
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/something.min.js" defer></script>', $result);
$this->assertStringContainsString('priority #3', $result);

$result = $this->js->renderJs('footer', 4, true, true);
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/async.js" defer async></script>', $result);
$this->assertStringContainsString('priority #4', $result);

}
/*
public function testSetDependency()
Expand Down

1 comment on commit c96d64e

@Jimmi08
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How this works if the third parameter is "jquery"? Or better - is your usage example correct?

Please sign in to comment.