From 95f311523e9c8166acb1be4d0175dca25074deb0 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Wed, 25 Nov 2009 20:04:04 -0800 Subject: [PATCH] [Haml] Allow - if with no content to be followed by - else with content. Closes gh-62 --- doc-src/HAML_CHANGELOG.md | 9 +++++++++ lib/haml/precompiler.rb | 8 +++++++- test/haml/engine_test.rb | 10 ++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/doc-src/HAML_CHANGELOG.md b/doc-src/HAML_CHANGELOG.md index bc24d465..33d5a9d6 100644 --- a/doc-src/HAML_CHANGELOG.md +++ b/doc-src/HAML_CHANGELOG.md @@ -3,6 +3,15 @@ * Table of contents {:toc} +## 2.2.15 (Unreleased) + +* Allow `if` statements with no content followed by `else` clauses. + For example: + + - if foo + - else + bar + ## [2.2.14](http://github.com/nex3/haml/commit/2.2.14) * Don't print warnings when escaping attributes containing non-ASCII characters diff --git a/lib/haml/precompiler.rb b/lib/haml/precompiler.rb index dd461450..a7dadb01 100644 --- a/lib/haml/precompiler.rb +++ b/lib/haml/precompiler.rb @@ -246,7 +246,13 @@ END # It's important to preserve tabulation modification for keywords # that involve choosing between posible blocks of code. if %w[else elsif when].include?(keyword) - @dont_indent_next_line, @dont_tab_up_next_text = @to_close_stack.last[1..2] + # @to_close_stack may not have a :script on top + # when the preceding "- if" has nothing nested + if @to_close_stack.last && @to_close_stack.last.first == :script + @dont_indent_next_line, @dont_tab_up_next_text = @to_close_stack.last[1..2] + else + push_and_tabulate([:script, @dont_indent_next_line, @dont_tab_up_next_text]) + end # when is unusual in that either it will be indented twice, # or the case won't have created its own indentation diff --git a/test/haml/engine_test.rb b/test/haml/engine_test.rb index a63f1dce..073ce7b6 100644 --- a/test/haml/engine_test.rb +++ b/test/haml/engine_test.rb @@ -581,6 +581,16 @@ HTML HAML end + def test_if_without_content_and_else + assert_equal(<