ruby-haml-js icon indicating copy to clipboard operation
ruby-haml-js copied to clipboard

Fix running JavaScript (-) to specification.

Open nickl- opened this issue 11 years ago • 1 comments

I saw the undocumented custom filter hacks referred to by #9 but those :if and :loop constructs are one of the gripes I have with jade, these are not required in a templating engine, IMO. We have sufficient ability to handle conditions and loops in the underlying language so it is unnecessary for the template engine to duplicate the effort.

Whether to keep the out of spec features or not is not what this patch is aiming for, instead it is an attempt to make the JavaScript lines run as - working as is expected.

What has been added:

  • running JavaScript blocks are completely processed to allow for other type lines to be included in blocks including nesting of JavaScript blocks
  • as per the haml specification to use indentation for blocks you are not required to end
    • if statements
    • for loops
    • while loops
    • async function blocks
  • includes several specs to test the commonly used combinations including
    • if statements with and without begin {
    • for loops with or without begin {
    • for in loop
    • while loop
    • variable assignment
    • async function blocks

See spec for example.

Although this is already very useful and serves the majority of use cases there are some things that still need attention. We currently only consider the immediate block of the parent but in certain conditions we will have to take the first outdent into consideration. This means that the following will not work yet.

  • else and else if statements will not work as we prepare the _$output variable for subsequent lines after the if statement was already closed.
  • It is not possible to implicitly close a block (this is not to spec but may be required for async function blocks)
  • only async functions as the last parameter of a method is currently supported due to the limitation mentioned previously.

Because we are not yet considering the first outdented line something like setTimeout is not possible yet. ex.

- setTimeout(function () {
 %p This html supplied asynchronously
- }, 1000);

But even with these remaining limitations I consider this patch a move in the right direction as the rawjs implementation is not of any use currently.

nickl- avatar Sep 22 '13 23:09 nickl-

Thanks for the PR. Looks good.

But unfortunately I will not accept any changes to the original HAML JS implementation (except the regular updates). You'll be better off sending this PR to https://github.com/creationix/haml-js. Then if it will be accepted, we will update it here.

dnagir avatar Sep 26 '13 01:09 dnagir