Skip to content

Commit

Permalink
fix: #3073 parsing quotes inside a string
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Oct 11, 2023
1 parent 7b9defb commit ac9052d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fix #3025: improve handling of matrices and error handling
in function `corr` (#3030). Thanks @vrushaket.
- Fix #3074: improve error message when using function `max` in `derivative`.
- Fix #3073: fix parsing quotes inside a string.


# 2023-09-20, 11.11.1
Expand Down
12 changes: 9 additions & 3 deletions src/expression/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,10 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
if (currentCharacter(state) === '\\') {
// escape character, immediately process the next
// character to prevent stopping at a next '\"'
str += currentCharacter(state)
const cNext = nextCharacter(state)
if (cNext !== "'") {
str += currentCharacter(state)
}
next(state)
}

Expand Down Expand Up @@ -1517,7 +1520,10 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
if (currentCharacter(state) === '\\') {
// escape character, immediately process the next
// character to prevent stopping at a next '\''
str += currentCharacter(state)
const cNext = nextCharacter(state)
if (cNext !== "'" && cNext !== '"') {
str += currentCharacter(state)
}
next(state)
}

Expand All @@ -1531,7 +1537,7 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
}
getToken(state)

return JSON.parse('"' + str + '"') // unescape escaped characters
return JSON.parse('"' + str.replace(/"/g, '\\"') + '"') // unescape escaped characters
}

/**
Expand Down
28 changes: 27 additions & 1 deletion test/unit-tests/expression/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,19 @@ describe('parse', function () {
assert.deepStrictEqual(parseAndEval(' "hi" '), 'hi')
})

it('should parse a string containing quotes', function () {
// quote
assert.deepStrictEqual(parseAndEval('"with\'quote"'), "with'quote")

// escaped quote -> remove escape character
assert.deepStrictEqual(parseAndEval('"with\\"quote"'), 'with"quote')
assert.deepStrictEqual(parseAndEval('"with\\\'quote"'), "with'quote")

// escaped escape character -> remove two escape characters
assert.deepStrictEqual(parseAndEval('"with\\\\\\"quote"'), 'with\\"quote')
assert.deepStrictEqual(parseAndEval('"with\\\\\'quote"'), "with\\'quote")
})

it('should parse a with escaped characters', function () {
assert.deepStrictEqual(parseAndEval('"line end\\nnext"'), 'line end\nnext')
assert.deepStrictEqual(parseAndEval('"line end\\n"'), 'line end\n')
Expand Down Expand Up @@ -420,7 +433,20 @@ describe('parse', function () {
assert.deepStrictEqual(parseAndEval(' \'hi\' '), 'hi')
})

it('should parse a with escaped characters', function () {
it('should parse a string containing quotes', function () {
// quote
assert.deepStrictEqual(parseAndEval("'with\"quote'"), 'with"quote')

// escaped quote -> remove escape character
assert.deepStrictEqual(parseAndEval("'with\\'quote'"), "with'quote")
assert.deepStrictEqual(parseAndEval("'with\\\"quote'"), 'with"quote')

// escaped escape character -> remove two escape characters
assert.deepStrictEqual(parseAndEval("'with\\\\\\'quote'"), "with\\'quote")
assert.deepStrictEqual(parseAndEval("'with\\\\\"quote'"), 'with\\"quote')
})

it('should parse a string with escaped characters', function () {
assert.deepStrictEqual(parseAndEval('\'line end\\nnext\''), 'line end\nnext')
assert.deepStrictEqual(parseAndEval('\'line end\\n\''), 'line end\n')
assert.deepStrictEqual(parseAndEval('\'tab\\tnext\''), 'tab\tnext')
Expand Down

0 comments on commit ac9052d

Please sign in to comment.