Skip to content

Paste handling: unwrap layout-only tables - #83131

Open
theaminulai wants to merge 2 commits into
WordPress:trunkfrom
theaminulai:add/paste-layout-table-unwrap
Open

theaminulai wants to merge 2 commits into
WordPress:trunkfrom
theaminulai:add/paste-layout-table-unwrap

Conversation

@theaminulai

Copy link
Copy Markdown
Member

What?

Unwraps <table> elements that are only being used for layout (every row has exactly one cell) when pasting content, so their content pastes as normal blocks instead of a Table block.

Why?

Closes #4171. HTML emails commonly use nested single-cell (or single-column) <table> elements purely for cross-client layout, since that's still the most reliable way to control layout across email clients — not to convey tabular data. Pasting that content into the editor turned every one of those wrapper tables into a Table block, exactly as described (and re-confirmed as of Gutenberg 21.1.0) in the issue, since the paste pipeline had no way to tell a layout table apart from a real one. This matches the issue's own suggested heuristic: "detect if the table only has one cell."

How?

  • New tableNormaliser filter in packages/blocks/src/api/raw-handling/table-normaliser.ts, added to the paste pipeline in paste-handler.ts next to the existing divNormaliser (a similar "unwrap a layout-only wrapper" concern).
  • A table is unwrapped when every one of its rows has exactly one cell — generalizing the issue's "single cell" suggestion to also cover the very common single-column, multi-row stacking pattern used for vertical layout in emails. A table where any row has more than one cell is left untouched, since that's a real row/column relationship worth keeping as a Table block.
  • Each row's cell content is normalised into its own blocks (via the existing normaliseBlocks) before being joined, so adjacent plain-text rows become separate paragraphs instead of merging into one.
  • Since the paste pipeline's filters run bottom-up (deepest nodes first), nested layout tables — another common email pattern — are unwrapped one level at a time. A genuine data table nested several layout tables deep is promoted all the way up while being left untouched itself, and still becomes a Table block.
  • Deliberately not implemented: the issue's second, fuzzier suggestion ("check if the cell has any block level HTML content"). A legitimate data table cell can contain a paragraph or list without the table itself being layout-only, so that heuristic risks false positives; the single-cell-per-row check is the safe, unambiguous signal.

Testing Instructions

  1. Paste <table><tr><td><p>Hello</p></td></tr></table> into the editor — confirm it becomes a Paragraph block, not a Table block.
  2. Paste a multi-row single-column table (e.g. three rows, one cell each) — confirm each row becomes its own block, in order.
  3. Paste a real 2-column table (e.g. with header and data rows) — confirm it still becomes a Table block, unaffected.
  4. Paste a single-cell layout table wrapping a real 2-column table — confirm the outer wrapper disappears and the inner table still becomes a Table block.
  5. npm run test:unit:vitest -- packages/blocks/src/api/raw-handling — 134/134 passing.

HTML emails commonly use nested single-cell (or single-column) <table>
elements purely for cross-client layout, not to convey tabular data.
Pasting that content into the editor turned every one of those wrapper
tables into a Table block, since the paste pipeline had no way to tell
a layout table apart from a real one.

Adds a tableNormaliser filter to the paste pipeline that unwraps a
table when every one of its rows has exactly one cell, promoting the
cells' content (each normalised into its own blocks) in its place. A
table where any row has more than one cell is left untouched, since
that's a real row/column relationship.

The filter runs bottom-up like the rest of the paste pipeline, so
nested layout tables are unwrapped one level at a time. A genuine data
table nested several layout tables deep (also a common email pattern)
is promoted all the way up while being left untouched itself, and
still becomes a Table block.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: theaminulai <theaminuldev@git.wordpress.org>
Co-authored-by: mtias <matveb@git.wordpress.org>
Co-authored-by: designsimply <designsimply@git.wordpress.org>
Co-authored-by: karmatosed <karmatosed@git.wordpress.org>
Co-authored-by: SirLouen <sirlouen@git.wordpress.org>
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions Bot added the [Package] Blocks /packages/blocks label Sep 18, 2026
@github-actions

Copy link
Copy Markdown

🤖 PR meta 🤖

🏷️ Labels

This pull request needs exactly one label indicating its type, and has 0.

  • Required: any label starting with [Type].
  • Found: none.

Read more about Type labels in Gutenberg. If you cannot add labels yourself, a reviewer can do it for you.

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

[Package] Blocks /packages/blocks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Paste from HTML email

1 participant