Skip to content

Commit

Permalink
ENH Allow setting options for extra css/js in LeftAndMain.
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Feb 14, 2022
1 parent ed227e7 commit 96270d1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
19 changes: 16 additions & 3 deletions code/LeftAndMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ class LeftAndMain extends Controller implements PermissionProvider
* LeftAndMain:
* extra_requirements_javascript:
* - mysite/javascript/myscript.js
* mysite/javascript/anotherscript.js:
* defer: true
* </code>
*
* @config
Expand Down Expand Up @@ -709,9 +711,10 @@ protected function init()
foreach ($extraJs as $file => $config) {
if (is_numeric($file)) {
$file = $config;
$config = array();
}

Requirements::javascript($file);
Requirements::javascript($file, $config);
}
}

Expand All @@ -723,8 +726,13 @@ protected function init()
$file = $config;
$config = array();
}
$media = null;
if (isset($config['media'])) {
$media = $config['media'];
unset($config['media']);
}

Requirements::css($file, isset($config['media']) ? $config['media'] : null);
Requirements::css($file, $media, $config);
}
}

Expand All @@ -736,8 +744,13 @@ protected function init()
$file = $config;
$config = array();
}
$media = null;
if (isset($config['media'])) {
$media = $config['media'];
unset($config['media']);
}

Requirements::themedCSS($file, isset($config['media']) ? $config['media'] : null);
Requirements::themedCSS($file, $media, $config);
}
}

Expand Down
32 changes: 26 additions & 6 deletions tests/php/LeftAndMainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ protected function setUp(): void

LeftAndMain::config()
->update('extra_requirements_css', array(
$assetsDir.'/LeftAndMainTest.css'
$assetsDir.'/LeftAndMainTest.css',
$assetsDir. '/LeftAndMainTestWithOptions.css' => [
'media' => 'print',
'crossorigin' => 'anonymous'
],
))
->update('extra_requirements_javascript', array(
$assetsDir. '/LeftAndMainTest.js'
$assetsDir. '/LeftAndMainTest.js',
$assetsDir. '/LeftAndMainTestWithOptions.js' => [
'crossorigin' => 'anonymous'
],
));

Requirements::set_combined_files_enabled(false);
Expand All @@ -63,15 +70,28 @@ public function testExtraCssAndJavascript()
$this->logInAs($admin);
$response = $this->get('admin/security');

// Check css
$this->assertMatchesRegularExpression(
'/tests\/php\/assets\/LeftAndMainTest.css/i',
'/<link.*?href="[^"]*?tests\/php\/assets\/LeftAndMainTest\.css.*?>/i',
$response->getBody(),
"body should contain custom css"
'body should contain custom css'
);
$this->assertMatchesRegularExpression(
'/tests\/php\/assets\/LeftAndMainTest.js/i',
'/<link.*?href="[^"]*?tests\/php\/assets\/LeftAndMainTestWithOptions\.css.*?(?=.*?crossorigin="anonymous")(?=.*media="print").*?>/i',
$response->getBody(),
"body should contain custom js"
'body should contain custom css with options'
);

// Check js
$this->assertMatchesRegularExpression(
'/<script.*?src="[^"]*?tests\/php\/assets\/LeftAndMainTest\.js.*?>/i',
$response->getBody(),
'body should contain custom js'
);
$this->assertMatchesRegularExpression(
'/<script.*?src="[^"]*?tests\/php\/assets\/LeftAndMainTestWithOptions\.js.*?crossorigin="anonymous".*?>/i',
$response->getBody(),
'body should contain custom js with options'
);
}

Expand Down
Empty file.
Empty file.

0 comments on commit 96270d1

Please sign in to comment.