From a52f7fcc1bdf5e2dcc48f94527b2e6ff1a47b71d Mon Sep 17 00:00:00 2001 From: Ibraheem Tuffaha Date: Fri, 14 Jun 2024 13:56:56 +0300 Subject: [PATCH] Run code in else block if ruby tag is standalone --- lib/erb/formatter.rb | 5 +++++ test/fixtures/yield.html.erb | 24 ++++++++++++++++++++++ test/fixtures/yield.html.expected.erb | 29 +++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 test/fixtures/yield.html.erb create mode 100644 test/fixtures/yield.html.expected.erb diff --git a/lib/erb/formatter.rb b/lib/erb/formatter.rb index b14b375..3e1db4f 100644 --- a/lib/erb/formatter.rb +++ b/lib/erb/formatter.rb @@ -61,6 +61,7 @@ class Error < StandardError; end end.freeze end + RUBY_STANDALONE_BLOCK = /\A(yield|next)\b/ RUBY_CLOSE_BLOCK = /\Aend\z/ RUBY_REOPEN_BLOCK = /\A(else|elsif\b(.*)|when\b(.*))\z/ @@ -306,6 +307,10 @@ def format_erb_tags(string) erb_open << ' ' unless ruby_code.start_with?('#') case ruby_code + when RUBY_STANDALONE_BLOCK + ruby_code = format_ruby(ruby_code, autoclose: false) + full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}" + html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag) when RUBY_CLOSE_BLOCK full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}" tag_stack_pop('%erb%', ruby_code) diff --git a/test/fixtures/yield.html.erb b/test/fixtures/yield.html.erb new file mode 100644 index 0000000..5dc9a73 --- /dev/null +++ b/test/fixtures/yield.html.erb @@ -0,0 +1,24 @@ +
+
+
+
+<%= yield %> +
+<%= next %> +
+<%= break %> +
+
+ +
+<% flash.each do |type, message| %> +<% next unless message.is_a?(String) %> + +<% case type %> +<% when "alert" %> +<%= render("shared/alert_flash", message: message) %> +<% when "notice" %> +<%= render("shared/notice_flash", message: message) %> +<% end %> +<% end %> +
diff --git a/test/fixtures/yield.html.expected.erb b/test/fixtures/yield.html.expected.erb new file mode 100644 index 0000000..adfbe8e --- /dev/null +++ b/test/fixtures/yield.html.expected.erb @@ -0,0 +1,29 @@ +
+
+
+
+ <%= yield %> +
+ <%= next %> +
+ <%= break %> +
+
+ +
+ <% flash.each do |type, message| %> + <% next unless message.is_a?(String) %> + + <% case type %> + <% when "alert" %> + <%= render("shared/alert_flash", message: message) %> + <% when "notice" %> + <%= render("shared/notice_flash", message: message) %> + <% end %> + <% end %> +