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

Template with single quote causes JSON.parse: bad escaped character error #8

Open
revbingo opened this issue Jul 19, 2011 · 0 comments

Comments

@revbingo
Copy link
Contributor

A template containing an apostrophe causes problems with client side templating. The browser shows an error which Firebug shows as "JSON.parse: bad escaped character", and in Chrome is simply "Uncaught SyntaxError: Unexpected token ILLEGAL".

The problem is that the #{mustache.meta} tag uses JavaExtensions.escapeJavascript() when inserting the template into JSON. This escapes single quotes, but the JSON specification does not allow single quotes to be escaped (see http://stackoverflow.com/questions/2275359/jquery-single-quote-in-json-response)

Test code. When you click "Click here", it ought to replace "mustache thing" with "client thing", but instead the error above is shown on page load, leading to "to_html is undefined" if you click on Click Here.

#{mustache.template 'thing'}
  this is a {{template}} with an a'postrophe
#{/mustache.template}

<span id="content">#{mustache.print "thing", context: [template: "mustache thing"] /}</span>

<span id="click">Click here</span>

<script>
$(function() {
    $("#click").click(function() {
        var html = PlayMustache.to_html("thing", { "template": "client thing" })
        $("#content").empty()
        $("#content").append(html)
    })
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant