Skip to content

Commit

Permalink
Convert   to regular spaces in decodeHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
chkn committed Nov 8, 2023
1 parent 0ea52f3 commit b2ae12c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/core/src/data-editor/data-editor-fns.ts
Expand Up @@ -173,6 +173,11 @@ export function unquote(str: string): string[][] {
return result;
}

function normalizeHTMLString(input: string): string {
// Convert   to regular spaces
return input.replace(/\u00A0/g, ' ');
}

export function decodeHTML(tableEl: HTMLTableElement): string[][] | undefined {
const walkEl: Element[] = [tableEl];
const result: string[][] = [];
Expand All @@ -192,7 +197,7 @@ export function decodeHTML(tableEl: HTMLTableElement): string[][] | undefined {
current = [];
walkEl.push(...[...el.children].reverse());
} else if (el instanceof HTMLTableCellElement) {
current?.push(el.innerText ?? el.textContent ?? "");
current?.push(normalizeHTMLString(el.innerText ?? el.textContent ?? ""));
}
}

Expand Down
21 changes: 21 additions & 0 deletions packages/core/test/data-editor-fns.test.ts
Expand Up @@ -25,6 +25,27 @@ describe("data-editor-fns", () => {
]);
});

test("decode html converts   characters to regular spaces", () => {
const root = document.createElement("table");
root.innerHTML = `
<tbody>
<tr>
<td>hi&nbsp;mom!</td>
</tr>
<tr>
<td>&nbsp; &nbsp;</td>
</tr>
</tbody>
`;

const decoded = decodeHTML(root);

expect(decoded).toEqual([
["hi mom!"],
[" "]
]);
});

test("format empty bubble cell", () => {
expect(
formatCell(
Expand Down

0 comments on commit b2ae12c

Please sign in to comment.