mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #5866 from tractorcow/pulls/4.0/custom-script-type
API Allow custom 'type' option for scripts
This commit is contained in:
commit
96213924a2
@ -141,6 +141,28 @@ class RequirementsTest extends SapphireTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCustomType() {
|
||||||
|
/** @var Requirements_Backend $backend */
|
||||||
|
$backend = Injector::inst()->create('Requirements_Backend');
|
||||||
|
$basePath = $this->getCurrentRelativePath();
|
||||||
|
$this->setupRequirements($backend);
|
||||||
|
|
||||||
|
// require files normally (e.g. called from a FormField instance)
|
||||||
|
$backend->javascript($basePath . '/RequirementsTest_a.js', [
|
||||||
|
'type' => 'application/json'
|
||||||
|
]);
|
||||||
|
$backend->javascript($basePath . '/RequirementsTest_b.js');
|
||||||
|
$result = $backend->includeInHTML(self::$html_template);
|
||||||
|
$this->assertContains(
|
||||||
|
'<script type="application/json" src="/framework/tests/forms/RequirementsTest_a.js',
|
||||||
|
$result
|
||||||
|
);
|
||||||
|
$this->assertContains(
|
||||||
|
'<script type="application/javascript" src="/framework/tests/forms/RequirementsTest_b.js',
|
||||||
|
$result
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testCombinedJavascript() {
|
public function testCombinedJavascript() {
|
||||||
/** @var Requirements_Backend $backend */
|
/** @var Requirements_Backend $backend */
|
||||||
$backend = Injector::inst()->create('Requirements_Backend');
|
$backend = Injector::inst()->create('Requirements_Backend');
|
||||||
|
@ -828,8 +828,18 @@ class Requirements_Backend
|
|||||||
* - 'provides' : List of scripts files included in this file
|
* - 'provides' : List of scripts files included in this file
|
||||||
* - 'async' : Boolean value to set async attribute to script tag
|
* - 'async' : Boolean value to set async attribute to script tag
|
||||||
* - 'defer' : Boolean value to set defer attribute to script tag
|
* - 'defer' : Boolean value to set defer attribute to script tag
|
||||||
|
* - 'type' : Override script type= value.
|
||||||
*/
|
*/
|
||||||
public function javascript($file, $options = array()) {
|
public function javascript($file, $options = array()) {
|
||||||
|
// Get type
|
||||||
|
$type = null;
|
||||||
|
if (isset($this->javascript[$file]['type'])) {
|
||||||
|
$type = $this->javascript[$file]['type'];
|
||||||
|
}
|
||||||
|
if (isset($options['type'])) {
|
||||||
|
$type = $options['type'];
|
||||||
|
}
|
||||||
|
|
||||||
// make sure that async/defer is set if it is set once even if file is included multiple times
|
// make sure that async/defer is set if it is set once even if file is included multiple times
|
||||||
$async = (
|
$async = (
|
||||||
isset($options['async']) && isset($options['async']) == true
|
isset($options['async']) && isset($options['async']) == true
|
||||||
@ -850,6 +860,7 @@ class Requirements_Backend
|
|||||||
$this->javascript[$file] = array(
|
$this->javascript[$file] = array(
|
||||||
'async' => $async,
|
'async' => $async,
|
||||||
'defer' => $defer,
|
'defer' => $defer,
|
||||||
|
'type' => $type,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Record scripts included in this file
|
// Record scripts included in this file
|
||||||
@ -1179,9 +1190,10 @@ class Requirements_Backend
|
|||||||
foreach($this->getJavascript() as $file => $attributes) {
|
foreach($this->getJavascript() as $file => $attributes) {
|
||||||
$async = (isset($attributes['async']) && $attributes['async'] == true) ? " async" : "";
|
$async = (isset($attributes['async']) && $attributes['async'] == true) ? " async" : "";
|
||||||
$defer = (isset($attributes['defer']) && $attributes['defer'] == true) ? " defer" : "";
|
$defer = (isset($attributes['defer']) && $attributes['defer'] == true) ? " defer" : "";
|
||||||
|
$type = Convert::raw2att(isset($attributes['type']) ? $attributes['type'] : "application/javascript");
|
||||||
$path = Convert::raw2att($this->pathForFile($file));
|
$path = Convert::raw2att($this->pathForFile($file));
|
||||||
if($path) {
|
if($path) {
|
||||||
$jsRequirements .= "<script type=\"application/javascript\" src=\"$path\"{$async}{$defer}></script>";
|
$jsRequirements .= "<script type=\"{$type}\" src=\"{$path}\"{$async}{$defer}></script>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user