function _less_rewrite_paths in Less CSS Preprocessor 7.4
Same name and namespace in other branches
- 8 includes/less.process.inc \_less_rewrite_paths()
- 7.2 less.module \_less_rewrite_paths()
- 7.3 less.module \_less_rewrite_paths()
Copied functionality from drupal_build_css_cache() for our own purposes.
This function processes $contents and rewrites relative paths to be absolute from web root. This is mainly used to ensure that compiled .less files still reference images at their original paths.
Parameters
string $input_filepath:
string $contents:
Return value
string Processed styles with replaced paths.
See also
2 calls to _less_rewrite_paths()
- LessUnitTest::test_less_rewrite_paths in tests/
less.test - Test the _less_rewrite_paths() function.
- _less_process_file in includes/
less.process.inc - Process a .less file and save the compiled styles.
File
- includes/
less.process.inc, line 263 - Contains functions related to compiling .less files.
Code
function _less_rewrite_paths($input_filepath, $contents) {
$output = '';
// Build the base URL of this CSS file: start with the full URL.
$css_base_url = file_create_url($input_filepath);
// Move to the parent.
$css_base_url = substr($css_base_url, 0, strrpos($css_base_url, '/'));
// Simplify to a relative URL if the stylesheet URL starts with the
// base URL of the website.
if (substr($css_base_url, 0, strlen($GLOBALS['base_root'])) == $GLOBALS['base_root']) {
$css_base_url = substr($css_base_url, strlen($GLOBALS['base_root']));
}
_drupal_build_css_path(NULL, $css_base_url . '/');
// Anchor all paths in the CSS with its base URL, ignoring external and absolute paths.
$output .= preg_replace_callback('/url\\(\\s*[\'"]?(?![a-z]+:|\\/+)([^\'")]+)[\'"]?\\s*\\)/i', '_drupal_build_css_path', $contents);
return $output;
}