Skip to content

Commit

Permalink
[FIX] cell: keep non-breaking spaces
Browse files Browse the repository at this point in the history
The non-breaking spaces were removed when updating a cell. This caused
issues is odoo pivots, since the formula contained a group name with
the non-breaking space removed, but the data was still using the
non-breaking space.

closes #5342

Task: 4403607
Signed-off-by: Rémi Rahir (rar) <[email protected]>
  • Loading branch information
hokolomopo committed Dec 16, 2024
1 parent f535004 commit 38c2a08
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/formulas/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 1 addition & 5 deletions src/plugins/core/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -452,9 +450,7 @@ export class CellPlugin extends CorePlugin<CoreState> 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;
Expand Down
4 changes: 4 additions & 0 deletions tests/formulas/tokenizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,8 @@ describe("tokenizer", () => {
{ type: "NUMBER", value: "4" },
]);
});

test("Space characters", () => {
expect(tokenize(" \u00A0 \u00A0")).toEqual([{ type: "SPACE", value: " \u00A0 \u00A0" }]);
});
});
6 changes: 6 additions & 0 deletions tests/plugins/cell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 38c2a08

Please sign in to comment.