Sha256: 54a20990c89ac06733f0c339ad4d832ce5fab512f013b634a8e74b5f0c0ca139

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require 'open-uri'
require 'zlib'

module PremailerRails
  module CSSHelper
    extend self

    @cache = {}
    attr :cache

    STRATEGIES = [
      CSSLoaders::CacheLoader,
      CSSLoaders::HassleLoader,
      CSSLoaders::AssetPipelineLoader,
      CSSLoaders::FileSystemLoader
    ]

    # Returns all linked CSS files concatenated as string.
    def css_for_doc(doc)
      urls = css_urls_in_doc(doc)
      if urls.empty?
        load_css(:default) unless has_inline_css? doc
      else
        urls.map { |url| load_css(url) }.join("\n")
      end
    end

    private

    def css_urls_in_doc(doc)
      doc.search('link[@type="text/css"]').map do |link|
        link.attributes['href'].to_s
      end
    end

    def has_inline_css?(doc)
      not doc.search('style[@type="text/css"]').empty?
    end

    def load_css(url)
      path = extract_path(url)

      @cache[path] = STRATEGIES.each do |strategy|
                       css = strategy.load(path)
                       break css if css
                     end
    end

    # Extracts the path of a url.
    def extract_path(url)
      if url.is_a? String
        # Remove everything after ? including ?
        url = url[0..(url.index('?') - 1)] if url.include? '?'
        # Remove the host
        url = url.sub(/^https?\:\/\/[^\/]*/, '') if url.index('http') == 0
      else
        url
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
premailer-rails3-1.3.0 lib/premailer-rails3/css_helper.rb