Skip to content

Commit

Permalink
Fix Handlebar lexer to parse multiple keys per line
Browse files Browse the repository at this point in the history
  • Loading branch information
karellm committed May 31, 2018
1 parent 0dcb40d commit 7cc4fb8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/lexers/handlebars-lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ HandlebarsLexer = function (_BaseLexer) {_inherits(HandlebarsLexer, _BaseLexer);

{
var functionPattern = this.functionPattern();
var curlyPattern = '(?:{{)' + functionPattern + '\\s+(.*)(?:}})';
var curlyPattern = '(?:{{)' + functionPattern + '\\s+(.*?)(?:}})';
var parenthesisPattern = '(?:\\()' + functionPattern + '\\s+(.*)(?:\\))';
var pattern = curlyPattern + '|' + parenthesisPattern;
this.functionRegex = new RegExp(pattern, 'gi');
Expand Down
2 changes: 1 addition & 1 deletion src/lexers/handlebars-lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class HandlebarsLexer extends BaseLexer {

createFunctionRegex() {
const functionPattern = this.functionPattern()
const curlyPattern = '(?:{{)' + functionPattern + '\\s+(.*)(?:}})'
const curlyPattern = '(?:{{)' + functionPattern + '\\s+(.*?)(?:}})'
const parenthesisPattern = '(?:\\()' + functionPattern + '\\s+(.*)(?:\\))'
const pattern = curlyPattern + '|' + parenthesisPattern
this.functionRegex = new RegExp(pattern, 'gi')
Expand Down
7 changes: 7 additions & 0 deletions test/lexers/handlebars-lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ describe('HandlebarsLexer', () => {
done()
})

it('extracts multiple keys on a single line', (done) => {
const Lexer = new HandlebarsLexer()
const content = '<p>{{t "first"}} {{t "second"}}</p>'
assert.deepEqual(Lexer.extract(content), [{ key: 'first' }, { key: 'second' }])
done()
})

it('extracts the second argument as defaultValue', (done) => {
const Lexer = new HandlebarsLexer()
const content = '<p>{{t "first" "bla"}}</p>'
Expand Down
3 changes: 1 addition & 2 deletions test/templating/handlebars.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<div class="foobar">
<p>{{t "first"}}</p>
<p>{{t "second" "defaultValue" option="foo" context="male"}}</p>
<p>{{t "first"}} {{t "second" "defaultValue" option="foo" context="male"}}</p>
<p>{{t "third" defaultValue="defaultValue" context="female"}}</p>
<p>{{t "fourth" context="male" bla='asd' defaultValue="defaultValue"}}</p>
<p>{{t
Expand Down

0 comments on commit 7cc4fb8

Please sign in to comment.