Skip to content

Commit

Permalink
Merge pull request #122 from iainbeeston/patch-3
Browse files Browse the repository at this point in the history
Added support for annotate_rendered_view_with_filenames
  • Loading branch information
rafaelfranca authored Mar 14, 2024
2 parents 2a94d6e + e795387 commit b0fc796
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
14 changes: 12 additions & 2 deletions lib/better_html/better_erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,20 @@ def generate(template, source)
klass ||= self.class.erb_implementation

escape = self.class.escape_ignore_list.include?(template.type)

options = {
escape: escape,
trim: (self.class.erb_trim_mode == "-"),
}
if BetterHtml.config.annotate_rendered_view_with_filenames && template.format == :html
options[:preamble] = "@output_buffer.safe_append='<!-- BEGIN #{template.short_identifier} -->';"
options[:postamble] = "@output_buffer.safe_append='<!-- END #{template.short_identifier} -->';" \
"@output_buffer.to_s"
end

generator = klass.new(
erb,
escape: escape,
trim: (self.class.erb_trim_mode == "-")
**options
)
generator.validate! if generator.respond_to?(:validate!)
generator.src
Expand Down
1 change: 1 addition & 0 deletions lib/better_html/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Config
property :template_exclusion_filter
property :lodash_safe_javascript_expression, default: -> { [/\AJSON\.stringify\(/] }
property :disable_parser_validation, default: false
property :annotate_rendered_view_with_filenames, default: false

def javascript_attribute_name?(name)
javascript_attribute_names.any? { |other| other === name.to_s } # rubocop:disable Style/CaseEquality
Expand Down
6 changes: 6 additions & 0 deletions lib/better_html/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ class Railtie < Rails::Railtie
initializer "better_html.better_erb.initialization" do
BetterHtml::BetterErb.prepend!
end

config.after_initialize do
ActiveSupport.on_load(:action_view) do
BetterHtml.config.annotate_rendered_view_with_filenames = ActionView::Base.annotate_rendered_view_with_filenames
end
end
end
end
18 changes: 16 additions & 2 deletions test/better_html/better_erb/implementation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,20 @@ class ImplementationTest < ActiveSupport::TestCase
end
end

if ActionView.version >= Gem::Version.new("6.1")
test "with ActionView 6.1 comments are added to show the filename when annotate_rendered_view_with_filenames=true" do
config = build_config(annotate_rendered_view_with_filenames: true)
assert_equal "<!-- BEGIN _better_test.html.erb --><foo>bar<foo><!-- END _better_test.html.erb -->",
render("<foo>bar<foo>", config: config, filename: "_better_test.html.erb")
end
else
test "with ActionView 6.0 annotate_rendered_view_with_filenames=true does not change the output" do
config = build_config(annotate_rendered_view_with_filenames: true)
assert_equal "<foo>bar<foo>",
render("<foo>bar<foo>", config: config, filename: "_better_test.html.erb")
end
end

test "capture works as intended" do
output = render(<<-HTML)
<%- foo = capture do -%>
Expand Down Expand Up @@ -420,13 +434,13 @@ def build_config(**options)
BetterHtml::Config.new(**options)
end

def render(source, config: build_config, locals: {})
def render(source, config: build_config, locals: {}, filename: "test.html.erb")
old_config = BetterHtml.config
BetterHtml.config = config

ActionView::Template.new(
source,
"test.html.erb",
filename,
ActionView::Template::Handlers::ERB.new,
virtual_path: "partial",
format: :html,
Expand Down

0 comments on commit b0fc796

Please sign in to comment.