Skip to content

Commit

Permalink
Merge pull request #1115 from d4rky-pl/local-assets-fixes
Browse files Browse the repository at this point in the history
Fix: Ensure assets without extensions are handled correctly
  • Loading branch information
unixmonkey committed Aug 11, 2024
2 parents 6c41464 + 25e2e73 commit 9081841
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/wicked_pdf/wicked_pdf_helper/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,32 @@ def find_asset(path)
elsif defined?(Propshaft::Assembly) && Rails.application.assets.is_a?(Propshaft::Assembly)
PropshaftAsset.new(Rails.application.assets.load_path.find(path))
elsif Rails.application.respond_to?(:assets_manifest)
asset_path = File.join(Rails.application.assets_manifest.dir, Rails.application.assets_manifest.assets[path])
relative_asset_path = get_asset_path_from_manifest(path)
return unless relative_asset_path

asset_path = File.join(Rails.application.assets_manifest.dir, relative_asset_path)
LocalAsset.new(asset_path) if File.file?(asset_path)
else
SprocketsEnvironment.find_asset(path, :base_path => Rails.application.root.to_s)
end
end

def get_asset_path_from_manifest(path)
assets = Rails.application.assets_manifest.assets

if File.extname(path).empty?
assets.find do |asset, _v|
directory = File.dirname(asset)
asset_path = File.basename(asset, File.extname(asset))
asset_path = File.join(directory, asset_path) if directory != '.'

asset_path == path
end&.last
else
assets[path]
end
end

# will prepend a http or default_protocol to a protocol relative URL
# or when no protcol is set.
def prepend_protocol(source)
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/subdirectory/nested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Nested js
68 changes: 68 additions & 0 deletions test/functional/wicked_pdf_helper_assets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,72 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase

teardown do
WickedPdf.config = @saved_config

# @see freerange/mocha#331
Rails.application.unstub(:assets)
Rails.application.unstub(:assets_manifest)
end

if Rails::VERSION::MAJOR > 3 || (Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR > 0)
test 'wicked_pdf_asset_base64 returns a base64 encoded asset' do
assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked.css')
end

test 'wicked_pdf_asset_base64 works without file extension when using sprockets' do
assert_match %r{data:application\/javascript;base64,.+}, wicked_pdf_asset_base64('wicked')
end

test 'wicked_pdf_asset_base64 works with nested files and without file extension when using sprockets' do
assert_match %r{data:application\/javascript;base64,.+}, wicked_pdf_asset_base64('subdirectory/nested')
end

test 'wicked_pdf_asset_base64 works without file extension when using asset manifest' do
stub_manifest = OpenStruct.new(
:dir => Rails.root.join('app/assets'),
:assets => { 'wicked.css' => 'stylesheets/wicked.css', 'wicked.js' => 'javascripts/wicked.js' }
)
Rails.application.stubs(:assets).returns(nil)
Rails.application.stubs(:assets_manifest).returns(stub_manifest)

assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked')
end

test 'wicked_pdf_asset_base64 works with nested files and without file extension when using asset manifest' do
stub_manifest = OpenStruct.new(
:dir => Rails.root.join('app/assets'),
:assets => { 'subdirectory/nested.js' => 'javascripts/subdirectory/nested.js' }
)
Rails.application.stubs(:assets).returns(nil)
Rails.application.stubs(:assets_manifest).returns(stub_manifest)

assert_match %r{data:text\/javascript;base64,.+}, wicked_pdf_asset_base64('subdirectory/nested')
end

test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
Rails.configuration.assets.expects(:compile => true)
assert_equal "<style type='text/css'>/* Wicked styles */\n\n</style>",
wicked_pdf_stylesheet_link_tag('wicked')
end

test 'wicked_pdf_stylesheet_link_tag should work without file extension when using sprockets' do
Rails.configuration.assets.expects(:compile => true)
assert_equal "<style type='text/css'>/* Wicked styles */\n\n</style>",
wicked_pdf_stylesheet_link_tag('wicked')
end

test 'wicked_pdf_stylesheet_link_tag should work without file extension when using asset manifest' do
stub_manifest = OpenStruct.new(
:dir => Rails.root.join('app/assets'),
:assets => { 'wicked.css' => 'stylesheets/wicked.css', 'wicked.js' => 'javascripts/wicked.js' }
)

Rails.application.stubs(:assets).returns(nil)
Rails.application.stubs(:assets_manifest).returns(stub_manifest)

assert_equal "<style type='text/css'>/* Wicked styles */\n</style>",
wicked_pdf_stylesheet_link_tag('wicked')
end

test 'wicked_pdf_stylesheet_link_tag should raise if the stylesheet is not available and config is set' do
Rails.configuration.assets.expects(:compile => true)
WickedPdf.config[:raise_on_missing_assets] = true
Expand Down Expand Up @@ -60,6 +113,21 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
wicked_pdf_stylesheet_link_tag('https://www.example.com/wicked.css')
end

test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in when assets are remote and using asset manifest' do
stub_manifest = OpenStruct.new(
:dir => Rails.root.join('app/assets'),
:assets => { 'wicked.css' => 'stylesheets/wicked.css', 'wicked.js' => 'javascripts/wicked.js' }
)

Rails.application.stubs(:assets).returns(nil)
Rails.application.stubs(:assets_manifest).returns(stub_manifest)

stub_request(:get, 'https://www.example.com/wicked.css').to_return(:status => 200, :body => '/* Wicked styles */')
expects(:precompiled_or_absolute_asset? => true).twice
assert_equal "<style type='text/css'>/* Wicked styles */</style>",
wicked_pdf_stylesheet_link_tag('https://www.example.com/wicked.css')
end

test 'wicked_pdf_stylesheet_link_tag should raise if remote assets are not available and config is set' do
WickedPdf.config[:raise_on_missing_assets] = true
stub_request(:get, 'https://www.example.com/wicked.css').to_return(:status => 404, :body => 'File not found')
Expand Down
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
source = File.read('test/fixtures/wicked.js')
File.open(destination, 'w') { |f| f.write(source) }

Dir.mkdir(js_dir.join('subdirectory')) unless File.directory?(js_dir.join('subdirectory'))
destination = js_dir.join('subdirectory/nested.js')
source = File.read('test/fixtures/subdirectory/nested.js')
File.open(destination, 'w') { |f| f.write(source) }

config_dir = assets_dir.join('config')
Dir.mkdir(config_dir) unless File.directory?(config_dir)
source = File.read('test/fixtures/manifest.js')
Expand Down

0 comments on commit 9081841

Please sign in to comment.