Skip to content

Commit

Permalink
Run :erb filter in the template context
Browse files Browse the repository at this point in the history
Close #1113
  • Loading branch information
k0kubun committed Nov 10, 2022
1 parent 6abb15c commit d7ecc85
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/haml/filters/erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Haml
class Filters
class Erb < TiltBase
def compile(node)
compile_with_tilt(node, 'erb')
precompiled_with_tilt(node, 'erb')
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/haml/filters/tilt_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def explicit_require?(needed_registration)

private

# TODO: support interpolation
def precompiled_with_tilt(node, name)
src = ::Tilt["t.#{name}"].new { node.value[:text] }.send(:precompiled, {}).first
[:dynamic, src]
end

def compile_with_tilt(node, name, indent_width: 0)
if ::Haml::Util.contains_interpolation?(node.value[:text])
dynamic_compile(node, name, indent_width: indent_width)
Expand Down
9 changes: 4 additions & 5 deletions test/haml/filters_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,10 @@ class ErbFilterTest < Haml::TestCase
assert_haml_ugly(%Q{:erb\n <%= "foo" +\n "bar" +\n "baz" %>})
end

test "should evaluate in the same context as Haml" do; skip
haml = ":erb\n <%= foo %>"
html = "bar\n"
scope = Object.new.instance_eval {foo = "bar"; nil if foo; binding}
assert_equal(html, render(haml, :scope => scope))
test "should evaluate in the same context as Haml" do
haml = "- foo = 'bar'\n:erb\n <%= foo %>"
html = "bar\n\n"
assert_equal(html, render(haml))
end

test "should use Rails's XSS safety features" do; skip
Expand Down

0 comments on commit d7ecc85

Please sign in to comment.