Skip to content

Commit

Permalink
[FIX] formatting: do not show escape character
Browse files Browse the repository at this point in the history
If you want to use double quotes in a string which is part of a formula, you
have to escape it using a backslash (="hello \"world\"")
However, the backslash is shown when displaying the cell content.

closes #3606

Task: 3698283
X-original-commit: b157972
Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
Signed-off-by: Pierre Rousseau (pro) <pro@odoo.com>
  • Loading branch information
LucasLefevre committed Feb 5, 2024
1 parent 1f22a5a commit 0becf1d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/helpers/format.ts
Expand Up @@ -126,6 +126,9 @@ export function formatValue(value: CellValue, { format, locale }: LocaleFormat):
}
switch (typeof value) {
case "string":
if (value.includes('\\"')) {
return value.replace(/\\"/g, '"');
}
return value;
case "boolean":
return value ? "TRUE" : "FALSE";
Expand Down
6 changes: 6 additions & 0 deletions tests/cells/cell_plugin.test.ts
Expand Up @@ -110,6 +110,12 @@ describe("getCellText", () => {
const result = clearCell(model, "A1");
expect(result).toBeCancelledBecause(CommandResult.NoChanges);
});

test("escape character is not display when formatting string", () => {
const model = new Model();
setCellContent(model, "A1", '="hello \\"world\\""');
expect(getEvaluatedCell(model, "A1")?.formattedValue).toBe('hello "world"');
});
});

describe("link cell", () => {
Expand Down
12 changes: 12 additions & 0 deletions tests/formats/format_helpers.test.ts
Expand Up @@ -10,6 +10,18 @@ import { FR_LOCALE } from "../test_helpers/constants";

const locale = DEFAULT_LOCALE;

describe("formatValue on string", () => {
test("apply on regular strings", () => {
expect(formatValue("", { locale })).toBe("");
expect(formatValue("test", { locale })).toBe("test");
expect(formatValue("test", { locale, format: "#,###.0" })).toBe("test");
});

test("apply on strings with escape characters", () => {
expect(formatValue('Hello \\"world\\"', { locale })).toBe('Hello "world"');
});
});

describe("formatValue on number", () => {
test("apply default format ", () => {
expect(formatValue(1, { locale })).toBe("1");
Expand Down

0 comments on commit 0becf1d

Please sign in to comment.