Sha256: 8b4ef1ad7f7c5f84deba13097ed4696320617c10222e26f24138b5939419dad8

Contents?: true

Size: 1.16 KB

Versions: 42

Compression:

Stored size: 1.16 KB

Contents

module HTML
  class Pipeline
    # This filter rewrites image tags with a max-width inline style and also wraps
    # the image in an <a> tag that causes the full size image to be opened in a
    # new tab.
    #
    # The max-width inline styles are especially useful in HTML email which
    # don't use a global stylesheets.
    class ImageMaxWidthFilter < Filter
      def call
        doc.search('img').each do |element|
          # Skip if there's already a style attribute. Not sure how this
          # would happen but we can reconsider it in the future.
          next if element['style']

          # Bail out if src doesn't look like a valid http url. trying to avoid weird
          # js injection via javascript: urls.
          next if element['src'].to_s.strip =~ /\Ajavascript/i

          element['style'] = "max-width:100%;"

          if !has_ancestor?(element, %w(a))
            link_image element
          end
        end

        doc
      end

      def link_image(element)
        link = doc.document.create_element('a', :href => element['src'], :target => '_blank')
        link.add_child(element.dup)
        element.replace(link)
      end
    end
  end
end

Version data entries

42 entries across 42 versions & 3 rubygems

Version Path
html-pipeline-1.6.0 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-1.5.0 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-1.4.0 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-1.3.0 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-1.1.0 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-1.0.0 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-0.3.1 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-0.3.0 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-0.2.1 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-0.2.0 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-0.1.0 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-0.0.14 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-0.0.13 lib/html/pipeline/image_max_width_filter.rb
geothird-html-pipeline-0.0.12 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-0.0.12 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-0.0.11 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-0.0.10 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-0.0.8 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-0.0.7 lib/html/pipeline/image_max_width_filter.rb
html-pipeline-no-charlock-0.0.6 lib/html/pipeline/image_max_width_filter.rb