class Roadie::UrlRewriter
@api private
Class that rewrites URLs in the DOM.
Constants
- CSS_URL_REGEXP
Regexp matching all the url() declarations in CSS
It matches without any quotes and with both single and double quotes inside the parenthesis. There's much room for improvement, of course.
Public Class Methods
@param [UrlGenerator] generator
# File lib/roadie/url_rewriter.rb, line 7 def initialize(generator) @generator = generator end
Public Instance Methods
Mutates passed CSS, rewriting url() directives.
This will make all URLs inside url() absolute.
- nil
-
is returned so no one can misunderstand that this method mutates
the passed string.
@param [String] css the css to mutate @return [nil] css is mutated
# File lib/roadie/url_rewriter.rb, line 39 def transform_css(css) css.gsub!(CSS_URL_REGEXP) do matches = Regexp.last_match "url(#{matches[:quote]}#{generate_url(matches[:url])}#{matches[:quote]})" end end
Mutates the passed DOM tree, rewriting certain element's attributes.
This will make all a and img into absolute URLs, as well as all “url()” directives inside style-attributes.
- nil
-
is returned so no one can misunderstand that this method mutates
the passed instance.
@param [Nokogiri::HTML::Document] dom @return [nil] DOM tree is mutated
# File lib/roadie/url_rewriter.rb, line 21 def transform_dom(dom) # Use only a single loop to do this dom.css("a[href], img[src], *[style]").each do |element| transform_element_style element if element.has_attribute?('style') transform_element element end nil end
Private Instance Methods
# File lib/roadie/url_rewriter.rb, line 47 def generate_url(*args) @generator.generate_url(*args) end
# File lib/roadie/url_rewriter.rb, line 69 def transform_element(element) case element.name when "a" then element["href"] = generate_url element["href"] when "img" then element["src"] = generate_url element["src"] end end
# File lib/roadie/url_rewriter.rb, line 76 def transform_element_style(element) # We need to use a setter for Nokogiri to detect the string mutation. # If nokogiri used "dumber" data structures, this would all be redundant. css = element["style"] transform_css css element["style"] = css end