Skip to content

Commit

Permalink
[Port] properly escape slashes in constant strings with StringExpress…
Browse files Browse the repository at this point in the history
…ion/ValueExpression (#2660)

* escape slashes in constantr strings

* add semicolon

Co-authored-by: Dong Lei <donglei@microsoft.com>
  • Loading branch information
feich-ms and boydc2014 authored Aug 7, 2020
1 parent c83fcf8 commit a8b0dc9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
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');
});
});

0 comments on commit a8b0dc9

Please sign in to comment.