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
26 changes: 21 additions & 5 deletions packages/block-library/src/image/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function getFirstAnchorAttributeFormHTML( html, attributeName ) {

const imageSchema = {
img: {
attributes: [ 'src', 'alt', 'title' ],
attributes: [ 'src', 'alt', 'title', 'width', 'height' ],
classes: [
'alignleft',
'aligncenter',
Expand All @@ -52,6 +52,14 @@ const imageSchema = {
},
};

// Read a pixel dimension from an `<img>` HTML attribute and normalise it to
// the `<value>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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there're any perf benefits, but something like - ! Number.isNaN( Number( '50%' ) ); could also be used for the check.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I preferred the regex solution because Number() accepts floats, negatives, empty strings, and exponents, all of which we'd then have to filter out. Happy to revisit if you'd prefer a numeric-style check, though.

}

const schema = ( { phrasingContentSchema } ) => ( {
figure: {
require: [ 'img' ],
Expand All @@ -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
Expand All @@ -108,6 +114,14 @@ const transforms = {
anchorElement && anchorElement.className
? anchorElement.className
: undefined;
// Preserve explicit pixel dimensions set on the source `<img>`
// (e.g. legacy "Add Media" output: `<img width="77" height="77">`).
const width = parsePixelDimension(
img.getAttribute( 'width' )
);
const height = parsePixelDimension(
img.getAttribute( 'height' )
);
const attributes = getBlockAttributes(
'core/image',
node.outerHTML,
Expand All @@ -119,6 +133,8 @@ const transforms = {
rel,
linkClass,
anchor,
width,
height,
}
);

Expand Down
60 changes: 60 additions & 0 deletions packages/blocks/src/api/raw-handling/test/paste-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 <img>', () => {
const [ result ] = pasteHandler( {
HTML: '<img src="https://example.com/i.jpg" width="77" height="77" />',
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 <img> inside a <figure>', () => {
const [ result ] = pasteHandler( {
HTML: '<figure><img src="https://example.com/i.jpg" width="120" height="80" /></figure>',
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: '<img src="https://example.com/i.jpg" width="50%" height="auto" />',
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: '<img src="https://example.com/i.jpg" />',
mode: 'BLOCKS',
} );

expect( console ).toHaveLogged();

expect( result.name ).toBe( 'core/image' );
expect( result.attributes.width ).toBeUndefined();
expect( result.attributes.height ).toBeUndefined();
} );
} );
4 changes: 2 additions & 2 deletions test/integration/fixtures/documents/google-docs-out.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ <h2 class="wp-block-heading">This is a <em>heading</em></h2>
<p>An image:</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://lh4.googleusercontent.com/ID" alt=""/></figure>
<!-- wp:image {"width":"544px","height":"184px"} -->
<figure class="wp-block-image is-resized"><img src="https://lh4.googleusercontent.com/ID" alt="" style="width:544px;height:184px"/></figure>
<!-- /wp:image -->
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ <h2 class="wp-block-heading">This is a <em>heading</em></h2>
<p>An image:</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://lh4.googleusercontent.com/ID" alt=""/></figure>
<!-- wp:image {"width":"544px","height":"184px"} -->
<figure class="wp-block-image is-resized"><img src="https://lh4.googleusercontent.com/ID" alt="" style="width:544px;height:184px"/></figure>
<!-- /wp:image -->
4 changes: 2 additions & 2 deletions test/integration/fixtures/documents/ms-word-out.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ <h2 class="wp-block-heading">This is a heading level 2</h2>
<p>An image:</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="" alt=""/></figure>
<!-- wp:image {"width":"451px","height":"338px"} -->
<figure class="wp-block-image is-resized"><img src="" alt="" style="width:451px;height:338px"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
Expand Down
4 changes: 2 additions & 2 deletions test/integration/fixtures/documents/one-image-out.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:image {"id":5114} -->
<figure class="wp-block-image"><img src="http://localhost/wp-content/uploads/2018/01/Dec-08-2017-15-12-24-17-300x137.gif" alt="" class="wp-image-5114"/></figure>
<!-- wp:image {"id":5114,"width":"300px","height":"137px"} -->
<figure class="wp-block-image is-resized"><img src="http://localhost/wp-content/uploads/2018/01/Dec-08-2017-15-12-24-17-300x137.gif" alt="" class="wp-image-5114" style="width:300px;height:137px"/></figure>
<!-- /wp:image -->
8 changes: 4 additions & 4 deletions test/integration/fixtures/documents/two-images-out.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- wp:image {"id":5114} -->
<figure class="wp-block-image"><img src="http://localhost/wp-content/uploads/2018/01/Dec-08-2017-15-12-24-17-300x137.gif" alt="" class="wp-image-5114"/></figure>
<!-- wp:image {"id":5114,"width":"300px","height":"137px"} -->
<figure class="wp-block-image is-resized"><img src="http://localhost/wp-content/uploads/2018/01/Dec-08-2017-15-12-24-17-300x137.gif" alt="" class="wp-image-5114" style="width:300px;height:137px"/></figure>
<!-- /wp:image -->

<!-- wp:image {"id":5109} -->
<figure class="wp-block-image"><img src="http://localhost/wp-content/uploads/2018/01/Dec-05-2017-17-52-09-9-300x248.gif" alt="" class="wp-image-5109"/></figure>
<!-- wp:image {"id":5109,"width":"300px","height":"248px"} -->
<figure class="wp-block-image is-resized"><img src="http://localhost/wp-content/uploads/2018/01/Dec-05-2017-17-52-09-9-300x248.gif" alt="" class="wp-image-5109" style="width:300px;height:248px"/></figure>
<!-- /wp:image -->
Loading