Permalink
Browse files

feature(views): allows changing relative URLs in CSS files

  • Loading branch information...
mrclay committed Apr 15, 2016
1 parent d6f5f17 commit 70d3aab719ade13804428b7b90bd848a28891f91
Showing with 41 additions and 2 deletions.
  1. +16 −2 engine/lib/views.php
  2. +25 −0 engine/tests/phpunit/ElggViewHelpersTest.php
View
@@ -371,8 +371,7 @@ function elgg_view_deprecated($view, array $vars, $suggestion, $version) {
* Priority can be specified and affects the order in which extensions
* are appended or prepended.
*
* @note Internal: View extensions are stored in
* $CONFIG->views->extensions[$view][$priority] = $view_extension
* @see elgg_prepend_css_urls() If the extension is CSS, you may need to use this to fix relative URLs.
*
* @param string $view The view to extend.
* @param string $view_extension This view is added to $view
@@ -398,6 +397,21 @@ function elgg_unextend_view($view, $view_extension) {
return _elgg_services()->views->unextendView($view, $view_extension);
}
/**
* In CSS content, prepend a path to relative URLs.
*
* This is useful to process a CSS view being used as an extension.
*
* @param string $css CSS
* @param string $path Path to prepend. E.g. "foo/bar/" or "../"
*
* @return string
* @since 2.2
*/
function elgg_prepend_css_urls($css, $path) {
return Minify_CSS_UriRewriter::prepend($css, $path);
}
/**
* Assembles and outputs a full page.
*
@@ -0,0 +1,25 @@
<?php
class ElggViewHelpersTest extends \PHPUnit_Framework_TestCase {
public function test_elgg_prepend_css_path() {
// basic tests
$css = 'foo{background:url(foo/bar.png)}';
$out = elgg_prepend_css_urls($css, '../');
$this->assertEquals('foo{background:url(../foo/bar.png)}', $out);
$css = 'foo{background:url("foo/bar.png")}';
$out = elgg_prepend_css_urls($css, 'bing/');
$this->assertEquals('foo{background:url("bing/foo/bar.png")}', $out);
$css = 'foo{background:url(/foo/bar.png)}';
$out = elgg_prepend_css_urls($css, '../');
$this->assertEquals($css, $out);
$css = 'foo{background:url("http://example.org/foo/bar.png")}';
$out = elgg_prepend_css_urls($css, '../');
$this->assertEquals($css, $out);
// more complete: https://github.com/mrclay/minify/blob/master/tests/MinifyCSSUriRewriterTest.php
}
}

0 comments on commit 70d3aab

Please sign in to comment.