Skip to content

Commit 5710bc2

Browse files
committed
fix char escape in expressions (fix #1929)
1 parent b7e83e6 commit 5710bc2

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

src/parsers/directive.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const reservedArgRE = /^in$|^-?\d+/
1010
*/
1111

1212
var str, dir
13-
var c, i, l, lastFilterIndex
13+
var c, prev, i, l, lastFilterIndex
1414
var inSingle, inDouble, curly, square, paren
1515

1616
/**
@@ -90,13 +90,14 @@ export function parseDirective (s) {
9090
dir = {}
9191

9292
for (i = 0, l = str.length; i < l; i++) {
93+
prev = c
9394
c = str.charCodeAt(i)
9495
if (inSingle) {
9596
// check single quote
96-
if (c === 0x27) inSingle = !inSingle
97+
if (c === 0x27 && prev !== 0x5C) inSingle = !inSingle
9798
} else if (inDouble) {
9899
// check double quote
99-
if (c === 0x22) inDouble = !inDouble
100+
if (c === 0x22 && prev !== 0x5C) inDouble = !inDouble
100101
} else if (
101102
c === 0x7C && // pipe
102103
str.charCodeAt(i + 1) !== 0x7C &&

src/parsers/expression.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ const improperKeywordsRE =
2323

2424
const wsRE = /\s/g
2525
const newlineRE = /\n/g
26-
const saveRE = /[\{,]\s*[\w\$_]+\s*:|('[^']*'|"[^"]*")|new |typeof |void /g
26+
const saveRE = /[\{,]\s*[\w\$_]+\s*:|('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")|new |typeof |void /g
2727
const restoreRE = /"(\d+)"/g
28-
const pathTestRE = /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*|\['.*?'\]|\[".*?"\]|\[\d+\]|\[[A-Za-z_$][\w$]*\])*$/
29-
const pathReplaceRE = /[^\w$\.]([A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*|\['.*?'\]|\[".*?"\])*)/g
30-
const booleanLiteralRE = /^(true|false)$/
28+
const pathTestRE = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['.*?'\]|\[".*?"\]|\[\d+\]|\[[A-Za-z_$][\w$]*\])*$/
29+
const pathReplaceRE = /[^\w$\.](?:[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['.*?'\]|\[".*?"\])*)/g
30+
const booleanLiteralRE = /^(?:true|false)$/
3131

3232
/**
3333
* Save / Rewrite / Restore

test/unit/specs/parsers/directive_spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ describe('Directive Parser', function () {
7878
expect(res.filters[0].args).toBeUndefined()
7979
})
8080

81+
it('escape string', function () {
82+
var res = parse("'a\\'b' | test")
83+
expect(res.expression).toBe("'a\\'b'")
84+
expect(res.filters.length).toBe(1)
85+
expect(res.filters[0].name).toBe('test')
86+
expect(res.filters[0].args).toBeUndefined()
87+
})
88+
8189
it('cache', function () {
8290
var res1 = parse('a || b | c')
8391
var res2 = parse('a || b | c')

test/unit/specs/parsers/expression_spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ var testCases = [
172172
expected: 8,
173173
paths: ['$a', 'b', 'c', 'e']
174174
},
175+
{
176+
// string with escaped quotes
177+
exp: "'a\\'b' + c",
178+
scope: {
179+
c: '\'c'
180+
},
181+
expected: "a\'b\'c",
182+
paths: ['c']
183+
},
175184
{
176185
// Math global, simple path
177186
exp: 'Math.PI',

0 commit comments

Comments
 (0)