API Add possibility to combine media-targeting stylesheets.

This commit is contained in:
Mateusz Uzdowski 2013-02-13 10:54:53 +13:00
parent ba825cec28
commit 53feb3a5ae
5 changed files with 44 additions and 8 deletions

View File

@ -56,6 +56,16 @@ By default it stores the generated file in the assets/ folder but you can config
If SilverStripe doesn't have permissions on your server to write these files it will default back to including them
individually .
You can also combine CSS files into a media-specific stylesheets as you would with the `Requirements::css` call - use
the third paramter of the `combine_files` function:
:::php
$printStylesheets = array(
"$themeDir/css/print_HomePage.css",
"$themeDir/css/print_Page.css",
);
Requirements::combine_files('print.css', $printStylesheets, 'print');
## Custom Inline Scripts
You can also quote custom script directly. This may seem a bit ugly, but is useful when you need to transfer some kind
@ -183,4 +193,4 @@ slightly different JS/CSS requirements, the whole lot will be refetched.
nature of an ajax-request. Needs some more research
## API Documentation
`[api:Requirements]`
`[api:Requirements]`

View File

@ -167,6 +167,29 @@ class RequirementsTest extends SapphireTest {
$backend->delete_combined_files('RequirementsTest_bc.js');
}
public function testCombinedCss() {
$basePath = $this->getCurrentRelativePath();
$backend = new Requirements_Backend;
$backend->set_combined_files_enabled(true);
$backend->combine_files(
'print.css',
array(
$basePath . '/RequirementsTest_print_a.css',
$basePath . '/RequirementsTest_print_b.css'
),
'print'
);
$html = $backend->includeInHTML(false, self::$html_template);
$this->assertTrue((bool)preg_match('/href=".*\/print\.css/', $html), 'Print stylesheets have been combined.');
$this->assertTrue((bool)preg_match(
'/media="print/', $html),
'Combined print stylesheet retains the media parameter'
);
}
public function testBlockedCombinedJavascript() {
$basePath = $this->getCurrentRelativePath();

View File

View File

View File

@ -246,9 +246,10 @@ class Requirements {
*
* @param string $combinedFileName
* @param array $files
* @param string $media
*/
public static function combine_files($combinedFileName, $files) {
self::backend()->combine_files($combinedFileName, $files);
public static function combine_files($combinedFileName, $files, $media = null) {
self::backend()->combine_files($combinedFileName, $files, $media);
}
/**
@ -871,8 +872,9 @@ class Requirements_Backend {
* @param string $combinedFileName Filename of the combined file (will be stored in {@link Director::baseFolder()}
* by default)
* @param array $files Array of filenames relative to the webroot
* @param string $media Comma-separated list of media-types (e.g. "screen,projector").
*/
public function combine_files($combinedFileName, $files) {
public function combine_files($combinedFileName, $files, $media = null) {
// duplicate check
foreach($this->combine_files as $_combinedFileName => $_files) {
$duplicates = array_intersect($_files, $files);
@ -889,7 +891,7 @@ class Requirements_Backend {
if (isset($file['type']) && in_array($file['type'], array('css', 'javascript', 'js'))) {
switch ($file['type']) {
case 'css':
$this->css($file['path']);
$this->css($file['path'], $media);
break;
default:
$this->javascript($file['path']);
@ -899,7 +901,7 @@ class Requirements_Backend {
} elseif (isset($file[1]) && in_array($file[1], array('css', 'javascript', 'js'))) {
switch ($file[1]) {
case 'css':
$this->css($file[0]);
$this->css($file[0], $media);
break;
default:
$this->javascript($file[0]);
@ -914,7 +916,7 @@ class Requirements_Backend {
if(substr($file, -2) == 'js') {
$this->javascript($file);
} elseif(substr($file, -3) == 'css') {
$this->css($file);
$this->css($file, $media);
} else {
user_error("Requirements_Backend::combine_files(): Couldn't guess file type for file '$file', "
. "please specify by passing using an array instead.", E_USER_NOTICE);
@ -998,7 +1000,8 @@ class Requirements_Backend {
foreach($this->css as $file => $params) {
if(isset($combinerCheck[$file])) {
$newCSSRequirements[$combinedFilesFolder . $combinerCheck[$file]] = true;
// Inherit the parameters from the last file in the combine set.
$newCSSRequirements[$combinedFilesFolder . $combinerCheck[$file]] = $params;
$combinedFiles[$combinerCheck[$file]] = true;
} else {
$newCSSRequirements[$file] = $params;