Skip to content

Commit

Permalink
Fallback to default image loading if loader returns nil
Browse files Browse the repository at this point in the history
  • Loading branch information
codez committed Jun 14, 2023
1 parent 07eef9f commit 4775426
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ This gem parses the given HTML and layouts the following elements in a vertical

All other elements are ignored, their content is added to the parent element. With a few exceptions, no CSS is processed. One exception is the `width` property of `img`, `td` and `th`, which may contain values in `cm`, `mm`, `px`, `pt`, `%` or `auto`. Another exception is the `rgb` or `cmyk` properties of the Prawn-specific `color` tag.

If no explicit loader is given (see above), images are loaded from `http(s)` addresses or may be contained in the `src` attribute as base64 encoded data URIs. Prawn only supports `PNG` and `JPG`.
If no explicit loader is given (see below), images are loaded from `http(s)` addresses or may be contained in the `src` attribute as base64 encoded data URIs. Prawn only supports `PNG` and `JPG`.

## Example

Expand Down Expand Up @@ -95,7 +95,7 @@ Beside these options handled by Prawn / prawn-table, the following values may be
- `:placeholder`
- `:too_large`: If the list content does not fit into the current bounding box, this text/callable is rendered instead. Defaults to '[list content too large]'.
- `:image`
- `:loader`: A callable that accepts the `src` attribute as an argument an returns a value understood by Prawn's `image` method. Loads `http(s)` URLs and base64 encoded data URIs by default.
- `:loader`: A callable that accepts the `src` attribute as an argument an returns a value understood by Prawn's `image` method (e.g. an `IO` object). If no loader is configured or if it returns `nil`, `http(s)` URLs and base64 encoded data URIs are loaded.
- `:placeholder`: If an image is not supported, this text/callable is rendered instead. Defaults to '[unsupported image]'.
- `:iframe`
- `:placeholder`: If the HTML contains IFrames, this text/callable is rendered instead.
Expand Down
8 changes: 6 additions & 2 deletions lib/prawn/markup/processor/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ def image_properties(src)
end

def load_image(src)
custom_load_image(src) ||
decode_base64_image(src) ||
load_remote_image(src)
end

def custom_load_image(src)
if options[:image] && options[:image][:loader]
options[:image][:loader].call(src)
else
decode_base64_image(src) || load_remote_image(src)
end
end

Expand Down
18 changes: 16 additions & 2 deletions spec/prawn/markup/processor/images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,24 @@
end

context 'with custom loader' do
let(:options) { { image: { loader: ->(src) { "spec/fixtures/#{src}" } } } }
let(:loader) do
->(src) do
match = src.match(/^fix:(.*?)$/)
"spec/fixtures/#{match[1]}" if match
end
end

let(:options) { { image: { loader: loader } } }

it 'render image with custom loader' do
processor.parse('<p>hello</p><img src="logo.png"><p>world</p>')
processor.parse('<p>hello</p><img src="fix:logo.png"><p>world</p>')

expect(left_positions).to eq([left, left])
expect(top_positions).to eq([top, top - line - LOGO_DIMENSION.last - p_gap - 5].map(&:round))
end

it 'falls back to default if loader returns nil' do
processor.parse("<p>hello</p><img src=\"#{encode_image('logo.png')}\"><p>world</p>")

expect(left_positions).to eq([left, left])
expect(top_positions).to eq([top, top - line - LOGO_DIMENSION.last - p_gap - 5].map(&:round))
Expand Down

0 comments on commit 4775426

Please sign in to comment.