mirror of
https://github.com/e107inc/e107.git
synced 2025-08-03 21:27:25 +02:00
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']);
This commit is contained in:
@@ -951,6 +951,10 @@ class e_jsmanager
|
|||||||
// e107 Core Minimum should function independently of framework.
|
// 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)
|
// 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.
|
// with basic functions like SyncWithServerTime() and expandit(), externalLinks() etc.
|
||||||
|
if(empty($opts))
|
||||||
|
{
|
||||||
|
$opts = [];
|
||||||
|
}
|
||||||
|
|
||||||
$pre = !empty($opts['pre']) ? $opts['pre'] : '';
|
$pre = !empty($opts['pre']) ? $opts['pre'] : '';
|
||||||
$post = !empty($opts['post']) ? $opts['post'] : '';
|
$post = !empty($opts['post']) ? $opts['post'] : '';
|
||||||
@@ -1105,11 +1109,16 @@ class e_jsmanager
|
|||||||
$post
|
$post
|
||||||
];
|
];
|
||||||
|
|
||||||
if(!empty($opts['defer']))
|
if(isset($opts['defer']) || in_array('defer', $opts, true))
|
||||||
{
|
{
|
||||||
$info[] = 'defer';
|
$info[] = 'defer';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!empty($opts['async']) || in_array('async', $opts, true))
|
||||||
|
{
|
||||||
|
$info[] = 'async';
|
||||||
|
}
|
||||||
|
|
||||||
$file_path = implode($this->_sep, $info);
|
$file_path = implode($this->_sep, $info);
|
||||||
$zone = (int) $runtime_location;
|
$zone = (int) $runtime_location;
|
||||||
|
|
||||||
@@ -1134,11 +1143,16 @@ class e_jsmanager
|
|||||||
$post
|
$post
|
||||||
];
|
];
|
||||||
|
|
||||||
if(!empty($opts['defer']))
|
if(isset($opts['defer']) || in_array('defer', $opts, true))
|
||||||
{
|
{
|
||||||
$info[] = 'defer';
|
$info[] = 'defer';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!empty($opts['async']) || in_array('async', $opts, true))
|
||||||
|
{
|
||||||
|
$info[] = 'async';
|
||||||
|
}
|
||||||
|
|
||||||
$file_path = implode($this->_sep, $info);
|
$file_path = implode($this->_sep, $info);
|
||||||
|
|
||||||
$zone = (int) $runtime_location;
|
$zone = (int) $runtime_location;
|
||||||
@@ -1531,7 +1545,7 @@ class e_jsmanager
|
|||||||
if($pre) $pre .= "\n";
|
if($pre) $pre .= "\n";
|
||||||
$post = varset($path[2], '');
|
$post = varset($path[2], '');
|
||||||
if($post) $post = "\n".$post;
|
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;
|
if($inline) $inline = " ".$inline;
|
||||||
$path = $path[0];
|
$path = $path[0];
|
||||||
|
|
||||||
|
@@ -191,6 +191,11 @@
|
|||||||
'zone' => 3,
|
'zone' => 3,
|
||||||
'opts' => array('defer'=>true)
|
'opts' => array('defer'=>true)
|
||||||
),
|
),
|
||||||
|
4 => array(
|
||||||
|
'file' => 'https://somewhere/async.js',
|
||||||
|
'zone' => 4,
|
||||||
|
'opts' => array('defer', 'async')
|
||||||
|
),
|
||||||
|
|
||||||
|
|
||||||
);
|
);
|
||||||
@@ -210,6 +215,10 @@
|
|||||||
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/something.min.js" defer></script>', $result);
|
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/something.min.js" defer></script>', $result);
|
||||||
$this->assertStringContainsString('zone #3', $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()
|
public function testFooterFile()
|
||||||
@@ -237,6 +246,12 @@
|
|||||||
'opts' => array('defer'=>true)
|
'opts' => array('defer'=>true)
|
||||||
),
|
),
|
||||||
|
|
||||||
|
4 => array(
|
||||||
|
'file' => 'https://somewhere/async.js',
|
||||||
|
'zone' => 4,
|
||||||
|
'opts' => array('defer', 'async')
|
||||||
|
),
|
||||||
|
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -255,6 +270,10 @@
|
|||||||
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/something.min.js" defer></script>', $result);
|
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/something.min.js" defer></script>', $result);
|
||||||
$this->assertStringContainsString('priority #3', $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()
|
public function testSetDependency()
|
||||||
|
Reference in New Issue
Block a user