Class: Roadie::UrlRewriter Private
- Inherits:
-
Object
- Object
- Roadie::UrlRewriter
- 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
-
#initialize(generator) ⇒ UrlRewriter
constructor
private
A new instance of UrlRewriter.
-
#transform_css(css) ⇒ String
private
Mutates passed CSS, rewriting url() directives.
-
#transform_dom(dom) ⇒ nil
private
Mutates the passed DOM tree, rewriting certain element’s attributes.
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.
9 10 11 |
# File 'lib/roadie/url_rewriter.rb', line 9 def initialize(generator) @generator = generator end |
Instance Method Details
#transform_css(css) ⇒ String
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.
Copy of CSS that is mutated is returned, passed string is not mutated.
44 45 46 47 48 49 |
# 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 |
#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.
23 24 25 26 27 28 29 30 31 32 33 34 |
# 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 |