class DynamicAssets::StylesheetReference
Constants
- RELATIVE_URL_ROOT
CSS style sheets can contain relative urls like this:
background: url(something.png)
The browser will look for the resource in the same location as the CSS file. However, we serve static resources like images from a different location, so the
StylesheetReference
will prependRELATIVE_URL_ROOT
to each such relative url in a stylesheet.
Public Instance Methods
formats()
click to toggle source
# File lib/dynamic_assets/reference/stylesheet_reference.rb, line 19 def formats %w(sass scss css) end
type()
click to toggle source
# File lib/dynamic_assets/reference/stylesheet_reference.rb, line 23 def type :stylesheets end
Protected Instance Methods
read_member(member_name, for_signature)
click to toggle source
Overridden to transform URLs embedded in the CSS
Calls superclass method
DynamicAssets::Reference#read_member
# File lib/dynamic_assets/reference/stylesheet_reference.rb, line 31 def read_member(member_name, for_signature) content_string = super return content_string if for_signature format = format_for_member_name member_name content_string = case format when :css content_string when :sass,:scss location = File.dirname path_for_member_name(member_name) Sass::Engine.new(content_string, :syntax => format, :load_paths => [location], :cache => false).render else raise "unknown format #{format}" end transform_urls member_name, content_string end
transform_urls(member_name, content_string)
click to toggle source
# File lib/dynamic_assets/reference/stylesheet_reference.rb, line 51 def transform_urls(member_name, content_string) # Prepend url_root to each relative url that doesn't start with a slash. # # Inside fancy.css, any of these: # url(one/thing.png) # url('one/thing.png') # url( "one/thing.png" ) # url(../one/thing.png) # will become # url(/stylesheets/fancy/one/thing.png) # content_string.gsub( /url\( *['"]?([^)]*[^'"])['"]? *\)/ ) do |s| url = $1 url.gsub! /^(\.\.|\.)\//, '' (url !~ /^\// && url.relative_url?) ? "url(#{RELATIVE_URL_ROOT}/#{member_name}/#{url})" : s end end