Skip to content

Commit 54cff6a

Browse files
committed
Add missing default attributes and fix unit tests
1 parent 50d2ab8 commit 54cff6a

File tree

8 files changed

+99
-13
lines changed

8 files changed

+99
-13
lines changed

packages/block-library/src/gallery/transforms.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,24 @@ const transforms = {
225225
},
226226
},
227227
},
228-
__experimentalTransform( { named: { ids } } ) {
228+
__experimentalTransform( { named: { ids, columns = 3, link } } ) {
229229
const imageIds = parseShortcodeIds( ids ).map( ( id ) =>
230230
parseInt( id, 10 )
231231
);
232232

233+
let linkTo = LINK_DESTINATION_NONE;
234+
if ( link === 'post' ) {
235+
linkTo = LINK_DESTINATION_ATTACHMENT;
236+
} else if ( link === 'file' ) {
237+
linkTo = LINK_DESTINATION_MEDIA;
238+
}
239+
233240
const galleryBlock = createBlock(
234241
'core/gallery',
235-
{},
242+
{
243+
columns: parseInt( columns, 10 ),
244+
linkTo,
245+
},
236246
imageIds.map( ( imageId ) =>
237247
createBlock( 'core/image', { id: imageId } )
238248
)

packages/blocks/src/api/raw-handling/shortcode-converter.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ function segmentHTMLToShortcodeBlock(
8383
] );
8484
}
8585

86-
const blockType = getBlockType( transformation.blockName );
86+
let blockType = getBlockType( transformation.blockName );
87+
if ( ! blockType ) {
88+
return [ HTML ];
89+
}
90+
blockType = { ...blockType, attributes: transformation.attributes };
91+
8792
let block;
8893
if ( typeof transformation.__experimentalTransform === 'function' ) {
8994
// Passing all of `match` as second argument is intentionally broad
@@ -107,8 +112,6 @@ function segmentHTMLToShortcodeBlock(
107112
( schema ) => schema.shortcode( match.shortcode.attrs, match )
108113
);
109114

110-
blockType.attributes = transformation.attributes;
111-
112115
block = createBlock(
113116
transformation.blockName,
114117
getBlockAttributes(
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"cookies": [
3+
{
4+
"name": "wordpress_test_cookie",
5+
"value": "WP%20Cookie%20check",
6+
"domain": "localhost",
7+
"path": "/",
8+
"expires": -1,
9+
"httpOnly": false,
10+
"secure": false,
11+
"sameSite": "Lax"
12+
},
13+
{
14+
"name": "wordpress_23778236db82f19306f247e20a353a99",
15+
"value": "admin%7C1656743669%7Cn8X4s8e686lxWiyIPPwhzhvf6CmD3FMuniyFJWK4pY6%7Ce8a2c602892c31c70a7d2ddd9deb788823e5014f02e3d66f52b55ab9ff5271c8",
16+
"domain": "localhost",
17+
"path": "/wp-content/plugins",
18+
"expires": -1,
19+
"httpOnly": true,
20+
"secure": false,
21+
"sameSite": "Lax"
22+
},
23+
{
24+
"name": "wordpress_23778236db82f19306f247e20a353a99",
25+
"value": "admin%7C1656743669%7Cn8X4s8e686lxWiyIPPwhzhvf6CmD3FMuniyFJWK4pY6%7Ce8a2c602892c31c70a7d2ddd9deb788823e5014f02e3d66f52b55ab9ff5271c8",
26+
"domain": "localhost",
27+
"path": "/wp-admin",
28+
"expires": -1,
29+
"httpOnly": true,
30+
"secure": false,
31+
"sameSite": "Lax"
32+
},
33+
{
34+
"name": "wordpress_logged_in_23778236db82f19306f247e20a353a99",
35+
"value": "admin%7C1656743669%7Cn8X4s8e686lxWiyIPPwhzhvf6CmD3FMuniyFJWK4pY6%7Cfa6901d987e7f698eb6315b5b1c37c2caf6a644f4cb2a08998dd5edc40c96f43",
36+
"domain": "localhost",
37+
"path": "/",
38+
"expires": -1,
39+
"httpOnly": true,
40+
"secure": false,
41+
"sameSite": "Lax"
42+
},
43+
{
44+
"name": "wp-settings-1",
45+
"value": "libraryContent%3Dupload",
46+
"domain": "localhost",
47+
"path": "/",
48+
"expires": 1688106872.725,
49+
"httpOnly": false,
50+
"secure": false,
51+
"sameSite": "Lax"
52+
},
53+
{
54+
"name": "wp-settings-time-1",
55+
"value": "1656570872",
56+
"domain": "localhost",
57+
"path": "/",
58+
"expires": 1688106872.725,
59+
"httpOnly": false,
60+
"secure": false,
61+
"sameSite": "Lax"
62+
}
63+
],
64+
"nonce": "858eb73108",
65+
"rootURL": "http://localhost:8889/index.php?rest_route=/"
66+
}

test/e2e/specs/editor/blocks/gallery.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ test.describe( 'Gallery', () => {
5353
await expect( img ).toBeVisible();
5454

5555
const editedPostContent = await editor.getEditedPostContent();
56-
expect( editedPostContent ).toBe( `<!-- wp:gallery {"linkTo":"none"} -->
57-
<figure class="wp-block-gallery has-nested-images columns-default is-cropped"><!-- wp:image {"id":${ uploadedMedia.id },"sizeSlug":"large","linkDestination":"none"} -->
56+
expect( editedPostContent )
57+
.toBe( `<!-- wp:gallery {"columns":3,"linkTo":"none"} -->
58+
<figure class="wp-block-gallery has-nested-images columns-3 is-cropped"><!-- wp:image {"id":${ uploadedMedia.id },"sizeSlug":"large","linkDestination":"none"} -->
5859
<figure class="wp-block-image size-large"><img src="${ uploadedMedia.source_url }" alt="${ uploadedMedia.alt_text }" class="wp-image-${ uploadedMedia.id }"/></figure>
5960
<!-- /wp:image --></figure>
6061
<!-- /wp:gallery -->` );

test/integration/__snapshots__/blocks-raw-handling.test.js.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ exports[`rawHandler should convert HTML post to blocks with minimal content chan
5757
<h3>Shortcode</h3>
5858
<!-- /wp:heading -->
5959
60-
<!-- wp:gallery {\\"shortCodeTransforms\\":[{\\"id\\":1}],\\"columns\\":3,\\"linkTo\\":\\"none\\"} -->
61-
<figure class=\\"wp-block-gallery has-nested-images columns-3 is-cropped\\"></figure>
60+
<!-- wp:gallery {\\"columns\\":3,\\"linkTo\\":\\"none\\"} -->
61+
<figure class=\\"wp-block-gallery has-nested-images columns-3 is-cropped\\"><!-- wp:image {\\"id\\":1} -->
62+
<figure class=\\"wp-block-image\\"><img alt=\\"\\" class=\\"wp-image-1\\"/></figure>
63+
<!-- /wp:image --></figure>
6264
<!-- /wp:gallery -->
6365
6466
<!-- wp:html -->
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<!-- wp:test/gallery {"ids":[40,41,42]} /-->
22

3-
<!-- wp:gallery {"shortCodeTransforms":[{"id":1000}],"columns":3,"linkTo":"none"} -->
4-
<figure class="wp-block-gallery has-nested-images columns-3 is-cropped"></figure>
3+
<!-- wp:gallery {"columns":3,"linkTo":"none"} -->
4+
<figure class="wp-block-gallery has-nested-images columns-3 is-cropped"><!-- wp:image {"id":1000} -->
5+
<figure class="wp-block-image"><img alt="" class="wp-image-1000"/></figure>
6+
<!-- /wp:image --></figure>
57
<!-- /wp:gallery -->
68

79
<!-- wp:test/gallery {"ids":[42]} /-->

test/integration/fixtures/documents/wordpress-out.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ <h3>More tag</h3>
1818
<h3>Shortcode</h3>
1919
<!-- /wp:heading -->
2020

21-
<!-- wp:gallery {"shortCodeTransforms":[{"id":1}],"columns":3,"linkTo":"none"} -->
22-
<figure class="wp-block-gallery has-nested-images columns-3 is-cropped"></figure>
21+
<!-- wp:gallery {"columns":3,"linkTo":"none"} -->
22+
<figure class="wp-block-gallery has-nested-images columns-3 is-cropped"><!-- wp:image {"id":1} -->
23+
<figure class="wp-block-image"><img alt="" class="wp-image-1"/></figure>
24+
<!-- /wp:image --></figure>
2325
<!-- /wp:gallery -->
2426

2527
<!-- wp:image {"id":5114} -->

0 commit comments

Comments
 (0)