1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Remove outer whitespace in the block

This commit is contained in:
Takashi Kokubun 2015-06-05 08:08:21 +09:00
parent 7858ed0c1e
commit a65d2520a2
6 changed files with 83 additions and 49 deletions

View file

@ -1,3 +1,5 @@
# This module is created to compile [:haml, :strip],
# which is sexp for whitespace inner removal.module Hamlit
module Hamlit
module Compilers
module Strip
@ -10,7 +12,6 @@ module Hamlit
def strip_newline(content)
indexes = newline_indexes(content)
return content if indexes.length < 2
content = content.dup
content.delete_at(indexes.last)

View file

@ -16,26 +16,36 @@ module Hamlit
inner_removal
end
def remove_last_outer_space!(ast)
index = find_last_newline_index(ast)
return unless index
ast.delete_at(index)
def remove_last_outer_space!(exps)
exps.reverse!
remove_first_outer_space!(exps)
ensure
exps.reverse!
end
private
# Find [:static, "\n"]'s position ignoring [:code].
# If it is not found, return nil.
def find_last_newline_index(ast)
ast.reverse_each.with_index do |node, index|
sexp, *args = node
next if sexp == :code
return if node != [:static, "\n"]
def remove_first_outer_space!(exps)
deleted = false
exps.delete_if do |exp|
break if deleted
return ast.length - index - 1
name, *args = exp
case name
when :static
break if args != ["\n"]
deleted = true
next true
when :code
next false
when :newline
next false
when :haml
remove_last_outer_space!(exp) if args.first == :script
end
break
end
nil
remove_last_outer_space!(exps) if deleted
end
def reset_outer_removal

View file

@ -186,42 +186,55 @@ describe Hamlit::Engine do
HTML
end
it 'removes outer whitespace by >' do
assert_render(<<-HAML, <<-HTML)
%span> a
%span b
%span c
%span>
d
%span
e
%span f
HAML
<span>a</span><span>b</span>
<span>c</span><span>
d
</span><span>
e
</span>
<span>f</span>
HTML
end
it 'removes outer whitespace by > from inside of block' do
assert_render(<<-HAML, <<-HTML)
%span a
- if true
describe 'whitespace removal' do
it 'removes outer whitespace by >' do
assert_render(<<-HAML, <<-HTML)
%span> a
%span b
%span c
%span>
b
%span
d
%span
e
%span f
HAML
<span>a</span><span>b</span>
<span>c</span><span>
d
</span><span>
e
</span>
<span>f</span>
HTML
end
it 'removes outer whitespace by > from inside of block' do
assert_render(<<-HAML, <<-HTML)
%span a
- if true
%span>
b
%span
c
HAML
<span>a</span><span>
b
</span><span>
c
HAML
<span>a</span><span>
b
</span><span>
c
</span>
HTML
</span>
HTML
end
it 'removes whitespaces inside block script' do
assert_render(<<-HAML, <<-HTML)
%span<
= 2.times do
= 'foo'
%span> bar
HAML
<span>foofoo2<span>bar</span></span>
HTML
end
end
end
end

View file

@ -0,0 +1,4 @@
%span<
= link_to '#' do
= 'foo'
%span> bar

View file

@ -9,6 +9,7 @@ Rails.application.routes.draw do
get :helpers
get :safe_buffer
get :old_attributes
get :whitespace
end
end
end

View file

@ -72,6 +72,11 @@ describe 'Hamlit rails integration', type: :request do
get form_users_path
expect(response.body).to include('row')
end
it 'renders whitespace removal inside #capture' do
get whitespace_users_path
expect(response.body).to include('<a href="#">foo</a>')
end
end
describe 'haml helpers' do