From 9f47973dacc365ef8c7c59bcc7cb6b2110365147 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 21 Jun 2024 08:07:12 -0700 Subject: [PATCH] Make SassTemplate ignore unsupported options when using sass-embedded When using sass and sassc, unsupported options are ignored, and that should be the same behavior when using sass-embedded. Use #parameters to attempt to determine the supported options. I didn't look at all versions of sass-embedded to determine if the code to get the supported options will work in all cases, so if there are any problems determining the supported options, treat all options as supported (same as previous behavior). Add a test that uses an unsupported option. --- .ci.gemfile | 4 +++- CHANGELOG.md | 1 + lib/tilt/sass.rb | 15 ++++++++++++--- test/tilt_sasstemplate_test.rb | 5 +++++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.ci.gemfile b/.ci.gemfile index d03f51a..15fa009 100644 --- a/.ci.gemfile +++ b/.ci.gemfile @@ -56,8 +56,10 @@ else gem 'haml', '>= 4' end -if RUBY_VERSION >= '3.1' +if RUBY_VERSION >= '3.2' gem 'sass-embedded' +elsif RUBY_VERSION >= '3.1' + gem 'sass-embedded', '< 1.70' elsif RUBY_ENGINE == 'jruby' gem 'sass' else diff --git a/CHANGELOG.md b/CHANGELOG.md index db7ad45..c2389e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * Support commonmarker 1.0+ API (unasuke) (#10) * Make etanni template work with frozen string literals (jeremyevans) * Deprecate erubis, wikicloth, and maruku templates as they require modifying frozen string literals (jeremyevans) +* Make SassTemplate ignore unsupported options when using sass-embedded (jeremyevans) ## 2.3.0 (2023-09-14) diff --git a/lib/tilt/sass.rb b/lib/tilt/sass.rb index ac2e2ba..24fb0d5 100644 --- a/lib/tilt/sass.rb +++ b/lib/tilt/sass.rb @@ -13,6 +13,13 @@ class SassTemplate < StaticTemplate # :nocov: require 'uri' + ALLOWED_KEYS = (defined?(::Sass::Compiler) ? ::Sass::Compiler : ::Sass::Embedded). + instance_method(:compile_string). + parameters. + map{|k, v| v if k == :key}. + compact rescue nil + private_constant :ALLOWED_KEYS + private def _prepare_output @@ -22,9 +29,11 @@ def _prepare_output def sass_options path = File.absolute_path(eval_file) path = '/' + path unless path.start_with?('/') - @options[:url] = ::URI::File.build([nil, ::URI::DEFAULT_PARSER.escape(path)]).to_s - @options[:syntax] = :indented - @options + opts = @options.dup + opts[:url] = ::URI::File.build([nil, ::URI::DEFAULT_PARSER.escape(path)]).to_s + opts[:syntax] = :indented + opts.delete_if{|k| !ALLOWED_KEYS.include?(k)} if ALLOWED_KEYS + opts end rescue LoadError => err begin diff --git a/test/tilt_sasstemplate_test.rb b/test/tilt_sasstemplate_test.rb index 4b6c167..79b5f00 100644 --- a/test/tilt_sasstemplate_test.rb +++ b/test/tilt_sasstemplate_test.rb @@ -22,4 +22,9 @@ template = Tilt::ScssTemplate.new({ style: :compressed }) { |t| "#main {\n background-color: #0000f1;\n}" } 3.times { assert_equal "#main{background-color:#0000f1}", template.render.chomp } end + + it "compiles and evaluates the template on #render with unsupported options" do + template = Tilt::ScssTemplate.new({ style: :compressed, outvar: '@a' }) { |t| "#main {\n background-color: #0000f1;\n}" } + 3.times { assert_equal "#main{background-color:#0000f1}", template.render.chomp } + end end