Skip to content
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
4 changes: 4 additions & 0 deletions packages/blocks/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Add support for the text shadow style property ([#79584](https://github.com/WordPress/gutenberg/pull/79584)).

### Enhancements

- Paste handling: Unwrap tables used purely for layout (every row has exactly one cell), such as those found in HTML emails, so their content pastes as normal blocks instead of a Table block. Nested layout tables are unwrapped one level at a time, so a real data table nested inside them is preserved ([#83131](https://github.com/WordPress/gutenberg/pull/83131)).

## 16.0.0 (2026-09-10)

### Breaking Changes
Expand Down
2 changes: 2 additions & 0 deletions packages/blocks/src/api/raw-handling/paste-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import listReducer from './list-reducer';
import imageCorrector from './image-corrector';
import blockquoteNormaliser from './blockquote-normaliser';
import divNormaliser from './div-normaliser';
import tableNormaliser from './table-normaliser';
import figureContentReducer from './figure-content-reducer';
import shortcodeConverter from './shortcode-converter';
import markdownConverter from './markdown-converter';
Expand Down Expand Up @@ -210,6 +211,7 @@ export function pasteHandler( {
figureContentReducer,
blockquoteNormaliser(),
divNormaliser,
tableNormaliser,
headingTransformer,
];

Expand Down
52 changes: 52 additions & 0 deletions packages/blocks/src/api/raw-handling/table-normaliser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import normaliseBlocks from './normalise-blocks';

/**
* Unwraps tables that are only being used for layout, such as those found in
* HTML emails, so their content is pasted as normal blocks instead of a
* Table block. A table is considered layout-only when every one of its rows
* has exactly one cell, since a genuine data table needs at least two cells
* in some row to express a relationship between them.
*
* Nested layout tables (a common pattern for cross-client email styling) are
* unwrapped one level at a time as this filter runs on each table from the
* innermost to the outermost, so a real data table nested several layout
* tables deep is promoted all the way up while being left untouched itself.
*
* @see https://github.com/WordPress/gutenberg/issues/4171
*
* @param node The node to normalise, if it is a `<table>`.
*/
export default function tableNormaliser( node: Node ): void {
if ( node.nodeName !== 'TABLE' ) {
return;
}

const table = node as HTMLTableElement;
const rows = Array.from( table.rows );

if ( ! rows.length ) {
return;
}

const cellsHTML: string[] = [];

for ( const row of rows ) {
if ( row.cells.length !== 1 ) {
// A row with more than one cell means this table conveys a real
// row/column relationship, so leave it as a Table block.
return;
}

cellsHTML.push( row.cells[ 0 ].innerHTML );
}

const doc = table.ownerDocument;
const wrapper = doc.createElement( 'div' );
// Each row is normalised on its own, and only then joined, so that plain
// text in adjacent rows doesn't get merged into a single paragraph.
wrapper.innerHTML = cellsHTML
.map( ( cellHTML ) => normaliseBlocks( cellHTML ) )
.join( '' );

table.replaceWith( ...Array.from( wrapper.childNodes ) );
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { pasteHandler, serialize } from '@wordpress/blocks';
import { init as initAndRegisterImageBlock } from '../../../../../block-library/src/image';
import { init as initAndRegisterTableBlock } from '../../../../../block-library/src/table';
import { init as initAndRegisterVideoBlock } from '../../../../../block-library/src/video';
import { init as initAndRegisterParagraphBlock } from '../../../../../block-library/src/paragraph';
import { init as initAndRegisterMissingBlock } from '../../../../../block-library/src/missing';

const tableWithHeaderFooterAndBodyUsingColspan = `
<table>
Expand Down Expand Up @@ -81,6 +83,8 @@ describe( 'pasteHandler', () => {
beforeAll( () => {
initAndRegisterTableBlock();
initAndRegisterVideoBlock();
initAndRegisterParagraphBlock();
initAndRegisterMissingBlock();
} );

it( 'can handle a table with thead, tbody and tfoot using colspan', () => {
Expand Down Expand Up @@ -259,6 +263,38 @@ describe( 'pasteHandler', () => {
} );
} );

it( 'unwraps a single-cell layout table (e.g. from an HTML email) into a paragraph instead of a Table block', () => {
const result = pasteHandler( {
HTML: '<table><tr><td><p>Hello from an email</p></td></tr></table>',
tagName: 'p',
} );

expect( console ).toHaveLogged();

expect( result ).toHaveLength( 1 );
expect( result[ 0 ].name ).toEqual( 'core/paragraph' );
expect( result[ 0 ].attributes.content.toString() ).toEqual(
'Hello from an email'
);
} );

it( 'unwraps nested single-column layout tables while still converting a real inner data table', () => {
const result = pasteHandler( {
HTML:
'<table><tr><td>' +
'<table><tr><td><p>Intro</p></td></tr></table>' +
tableWithCellAlignments +
'</td></tr></table>',
tagName: 'p',
} );

expect( console ).toHaveLogged();
expect( result.map( ( block ) => block.name ) ).toEqual( [
'core/paragraph',
'core/table',
] );
} );

it( 'preserves <img> wrapped in <a> when source is plain text only', () => {
const result = pasteHandler( {
HTML: '',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { describe, expect, it } from 'vitest';
import tableNormaliser from '../table-normaliser';
import { deepFilterHTML } from '../utils';

describe( 'tableNormaliser', () => {
it( 'unwraps a single-cell table into its cell content', () => {
const input = '<table><tr><td>Hello there</td></tr></table>';

expect( deepFilterHTML( input, [ tableNormaliser ] ) ).toEqual(
'<p>Hello there</p>'
);
} );

it( 'unwraps a single-column table, joining each row in order', () => {
const input =
'<table>' +
'<tr><td>First</td></tr>' +
'<tr><td>Second</td></tr>' +
'</table>';

expect( deepFilterHTML( input, [ tableNormaliser ] ) ).toEqual(
'<p>First</p><p>Second</p>'
);
} );

it( 'leaves a table with a multi-cell row untouched', () => {
const input = '<table><tr><td>A</td><td>B</td></tr></table>';

// The browser wraps bare <tr> elements in a <tbody> when parsing,
// independently of the normaliser.
expect( deepFilterHTML( input, [ tableNormaliser ] ) ).toEqual(
'<table><tbody><tr><td>A</td><td>B</td></tr></tbody></table>'
);
} );

it( 'leaves untouched a single-column table where only one row has more than one cell', () => {
const input =
'<table>' +
'<tr><td>Header spanning</td></tr>' +
'<tr><td>A</td><td>B</td></tr>' +
'</table>';

expect( deepFilterHTML( input, [ tableNormaliser ] ) ).toEqual(
'<table><tbody><tr><td>Header spanning</td></tr><tr><td>A</td><td>B</td></tr></tbody></table>'
);
} );

it( 'leaves an empty table untouched', () => {
const input = '<table></table>';

expect( deepFilterHTML( input, [ tableNormaliser ] ) ).toEqual( input );
} );

it( 'unwraps nested single-cell layout tables while preserving a real inner data table', () => {
const input =
'<table><tr><td>' +
'<table><tr><td>' +
'<table><tr><td>A</td><td>B</td></tr><tr><td>C</td><td>D</td></tr></table>' +
'</td></tr></table>' +
'</td></tr></table>';

expect( deepFilterHTML( input, [ tableNormaliser ] ) ).toEqual(
'<table><tbody><tr><td>A</td><td>B</td></tr><tr><td>C</td><td>D</td></tr></tbody></table>'
);
} );
} );
Loading