Permalink
Browse files

API Add possibility to combine media-targeting stylesheets.

  • Loading branch information...
1 parent ba825ce commit 53feb3a5ae399ef68f8e1d0c752f6c9b13929bc3 @mateusz mateusz committed Feb 12, 2013
@@ -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]`
@@ -167,6 +167,29 @@ public function testCombinedJavascript() {
$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();
No changes.
No changes.
View
@@ -246,9 +246,10 @@ public static function add_i18n_javascript($langDir, $return = false, $langOnly
*
* @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 @@ protected function path_for_file($fileOrUrl) {
* @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 @@ public function combine_files($combinedFileName, $files) {
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 @@ public function combine_files($combinedFileName, $files) {
} 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 @@ public function combine_files($combinedFileName, $files) {
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 @@ public function process_combined_files() {
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;

0 comments on commit 53feb3a

Please sign in to comment.