Skip to content

Preserve content when pasting multiple HTML tables - #128

Open
cyberianin wants to merge 2 commits into
github:mainfrom
cyberianin:fix/multiple-table-paste-data-loss
Open

cyberianin wants to merge 2 commits into
github:mainfrom
cyberianin:fix/multiple-table-paste-data-loss

Conversation

@cyberianin

@cyberianin cyberianin commented Oct 27, 2026 •

Copy link
Copy Markdown

Problem

When clipboard HTML contains two tables, generateText() takes the source before the first <table, converts only the first parsed table, and resumes after the last </table>. This removes the intervening content and the second table. The same function handles paste and drop.

Fix

For multiple, nested, or ambiguously bounded tables, accept text/plain only when a conservative check establishes that simple paragraphs and independent tables match the Markdown blocks in order. Otherwise, preserve the original HTML string in the editor. Fragments containing skipped tables are left to the normal paste/drop chain. The existing single-table conversion path remains.

Minimal reproduction

Clipboard text/html:

<p>Before</p><table><tr><th>First</th></tr><tr><td>One</td></tr></table><p>Between</p><table><tr><th>Second</th></tr><tr><td>Two</td></tr></table><p>After</p>

Clipboard text/plain:

Before

| First |
| --- |
| One |

Between

| Second |
| --- |
| Two |

After

Previously the table handler removed Between and the second table. With this change, it preserves the complete verified Markdown; if the plain representation is absent, incomplete, or ambiguous, it preserves the original HTML instead.

Review and production testing

Following review and additional testing against GitHub's production editor, this PR has been narrowed to the multi-table content preservation fix. Earlier changes to unrelated paste/drop handlers have been reverted from the final diff. The follow-up commit also addresses the four review findings concerning skipped tables, indented Markdown, CRLF line endings, and ordered-list markers.

Production A/B testing showed that the original table handler loses intermediate content and a subsequent table in a complex paste, while the revised implementation preserves both tables and their structure. The testing also exposed a separate, pre-existing duplicate insertion behavior involving competing paste handlers. That issue is deliberately left out of this PR and will be investigated in a follow-up change. Unrelated Markdown Preview rendering limitations are likewise outside this PR's scope.

Verification and limitations

  • npm test: 44 passed, 0 failed.
  • npm run lint, npm run build, and git diff --check: passed.
  • Regression coverage includes complex tables with intervening content for paste/drop, incomplete and reordered plaintext, skipped tables, indentation, CRLF, and ordered-list markers.
  • Synthetic skipped-table tests verify that the table handler does not prevent the event; later handlers and native insertion are separate.
  • HTML fallback preserves source content, but does not guarantee equivalent rendering in GitHub Flavored Markdown. The plaintext check is conservative and is not a general HTML-to-Markdown converter.

@cyberianin
cyberianin marked this pull request as ready for review October 27, 2026 21:03
@cyberianin
cyberianin requested a review from a team as a code owner October 27, 2026 21:03
@cyberianin

Copy link
Copy Markdown
Author

Hi maintainers, a gentle follow-up on this PR. The "Build" workflow is still showing "action_required" and appears to be waiting for approval to run for a first-time external contributor. Could someone approve the workflow run when convenient?

The patch passed local verification ("npm test": 60 passed; "npm run lint" and "npm run build": passed). I'm happy to address any review feedback. Thanks!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Paste validation and fallback still mishandle indented tables, skipped layout tables, CRLF text, and literal numbered paragraphs, causing incorrect rendering or paste behavior.

4 open issues
What changed in this PR

Updates table handling in the paste-markdown pipeline to preserve content during paste and drop when clipboard HTML contains multiple tables. It uses verified Markdown when possible and otherwise preserves the original HTML source.

Changes:

  • Adds conservative plaintext validation and HTML fallbacks for complex or ambiguous table fragments.
  • Makes later handlers respect defaultPrevented to avoid duplicate insertion.
  • Expands regression coverage for content preservation, malformed tables, and event handling.
File Description
test/​test.js Adds paste/drop regressions and makes synthetic paste events cancelable.
src/​paste-markdown-text.ts Skips paste events already handled elsewhere.
src/​paste-markdown-table.ts Adds complete-plaintext validation, source-preserving fallbacks, and event guards.
src/​paste-markdown-link.ts Avoids processing already-handled paste events.
src/​paste-markdown-image-link.ts Respects handled paste and drop events.
src/​paste-markdown-html.ts Avoids processing already-handled paste events.

Review effort: Max

Comment thread src/paste-markdown-table.ts Resolved
Comment thread src/paste-markdown-table.ts Resolved
Comment thread src/paste-markdown-table.ts Resolved
Comment thread src/paste-markdown-table.ts Resolved
@cyberianin

Copy link
Copy Markdown
Author

Additional context on the scope of this PR:

The initial implementation passed its local regression tests, lint, and build checks. Subsequent A/B testing against GitHub's production editor provided additional coverage of interactions between paste handlers.

These tests confirmed that the multi-table content loss is addressed by this PR. They also reproduced a separate, pre-existing duplicate insertion issue in the unmodified implementation, involving multiple handlers processing the same paste operation.

To keep the fix focused and independently reviewable, the follow-up commit removes changes to unrelated handlers while retaining the verified multi-table correction and incorporating the review feedback.

The duplicate insertion behavior will be investigated separately, with dedicated reproduction cases and tests for handler interactions. It is not considered resolved by this PR.

This branch has not been deployed

No deployments
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