Class: Rack::PageSpeed::Filters::MinifyCSS

Inherits:
Base
  • Object
show all
Defined in:
lib/rack/pagespeed/filters/minify_css.rb

Instance Attribute Summary

Attributes inherited from Base

#document, #options

Instance Method Summary collapse

Methods inherited from Base

available_filters, inherited, #initialize, name, priority, requires_store

Constructor Details

This class inherits a constructor from Rack::PageSpeed::Filters::Base

Instance Method Details

#execute!(document) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rack/pagespeed/filters/minify_css.rb', line 14

def execute! document
  nodes = document.css('link[rel="stylesheet"][href]')
  return false unless nodes.count > 0
  nodes.each do |node|
    if match = %r(^/rack-pagespeed-(.*)).match(node['href'])
      store = @options[:store]
      store[match[1]] = Csso.optimize(store[match[1]])
    else
      status, headers, body = content_for node
      next unless node.name == 'link' && status == 200
      css = ""; body.each do |part| css << part end
      hash = Digest::MD5.hexdigest headers['Last-Modified'] + css
      @options[:store]["#{hash}.css"] = Csso.optimize(css)
      node['href'] = "/rack-pagespeed-#{hash}.css"
    end
  end
end