Skip to content

Commit

Permalink
Issue #31 - add an final trim before genrating a statment with eval t…
Browse files Browse the repository at this point in the history
…o catch any newlines that slip through
  • Loading branch information
Ronald Holshausen committed Oct 28, 2012
1 parent 990b1d7 commit dc5bd40
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/haml.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/haml.min.js

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion spec/haml-spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 35 additions & 7 deletions spec/issues-spec.coffee
Expand Up @@ -81,7 +81,7 @@ describe 'haml issues', () ->
'<div class="embedded-null">\n' +
' \n' +
'</div>\n')

describe 'Issue 13 - comments', () ->

beforeEach () ->
Expand All @@ -107,7 +107,7 @@ describe 'haml issues', () ->
'<div id="div3">\n' +
' You should see me\n' +
'</div>\n')

describe 'Issue #21 - text node followed by tag node fails', () ->

hex = (str) ->
Expand Down Expand Up @@ -246,8 +246,8 @@ describe 'haml issues', () ->
</div>
'''
options = [
{value: '1', text: 'text 1'},
{value: '2', text: 'text 2'},
{value: '1', text: 'text 1'},
{value: '2', text: 'text 2'},
{value: '3', text: 'text 3'}
]
expect(_(haml.compileHaml(source: hamlSource, generator: 'coffeescript').call(options: options, selected: '1')).trim()).toEqual(expected)
Expand Down Expand Up @@ -278,8 +278,8 @@ describe 'haml issues', () ->
</div>
'''
options = [
{value: '1', text: 'text 1'},
{value: '2', text: 'text 2'},
{value: '1', text: 'text 1'},
{value: '2', text: 'text 2'},
{value: '3', text: 'text 3'}
]
expect(_(haml.compileHaml(source: hamlSource, generator: 'coffeescript').call(options: options, selected: '1')).trim()).toEqual(expected)
Expand All @@ -299,4 +299,32 @@ describe 'haml issues', () ->
&copy; Company 2012
</p>
'''
expect(_(haml.compileHaml(source: hamlSource)()).trim()).toEqual(expected)
expect(_(haml.compileHaml(source: hamlSource)()).trim()).toEqual(expected)

describe 'Issue #31 - new line in eval breaking generated function', () ->

it 'should not blow up', () ->

hamlSource =
'''
%div{class: 'hero-unit'}
%h2= email.subject
%iframe{class: 'email-preview', src: iframeSource}
'''
expected =
'''
<div class="hero-unit">
<h2>
Issue #31
</h2>
<iframe class="email-preview" src="blahblahblah">
</iframe>
</div>
'''

data = {
email: {subject: 'Issue #31'},
iframeSource: 'blahblahblah'
}

expect(_(haml.compileHaml(source: hamlSource)(data)).trim()).toEqual(expected)
2 changes: 1 addition & 1 deletion src/jscodegenerator.coffee
Expand Up @@ -14,7 +14,7 @@ class JsCodeGenerator extends CodeGenerator

@outputBuffer.appendToOutputBuffer(indentText + 'try {\n')
@outputBuffer.appendToOutputBuffer(indentText + ' var value = eval("' +
expression.replace(/"/g, '\\"').replace(/\\n/g, '\\\\n') + '");\n')
(_.str || _).trim(expression).replace(/"/g, '\\"').replace(/\\n/g, '\\\\n') + '");\n')
@outputBuffer.appendToOutputBuffer(indentText + ' value = value === null ? "" : value;')
if escapeContents
@outputBuffer.appendToOutputBuffer(indentText + ' html.push(haml.HamlRuntime.escapeHTML(String(value)));\n')
Expand Down
2 changes: 1 addition & 1 deletion src/productionjscodegenerator.coffee
Expand Up @@ -9,7 +9,7 @@ class ProductionJsCodeGenerator extends JsCodeGenerator
appendEmbeddedCode: (indentText, expression, escapeContents, perserveWhitespace, currentParsePoint) ->
@outputBuffer.flush()

@outputBuffer.appendToOutputBuffer(indentText + ' value = ' + expression + ';\n')
@outputBuffer.appendToOutputBuffer(indentText + ' value = ' + (_.str || _).trim(expression) + ';\n')
@outputBuffer.appendToOutputBuffer(indentText + ' value = value === null ? "" : value;')
if escapeContents
@outputBuffer.appendToOutputBuffer(indentText + ' html.push(haml.HamlRuntime.escapeHTML(String(value)));\n')
Expand Down
1 change: 0 additions & 1 deletion src/tokiniser.coffee
Expand Up @@ -257,7 +257,6 @@ class Tokeniser
text += contents.substring(0, contents.length - 1)
@advanceCharsInBuffer(contents.length - 1)
@getNextToken()
#@pushBackToken()
text

###
Expand Down

0 comments on commit dc5bd40

Please sign in to comment.