Skip to content
Merged
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/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fixes

- Shortcode: Only offer a transform in the block switcher when the shortcode meets the target block's conditions, so Gallery is no longer offered for `[gallery]` without `ids` ([#87063](https://github.com/WordPress/gutenberg/pull/87063)).

## 12.2.0 (2026-11-18)

### Enhancements
Expand Down
13 changes: 10 additions & 3 deletions packages/block-library/src/shortcode/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ const getShortcodeFromTransforms = () =>

// Single-shortcode only: keeps the transform menu honest (one block in, one
// block out) and avoids unmatched shortcodes silently falling back to freeform.
const isSingleShortcode = ( text, tag ) => {
// The target's own `isMatch` must pass too (e.g. Gallery needs `ids`), as it
// does on paste; otherwise the transform falls back to a Shortcode block.
const isSingleShortcode = ( text, tag, isMatch ) => {
const trimmed = text.trim();
const match = next( tag, trimmed );
return (
!! match && match.index === 0 && match.content.length === trimmed.length
!! match &&
match.index === 0 &&
match.content.length === trimmed.length &&
( ! isMatch || isMatch( match.shortcode.attrs ) )
);
};

Expand Down Expand Up @@ -53,7 +58,9 @@ const transforms = {
isMatch: ( { text = '' } ) => {
return []
.concat( fromTransform.tag )
.some( ( tag ) => isSingleShortcode( text, tag ) );
.some( ( tag ) =>
isSingleShortcode( text, tag, fromTransform.isMatch )
);
},
transform: ( { text = '' } ) => {
return rawHandler( { HTML: `<p>${ text.trim() }</p>` } );
Expand Down
22 changes: 22 additions & 0 deletions test/e2e/specs/editor/blocks/shortcode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@
] );
} );

test( 'should not offer a transform when the shortcode does not meet the block conditions', async ( {

Check notice on line 55 in test/e2e/specs/editor/blocks/shortcode.spec.js

View workflow job for this annotation

GitHub Actions / Run new e2e tests on trunk

This test fails on trunk, a good sign that it catches the bug.
editor,
page,
} ) => {
// The Gallery block only converts a gallery shortcode that has `ids`.
await editor.insertBlock( {
name: 'core/shortcode',
attributes: { text: '[gallery]' },
} );
await editor.clickBlockToolbarButton( 'Shortcode' );

const transformations = page
.getByRole( 'menu', { name: 'Shortcode' } )
.getByRole( 'menuitem' );
await expect( transformations ).toHaveCount( 3 );
await expect( transformations ).toHaveText( [
'Columns',
'Details',
'Group',
] );
} );

test( 'should not offer transforms when the text contains more than one shortcode', async ( {
editor,
page,
Expand Down
Loading