diff --git a/lib/hamlit/whitespace_handler.rb b/lib/hamlit/whitespace_handler.rb index 1564c4aa..20278aa9 100644 --- a/lib/hamlit/whitespace_handler.rb +++ b/lib/hamlit/whitespace_handler.rb @@ -4,26 +4,53 @@ module Hamlit temple = [:multi] return temple if node.children.empty? - temple << [:static, "\n"] if prepend_whitespace?(node) + temple << :whitespace if prepend_whitespace?(node) node.children.each do |n| + rstrip_whitespace!(temple) if nuke_outer_whitespace?(n) temple << yield(n) - temple << [:static, "\n"] if insert_whitespace?(n) + temple << :whitespace if insert_whitespace?(n) end - temple + rstrip_whitespace!(temple) if nuke_inner_whitespace?(node) + confirm_whitespace(temple) end private + def confirm_whitespace(temple) + temple.map do |exp| + case exp + when :whitespace + [:static, "\n"] + else + exp + end + end + end + def prepend_whitespace?(node) - case node.type - when :tag - true - else - false + return false if node.type != :tag + !nuke_inner_whitespace?(node) + end + + def nuke_inner_whitespace?(node) + return false if node.type != :tag + node.value[:nuke_inner_whitespace] + end + + def nuke_outer_whitespace?(node) + return false if node.type != :tag + node.value[:nuke_outer_whitespace] + end + + def rstrip_whitespace!(temple) + if temple[-1] == :whitespace + temple.delete_at(-1) end end def insert_whitespace?(node) + return false if nuke_outer_whitespace?(node) + case node.type when :doctype node.value[:type] != 'xml' diff --git a/test/haml-spec/tests.yml b/test/haml-spec/tests.yml index 9886c247..a1c3192a 100644 --- a/test/haml-spec/tests.yml +++ b/test/haml-spec/tests.yml @@ -488,51 +488,51 @@ boolean attributes: html: "" config: format: html5 -# whitespace preservation: +whitespace preservation: # following the '~' operator: # haml: ~ "Foo\n
Bar\nBaz
" # html: |- # Foo #
Bar
Baz
# optional: true -# inside a textarea tag: -# haml: |- -# %textarea -# hello -# hello -# html: |- -# -# inside a pre tag: -# haml: |- -# %pre -# hello -# hello -# html: |- -#
hello
-#       hello
-# whitespace removal: -# a tag with '>' appended and inline content: -# haml: |- -# %li hello -# %li> world -# %li again -# html: "
  • hello
  • world
  • again
  • " -# a tag with '>' appended and nested content: -# haml: |- -# %li hello -# %li> -# world -# %li again -# html: |- -#
  • hello
  • -# world -#
  • again
  • -# a tag with '<' appended: -# haml: |- -# %p< -# hello -# world -# html: |- -#

    hello -# world

    + inside a textarea tag: + haml: |- + %textarea + hello + hello + html: |- + + inside a pre tag: + haml: |- + %pre + hello + hello + html: |- +
    hello
    +      hello
    +whitespace removal: + a tag with '>' appended and inline content: + haml: |- + %li hello + %li> world + %li again + html: "
  • hello
  • world
  • again
  • " + a tag with '>' appended and nested content: + haml: |- + %li hello + %li> + world + %li again + html: |- +
  • hello
  • + world +
  • again
  • + a tag with '<' appended: + haml: |- + %p< + hello + world + html: |- +

    hello + world