Class: Roadie::UrlRewriter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/roadie/url_rewriter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class that rewrites URLs in the DOM.

Instance Method Summary collapse

Constructor Details

#initialize(generator) ⇒ UrlRewriter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of UrlRewriter.

Parameters:



7
8
9
# File 'lib/roadie/url_rewriter.rb', line 7

def initialize(generator)
  @generator = generator
end

Instance Method Details

#transform_css(css) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Parameters:

  • css (String)

    the css to mutate

Returns:

  • (nil)

    css is mutated



39
40
41
42
43
44
# 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

#transform_dom(dom) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Parameters:

  • dom (Nokogiri::HTML::Document)

Returns:

  • (nil)

    DOM tree is mutated



21
22
23
24
25
26
27
28
# 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