Class: Rack::PageSpeed::Filters::InlineImages

Inherits:
Rack::PageSpeed::Filter
  • Object
show all
Defined in:
lib/rack/pagespeed/filters/inline_images.rb

Instance Method Summary collapse

Instance Method Details

#execute!(document) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rack/pagespeed/filters/inline_images.rb', line 4

def execute! document
  nodes = document.css('img')
  return false unless nodes.count > 0
  nodes.each do |node|
    file = file_for node
    next if !file or file.stat.size > (@options[:max_size] or 1024)
    img = node.clone
    img['src'] = "data:#{Rack::Mime.mime_type(File.extname(file.path))};base64,#{[file.read].pack('m')}"
    img['alt'] = node['alt'] if node['alt']
    node.before img
    node.remove
  end
end