class Roadie::AssetScanner

@api private

The asset scanner's main usage is finding and/or extracting styles from a DOM tree. Referenced styles will be found using the provided asset provider.

Any style declaration tagged with data-roadie-ignore will be ignored, except for having the attribute itself removed.

Constants

CLEANING_MATCHER

Cleans out stupid CDATA and/or HTML comments from the style text TinyMCE causes this, allegedly

STYLE_ELEMENT_QUERY

Attributes

dom[R]
external_asset_provider[R]
normal_asset_provider[R]

Public Class Methods

new(dom, normal_asset_provider, external_asset_provider) click to toggle source

@param [Nokogiri::HTML::Document] dom @param [#find_stylesheet!] #normal_asset_provider @param [#find_stylesheet!] #external_asset_provider

# File lib/roadie/asset_scanner.rb, line 16
def initialize(dom, normal_asset_provider, external_asset_provider)
  @dom = dom
  @normal_asset_provider = normal_asset_provider
  @external_asset_provider = external_asset_provider
end

Public Instance Methods

extract_css() click to toggle source

Looks for all non-ignored stylesheets, removes their references from the DOM and then returns them.

This will mutate the DOM tree.

The order of the array corresponds with the document order in the DOM.

@see find_css @return [Enumerable<Stylesheet>] every extracted stylesheet

# File lib/roadie/asset_scanner.rb, line 43
def extract_css
  stylesheets = @dom.css(STYLE_ELEMENT_QUERY).map { |element|
    stylesheet = read_stylesheet(element)
    element.remove if stylesheet
    stylesheet
  }.compact
  remove_ignore_markers
  stylesheets
end
find_css() click to toggle source

Looks for all non-ignored stylesheets and returns them.

This method will not mutate the DOM and is safe to call multiple times.

The order of the array corresponds with the document order in the DOM.

@see extract_css @return [Enumerable<Stylesheet>] every found stylesheet

# File lib/roadie/asset_scanner.rb, line 30
def find_css
  @dom.css(STYLE_ELEMENT_QUERY).map { |element| read_stylesheet(element) }.compact
end

Private Instance Methods

clean_css(css) click to toggle source
# File lib/roadie/asset_scanner.rb, line 94
def clean_css(css)
  css.gsub(CLEANING_MATCHER, '')
end
read_style_element(element) click to toggle source
# File lib/roadie/asset_scanner.rb, line 82
def read_style_element(element)
  Stylesheet.new "(inline)", clean_css(element.text.strip)
end
read_stylesheet(element) click to toggle source
# File lib/roadie/asset_scanner.rb, line 74
def read_stylesheet(element)
  if element.name == "style"
    read_style_element element
  elsif element.name == "link" && element['media'] != "print" && element["href"]
    read_link_element element
  end
end
remove_ignore_markers() click to toggle source
# File lib/roadie/asset_scanner.rb, line 106
def remove_ignore_markers
  @dom.css("[data-roadie-ignore]").each do |node|
    node.remove_attribute "data-roadie-ignore"
  end
end
should_find_external?() click to toggle source
# File lib/roadie/asset_scanner.rb, line 98
def should_find_external?
  return false unless external_asset_provider
  # If external_asset_provider is empty list; don't use it.
  return false if external_asset_provider.respond_to?(:empty?) && external_asset_provider.empty?

  true
end