Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix($parse): allow for new lines in expr when promise unwrapping is on #4718

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,10 @@ function getterFn(path, options, fullExp) {
: '((k&&k.hasOwnProperty("' + key + '"))?k:s)') + '["' + key + '"]' + ';\n' +
(options.unwrapPromises
? 'if (s && s.then) {\n' +
' pw("' + fullExp.replace(/\"/g, '\\"') + '");\n' +
' pw("' +
fullExp.replace(/\"/g, '\\"').replace(/[\r\n]/g, function (lineEnding) {
return lineEnding === '\n' ? '\\n' : '\\r';
}) + '");\n' +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be sufficient to do just:

' pw("' + fullExp.replace(/(["\r\n])/g, '\\$1') + '");\n' +

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, this isn't 100% equivalent

// $1 way
new Function('return "' + "a\nb".replace(/(["\r\n])/g, '\\$1') + '"')() === "ab"; // lost the new line

// function way
new Function('return "' + "a\nb".replace(/[\r\n]/g, function (lineEnding) {
  return lineEnding === '\n' ? '\\n' : '\\r';
}) + '"')() === "a\nb"

So the $1 way will remove any \n and \r from the expression that will be outputted for the warning (hence avoiding the initial error)

Is that okay? it's not that important of a bug imo and can be ignored.

' if (!("$$v" in s)) {\n' +
' p=s;\n' +
' p.$$v = undefined;\n' +
Expand Down
20 changes: 15 additions & 5 deletions test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('parser', function() {
});

it('should tokenize function invocation', function() {
var tokens = lex("a()")
var tokens = lex("a()");
expect(map(tokens, function(t) { return t.text;})).toEqual(['a', '(', ')']);
});

Expand Down Expand Up @@ -199,16 +199,20 @@ describe('parser', function() {
}]));


forEach([true, false], function(cspEnabled) {
forEach([[true, true],[!0, !1],[!1, !0],[false, false]], function(options /* [csp, unwrapPromises] */) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is super hard to read. using two nested forEach loops would make the code much more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went for the nested forEach approach at first, but I reverted to this because the nested approach would create a huge unnecessary diff (cz of the added indentation) that I wanted to avoid. Didn't want to be blamed for all of the tests, too late now :p

var cspEnabled = options[0], unwrapPromisesEnabled = options[1];

describe('csp ' + cspEnabled, function() {
describe('csp ' + cspEnabled + ", unwrapPromises " + unwrapPromisesEnabled, function() {

beforeEach(module(function ($parseProvider) {
$parseProvider.unwrapPromises(unwrapPromisesEnabled);
}));

beforeEach(inject(function ($rootScope, $sniffer) {
scope = $rootScope;
$sniffer.csp = cspEnabled;
}));


it('should parse expressions', function() {
expect(scope.$eval("-1")).toEqual(-1);
expect(scope.$eval("1 + 2.5")).toEqual(3.5);
Expand Down Expand Up @@ -590,6 +594,12 @@ describe('parser', function() {
expect(scope.$eval('bool.toString()')).toBe('false');
});

it('should evaluate expressions with line terminators', function() {
scope.a = "a";
scope.b = {c: "bc"};
expect(scope.$eval('a + \n b.c + \r "\td" + \t \r\n\r "\r\n\n"')).toEqual("abc\td\r\n\n");
});

describe('sandboxing', function() {
describe('Function constructor', function() {
it('should NOT allow access to Function constructor in getter', function() {
Expand Down Expand Up @@ -768,7 +778,7 @@ describe('parser', function() {
// User defined value assigned to constructor.
scope.foo.constructor = function constructor() {
return "custom constructor";
}
};
// Dot operator should still block it.
expect(function() {
scope.$eval('foo.constructor()', scope)
Expand Down