summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/image_lazy_load_filter.rb
blob: 7a81d583b822d69dca4f2b0fc0fcb2b291b081d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module Banzai
  module Filter
    # HTML filter that moves the value of the src attribute to the data-src attribute so it can be lazy loaded
    class ImageLazyLoadFilter < HTML::Pipeline::Filter
      def call
        doc.xpath('descendant-or-self::img').each do |img|
          img['class'] ||= '' << 'lazy'
          img['data-src'] = img['src']
          img['src'] = LazyImageTagHelper.placeholder_image          
        end
        
        doc
      end
    end
  end
end