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 9 def initialize(generator) @generator = generator end
Public Instance Methods
Mutates passed CSS, rewriting url() directives.
This will make all URLs inside url() absolute.
Copy of CSS that is mutated is returned, passed string is not mutated.
@param [String] css the css to mutate @return [String] copy of css that is mutated
# File lib/roadie/url_rewriter.rb, line 44 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 23 def transform_dom(dom) # Use only a single loop to do this dom.css( "a[href]:not([data-roadie-ignore]), " \ "img[src]:not([data-roadie-ignore]), " \ "*[style]:not([data-roadie-ignore])", ).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 52 def generate_url(*args) @generator.generate_url(*args) end
# File lib/roadie/url_rewriter.rb, line 74 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 81 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"] element["style"] = transform_css(css) end