Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Port] properly escape slashes in constant strings with StringExpression/ValueExpression #2660

Merged
merged 4 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class StringExpression extends ExpressionProperty<string> {
}

// Initialize value
this.expressionText = `=\`${ value }\``;
this.expressionText = `=\`${ value.replace(/\\/g, '\\\\') }\``;
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class ValueExpression extends ExpressionProperty<any> {
}

// keep the string as quoted expression, which will be literal unless string interpolation is used.
this.expressionText = `=\`${ value }\``;
this.expressionText = `=\`${ value.replace(/\\/g, '\\\\') }\``;
return;
}

Expand Down
20 changes: 20 additions & 0 deletions libraries/adaptive-expressions/tests/expressionProperty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ describe('expressionProperty tests', () => {
result = str.getValue(data);
assert.equal(result, 'joe');
assert.equal(str.toExpression().toString(), 'test');

// slashes are the chars
str = new StringExpression('c:\\test\\test\\test');
result = str.getValue(data);
assert.equal(result, 'c:\\test\\test\\test');

// tabs are the chars
str = new StringExpression('c:\test\test\test');
result = str.getValue(data);
assert.equal(result, 'c:\test\test\test');
});

it('ValueExpression', () => {
Expand Down Expand Up @@ -195,5 +205,15 @@ describe('expressionProperty tests', () => {
result = val.getValue(data);
assert.equal(result, undefined);
assert.equal(val.toExpression().toString(), 'null');

// slashes are the chars
val = new ValueExpression('c:\\test\\test\\test');
result = val.getValue(data);
assert.equal(result, 'c:\\test\\test\\test');

// tabs are the chars
val = new ValueExpression('c:\test\test\test');
result = val.getValue(data);
assert.equal(result, 'c:\test\test\test');
});
});