diff --git a/src/formulas/tokenizer.ts b/src/formulas/tokenizer.ts index 130707484f..694c09623d 100644 --- a/src/formulas/tokenizer.ts +++ b/src/formulas/tokenizer.ts @@ -199,14 +199,13 @@ function tokenizeSymbol(chars: string[]): Token | null { const whiteSpaceRegexp = /\s/; function tokenizeSpace(chars: string[]): Token | null { - let length = 0; + let spaces = ""; while (chars[0] && chars[0].match(whiteSpaceRegexp)) { - length++; - chars.shift(); + spaces += chars.shift(); } - if (length) { - return { type: "SPACE", value: " ".repeat(length) }; + if (spaces) { + return { type: "SPACE", value: spaces }; } return null; } diff --git a/src/plugins/core/cell.ts b/src/plugins/core/cell.ts index ff3661335d..457409d966 100644 --- a/src/plugins/core/cell.ts +++ b/src/plugins/core/cell.ts @@ -34,8 +34,6 @@ import { } from "../../types/index"; import { CorePlugin } from "../core_plugin"; -const nbspRegexp = new RegExp(String.fromCharCode(160), "g"); - const LINK_STYLE = { textColor: LINK_COLOR }; interface CoreState { @@ -452,9 +450,7 @@ export class CellPlugin extends CorePlugin implements CoreState { const hasContent = "content" in after || "formula" in after; // Compute the new cell properties - const afterContent = hasContent - ? after.content?.replace(nbspRegexp, "") || "" - : before?.content || ""; + const afterContent = (hasContent ? after.content : before?.content) || ""; let style: Style | undefined; if (after.style !== undefined) { style = after.style || undefined; diff --git a/tests/formulas/tokenizer.test.ts b/tests/formulas/tokenizer.test.ts index 45ac00b81e..cd8f14d01c 100644 --- a/tests/formulas/tokenizer.test.ts +++ b/tests/formulas/tokenizer.test.ts @@ -253,4 +253,8 @@ describe("tokenizer", () => { { type: "NUMBER", value: "4" }, ]); }); + + test("Space characters", () => { + expect(tokenize(" \u00A0 \u00A0")).toEqual([{ type: "SPACE", value: " \u00A0 \u00A0" }]); + }); }); diff --git a/tests/plugins/cell.test.ts b/tests/plugins/cell.test.ts index a5c1f78a0f..362cf231bc 100644 --- a/tests/plugins/cell.test.ts +++ b/tests/plugins/cell.test.ts @@ -189,6 +189,12 @@ describe("getCellText", () => { setCellContent(model, "A1", '="hello \\"world\\""'); expect(getCell(model, "A1")?.formattedValue).toBe('hello "world"'); }); + + test("Non breaking spaces are kept on cell insertion", () => { + const model = new Model(); + setCellContent(model, "A1", "hello\u00A0world"); + expect(getCellText(model, "A1")).toBe("hello\u00A0world"); + }); }); describe("link cell", () => {