Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Unecessary Whitespace #336

Merged
merged 13 commits into from

10 participants

@jayfunk

I have come across an issue with the each block helper. It appears that it is creating unecessary whitespace. I copied the each block helper example (http://handlebarsjs.com/#iteration) exactly and my results were not the same. My results ended up like the following:

<ul>

  <li>ele1</li>

  <li>ele2</li>

  <li>ele3</li>

</ul>

instead of the expected:

<ul>
  <li>ele1</li>
  <li>ele2</li>
  <li>ele3</li>
</ul>

This might seem like a non issue but say we have a template like the following:

<script id="onlyEachTemplate" type="text/x-handlebars-template">
{{#each collection}}
<div>{{this}}</div>
{{/each}}
</script>

My expected output would be:

<div>ele1</div>
<div>ele2</div>
<div>ele</div>

but I instead get:

<div>ele1</div>

<div>ele2</div>

<div>ele3</div>

In an AJAX application where regular polling for data and rendering that data with a template like the previous can result in a large amount of unecessary dom growth. We actually encountered such an issue. All of the extra whitespace elements would spike the CPU usage under FF15 100% after 12 hours.
Here is a link to a set of tests I wrote to demonstrate the issue https://github.com/jayfunk/Handlebars-Whitespace-Test

@kpdecker
Collaborator

This is the same number of whitespace nodes in both the examples, one just has additional newline characters.

If the whitespace is causing significant overhead or layout issues you would currently need to do something like this:

{{#each collection}}<div>{{this}}</div>{{/each}}

To get the rendering that you expected above you would need something like this:

{{#each collection}}<div>{{this}}</div>
{{/each}}

I could see some use for trimming whitespace around block elements but I'd also be concerned about this breaking existing code that is in the wild.

@jayfunk

I did discover both of those solutions when I was writing the tests. Both work fine. However, I was hoping to find a solution at the source and one that side steps:
{{#each collection}}<div>{{this}}</div>{{/each}}

which would be some kind of template cleanup during packaging or:

{{#each collection}}<div>{{this}}</div>
{{/each}}

Which could make more complicated templates difficult to read.

@kpdecker
Collaborator

#479 proposes a very interesting fix to this issue. Introducing a token on the mustache delimiter that specifies if whitespace should be trimmed in a particular block's output.

http://jinja.pocoo.org/docs/templates/#whitespace-control

@mherodev

Please add support for some sort of white-space control. My templates are looking a bit rusty because of this. #479 sounds reasonable to me. Some sort of white-space trimming flag for compilation might be easier and more intuitive, too.

@johtso

This functionality would be fantastic! The fact that making your templates readable causes whitespace to be added can be a real pain. :+1:

@johtso johtso referenced this pull request in SlexAxton/require-handlebars-plugin
Closed

Optionally remove whitespace and HTML comments when compiled #49

@quangv
a

{{-#if true -}}
   b
{{-/if-}}

c

hbs-whitespace-control

I believe that's how the Jinja2 whitespace control works... 4 possible whitespace area that can be removed... any thoughts on syntax? {{- -}} ?

@jayfunk

My initial complaint was more that the mark up for handlebars will create unexpected spaces. The default in my mind would match the formatting as if the handlebars did not exist.
So:

<ul>
{{#if test}}
 <li></li>
{{/if test}}
</ul>
equals:
<ul>
 <li></li>
</ul>

or if we used the suggestions of all whitespace being removed:

<ul> <li></li></ul>
@quangv

hey jayfunk, can you use a different example... all i see is bullets...

edited: thanks

@Arkkimaagi Arkkimaagi referenced this pull request from a commit in Arkkimaagi/assemble
@Arkkimaagi Arkkimaagi Added an option to remove extra whitespace around hbs tags
It annoys me and apparently many others that handlebars does not strip extra whitespace around its elements when compiling. 

wycats/handlebars.js#336

This is a quick hack to make it strip that extra whitespace as if the rows with only a single hbs tag were not separate rows. This pull possibly has some bugs in its regexp, but it's a start.

I thought that maybe this should be toggleable by the user, so only by adding an option: removeHbsWhitespace:true to grunt config of assemble will enable this feature. (without it, no ill effects should happen)

With removeHbsWhitespace enabled code like this:
```
{{#each collection}}
<div>{{this}}</div>
{{/each}}
```
normally resulting this:
```
<div>ele1</div>

<div>ele2</div>

<div>ele3</div>
```

would be preprocessed to be like this:
´´´
{{#each collection}}<div>{{this}}</div>{{/each}}
´´´
resulting this:
```
<div>ele1</div>
<div>ele2</div>
<div>ele3</div>
```

This feels more natural and helps creating cleaner and more readable layouts, partials and page code.
6e9b039
@chovy

so how do we handle this? I find css rules that use tbody:empty don't apply because of the whitespace issue.

@kpdecker
Collaborator

I've attached the candidate pull request to this issue.

All of the possible permutations of this are outlined in the test file but at a high level this looks something like:

 {{~#if foo~}} bar {{~/if~}} 

My goal is to get this into the impending 1.1. release.

@jonschlinkert

awesome! @kpdecker is the syntax locked, or is there time to consider alternatives?

@kpdecker
Collaborator
@jonschlinkert

Thanks for the reply! Yeah I agree with the consensus on that. A couple of questions:

  • do the characters need to be present on both open/close tags? or can we consider options with the special syntax applied only to the opening tag?
  • can we consider options where an additional character is only required on one side of the expression?
  • are you sure you want to do this? Lol, I ask because, IMHO, you might end up getting more issues from this than it resolves. Even dedicated HTML formatting libs can't get this right. Just something to consider.
@kpdecker
Collaborator

The characters are all independent. They can be placed on one side or the other, both, or omitted entirely. This applies to both the beginning and ending tags for block elements.

Can you provide some examples WRT the getting it right comment?

@jonschlinkert

Here is one example: https://github.com/assemble/assemble-docs/blob/gh-pages/index.html#L22-L25

However:
1. the example uses this helper: https://github.com/helpers/handlebars-helper-prettify, which depends on js-beautify,
2. Althougth js-beautify is pretty great, I had to do add some janky fixes (https://github.com/helpers/handlebars-helper-prettify/blob/master/prettify.js#L14-L24) to get the formatting to output the way I wanted it (which is part of the challenges with formatting... pleasing everyone)
3. I have learned to doing things in my HTML that don't format well, so you're seeing a really sanitized example.

Code comments in particular are the biggest PITA to get right, because they can be written in so many different ways. For example, Bootstrap does something like this:

      <!-- Component
      ================================ -->

Which never turns our right when it's "beautified". It usually ends up like this:

      <!-- Component================================ -->

or like this:

      <!-- Component
================================ -->
@kpdecker
Collaborator

I think for that particular example you would not want to use whitespace control or at least not use it completely. It's all or nothing and doesn't have any knowledge of composed language semantics, which is by design.

<!-- Component {{foo ~}}

============== -->

Would serve those purposes but this would be left to the author to implement per their needs.

@stefanpenner stefanpenner referenced this pull request in emberjs/ember.js
Closed

IE8 'movesWhitespace' issue #3650

@kpdecker kpdecker merged commit 320c0a6 into master
@kpdecker kpdecker deleted the whitespace-control branch
@oliverzy oliverzy referenced this pull request in daaain/Handlebars
Closed

support whitespace control #26

@kpdecker
Collaborator

Released in 1.1.0

@mcw0933

Question - this behavior can only be specified in the template, and can't be invoked programmatically from a helper?

For example, if I have a helper that should conditionally collapse the whitespace in front of itself... I can't do that? I have to call it with {{~helper}} (to always collapse). And then instead conditionally add back any desired whitespace to the result?

@kpdecker
Collaborator

@mcw0933 Within a helper I would just do a trim or regex replace to remove the whitespace in question.

@mcw0933

Right - that I get. I'm thinking of cases where the helper could have preceding whitespace in the template. (Whitespace that isn't strictly under the control of the helper).

There's no way for the helper to force that whitespace to collapse? Right? The template implementer has to remember to either leave no whitespace, or use the {{~helper~}} syntax.

@kpdecker
Collaborator

Gotcha. Correct it's up to the template and I don't think we want to implement something like that for two reasons.

  1. The whitespace control behavior is done at compile time and adding runtime support would complicate things quite a bit.
  2. Allowing a helper control anything other than it's own content feels very weird and it could lead to suprises for the users of the help who are wondering why their template isn't rendering things that to them appear to be there.
@mcw0933

Sure - makes sense. Thanks!

@donaldpipowitch

Hi,

this problem doesn't seem to be resolved. To come back to the original example: I want this template:

<ul>
  {{#each collection}}
  <li>test</li>
  {{/each}}
</ul>

to compile to this:

<ul>
  <li>test</li>
  <li>test</li>
  <li>test</li>
</ul>

But I get this:

<ul>

  <li>test</li>

  <li>test</li>

  <li>test</li>

</ul>

With {{~#each collection~}}{{~/each~}} I get this:

<ul>
  <li>test</li><li>test</li><li>test</li>
</ul>
@scurker

@donaldpipowitch The whitespace control determine which side the whitespace is trimmed from.

{{~#each collection}} - trims whitespace before

{{#each collection~}} - trims whitespace after

{{~#each collection~}} - trims whitespace before and after

Depending on how your template is written, you need to determine where to place the white space controls in order to get your template looking how you want.

@kkirsche kkirsche referenced this pull request from a commit in kkirsche/rubyloco.com
@kkirsche kkirsche Update handlebars from 1.0.0-rc.4 to 3.0.0
Update handlebars from 1.0.0-rc.4 to 3.0.0

## v3.0.0 - February 10th, 2015
- [#941](wycats/handlebars.js#941) - Add support for dynamic partial names ([@kpdecker](https://api.github.com/users/kpdecker))
- [#940](wycats/handlebars.js#940) - Add missing reserved words so compiler knows to use array syntax: ([@mattflaschen](https://api.github.com/users/mattflaschen))
- [#938](wycats/handlebars.js#938) - Fix example using #with helper ([@diwo](https://api.github.com/users/diwo))
- [#930](wycats/handlebars.js#930) - Add parent tracking and mutation to AST visitors ([@kpdecker](https://api.github.com/users/kpdecker))
- [#926](wycats/handlebars.js#926) - Depthed lookups fail when program duplicator runs ([@kpdecker](https://api.github.com/users/kpdecker))
- [#918](wycats/handlebars.js#918) - Add instructions for 'spec/mustache' to CONTRIBUTING.md, fix a few typos ([@oneeman](https://api.github.com/users/oneeman))
- [#915](wycats/handlebars.js#915) - Ast update ([@kpdecker](https://api.github.com/users/kpdecker))
- [#910](wycats/handlebars.js#910) - Different behavior of {{@last}} when {{#each}} in {{#each}} ([@zordius](https://api.github.com/users/zordius))
- [#907](wycats/handlebars.js#907) - Implement named helper variable references ([@kpdecker](https://api.github.com/users/kpdecker))
- [#906](wycats/handlebars.js#906) - Add parser support for block params ([@mmun](https://api.github.com/users/mmun))
- [#903](wycats/handlebars.js#903) - Only provide aliases for multiple use calls ([@kpdecker](https://api.github.com/users/kpdecker))
- [#902](wycats/handlebars.js#902) - Generate Source Maps ([@kpdecker](https://api.github.com/users/kpdecker))
- [#901](wycats/handlebars.js#901) - Still escapes with noEscape enabled on isolated Handlebars environment ([@zedknight](https://api.github.com/users/zedknight))
- [#896](wycats/handlebars.js#896) - Simplify BlockNode by removing intermediate MustacheNode ([@mmun](https://api.github.com/users/mmun))
- [#892](wycats/handlebars.js#892) - Implement parser for else chaining of helpers ([@kpdecker](https://api.github.com/users/kpdecker))
- [#889](wycats/handlebars.js#889) - Consider extensible parser API ([@kpdecker](https://api.github.com/users/kpdecker))
- [#887](wycats/handlebars.js#887) - Handlebars.noConflict() option? ([@bradvogel](https://api.github.com/users/bradvogel))
- [#886](wycats/handlebars.js#886) - Add SafeString to context (or use duck-typing) ([@dominicbarnes](https://api.github.com/users/dominicbarnes))
- [#870](wycats/handlebars.js#870) - Registering undefined partial throws exception. ([@max-b](https://api.github.com/users/max-b))
- [#866](wycats/handlebars.js#866) - comments don't respect whitespace control ([@75lb](https://api.github.com/users/75lb))
- [#863](wycats/handlebars.js#863) - + jsDelivr CDN info ([@tomByrer](https://api.github.com/users/tomByrer))
- [#858](wycats/handlebars.js#858) - Disable new default auto-indent at included partials ([@majodev](https://api.github.com/users/majodev))
- [#856](wycats/handlebars.js#856) - jspm compatibility ([@MajorBreakfast](https://api.github.com/users/MajorBreakfast))
- [#805](wycats/handlebars.js#805) - Request: "strict" lookups ([@nzakas](https://api.github.com/users/nzakas))

- Export the default object for handlebars/runtime - 5594416
- Lookup partials when undefined - 617dd57

Compatibility notes:
- Runtime breaking changes. Must match 3.x runtime and precompiler.
- The AST has been upgraded to a public API.
  - There are a number of changes to this, but the format is now documented in docs/compiler-api.md
  - The Visitor API has been expanded to support mutation and provide a base implementation
- The `JavaScriptCompiler` APIs have been formalized and documented. As part of the sourcemap handling these should be updated to return arrays for concatenation.
- `JavaScriptCompiler.namespace` has been removed as it was unused.
- `SafeString` is now duck typed on `toHTML`

New Features:
- noConflict
- Source Maps
- Block Params
- Strict Mode
- @last and other each changes
- Chained else blocks
- @data methods can now have helper parameters passed to them
- Dynamic partials

[Commits](wycats/handlebars.js@v2.0.0...v3.0.0)

## v2.0.0 - September 1st, 2014
- Update jsfiddle to 2.0.0-beta.1 - 0670f65
- Add contrib note regarding handlebarsjs.com docs - 4d17e3c
- Play nice with gemspec version numbers - 64d5481

[Commits](wycats/handlebars.js@v2.0.0-beta.1...v2.0.0)

## v2.0.0-beta.1 - August 26th, 2014
- [#787](wycats/handlebars.js#787) - Remove whitespace surrounding standalone statements ([@kpdecker](https://api.github.com/users/kpdecker))
- [#827](wycats/handlebars.js#827) - Render false literal as “false” ([@scoot557](https://api.github.com/users/scoot557))
- [#767](wycats/handlebars.js#767) - Subexpressions bug with hash and context ([@evensoul](https://api.github.com/users/evensoul))
- Changes to 0/undefined handling
  - [#731](wycats/handlebars.js#731) - Strange behavior for {{#foo}} {{bar}} {{/foo}} when foo is 0 ([@kpdecker](https://api.github.com/users/kpdecker))
  - [#820](wycats/handlebars.js#820) - strange behavior for {{foo.bar}} when foo is 0 or null or false ([@zordius](https://api.github.com/users/zordius))
  - [#837](wycats/handlebars.js#837) - Strange input for custom helper ( foo.bar == false when foo is undefined ) ([@zordius](https://api.github.com/users/zordius))
- [#819](wycats/handlebars.js#819) - Implement recursive field lookup ([@kpdecker](https://api.github.com/users/kpdecker))
- [#764](wycats/handlebars.js#764) - This reference not working for helpers ([@kpdecker](https://api.github.com/users/kpdecker))
- [#773](wycats/handlebars.js#773) - Implicit parameters in {{#each}} introduces a peculiarity in helpers calling convention  ([@Bertrand](https://api.github.com/users/Bertrand))
- [#783](wycats/handlebars.js#783) - helperMissing and consistency for different expression types ([@ErisDS](https://api.github.com/users/ErisDS))
- [#795](wycats/handlebars.js#795) - Turn the precompile script into a wrapper around a module. ([@jwietelmann](https://api.github.com/users/jwietelmann))
- [#823](wycats/handlebars.js#823) - Support inverse sections on the with helper ([@dan-manges](https://api.github.com/users/dan-manges))
- [#834](wycats/handlebars.js#834) - Refactor blocks, programs and inverses ([@mmun](https://api.github.com/users/mmun))
- [#852](wycats/handlebars.js#852) - {{foo~}} space control behavior is different from older version ([@zordius](https://api.github.com/users/zordius))
- [#835](wycats/handlebars.js#835) - Templates overwritten if file is loaded twice

- Expose escapeExpression on the root object - 980c38c
- Remove nested function eval in blockHelperMissing - 6f22ec1
- Fix compiler program de-duping - 9e3f824

Compatibility notes:
- The default build now outputs a generic UMD wrapper. This should be transparent change but may cause issues in some environments.
- Runtime compatibility breaks in both directions. Ensure that both compiler and client are upgraded to 2.0.0-beta.1 or higher at the same time.
  - `programWithDepth` has been removed an instead an array of context values is passed to fields needing depth lookups.
- `false` values are now printed to output rather than silently dropped
- Lines containing only block statements and whitespace are now removed. This matches the Mustache spec but may cause issues with code that expects whitespace to exist but would not otherwise.
- Partials that are standalone will now indent their rendered content
- `AST.ProgramNode`'s signature has changed.
- Numerious methods/features removed from psuedo-API classes
  - `JavaScriptCompiler.register`
  - `JavaScriptCompiler.replaceStack` no longer supports non-inline replace
  - `Compiler.disassemble`
  - `DECLARE` opcode
  - `strip` opcode
  - `lookup` opcode
  - Content nodes may have their `string` values mutated over time. `original` field provides the unmodified value.
- Removed unused `Handlebars.registerHelper` `inverse` parameter
- `each` helper requires iterator parameter

[Commits](wycats/handlebars.js@v2.0.0-alpha.4...v2.0.0-beta.1)

## v2.0.0-alpha.4 - May 19th, 2014
- Expose setup wrappers for compiled templates - 3638874

[Commits](wycats/handlebars.js@v2.0.0-alpha.3...v2.0.0-alpha.4)

## v2.0.0-alpha.3 - May 19th, 2014
- [#797](wycats/handlebars.js#797) - Pass full helper ID to helperMissing when options are provided ([@tomdale](https://api.github.com/users/tomdale))
- [#793](wycats/handlebars.js#793) - Ensure isHelper is coerced to a boolean ([@mmun](https://api.github.com/users/mmun))
- Refactor template init logic - 085e5e1

[Commits](wycats/handlebars.js@v2.0.0-alpha.2...v2.0.0-alpha.3)

## v2.0.0-alpha.2 - March 6th, 2014
- [#756](wycats/handlebars.js#756) - fix bug in IE<=8 (no Array::map), closes #751 ([@jenseng](https://api.github.com/users/jenseng))
- [#749](wycats/handlebars.js#749) - properly handle multiple subexpressions in the same hash, fixes #748 ([@jenseng](https://api.github.com/users/jenseng))
- [#743](wycats/handlebars.js#743) - subexpression confusion/problem? ([@waynedpj](https://api.github.com/users/waynedpj))
- [#746](wycats/handlebars.js#746) - [CLI] support `handlebars --version` ([@apfelbox](https://api.github.com/users/apfelbox))
- [#747](wycats/handlebars.js#747) - updated grunt-saucelabs, failing tests revealed ([@Jonahss](https://api.github.com/users/Jonahss))
- Make JSON a requirement for the compiler. - 058c0fb
- Temporarily kill the AWS publish CI step - 8347ee2

Compatibility notes:
- A JSON polyfill is required to run the compiler under IE8 and below. It's recommended that the precompiler be used in lieu of running the compiler on these legacy environments.

[Commits](wycats/handlebars.js@v2.0.0-alpha.1...v2.0.0-alpha.2)

## v2.0.0-alpha.1 - February 10th, 2014
- [#182](wycats/handlebars.js#182) - Allow passing hash parameters to partials ([@kpdecker](https://api.github.com/users/kpdecker))
- [#392](wycats/handlebars.js#392) - Access to root context in partials and helpers ([@kpdecker](https://api.github.com/users/kpdecker))
- [#472](wycats/handlebars.js#472) - Helpers cannot have decimal parameters ([@kayleg](https://api.github.com/users/kayleg))
- [#569](wycats/handlebars.js#569) - Unable to lookup array values using @index ([@kpdecker](https://api.github.com/users/kpdecker))
- [#491](wycats/handlebars.js#491) - For nested helpers: get the @ variables of the outer helper from the inner one ([@kpdecker](https://api.github.com/users/kpdecker))
- [#669](wycats/handlebars.js#669) - Ability to unregister a helper ([@dbachrach](https://api.github.com/users/dbachrach))
- [#730](wycats/handlebars.js#730) - Raw block helpers ([@kpdecker](https://api.github.com/users/kpdecker))
- [#634](wycats/handlebars.js#634) - It would be great to have the helper name passed to `blockHelperMissing` ([@kpdecker](https://api.github.com/users/kpdecker))
- [#729](wycats/handlebars.js#729) - Convert template spec to object literal ([@kpdecker](https://api.github.com/users/kpdecker))

- [#658](wycats/handlebars.js#658) - Depthed helpers do not work after an upgrade from 1.0.0 ([@xibxor](https://api.github.com/users/xibxor))
- [#671](wycats/handlebars.js#671) - Crashes on no-parameter {{#each}} ([@stepancheg](https://api.github.com/users/stepancheg))
- [#689](wycats/handlebars.js#689) - broken template precompilation ([@AAS](https://api.github.com/users/AAS))
- [#698](wycats/handlebars.js#698) - Fix parser generation under windows ([@osiris43](https://api.github.com/users/osiris43))
- [#699](wycats/handlebars.js#699) - @DATA not compiles to invalid JS in stringParams mode ([@kpdecker](https://api.github.com/users/kpdecker))
- [#705](wycats/handlebars.js#705) - 1.3.0 can not be wrapped in an IIFE ([@craigteegarden](https://api.github.com/users/craigteegarden))
- [#706](wycats/handlebars.js#706) - README: Use with helper instead of relying on blockHelperMissing ([@scottgonzalez](https://api.github.com/users/scottgonzalez))

- [#700](wycats/handlebars.js#700) - Remove redundant conditions ([@blakeembrey](https://api.github.com/users/blakeembrey))
- [#704](wycats/handlebars.js#704) - JavaScript Compiler Cleanup ([@blakeembrey](https://api.github.com/users/blakeembrey))

Compatibility notes:
- `helperMissing` helper no longer has the indexed name argument. Helper name is now available via `options.name`.
- Precompiler output has changed, which breaks compatibility with prior versions of the runtime and precompiled output.
- `JavaScriptCompiler.compilerInfo` now returns generic objects rather than javascript source.
- AST changes
  - INTEGER -> NUMBER
  - Additional PartialNode hash parameter
  - New RawBlockNode type
- Data frames now have a `_parent` field. This is internal but is enumerable for performance/compatability reasons.

[Commits](wycats/handlebars.js@v1.3.0...v2.0.0-alpha.1)

## v1.3.0 - January 1st, 2014
- [#690](wycats/handlebars.js#690) - Added support for subexpressions ([@machty](https://api.github.com/users/machty))
- [#696](wycats/handlebars.js#696) - Fix for reserved keyword "default" ([@nateirwin](https://api.github.com/users/nateirwin))
- [#692](wycats/handlebars.js#692) - add line numbers to nodes when parsing ([@fivetanley](https://api.github.com/users/fivetanley))
- [#695](wycats/handlebars.js#695) - Pull options out from param setup to allow easier extension ([@blakeembrey](https://api.github.com/users/blakeembrey))
- [#694](wycats/handlebars.js#694) - Make the environment reusable ([@blakeembrey](https://api.github.com/users/blakeembrey))
- [#636](wycats/handlebars.js#636) - Print line and column of errors ([@sgronblo](https://api.github.com/users/sgronblo))
- Use literal for data lookup - c1a93d3
- Add stack handling sanity checks - cd885bf
- Fix stack id "leak" on replaceStack - ddfe457
- Fix incorrect stack pop when replacing literals - f4d337d

[Commits](wycats/handlebars.js@v1.2.1...v1.3.0)

## v1.2.1 - December 26th, 2013
- [#684](wycats/handlebars.js#684) - Allow any number of trailing characters for valid JavaScript variable ([@blakeembrey](https://api.github.com/users/blakeembrey))
- [#686](wycats/handlebars.js#686) - Falsy AMD module names in version 1.2.0 ([@kpdecker](https://api.github.com/users/kpdecker))

[Commits](wycats/handlebars.js@v1.2.0...v1.2.1)

## v1.2.0 - December 23rd, 2013
- [#675](wycats/handlebars.js#675) - Cannot compile empty template for partial ([@erwinw](https://api.github.com/users/erwinw))
- [#677](wycats/handlebars.js#677) - Triple brace statements fail under IE ([@hamzaCM](https://api.github.com/users/hamzaCM))
- [#655](wycats/handlebars.js#655) - Loading Handlebars using bower ([@niki4810](https://api.github.com/users/niki4810))
- [#657](wycats/handlebars.js#657) - Fixes issue where cli compiles non handlebars templates ([@chrishoage](https://api.github.com/users/chrishoage))
- [#681](wycats/handlebars.js#681) - Adds in-browser testing and Saucelabs CI ([@kpdecker](https://api.github.com/users/kpdecker))
- [#661](wycats/handlebars.js#661) - Add @first and @index to #each object iteration ([@cgp](https://api.github.com/users/cgp))
- [#650](wycats/handlebars.js#650) - Handlebars is MIT-licensed ([@thomasboyt](https://api.github.com/users/thomasboyt))
- [#641](wycats/handlebars.js#641) - Document ember testing process ([@kpdecker](https://api.github.com/users/kpdecker))
- [#662](wycats/handlebars.js#662) - handlebars-source 1.1.2 is missing from RubyGems.
- [#656](wycats/handlebars.js#656) - Expose COMPILER_REVISION checks as a hook ([@machty](https://api.github.com/users/machty))
- [#668](wycats/handlebars.js#668) - Consider publishing handlebars-runtime as a separate module on npm ([@dlmanning](https://api.github.com/users/dlmanning))
- [#679](wycats/handlebars.js#679) - Unable to override invokePartial ([@mattbrailsford](https://api.github.com/users/mattbrailsford))
- [#646](wycats/handlebars.js#646) - Fix "\\{{" immediately following "\{{" ([@dmarcotte](https://api.github.com/users/dmarcotte))
- Allow extend to work with non-prototyped objects - eb53f2e
- Add JavascriptCompiler public API tests - 1a751b2
- Add AST test coverage for more complex paths - ddea5be
- Fix handling of boolean escape in MustacheNode - b4968bb

Compatibility notes:
- `@index` and `@first` are now supported for `each` iteration on objects
- `Handlebars.VM.checkRevision` and `Handlebars.JavaScriptCompiler.prototype.compilerInfo` now available to modify the version checking behavior.
- Browserify users may link to the runtime library via `require('handlebars/runtime')`

[Commits](wycats/handlebars.js@v1.1.2...v1.2.0)

## v1.1.2 - November 5th, 2013

- [#645](wycats/handlebars.js#645) - 1.1.1 fails under IE8 ([@kpdecker](https://api.github.com/users/kpdecker))
- [#644](wycats/handlebars.js#644) - Using precompiled templates (AMD mode) with handlebars.runtime 1.1.1 ([@fddima](https://api.github.com/users/fddima))

- Add simple binary utility tests - 96a45a4
- Fix empty string compilation - eea708a

[Commits](wycats/handlebars.js@v1.1.1...v1.1.2)

## v1.1.1 - November 4th, 2013

- [#642](wycats/handlebars.js#642) - handlebars 1.1.0 are broken with nodejs

- Fix release notes link - 17ba258

[Commits](wycats/handlebars.js@v1.1.0...v1.1.1)

## v1.1.0 - November 3rd, 2013

- [#628](wycats/handlebars.js#628) - Convert code to ES6 modules ([@kpdecker](https://api.github.com/users/kpdecker))
- [#336](wycats/handlebars.js#336) - Add whitespace control syntax ([@kpdecker](https://api.github.com/users/kpdecker))
- [#535](wycats/handlebars.js#535) - Fix for probable JIT error under Safari ([@sorentwo](https://api.github.com/users/sorentwo))
- [#483](wycats/handlebars.js#483) - Add first and last @ vars to each helper ([@denniskuczynski](https://api.github.com/users/denniskuczynski))
- [#557](wycats/handlebars.js#557) - `\\{{foo}}` escaping only works in some situations ([@dmarcotte](https://api.github.com/users/dmarcotte))
- [#552](wycats/handlebars.js#552) - Added BOM removal flag. ([@blessenm](https://api.github.com/users/blessenm))
- [#543](wycats/handlebars.js#543) - publish passing master builds to s3 ([@fivetanley](https://api.github.com/users/fivetanley))

- [#608](wycats/handlebars.js#608) - Add `includeZero` flag to `if` conditional
- [#498](wycats/handlebars.js#498) - `Handlebars.compile` fails on empty string although a single blank works fine
- [#599](wycats/handlebars.js#599) - lambda helpers only receive options if used with arguments
- [#592](wycats/handlebars.js#592) - Optimize array and subprogram performance
- [#571](wycats/handlebars.js#571) - uglify upgrade breaks compatibility with older versions of node
- [#587](wycats/handlebars.js#587) - Partial inside partial breaks?

Compatibility notes:
- The project now includes separate artifacts for AMD, CommonJS, and global objects.
  - AMD: Users may load the bundled `handlebars.amd.js` or `handlebars.runtime.amd.js` files or load individual modules directly. AMD users should also note that the handlebars object is exposed via the `default` field on the imported object. This [gist](https://gist.github.com/wycats/7417be0dc361a69d5916) provides some discussion of possible compatibility shims.
  - CommonJS/Node: Node loading occurs as normal via `require`
  - Globals: The `handlebars.js` and `handlebars.runtime.js` files should behave in the same manner as the v1.0.12 / 1.0.0 release.
- Build artifacts have been removed from the repository. [npm][npm], [components/handlebars.js][components], [cdnjs][cdnjs], or the [builds page][builds-page] should now be used as the source of built artifacts.
- Context-stored helpers are now always passed the `options` hash. Previously no-argument helpers did not have this argument.

[Commits](wycats/handlebars.js@v1.0.12...v1.1.0)

## v1.0.12 / 1.0.0 - May 31 2013

- [#515](wycats/handlebars.js#515) - Add node require extensions support ([@jjclark1982](https://github.com/jjclark1982))
- [#517](wycats/handlebars.js#517) - Fix amd precompiler output with directories ([@blessenm](https://github.com/blessenm))
- [#433](wycats/handlebars.js#433) - Add support for unicode ids
- [#469](wycats/handlebars.js#469) - Add support for `?` in ids
- [#534](wycats/handlebars.js#534) - Protect from object prototype modifications
- [#519](wycats/handlebars.js#519) - Fix partials with . name ([@jamesgorrie](https://github.com/jamesgorrie))
- [#519](wycats/handlebars.js#519) - Allow ID or strings in partial names
- [#437](wycats/handlebars.js#437) - Require matching brace counts in escaped expressions
- Merge passed partials and helpers with global namespace values
- Add support for complex ids in @data references
- Docs updates

Compatibility notes:
- The parser is now stricter on `{{{`, requiring that the end token be `}}}`. Templates that do not
  follow this convention should add the additional brace value.
- Code that relies on global the namespace being muted when custom helpers or partials are passed will need to explicitly pass an `undefined` value for any helpers that should not be available.
- The compiler version has changed. Precompiled templates with 1.0.12 or higher must use the 1.0.0 or higher runtime.

[Commits](wycats/handlebars.js@v1.0.11...v1.0.12)

## v1.0.11 / 1.0.0-rc4 - May 13 2013

- [#458](wycats/handlebars.js#458) - Fix `./foo` syntax ([@jpfiset](https://github.com/jpfiset))
- [#460](wycats/handlebars.js#460) - Allow `:` in unescaped identifers ([@jpfiset](https://github.com/jpfiset))
- [#471](wycats/handlebars.js#471) - Create release notes (These!)
- [#456](wycats/handlebars.js#456) - Allow escaping of `\\`
- [#211](wycats/handlebars.js#211) - Fix exception in `escapeExpression`
- [#375](wycats/handlebars.js#375) - Escape unicode newlines
- [#461](wycats/handlebars.js#461) - Do not fail when compiling `""`
- [#302](wycats/handlebars.js#302) - Fix sanity check in knownHelpersOnly mode
- [#369](wycats/handlebars.js#369) - Allow registration of multiple helpers and partial by passing definition object
- Add bower package declaration ([@DevinClark](https://github.com/DevinClark))
- Add NuSpec package declaration ([@MikeMayer](https://github.com/MikeMayer))
- Handle empty context in `with` ([@thejohnfreeman](https://github.com/thejohnfreeman))
- Support custom template extensions in CLI ([@matteoagosti](https://github.com/matteoagosti))
- Fix Rhino support ([@broady](https://github.com/broady))
- Include contexts in string mode ([@leshill](https://github.com/leshill))
- Return precompiled scripts when compiling to AMD ([@JamesMaroney](https://github.com/JamesMaroney))
- Docs updates ([@iangreenleaf](https://github.com/iangreenleaf), [@gilesbowkett](https://github.com/gilesbowkett), [@utkarsh2012](https://github.com/utkarsh2012))
- Fix `toString` handling under IE and browserify ([@tommydudebreaux](https://github.com/tommydudebreaux))
- Add program metadata
daf67c9
@kkirsche kkirsche referenced this pull request in rubyloco/rubyloco.com
Merged

Update handlebars from 1.0.0-rc.4 to 3.0.0 #16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
35 lib/handlebars/compiler/ast.js
@@ -1,15 +1,25 @@
import Exception from "../exception";
-export function ProgramNode(statements, inverse) {
+export function ProgramNode(statements, inverseStrip, inverse) {
this.type = "program";
this.statements = statements;
- if(inverse) { this.inverse = new ProgramNode(inverse); }
+ this.strip = {};
+
+ if(inverse) {
+ this.inverse = new ProgramNode(inverse, inverseStrip);
+ this.strip.right = inverseStrip.left;
+ } else if (inverseStrip) {
+ this.strip.left = inverseStrip.right;
+ }
}
-export function MustacheNode(rawParams, hash, unescaped) {
+export function MustacheNode(rawParams, hash, open, strip) {
this.type = "mustache";
- this.escaped = !unescaped;
this.hash = hash;
+ this.strip = strip;
+
+ var escapeFlag = open[3] || open[2];
+ this.escaped = escapeFlag !== '{' && escapeFlag !== '&';
var id = this.id = rawParams[0];
var params = this.params = rawParams.slice(1);
@@ -28,15 +38,16 @@ export function MustacheNode(rawParams, hash, unescaped) {
// pass or at runtime.
}
-export function PartialNode(partialName, context) {
+export function PartialNode(partialName, context, strip) {
this.type = "partial";
this.partialName = partialName;
this.context = context;
+ this.strip = strip;
}
export function BlockNode(mustache, program, inverse, close) {
- if(mustache.id.original !== close.original) {
- throw new Exception(mustache.id.original + " doesn't match " + close.original);
+ if(mustache.id.original !== close.path.original) {
+ throw new Exception(mustache.id.original + " doesn't match " + close.path.original);
}
this.type = "block";
@@ -44,7 +55,15 @@ export function BlockNode(mustache, program, inverse, close) {
this.program = program;
this.inverse = inverse;
- if (this.inverse && !this.program) {
+ this.strip = {
+ left: mustache.strip.left,
+ right: close.strip.right
+ };
+
+ (program || inverse).strip.left = mustache.strip.right;
+ (inverse || program).strip.right = close.strip.left;
+
+ if (inverse && !program) {
this.isInverse = true;
}
}
View
23 lib/handlebars/compiler/compiler.js
@@ -72,6 +72,7 @@ Compiler.prototype = {
guid: 0,
compile: function(program, options) {
+ this.opcodes = [];
this.children = [];
this.depths = {list: []};
this.options = options;
@@ -93,20 +94,30 @@ Compiler.prototype = {
}
}
- return this.program(program);
+ return this.accept(program);
},
accept: function(node) {
- return this[node.type](node);
+ var strip = node.strip || {},
+ ret;
+ if (strip.left) {
+ this.opcode('strip');
+ }
+
+ ret = this[node.type](node);
+
+ if (strip.right) {
+ this.opcode('strip');
+ }
+
+ return ret;
},
program: function(program) {
- var statements = program.statements, statement;
- this.opcodes = [];
+ var statements = program.statements;
for(var i=0, l=statements.length; i<l; i++) {
- statement = statements[i];
- this[statement.type](statement);
+ this.accept(statements[i]);
}
this.isSimple = l === 1;
View
71 lib/handlebars/compiler/javascript-compiler.js
@@ -75,18 +75,17 @@ JavaScriptCompiler.prototype = {
} else {
this[opcode.opcode].apply(this, opcode.args);
}
- }
- return this.createFunctionContext(asObject);
- },
+ // Reset the stripNext flag if it was not set by this operation.
+ if (opcode.opcode !== this.stripNext) {
+ this.stripNext = false;
+ }
+ }
- nextOpcode: function() {
- var opcodes = this.environment.opcodes;
- return opcodes[this.i + 1];
- },
+ // Flush any trailing content that might be pending.
+ this.pushSource('');
- eat: function() {
- this.i = this.i + 1;
+ return this.createFunctionContext(asObject);
},
preamble: function() {
@@ -141,7 +140,7 @@ JavaScriptCompiler.prototype = {
}
if (!this.environment.isSimple) {
- this.source.push("return buffer;");
+ this.pushSource("return buffer;");
}
var params = this.isChild ? ["depth0", "data"] : ["Handlebars", "depth0", "helpers", "partials", "data"];
@@ -232,7 +231,7 @@ JavaScriptCompiler.prototype = {
// Use the options value generated from the invocation
params[params.length-1] = 'options';
- this.source.push("if (!" + this.lastHelper + ") { " + current + " = blockHelperMissing.call(" + params.join(", ") + "); }");
+ this.pushSource("if (!" + this.lastHelper + ") { " + current + " = blockHelperMissing.call(" + params.join(", ") + "); }");
},
// [appendContent]
@@ -242,7 +241,28 @@ JavaScriptCompiler.prototype = {
//
// Appends the string value of `content` to the current buffer
appendContent: function(content) {
- this.source.push(this.appendToBuffer(this.quotedString(content)));
+ if (this.pendingContent) {
+ content = this.pendingContent + content;
+ }
+ if (this.stripNext) {
+ content = content.replace(/^\s+/, '');
+ }
+
+ this.pendingContent = content;
+ },
+
+ // [strip]
+ //
+ // On stack, before: ...
+ // On stack, after: ...
+ //
+ // Removes any trailing whitespace from the prior content node and flags
+ // the next operation for stripping if it is a content node.
+ strip: function() {
+ if (this.pendingContent) {
+ this.pendingContent = this.pendingContent.replace(/\s+$/, '');
+ }
+ this.stripNext = 'strip';
},
// [append]
@@ -259,9 +279,9 @@ JavaScriptCompiler.prototype = {
// when we examine local
this.flushInline();
var local = this.popStack();
- this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
+ this.pushSource("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
if (this.environment.isSimple) {
- this.source.push("else { " + this.appendToBuffer("''") + " }");
+ this.pushSource("else { " + this.appendToBuffer("''") + " }");
}
},
@@ -274,7 +294,7 @@ JavaScriptCompiler.prototype = {
appendEscaped: function() {
this.context.aliases.escapeExpression = 'this.escapeExpression';
- this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")"));
+ this.pushSource(this.appendToBuffer("escapeExpression(" + this.popStack() + ")"));
},
// [getContext]
@@ -498,8 +518,8 @@ JavaScriptCompiler.prototype = {
var nonHelper = this.nameLookup('depth' + this.lastContext, name, 'context');
var nextStack = this.nextStack();
- this.source.push('if (' + nextStack + ' = ' + helperName + ') { ' + nextStack + ' = ' + nextStack + '.call(' + helper.callParams + '); }');
- this.source.push('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '.call(' + helper.callParams + ') : ' + nextStack + '; }');
+ this.pushSource('if (' + nextStack + ' = ' + helperName + ') { ' + nextStack + ' = ' + nextStack + '.call(' + helper.callParams + '); }');
+ this.pushSource('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '.call(' + helper.callParams + ') : ' + nextStack + '; }');
},
// [invokePartial]
@@ -606,7 +626,7 @@ JavaScriptCompiler.prototype = {
register: function(name, val) {
this.useRegister(name);
- this.source.push(name + " = " + val + ";");
+ this.pushSource(name + " = " + val + ";");
},
useRegister: function(name) {
@@ -620,12 +640,23 @@ JavaScriptCompiler.prototype = {
return this.push(new Literal(item));
},
+ pushSource: function(source) {
+ if (this.pendingContent) {
+ this.source.push(this.appendToBuffer(this.quotedString(this.pendingContent)));
+ this.pendingContent = undefined;
+ }
+
+ if (source) {
+ this.source.push(source);
+ }
+ },
+
pushStack: function(item) {
this.flushInline();
var stack = this.incrStack();
if (item) {
- this.source.push(stack + " = " + item + ";");
+ this.pushSource(stack + " = " + item + ";");
}
this.compileStack.push(stack);
return stack;
@@ -668,7 +699,7 @@ JavaScriptCompiler.prototype = {
stack = this.nextStack();
}
- this.source.push(stack + " = (" + prefix + item + ");");
+ this.pushSource(stack + " = (" + prefix + item + ");");
}
return stack;
},
View
62 spec/whitespace-control.js
@@ -0,0 +1,62 @@
+describe('whitespace control', function() {
+ it('should strip whitespace around mustache calls', function() {
+ var hash = {foo: 'bar<'};
+
+ shouldCompileTo(' {{~foo~}} ', hash, 'bar&lt;');
+ shouldCompileTo(' {{~foo}} ', hash, 'bar&lt; ');
+ shouldCompileTo(' {{foo~}} ', hash, ' bar&lt;');
+
+ shouldCompileTo(' {{~&foo~}} ', hash, 'bar<');
+ shouldCompileTo(' {{~{foo}~}} ', hash, 'bar<');
+ });
+
+ describe('blocks', function() {
+ it('should strip whitespace around simple block calls', function() {
+ var hash = {foo: 'bar<'};
+
+ shouldCompileTo(' {{~#if foo~}} bar {{~/if~}} ', hash, 'bar');
+ shouldCompileTo(' {{#if foo~}} bar {{/if~}} ', hash, ' bar ');
+ shouldCompileTo(' {{~#if foo}} bar {{~/if}} ', hash, ' bar ');
+ shouldCompileTo(' {{#if foo}} bar {{/if}} ', hash, ' bar ');
+ });
+ it('should strip whitespace around inverse block calls', function() {
+ var hash = {};
+
+ shouldCompileTo(' {{~^if foo~}} bar {{~/if~}} ', hash, 'bar');
+ shouldCompileTo(' {{^if foo~}} bar {{/if~}} ', hash, ' bar ');
+ shouldCompileTo(' {{~^if foo}} bar {{~/if}} ', hash, ' bar ');
+ shouldCompileTo(' {{^if foo}} bar {{/if}} ', hash, ' bar ');
+ });
+ it('should strip whitespace around complex block calls', function() {
+ var hash = {foo: 'bar<'};
+
+ shouldCompileTo('{{#if foo~}} bar {{~^~}} baz {{~/if}}', hash, 'bar');
+ shouldCompileTo('{{#if foo~}} bar {{^~}} baz {{/if}}', hash, 'bar ');
+ shouldCompileTo('{{#if foo}} bar {{~^~}} baz {{~/if}}', hash, ' bar');
+ shouldCompileTo('{{#if foo}} bar {{^~}} baz {{/if}}', hash, ' bar ');
+
+ shouldCompileTo('{{#if foo~}} bar {{~else~}} baz {{~/if}}', hash, 'bar');
+
+ hash = {};
+
+ shouldCompileTo('{{#if foo~}} bar {{~^~}} baz {{~/if}}', hash, 'baz');
+ shouldCompileTo('{{#if foo}} bar {{~^~}} baz {{/if}}', hash, 'baz ');
+ shouldCompileTo('{{#if foo~}} bar {{~^}} baz {{~/if}}', hash, ' baz');
+ shouldCompileTo('{{#if foo~}} bar {{~^}} baz {{/if}}', hash, ' baz ');
+
+ shouldCompileTo('{{#if foo~}} bar {{~else~}} baz {{~/if}}', hash, 'baz');
+ });
+ });
+
+ it('should strip whitespace around partials', function() {
+ shouldCompileToWithPartials('foo {{~> dude~}} ', [{}, {}, {dude: 'bar'}], true, 'foobar');
+ shouldCompileToWithPartials('foo {{> dude~}} ', [{}, {}, {dude: 'bar'}], true, 'foo bar');
+ shouldCompileToWithPartials('foo {{> dude}} ', [{}, {}, {dude: 'bar'}], true, 'foo bar ');
+ });
+
+ it('should only strip whitespace once', function() {
+ var hash = {foo: 'bar'};
+
+ shouldCompileTo(' {{~foo~}} {{foo}} {{foo}} ', hash, 'barbar bar ');
+ });
+});
View
35 src/handlebars.l
@@ -9,6 +9,11 @@ function strip(start, end) {
%}
+LEFT_STRIP "~"
+RIGHT_STRIP "~"
+
+LOOKAHEAD [=~}\s\/.]
+LITERAL_LOOKAHEAD [~}\s]
/*
ID is the inverse of control characters.
@@ -19,7 +24,7 @@ Control characters ranges:
[\[-\^`] [, \, ], ^, `, Exceptions in range: _
[\{-~] {, |, }, ~
*/
-ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.]
+ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD}
%%
@@ -46,30 +51,30 @@ ID [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.]
<com>[\s\S]*?"--}}" strip(0,4); this.popState(); return 'COMMENT';
-<mu>"{{>" return 'OPEN_PARTIAL';
-<mu>"{{#" return 'OPEN_BLOCK';
-<mu>"{{/" return 'OPEN_ENDBLOCK';
-<mu>"{{^" return 'OPEN_INVERSE';
-<mu>"{{"\s*"else" return 'OPEN_INVERSE';
-<mu>"{{{" return 'OPEN_UNESCAPED';
-<mu>"{{&" return 'OPEN';
+<mu>"{{"{LEFT_STRIP}?">" return 'OPEN_PARTIAL';
+<mu>"{{"{LEFT_STRIP}?"#" return 'OPEN_BLOCK';
+<mu>"{{"{LEFT_STRIP}?"/" return 'OPEN_ENDBLOCK';
+<mu>"{{"{LEFT_STRIP}?"^" return 'OPEN_INVERSE';
+<mu>"{{"{LEFT_STRIP}?\s*"else" return 'OPEN_INVERSE';
+<mu>"{{"{LEFT_STRIP}?"{" return 'OPEN_UNESCAPED';
+<mu>"{{"{LEFT_STRIP}?"&" return 'OPEN';
<mu>"{{!--" this.popState(); this.begin('com');
<mu>"{{!"[\s\S]*?"}}" strip(3,5); this.popState(); return 'COMMENT';
-<mu>"{{" return 'OPEN';
+<mu>"{{"{LEFT_STRIP}? return 'OPEN';
<mu>"=" return 'EQUALS';
-<mu>"."/[}\/ ] return 'ID';
<mu>".." return 'ID';
+<mu>"."/{LOOKAHEAD} return 'ID';
<mu>[\/.] return 'SEP';
<mu>\s+ /*ignore whitespace*/
-<mu>"}}}" this.popState(); return 'CLOSE_UNESCAPED';
-<mu>"}}" this.popState(); return 'CLOSE';
+<mu>"}"{RIGHT_STRIP}?"}}" this.popState(); return 'CLOSE_UNESCAPED';
+<mu>{RIGHT_STRIP}?"}}" this.popState(); return 'CLOSE';
<mu>'"'("\\"["]|[^"])*'"' yytext = strip(1,2).replace(/\\"/g,'"'); return 'STRING';
<mu>"'"("\\"[']|[^'])*"'" yytext = strip(1,2).replace(/\\'/g,"'"); return 'STRING';
<mu>"@" return 'DATA';
-<mu>"true"/[}\s] return 'BOOLEAN';
-<mu>"false"/[}\s] return 'BOOLEAN';
-<mu>\-?[0-9]+/[}\s] return 'INTEGER';
+<mu>"true"/{LITERAL_LOOKAHEAD} return 'BOOLEAN';
+<mu>"false"/{LITERAL_LOOKAHEAD} return 'BOOLEAN';
+<mu>\-?[0-9]+/{LITERAL_LOOKAHEAD} return 'INTEGER';
<mu>{ID} return 'ID';
View
36 src/handlebars.yy
@@ -2,6 +2,17 @@
%ebnf
+%{
+
+function stripFlags(open, close) {
+ return {
+ left: open[2] === '~',
+ right: close[0] === '~' || close[1] === '~'
+ };
+}
+
+%}
+
%%
root
@@ -9,9 +20,9 @@ root
;
program
- : simpleInverse statements -> new yy.ProgramNode([], $2)
- | statements simpleInverse statements -> new yy.ProgramNode($1, $3)
- | statements simpleInverse -> new yy.ProgramNode($1, [])
+ : simpleInverse statements -> new yy.ProgramNode([], $1, $2)
+ | statements simpleInverse statements -> new yy.ProgramNode($1, $2, $3)
+ | statements simpleInverse -> new yy.ProgramNode($1, $2, [])
| statements -> new yy.ProgramNode($1)
| simpleInverse -> new yy.ProgramNode([])
| "" -> new yy.ProgramNode([])
@@ -32,32 +43,31 @@ statement
;
openBlock
- : OPEN_BLOCK inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1])
+ : OPEN_BLOCK inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1], $1, stripFlags($1, $3))
;
openInverse
- : OPEN_INVERSE inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1])
+ : OPEN_INVERSE inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1], $1, stripFlags($1, $3))
;
closeBlock
- : OPEN_ENDBLOCK path CLOSE -> $2
+ : OPEN_ENDBLOCK path CLOSE -> {path: $2, strip: stripFlags($1, $3)}
;
mustache
- : OPEN inMustache CLOSE {
- // Parsing out the '&' escape token at this level saves ~500 bytes after min due to the removal of one parser node.
- $$ = new yy.MustacheNode($2[0], $2[1], $1[2] === '&');
- }
- | OPEN_UNESCAPED inMustache CLOSE_UNESCAPED -> new yy.MustacheNode($2[0], $2[1], true)
+ // Parsing out the '&' escape token at AST level saves ~500 bytes after min due to the removal of one parser node.
+ // This also allows for handler unification as all mustache node instances can utilize the same handler
+ : OPEN inMustache CLOSE -> new yy.MustacheNode($2[0], $2[1], $1, stripFlags($1, $3))
+ | OPEN_UNESCAPED inMustache CLOSE_UNESCAPED -> new yy.MustacheNode($2[0], $2[1], $1, stripFlags($1, $3))
;
partial
- : OPEN_PARTIAL partialName path? CLOSE -> new yy.PartialNode($2, $3)
+ : OPEN_PARTIAL partialName path? CLOSE -> new yy.PartialNode($2, $3, stripFlags($1, $4))
;
simpleInverse
- : OPEN_INVERSE CLOSE { }
+ : OPEN_INVERSE CLOSE -> stripFlags($1, $2)
;
inMustache
Something went wrong with that request. Please try again.