Skip to content

Commit

Permalink
Added support for annotate_rendered_view_with_filenames
Browse files Browse the repository at this point in the history
ActionView's erb template handler can annotate html with comments to indicate which file any given snippet of html comes from. This is very useful in development, but it isn't supported by better-html.

This adds equivalent support to better-html, using the same approach that ActionView takes, so it should work automatically for anyone switching from rails' default erb handler.
  • Loading branch information
iainbeeston committed Mar 14, 2024
1 parent 6bed0a9 commit 2a8991c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/better_html/better_erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,19 @@ def generate(template, source)
klass ||= self.class.erb_implementation

escape = self.class.escape_ignore_list.include?(template.type)
generator = klass.new(
erb,

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,
**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 |app|
ActiveSupport.on_load(:action_view) do
BetterHtml.config.annotate_rendered_view_with_filenames = ActionView::Base.annotate_rendered_view_with_filenames
end
end
end
end
12 changes: 10 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,14 @@ class ImplementationTest < ActiveSupport::TestCase
end
end

if ActionView.version >= Gem::Version.new("6.1")
test "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
end

test "capture works as intended" do
output = render(<<-HTML)
<%- foo = capture do -%>
Expand Down Expand Up @@ -420,13 +428,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 2a8991c

Please sign in to comment.