Skip to content

Preserve literal quotes in view-mode paste#437

Open
jabelic wants to merge 1 commit intoiddan:masterfrom
jabelic:fix/paste-literal-quotes
Open

Preserve literal quotes in view-mode paste#437
jabelic wants to merge 1 commit intoiddan:masterfrom
jabelic:fix/paste-literal-quotes

Conversation

@jabelic
Copy link

@jabelic jabelic commented Mar 24, 2026

Thanks for maintaining react-spreadsheet and for taking a look at this bug fix.

Summary

  • preserve literal quotes when view-mode paste receives simple quoted fields such as "aa"
  • keep legacy behavior for quoted numeric single values such as "123"
  • limit the change to the paste path by moving the policy into src/paste.ts
  • leave src/matrix.ts untouched to avoid broad parser behavior changes

Why

In the current behavior, view-mode paste can lose user data when a simple quoted field is pasted.

For example:

  • pasting "aa"\ta turns the first cell into aa
  • real-world multi-row TSV data with a first column like "aa" loses the literal quotes in that column

This is a data-loss issue in the paste path, not a generic Matrix.split() issue. To keep the scope conservative, this change only adjusts the clipboard interpretation used by paste.

Behavior changes

Before:

  • "Value" pasted as a single cell became Value
  • "123" pasted as a single cell became 123
  • "aa"\ta lost quotes in the first cell and became aa, a
  • "Value\t1"\tValue2 was interpreted by the existing parser and unquoted the first field

After:

  • "Value" pasted as a single cell stays "Value"
  • "123" pasted as a single cell still becomes 123 to preserve legacy compatibility
  • "aa"\ta keeps the first cell as "aa" and the second cell as a
  • "Value\t1"\tValue2 still unquotes the first field because the quotes are protecting a tab separator

Scope

  • changed files: src/paste.ts, src/reducer.ts, src/paste.test.ts, src/reducer.test.ts, src/Spreadsheet.test.tsx
  • no Matrix.split() changes
  • no attempt to make Matrix.split() a complete TSV/CSV parser in this PR

Compatibility notes

  • this PR intentionally preserves the old behavior for quoted numeric single values such as "123"
  • the new behavior is limited to the internal paste policy and does not change the public matrix parsing API

Move paste-specific quote handling into a dedicated parser so simple quoted fields no longer lose quote characters during paste. Keep legacy handling for quoted numeric single values while covering the new behavior with reducer, parser, and UI tests.

Made-with: Cursor
@iddan
Copy link
Owner

iddan commented Mar 25, 2026

Thank you for your contribution! Does this match the behaviour on Google Sheets?

@jabelic
Copy link
Author

jabelic commented Mar 25, 2026

Thank you for your contribution! Does this match the behaviour on Google Sheets?

@iddan

Thanks! Not completely.

The main behavior I aligned with Google Sheets here is that simple quoted text fields like "aa" keep their quotes on paste, including multi-cell TSV pastes. Quoted fields that protect separators, such as "Value\t1"\tValue2, are also still unquoted as expected.

One intentional compatibility exception in this PR is quoted numeric single values like "123": this PR keeps the existing unquoting behavior there, while Google Sheets keeps the quotes. I kept that exception to reduce compatibility risk, but if you'd prefer stricter Google Sheets parity, I'm happy to update that behavior.

@iddan
Copy link
Owner

iddan commented Mar 25, 2026

Yes, I prefer full parity as much as possible - thank you so much!!!

@jabelic
Copy link
Author

jabelic commented Mar 25, 2026

@iddan
Thanks, that makes sense.

I think preserving quoted numeric single values like \"123\" is a separate behavior change from the data-loss cases fixed in this PR, so I prepared it as a small follow-up on top of this branch: jabelic#1.

That follow-up removes the quoted-numeric compatibility exception so single-cell and multi-cell quoted fields behave consistently and match Google Sheets more closely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants