Skip to content

[FIX] clipboard: fix copy-paste from Excel #6124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 18.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/helpers/clipboard/clipboard_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ export function parseOSClipboardContent(content: OSClipboardContent): ParsedOSCl
content[ClipboardMIMEType.Html],
"text/html"
);
const oSheetClipboardData = htmlDocument
.querySelector("div")
?.getAttribute("data-osheet-clipboard");
const spreadsheetContent = oSheetClipboardData && JSON.parse(oSheetClipboardData);
return {
text: content[ClipboardMIMEType.PlainText],
data: spreadsheetContent,
data: getOSheetDataFromHTML(htmlDocument),
};
}

function getOSheetDataFromHTML(htmlDocument: Document) {
if (htmlDocument.body.children.length !== 1) {
return undefined;
}
const oSheetClipboardData =
htmlDocument.body.firstElementChild?.getAttribute("data-osheet-clipboard");
return oSheetClipboardData && JSON.parse(oSheetClipboardData);
}
24 changes: 24 additions & 0 deletions tests/clipboard/clipboard_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2932,6 +2932,30 @@ describe("cross spreadsheet copy/paste", () => {
expect(getCell(modelA, "A1")?.content).toBe(escapableString);
expect(getCell(modelB, "D2")?.content).toBe(escapableString);
});

test("wrongly placed o-spreadsheet data in the clipboard is ignored", () => {
const modelA = new Model();
const modelB = new Model();

setCellContent(modelA, "A1", "oldContent");
copy(modelA, "A1");
const clipboardContent = modelA.getters.getClipboardContent();
const oldHTML = clipboardContent["text/html"];

let content = parseOSClipboardContent({
"text/html": `<html>${oldHTML}<body><div>randomContent</div></body></html>`,
"text/plain": "newContent",
});
pasteFromOSClipboard(modelB, "D2", content);
expect(getCellContent(modelB, "D2")).toBe("newContent");

content = parseOSClipboardContent({
"text/html": `<html><body>${oldHTML}<div>ThatsNotWhatWeGenerate</div></body></html>`,
"text/plain": "newContent2",
});
pasteFromOSClipboard(modelB, "D2", content);
expect(getCellContent(modelB, "D2")).toBe("newContent2");
});
});

test("Can use clipboard handlers to paste in a sheet other than the active sheet", () => {
Expand Down