Skip to content

Commit

Permalink
fix: safely stringify raw lines
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 12, 2020
1 parent 0ad9fc7 commit 48ed63c
Show file tree
Hide file tree
Showing 34 changed files with 73 additions and 49 deletions.
2 changes: 1 addition & 1 deletion fixtures/array-w-identifiers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'The even numbers are ';
out += "The even numbers are ";
out += `${ctx.escape([ctx.resolve('num1'), ctx.resolve('num2'), ctx.resolve('num3')].filter(num => num % 2 === 0))}`;
} catch (error) {
ctx.reThrow(error);
Expand Down
2 changes: 1 addition & 1 deletion fixtures/arrays/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'The even numbers are ';
out += "The even numbers are ";
out += `${ctx.escape([1, 2, 3, 4].filter(num => num % 2 === 0))}`;
} catch (error) {
ctx.reThrow(error);
Expand Down
2 changes: 1 addition & 1 deletion fixtures/assignment-expression-callable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += `${ctx.escape(name = ctx.resolve('username').toUpperCase())}`;
out += '\n';
out += "\n";
ctx.$lineNumber = 2;
out += `${ctx.escape(name = ctx.resolve('getUser')().username)}`;
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion fixtures/binary-expressions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += `${ctx.escape(2 + 2)}`;
out += ' = 4';
out += " = 4";
} catch (error) {
ctx.reThrow(error);
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/complex-function-calls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Hello ';
out += "Hello ";
out += `${ctx.escape(ctx.resolve('upper')(ctx.resolve('auth').user.getUsername()))}`;
} catch (error) {
ctx.reThrow(error);
Expand Down
8 changes: 4 additions & 4 deletions fixtures/escape-tags-multiline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += '@if(';
out += ' 2 + 2 === 4';
out += ')';
out += '@endif';
out += "@if(";
out += " 2 + 2 === 4";
out += ")";
out += "@endif";
} catch (error) {
ctx.reThrow(error);
}
Expand Down
4 changes: 2 additions & 2 deletions fixtures/escape-tags-with-backticks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += '@if(`i${username}` === \'ivirk\')';
out += '@endif';
out += "@if(`i${username}` === 'ivirk')";
out += "@endif";
} catch (error) {
ctx.reThrow(error);
}
Expand Down
6 changes: 3 additions & 3 deletions fixtures/escape-tags-with-content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += '@if(username)';
out += ' Hello ';
out += "@if(username)";
out += " Hello ";
ctx.$lineNumber = 2;
out += `${ctx.escape(ctx.resolve('username'))}`;
out += '@endif';
out += "@endif";
} catch (error) {
ctx.reThrow(error);
}
Expand Down
4 changes: 2 additions & 2 deletions fixtures/escape-tags/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += '@if(username)';
out += '@endif';
out += "@if(username)";
out += "@endif";
} catch (error) {
ctx.reThrow(error);
}
Expand Down
12 changes: 6 additions & 6 deletions fixtures/escaped-mustache-multiline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Hello ';
out += '{{';
out += ' users.map((user) => {';
out += ' return user.username';
out += ' })';
out += '}}';
out += "Hello ";
out += "{{";
out += " users.map((user) =\u003E {";
out += " return user.username";
out += " })";
out += "}}";
} catch (error) {
ctx.reThrow(error);
}
Expand Down
4 changes: 2 additions & 2 deletions fixtures/escaped-mustache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Hello ';
out += '{{ username }}';
out += "Hello ";
out += "{{ username }}";
} catch (error) {
ctx.reThrow(error);
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/function-call/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Hello ';
out += "Hello ";
out += `${ctx.escape(ctx.resolve('upper')(ctx.resolve('username')))}`;
} catch (error) {
ctx.reThrow(error);
Expand Down
3 changes: 3 additions & 0 deletions fixtures/html-script/index.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
var reg = /\+/g
</script>
15 changes: 15 additions & 0 deletions fixtures/html-script/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
return (function (template, ctx) {
let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += "\u003Cscript\u003E";
out += "\n";
out += " var reg = \u002F\\+\u002Fg";
out += "\n";
out += "\u003C\u002Fscript\u003E";
} catch (error) {
ctx.reThrow(error);
}
return out;
})(template, ctx)
2 changes: 1 addition & 1 deletion fixtures/multiline-expressions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Your friends are ';
out += "Your friends are ";
out += `${ctx.escape(ctx.resolve('users').map(user => {
return user.username;
}))}`;
Expand Down
2 changes: 1 addition & 1 deletion fixtures/multiline-template-literals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Your friends are ';
out += "Your friends are ";
out += ctx.escape(`${ctx.resolve('users').map(user => {
return user.username;
})}`);
Expand Down
2 changes: 1 addition & 1 deletion fixtures/mustache-template-literals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Hello ';
out += "Hello ";
out += ctx.escape(`${ctx.resolve('username')} - ${ctx.resolve('age')}`);
} catch (error) {
ctx.reThrow(error);
Expand Down
2 changes: 1 addition & 1 deletion fixtures/mustache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Hello ';
out += "Hello ";
out += `${ctx.escape(ctx.resolve('username'))}`;
} catch (error) {
ctx.reThrow(error);
Expand Down
2 changes: 1 addition & 1 deletion fixtures/nested-binary-expression/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += `${ctx.escape((2 + 2) * 4)}`;
out += ' = 16';
out += " = 16";
} catch (error) {
ctx.reThrow(error);
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/prop-function-args/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Hello ';
out += "Hello ";
out += `${ctx.escape(ctx.resolve('username').toString(true))}`;
} catch (error) {
ctx.reThrow(error);
Expand Down
2 changes: 1 addition & 1 deletion fixtures/prop-function/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Hello ';
out += "Hello ";
out += `${ctx.escape(ctx.resolve('username').toUpperCase())}`;
} catch (error) {
ctx.reThrow(error);
Expand Down
2 changes: 1 addition & 1 deletion fixtures/raw-with-quotes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'This is Susan\'s pet.';
out += "This is Susan's pet.";
} catch (error) {
ctx.reThrow(error);
}
Expand Down
4 changes: 2 additions & 2 deletions fixtures/safe-mustache-escaped/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Hello ';
out += '{{{ username }}}';
out += "Hello ";
out += "{{{ username }}}";
} catch (error) {
ctx.reThrow(error);
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/safe-mustache-interpol/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Hello ';
out += "Hello ";
out += `${ctx.resolve('greeting')}`;
} catch (error) {
ctx.reThrow(error);
Expand Down
2 changes: 1 addition & 1 deletion fixtures/safe-mustache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Hello ';
out += "Hello ";
out += `${'<p> World </p>'}`;
} catch (error) {
ctx.reThrow(error);
Expand Down
2 changes: 1 addition & 1 deletion fixtures/sequence-expression/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Hello ';
out += "Hello ";
out += `${ctx.escape((true, false))}`;
} catch (error) {
ctx.reThrow(error);
Expand Down
2 changes: 1 addition & 1 deletion fixtures/unary-expression/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'Inspect ';
out += "Inspect ";
out += `${ctx.escape(typeof ctx.resolve('username'))}`;
} catch (error) {
ctx.reThrow(error);
Expand Down
4 changes: 2 additions & 2 deletions fixtures/with-backticks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ let out = '';
ctx.$lineNumber = 1;
ctx.$filename = '{{ __dirname }}index.edge';
try {
out += 'This is ';
out += "This is ";
out += `${ctx.escape(ctx.resolve('username'))}`;
out += '\'s `pet`.';
out += "'s `pet`.";
} catch (error) {
ctx.reThrow(error);
}
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"acorn": "^7.1.1",
"astring": "^1.4.3",
"edge-error": "^1.0.4",
"edge-lexer": "^2.1.0"
"edge-lexer": "^2.1.0",
"js-stringify": "^1.0.2"
},
"devDependencies": {
"@adonisjs/mrm-preset": "^2.2.4",
Expand Down
4 changes: 2 additions & 2 deletions src/EdgeBuffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import { EOL } from 'os'
import stringify from 'js-stringify'

/**
* Buffer class to construct template
Expand Down Expand Up @@ -148,8 +149,7 @@ export class EdgeBuffer {
* Write raw text to the output variable
*/
public outputRaw (text: string) {
text = text.replace(/[']/g, '\\\'')
this.buffer.push(`${this.options.outputVar} += '${text}';`)
this.buffer.push(`${this.options.outputVar} += ${stringify(text)};`)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class Parser {
buffer.outputRaw(token.value)
break
case 'newline':
buffer.outputRaw(EOL === '\n' ? '\\n' : '\\r\\n')
buffer.outputRaw(EOL === '\n' ? '\n' : '\r\n')
break
case TagTypes.TAG:
this.tags[token.properties.name].compile(this, buffer, token as TagToken)
Expand Down
6 changes: 3 additions & 3 deletions test/buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test.group('Buffer', () => {
ctx.$lineNumber = 1;
ctx.$filename = 'eval.edge';
try {
out += 'hello world';
out += "hello world";
} catch (error) {
ctx.reThrow(error);
}
Expand All @@ -58,7 +58,7 @@ test.group('Buffer', () => {
ctx.$lineNumber = 1;
ctx.$filename = 'eval.edge';
try {
out += '\\'hello world\\'';
out += "'hello world'";
} catch (error) {
ctx.reThrow(error);
}
Expand Down Expand Up @@ -95,7 +95,7 @@ test.group('Buffer', () => {
ctx.$filename = 'eval.edge';
try {
if (username) {
out += 'hello world';
out += "hello world";
ctx.$lineNumber = 3;
}
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const tags = {

function normalizeNewLines (value: string) {
// eslint-disable-next-line @typescript-eslint/quotes
return value.replace(/out\s\+=\s'\\n'/, `out += ${EOL === '\n' ? `'\\n'` : `'\\r\\n'`}`)
return value.replace(/out\s\+=\s"\\n"/g, `out += ${EOL === '\n' ? `"\\n"` : `"\\r\\n"`}`)
}

test.group('Fixtures', () => {
Expand Down

0 comments on commit 48ed63c

Please sign in to comment.