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:
parent
7858ed0c1e
commit
a65d2520a2
6 changed files with 83 additions and 49 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
nil
|
||||
break
|
||||
end
|
||||
remove_last_outer_space!(exps) if deleted
|
||||
end
|
||||
|
||||
def reset_outer_removal
|
||||
|
|
|
@ -186,6 +186,7 @@ describe Hamlit::Engine do
|
|||
HTML
|
||||
end
|
||||
|
||||
describe 'whitespace removal' do
|
||||
it 'removes outer whitespace by >' do
|
||||
assert_render(<<-HAML, <<-HTML)
|
||||
%span> a
|
||||
|
@ -223,5 +224,17 @@ describe Hamlit::Engine do
|
|||
</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
|
||||
|
|
4
spec/rails/app/views/users/whitespace.html.haml
Normal file
4
spec/rails/app/views/users/whitespace.html.haml
Normal file
|
@ -0,0 +1,4 @@
|
|||
%span<
|
||||
= link_to '#' do
|
||||
= 'foo'
|
||||
%span> bar
|
|
@ -9,6 +9,7 @@ Rails.application.routes.draw do
|
|||
get :helpers
|
||||
get :safe_buffer
|
||||
get :old_attributes
|
||||
get :whitespace
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue