diff --git a/lib/template.js b/lib/template.js index 04219c5..4154227 100644 --- a/lib/template.js +++ b/lib/template.js @@ -8,8 +8,12 @@ const findInsideDoubleBracesIndices = (input) => { for (let i = 0; i < input.length; i += 1) { const char = input[i]; - if (char === '"' || char === "'") { - // If inside quotes, ignore braces + if (insideDoubleBracesStart && char === '\\') { + // If next character is escaped, skip it + i += 1; + } + if (insideDoubleBracesStart && (char === '"' || char === "'")) { + // If inside double braces and inside quotes, ignore braces if (!wrappingQuote) { wrappingQuote = char; } else if (wrappingQuote === char) { diff --git a/test/template.test.js b/test/template.test.js index 1817557..bad51d2 100644 --- a/test/template.test.js +++ b/test/template.test.js @@ -136,5 +136,18 @@ describe('template', () => { expect(render([{'{{.bar}}': [false, '/foo/{{.foo + .bar}}']}])).toEqual([{foo: [false, '/foo/barfoo']}]); expect(render({foo: [{bar: '{{1}}'}, '{{empty}}']})).toEqual({foo: [{bar: 1}, undefined]}); }); + it('should accept quotes outside of template', () => { + const json = { foo: 'bar', bar: 'foo' }; + const render = (input) => jq.renderRecursively(json, input); + + expect(render('"{{.foo}}"')).toEqual('"bar"'); + expect(render('\'{{.foo}}\'')).toEqual('\'bar\''); + }); + it('should accept escaped quotes inside jq template', () => { + const json = { foo: 'bar', bar: 'foo' }; + const render = (input) => jq.renderRecursively(json, input); + + expect(render('{{"\\"foo\\""}}')).toEqual('"foo"'); + }); })