From 15ae22e3b2f6fe45c07a36132ad839debc5aa6df Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sun, 11 Oct 2015 23:04:33 +0900 Subject: [PATCH] Insert newline inside tag --- lib/hamlit/whitespace_handler.rb | 12 ++++++ test/haml-spec/tests.yml | 74 ++++++++++++++++---------------- 2 files changed, 49 insertions(+), 37 deletions(-) diff --git a/lib/hamlit/whitespace_handler.rb b/lib/hamlit/whitespace_handler.rb index db0e25d0..1564c4aa 100644 --- a/lib/hamlit/whitespace_handler.rb +++ b/lib/hamlit/whitespace_handler.rb @@ -2,6 +2,9 @@ module Hamlit class WhitespaceHandler def compile_children(node, &block) temple = [:multi] + return temple if node.children.empty? + + temple << [:static, "\n"] if prepend_whitespace?(node) node.children.each do |n| temple << yield(n) temple << [:static, "\n"] if insert_whitespace?(n) @@ -11,6 +14,15 @@ module Hamlit private + def prepend_whitespace?(node) + case node.type + when :tag + true + else + false + end + end + def insert_whitespace?(node) case node.type when :doctype diff --git a/test/haml-spec/tests.yml b/test/haml-spec/tests.yml index dbe8fee4..fada4879 100644 --- a/test/haml-spec/tests.yml +++ b/test/haml-spec/tests.yml @@ -173,37 +173,37 @@ tags with unusual CSS identifiers: #

text

# # -# tags with nested content: -# Nested content simple tag: -# haml: |- -# %p -# hello -# html: |- -#

-# hello -#

-# Nested content tag with CSS: -# haml: |- -# %p.class1 -# hello -# html: |- -#

-# hello -#

-# Nested content multiple simple tags: -# haml: |- -# %div -# %div -# %p -# text -# html: |- -#
-#
-#

-# text -#

-#
-#
+tags with nested content: + Nested content simple tag: + haml: |- + %p + hello + html: |- +

+ hello +

+ Nested content tag with CSS: + haml: |- + %p.class1 + hello + html: |- +

+ hello +

+ Nested content multiple simple tags: + haml: |- + %div + %div + %p + text + html: |- +
+
+

+ text +

+
+
tags with HTML-style attributes: HTML-style one attribute: haml: "%p(a='b')" @@ -477,17 +477,17 @@ Ruby-style interpolation: # html: <"&> # config: # escape_html: 'true' -# boolean attributes: +boolean attributes: # boolean attribute with XHTML: # haml: "%input(checked=true)" # html: "" # config: # format: xhtml -# boolean attribute with HTML: -# haml: "%input(checked=true)" -# html: "" -# config: -# format: html5 + boolean attribute with HTML: + haml: "%input(checked=true)" + html: "" + config: + format: html5 # whitespace preservation: # following the '~' operator: # haml: ~ "Foo\n
Bar\nBaz
"