Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handlebars parser is not handling JSON data correctly #428

Closed
wardkj opened this issue Sep 15, 2015 · 4 comments
Closed

Handlebars parser is not handling JSON data correctly #428

wardkj opened this issue Sep 15, 2015 · 4 comments
Assignees
Labels
Milestone

Comments

@wardkj
Copy link

wardkj commented Sep 15, 2015

Hi,

the hanlebars parser doesn't like JSON that ends in two braces.

If, say, we pass in a template like

<template requires=""><button someAttr="{&quot;one&quot;: &quot;value&quot;,&quot;two&quot;: {&quot;inner&quot;: &quot;value&quot;}}"></button></template>

once its parsed, the final }} are missing from the attribute value

image

@wkeese
Copy link
Member

wkeese commented Sep 15, 2015

Huh? The whole point of handlebars is to bind the DOM to properties in the widget, and that's what the {{ ... }} syntax is for. For example, <button someAttr="{{propertyXYZ}}">. You're expecting the }} to just be treated as literal text?

@martinfitzg
Copy link

The use case here is that certain attributes that are in the markup of the template contain json eg

<template>
     <my-custom-component someattr="{name: {first : 'john', last: 'doe'}}"></my-custom-component >
</template>

As the markup for this template is been generated server side and the json that is been set here drives some sort of state when the widget initializes.

The problem here is the last part of the json .... 'doe'}} .... as there is 2 curly closing brackets together. This causes issue with handlebars 'compiling', as it's not valid as far as it is concerned, as there is no way to tell handlebars that it should be ignored.

Is there any way that it can be told to ignore this block of json during it's 'compile' phase ?

@wkeese
Copy link
Member

wkeese commented Sep 16, 2015

Hmm, there's no way to do that currently. I suppose the toJs() method in handlebars.js should be modified to be less fragile, and ignore the case above since there's no opening {{ characters. You've shown an example where JSON contains }} but I suppose there's never a case in JSON where there's a {{?

@wkeese
Copy link
Member

wkeese commented Sep 16, 2015

PS: You're walking on thin ice though, because if you insert arbitrary data into the template then theoretically the string literals themselves could contain curly brackets:

<template>
     <my-custom-component someattr="{name: '{{weird name}}'}"></my-custom-component >
</template>

At that point I guess you would need to start escaping strings on the server, converting curly braces to &#123; and &#124;. Actually you should probably be escaping already in case your user text contains an & character.

PS: Hmm maybe even encoding wouldn't work; the browser might automatically decode before handlebars.js sees it. Not sure.

@wkeese wkeese added this to the 0.8.0 milestone Sep 16, 2015
@wkeese wkeese added the bug label Sep 16, 2015
@wkeese wkeese self-assigned this Sep 16, 2015
@wkeese wkeese closed this as completed in 24e48a3 Sep 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants