diff --git a/packages/block-library/src/image/transforms.js b/packages/block-library/src/image/transforms.js
index 0119009b2182c4..2a56e0e7e210a8 100644
--- a/packages/block-library/src/image/transforms.js
+++ b/packages/block-library/src/image/transforms.js
@@ -41,7 +41,7 @@ function getFirstAnchorAttributeFormHTML( html, attributeName ) {
const imageSchema = {
img: {
- attributes: [ 'src', 'alt', 'title' ],
+ attributes: [ 'src', 'alt', 'title', 'width', 'height' ],
classes: [
'alignleft',
'aligncenter',
@@ -52,6 +52,14 @@ const imageSchema = {
},
};
+// Read a pixel dimension from an `
` HTML attribute and normalise it to
+// the `px` form the Image block stores in its `width` / `height`
+// attributes. Non-integer values (e.g. `50%`) are dropped because the block
+// attribute round-trips through inline styles that expect pixel units.
+function parsePixelDimension( value ) {
+ return value && /^\d+$/.test( value ) ? `${ value }px` : undefined;
+}
+
const schema = ( { phrasingContentSchema } ) => ( {
figure: {
require: [ 'img' ],
@@ -77,12 +85,10 @@ const transforms = {
node.nodeName === 'FIGURE' && !! node.querySelector( 'img' ),
schema,
transform: ( node ) => {
+ const img = node.querySelector( 'img' );
// Search both figure and image classes. Alignment could be
// set on either. ID is set on the image.
- const className =
- node.className +
- ' ' +
- node.querySelector( 'img' ).className;
+ const className = node.className + ' ' + img.className;
const alignMatches =
/(?:^|\s)align(left|center|right)(?:$|\s)/.exec(
className
@@ -108,6 +114,14 @@ const transforms = {
anchorElement && anchorElement.className
? anchorElement.className
: undefined;
+ // Preserve explicit pixel dimensions set on the source `
`
+ // (e.g. legacy "Add Media" output: `
`).
+ const width = parsePixelDimension(
+ img.getAttribute( 'width' )
+ );
+ const height = parsePixelDimension(
+ img.getAttribute( 'height' )
+ );
const attributes = getBlockAttributes(
'core/image',
node.outerHTML,
@@ -119,6 +133,8 @@ const transforms = {
rel,
linkClass,
anchor,
+ width,
+ height,
}
);
diff --git a/packages/blocks/src/api/raw-handling/test/paste-handler.js b/packages/blocks/src/api/raw-handling/test/paste-handler.js
index 5b5aa8bc517ab4..c7df4e68852ced 100644
--- a/packages/blocks/src/api/raw-handling/test/paste-handler.js
+++ b/packages/blocks/src/api/raw-handling/test/paste-handler.js
@@ -5,6 +5,7 @@ import { pasteHandler } from '@wordpress/blocks';
/**
* Internal dependencies
*/
+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';
@@ -288,3 +289,62 @@ describe( 'pasteHandler', () => {
expect( result.isValid ).toBeTruthy();
} );
} );
+
+describe( 'pasteHandler — core/image', () => {
+ beforeAll( () => {
+ initAndRegisterImageBlock();
+ } );
+
+ it( 'preserves explicit pixel width and height from a bare
', () => {
+ const [ result ] = pasteHandler( {
+ HTML: '
',
+ mode: 'BLOCKS',
+ } );
+
+ expect( console ).toHaveLogged();
+
+ expect( result.name ).toBe( 'core/image' );
+ expect( result.attributes.url ).toBe( 'https://example.com/i.jpg' );
+ expect( result.attributes.width ).toBe( '77px' );
+ expect( result.attributes.height ).toBe( '77px' );
+ } );
+
+ it( 'preserves explicit pixel width and height from an
inside a ', () => {
+ const [ result ] = pasteHandler( {
+ HTML: '
',
+ mode: 'BLOCKS',
+ } );
+
+ expect( console ).toHaveLogged();
+
+ expect( result.name ).toBe( 'core/image' );
+ expect( result.attributes.width ).toBe( '120px' );
+ expect( result.attributes.height ).toBe( '80px' );
+ } );
+
+ it( 'drops non-pixel width/height (e.g. percentages)', () => {
+ const [ result ] = pasteHandler( {
+ HTML: '
',
+ mode: 'BLOCKS',
+ } );
+
+ expect( console ).toHaveLogged();
+
+ expect( result.name ).toBe( 'core/image' );
+ expect( result.attributes.width ).toBeUndefined();
+ expect( result.attributes.height ).toBeUndefined();
+ } );
+
+ it( 'leaves width/height unset when not present on the source', () => {
+ const [ result ] = pasteHandler( {
+ HTML: '
',
+ mode: 'BLOCKS',
+ } );
+
+ expect( console ).toHaveLogged();
+
+ expect( result.name ).toBe( 'core/image' );
+ expect( result.attributes.width ).toBeUndefined();
+ expect( result.attributes.height ).toBeUndefined();
+ } );
+} );
diff --git a/test/integration/fixtures/documents/google-docs-out.html b/test/integration/fixtures/documents/google-docs-out.html
index 3ee7d25875abc6..f98994713593f2 100644
--- a/test/integration/fixtures/documents/google-docs-out.html
+++ b/test/integration/fixtures/documents/google-docs-out.html
@@ -54,6 +54,6 @@ This is a heading
An image:
-
-
+
+
diff --git a/test/integration/fixtures/documents/google-docs-with-comments-out.html b/test/integration/fixtures/documents/google-docs-with-comments-out.html
index 3ee7d25875abc6..f98994713593f2 100644
--- a/test/integration/fixtures/documents/google-docs-with-comments-out.html
+++ b/test/integration/fixtures/documents/google-docs-with-comments-out.html
@@ -54,6 +54,6 @@ This is a heading
An image:
-
-
+
+
diff --git a/test/integration/fixtures/documents/ms-word-out.html b/test/integration/fixtures/documents/ms-word-out.html
index 81b82b5f9502fe..b66fd7eb0fcdb6 100644
--- a/test/integration/fixtures/documents/ms-word-out.html
+++ b/test/integration/fixtures/documents/ms-word-out.html
@@ -58,8 +58,8 @@ This is a heading level 2
An image:
-
-
+
+
diff --git a/test/integration/fixtures/documents/one-image-out.html b/test/integration/fixtures/documents/one-image-out.html
index defa0138370faf..e03b9d4b7edd5e 100644
--- a/test/integration/fixtures/documents/one-image-out.html
+++ b/test/integration/fixtures/documents/one-image-out.html
@@ -1,3 +1,3 @@
-
-
+
+
diff --git a/test/integration/fixtures/documents/two-images-out.html b/test/integration/fixtures/documents/two-images-out.html
index 4b96a052a52b7b..af52a0b91caf17 100644
--- a/test/integration/fixtures/documents/two-images-out.html
+++ b/test/integration/fixtures/documents/two-images-out.html
@@ -1,7 +1,7 @@
-
-
+
+
-
-
+
+