module SprocketsRelativeUrl class Processor < Sprockets::Processor CSS_URL_REGEXP = / (?<=\burl\() # Match url\( prefix \s* (?['"]?) # Opening quote (?[^\)]+?) # Capture actual path \k # Backref for closing quote \s* (?=\)) # Match \) terminator /x def evaluate(context, *) data.gsub(CSS_URL_REGEXP) do path = Regexp.last_match[:path] next path if should_skip?(path) root_relative_path(context, path) end end private def root_relative_path(context, path) asset_full_path = context.pathname.parent.join(path) asset = context.environment.find_asset(asset_full_path) return path if asset.nil? context.asset_path(asset.logical_path) end def should_skip?(path) path.start_with?('/') || path.include?('\\') || URI.parse(path).absolute? end end end