You are here

function _less_rewrite_paths in Less CSS Preprocessor 7.3

Same name and namespace in other branches
  1. 8 includes/less.process.inc \_less_rewrite_paths()
  2. 7.4 includes/less.process.inc \_less_rewrite_paths()
  3. 7.2 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.

Return value

string Processed styles with replaced paths.

See also

drupal_build_css_cache()

1 call to _less_rewrite_paths()
_less_process_file in ./less.process.inc
@file Contains functions related to compiling .less files.

File

./less.module, line 232
Handles compiling of .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;
}