Skip to content

Commit

Permalink
Make SassTemplate ignore unsupported options when using sass-embedded
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jeremyevans committed Jun 21, 2024
1 parent ac9640c commit 9f47973
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .ci.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 12 additions & 3 deletions lib/tilt/sass.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/tilt_sasstemplate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9f47973

Please sign in to comment.