Merge pull request #1180 from mateusz/css-combining-media

API Add possibility to combine media-targeting stylesheets.
This commit is contained in:
Ingo Schommer 2013-02-12 14:13:45 -08:00
commit b44720179c
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 If SilverStripe doesn't have permissions on your server to write these files it will default back to including them
individually . 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 ## 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 You can also quote custom script directly. This may seem a bit ugly, but is useful when you need to transfer some kind

View File

@ -167,6 +167,29 @@ class RequirementsTest extends SapphireTest {
$backend->delete_combined_files('RequirementsTest_bc.js'); $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() { public function testBlockedCombinedJavascript() {
$basePath = $this->getCurrentRelativePath(); $basePath = $this->getCurrentRelativePath();

View File

View File

View File

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