问题
This HAML
%script{:type => "text/javascript"}
:plain
$(document).ready(function() {
bar();
var foo = foo_func("#{}");
});
as expected gives this:
<script type='text/javascript'>
$(document).ready(function() {
bar();
var foo = foo_func("");
});
</script>
But this ALMOST IDENTICAL HAML (changed only bar()
to prep()
):
%script{:type => "text/javascript"}
:plain
$(document).ready(function() {
prep();
var foo = foo_func("#{}");
});
gives this:
<script type='text/javascript'>
$(document).ready(function() {
prep();
var foo = foo_func("");
});
</script>
NOTE THE MESSED UP INDENTATION in the second case.
Why would changing bar()
to prep()
cause this weird difference?
回答1:
This is being caused by the characters pre
in prep()
matching a regex that Haml is using to deal with whitespace.
In Haml you use whitespace to specify the contents of elements, and normally this is okay since when viewing HTML whitespace is “squashed” so that it appears as a single character. However, whitespace is important in some HTML elements (pre
, code
and textarea
), and Haml tries to detect and deal with these elements. In this case the regex is matched and the block after the first line isn’t indented.
This code has been changed in the latest version (currently 4.0.1.rc.1) and this doesn’t happen in that version. I’ve also created a pull request that fixes the regex in the 3-1 branch.
来源:https://stackoverflow.com/questions/15312365/haml-a-very-weird-indentation-difference-bug