From 6088b6bafe31b3d06943838486e6a691b2c0ef45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matyas?= Date: Sat, 4 May 2024 15:59:50 +0200 Subject: [PATCH 1/2] Ensure assets without extensions are handled correctly --- lib/wicked_pdf/wicked_pdf_helper/assets.rb | 15 +++++- .../wicked_pdf_helper_assets_test.rb | 53 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/lib/wicked_pdf/wicked_pdf_helper/assets.rb b/lib/wicked_pdf/wicked_pdf_helper/assets.rb index 77163265..929c6eaa 100644 --- a/lib/wicked_pdf/wicked_pdf_helper/assets.rb +++ b/lib/wicked_pdf/wicked_pdf_helper/assets.rb @@ -202,13 +202,26 @@ 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 { |asset, _v| File.basename(asset, File.extname(asset)) == path }&.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) diff --git a/test/functional/wicked_pdf_helper_assets_test.rb b/test/functional/wicked_pdf_helper_assets_test.rb index 2b8368b1..9346567c 100644 --- a/test/functional/wicked_pdf_helper_assets_test.rb +++ b/test/functional/wicked_pdf_helper_assets_test.rb @@ -11,6 +11,10 @@ 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) @@ -18,12 +22,46 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase 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 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_stylesheet_link_tag should inline the stylesheets passed in' do Rails.configuration.assets.expects(:compile => true) assert_equal "", 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 "", + 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 "", + 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 @@ -60,6 +98,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 "", + 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') From 25e2e73dfc8f64d6956b3a84096e4a71adcc72af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matyas?= Date: Wed, 29 May 2024 10:09:37 +0200 Subject: [PATCH 2/2] Support files in subdirectories correctly --- lib/wicked_pdf/wicked_pdf_helper/assets.rb | 8 +++++++- test/fixtures/subdirectory/nested.js | 1 + test/functional/wicked_pdf_helper_assets_test.rb | 15 +++++++++++++++ test/test_helper.rb | 5 +++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/subdirectory/nested.js diff --git a/lib/wicked_pdf/wicked_pdf_helper/assets.rb b/lib/wicked_pdf/wicked_pdf_helper/assets.rb index 929c6eaa..686d2d0c 100644 --- a/lib/wicked_pdf/wicked_pdf_helper/assets.rb +++ b/lib/wicked_pdf/wicked_pdf_helper/assets.rb @@ -216,7 +216,13 @@ def get_asset_path_from_manifest(path) assets = Rails.application.assets_manifest.assets if File.extname(path).empty? - assets.find { |asset, _v| File.basename(asset, File.extname(asset)) == path }&.last + 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 diff --git a/test/fixtures/subdirectory/nested.js b/test/fixtures/subdirectory/nested.js new file mode 100644 index 00000000..700bfaac --- /dev/null +++ b/test/fixtures/subdirectory/nested.js @@ -0,0 +1 @@ +// Nested js diff --git a/test/functional/wicked_pdf_helper_assets_test.rb b/test/functional/wicked_pdf_helper_assets_test.rb index 9346567c..e4df813a 100644 --- a/test/functional/wicked_pdf_helper_assets_test.rb +++ b/test/functional/wicked_pdf_helper_assets_test.rb @@ -26,6 +26,10 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase 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'), @@ -37,6 +41,17 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase 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 "", diff --git a/test/test_helper.rb b/test/test_helper.rb index 4820d5b2..26bb92df 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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')