Skip to content

Commit

Permalink
Merge pull request #5866 from tractorcow/pulls/4.0/custom-script-type
Browse files Browse the repository at this point in the history
API Allow custom 'type' option for scripts
  • Loading branch information
dhensby committed Aug 2, 2016
2 parents 18465e7 + 4ca3d1d commit 9621392
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
22 changes: 22 additions & 0 deletions tests/forms/RequirementsTest.php
Expand Up @@ -141,6 +141,28 @@ protected function setupCombinedRequirementsJavascriptAsyncDefer($backend, $asyn
);
}

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() {
/** @var Requirements_Backend $backend */
$backend = Injector::inst()->create('Requirements_Backend');
Expand Down
14 changes: 13 additions & 1 deletion view/Requirements.php
Expand Up @@ -828,8 +828,18 @@ public function setMinifyCombinedJSFiles($minify) {
* - 'provides' : List of scripts files included in this file
* - 'async' : Boolean value to set async 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()) {
// 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
$async = (
isset($options['async']) && isset($options['async']) == true
Expand All @@ -850,6 +860,7 @@ public function javascript($file, $options = array()) {
$this->javascript[$file] = array(
'async' => $async,
'defer' => $defer,
'type' => $type,
);

// Record scripts included in this file
Expand Down Expand Up @@ -1179,9 +1190,10 @@ public function includeInHTML($content) {
foreach($this->getJavascript() as $file => $attributes) {
$async = (isset($attributes['async']) && $attributes['async'] == true) ? " async" : "";
$defer = (isset($attributes['defer']) && $attributes['defer'] == true) ? " defer" : "";
$type = Convert::raw2att(isset($attributes['type']) ? $attributes['type'] : "application/javascript");
$path = Convert::raw2att($this->pathForFile($file));
if($path) {
$jsRequirements .= "<script type=\"application/javascript\" src=\"$path\"{$async}{$defer}></script>";
$jsRequirements .= "<script type=\"{$type}\" src=\"{$path}\"{$async}{$defer}></script>";
}
}

Expand Down

0 comments on commit 9621392

Please sign in to comment.