mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
fix: #3073 parsing quotes inside a string
This commit is contained in:
parent
7b9defb9ff
commit
ac9052d3e8
@ -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
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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')
|
||||
@ -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')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user