HAML - a very weird indentation difference - bug?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 13:27:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!