documentcloud / jammit

Industrial Strength Asset Packaging for Rails
http://documentcloud.github.com/jammit/
MIT License
1.16k stars 197 forks source link

Templates break due to unterminated string literal #176

Closed colbin8r closed 13 years ago

colbin8r commented 13 years ago

Templates break in Opera 11.11, Chrome 11, and Firefox 4 due to an 'unterminated string literal' error.

I have Jammit configured to use the Underscore template engine and add templates to the App.Templates namespace. Here is a gist that should be able to be used to reproduce the error (if you configure assets.yml like I described above): https://gist.github.com/984934

common.jst = javascript template package delivered by jammit. You can see how it might mark the string as unterminated. application.js = script loaded after templates to test the simple template by loading it into the body (using jQuery) simple.jst = a simple template

colbin8r commented 13 years ago

Actually, after further testing, the error doesn't occur in Chrome. It recognizes the template as a single line. However, I do receive an 'Unexpected token ILLEGAL" error, and the template is still not added to the template namespace.

colbin8r commented 13 years ago

If you have a template that is strictly one-line, then this template works fine and is usable in Firefox 4.

Example _Template (oneline.jst)

Test Execute this in your console:

Result The result should be 'One-line template.'

jashkenas commented 13 years ago

That doesn't look right -- newlines in templates should be correctly escaped. Nobodies templates would ever work otherwise.

What versions of Jammit/Rails/Ruby are you running?

colbin8r commented 13 years ago

Rails: 3.0.4 Ruby: 1.8.7 Jammit: 0.6.0

Can you reproduce the error? Is it possible that this could be browser-specific? How is Jammit escaping newlines?

A tad of research turned up that Javascript needs backslashes at the end of lines to correctly escape line breaks in string literals. Would something along the lines of http://bit.ly/ltzzg6 (but the escaping is backwards as per my note) fix something like that?

jashkenas commented 13 years ago

Yes ... Jammit should be removing newlines and replacing them with \n ... take a look at jst_test_nested.js within the test/fixtures/jammed folder.

colbin8r commented 13 years ago

Is the code that does this at lib/jammit/compressor.rb line 103?

jashkenas commented 13 years ago

Yep.

colbin8r commented 13 years ago

I'm fairly new to Ruby on Rails/gem development, but I found that the same line in my source looked like this: https://gist.github.com/988964

jashkenas commented 13 years ago

You'll need to wait for the updated version of Jammit to be released (hopefully this afternoon), or install off of master.

colbin8r commented 13 years ago

Thanks! Jammit 0.6.3 works like a charm.