|
6 | 6 | var ngEventAttributes = require('../lib/event-directives'),
|
7 | 7 | MODULE_NAME = 'Events';
|
8 | 8 |
|
| 9 | +/* |
| 10 | + * Remove string expressions except property accessors. |
| 11 | + * ex. abc["def"] = "gef"; // `removeStringExp` will remove "gef" but not "def". |
| 12 | + */ |
| 13 | +function removeStringExp(str) { |
| 14 | + return str.replace(/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/g, |
| 15 | + function(match, pos, full) { |
| 16 | + // this is our lookaround code so that our regex doesn't become so |
| 17 | + // complicated. |
| 18 | + if (pos !== 0 && (match.length + pos) !== full.length && |
| 19 | + full[pos - 1] === '[' && full[pos + match.length] === ']') { |
| 20 | + return match; |
| 21 | + } |
| 22 | + return ''; |
| 23 | + }); |
| 24 | +} |
9 | 25 |
|
10 | 26 | var getFunctionNames = function(str) {
|
11 | 27 | if (typeof str !== 'string') {
|
12 | 28 | return [];
|
13 | 29 | }
|
14 |
| - var results = str.replace(/\s+/g, '').split(/[\+\-\/\|\<\>\^=&!%~;]/g).map(function(x) { |
15 |
| - if (isNaN(+x)) { |
16 |
| - if (x.match(/\w+\(.*\)$/)){ |
17 |
| - return x.substr(0, x.indexOf('(')); |
| 30 | + // There are still a bunch of corner cases here where we aren't going to be able to handle |
| 31 | + // but we shouldn't break the user's app and we should handle most common cases. |
| 32 | + // example of corner cases: we can't check for properties inside of function |
| 33 | + // arguments like `move(a.b.c)` with the current implementation |
| 34 | + // or property accessors with parentheses in them |
| 35 | + // like `prop["hello (world)"] = "test";`. |
| 36 | + // To fully fix these issues we would need a full blown expression parser. |
| 37 | + var results = removeStringExp(str.replace(/\s+/g, '')) |
| 38 | + .replace(/\(.*?\)/g, '') |
| 39 | + .split(/[\+\-\/\|\<\>\^=&!%~;]/g).map(function(x) { |
| 40 | + if (isNaN(+x)) { |
| 41 | + if (x.match(/\w+\(.*\)$/)){ |
| 42 | + return x.substr(0, x.indexOf('(')); |
| 43 | + } |
| 44 | + return x; |
18 | 45 | }
|
19 |
| - return x; |
20 |
| - } |
21 | 46 | }).filter(function(x){
|
22 | 47 | return x;
|
23 | 48 | });
|
|
0 commit comments