Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Pasting captions without an image fails #14365

Merged
merged 2 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 14 additions & 8 deletions packages/block-library/src/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ function getFirstAnchorAttributeFormHTML( html, attributeName ) {
}
}

export function stripFirstImage( attributes, { shortcode } ) {
const { body } = document.implementation.createHTMLDocument( '' );

body.innerHTML = shortcode.content;

const firstImage = body.querySelector( 'img' );
if ( firstImage ) {
firstImage.parentNode.removeChild( firstImage );
}

return body.innerHTML.trim();
}

export const settings = {
title: __( 'Image' ),

Expand Down Expand Up @@ -196,14 +209,7 @@ export const settings = {
selector: 'img',
},
caption: {
shortcode: ( attributes, { shortcode } ) => {
const { body } = document.implementation.createHTMLDocument( '' );

body.innerHTML = shortcode.content;
body.removeChild( body.firstElementChild );

return body.innerHTML.trim();
},
shortcode: stripFirstImage,
},
href: {
shortcode: ( attributes, { shortcode } ) => {
Expand Down
28 changes: 28 additions & 0 deletions packages/block-library/src/image/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Internal dependencies
*/
import { stripFirstImage } from '../';

describe( 'stripFirstImage', () => {
test( 'should do nothing if no image is present', () => {
expect( stripFirstImage( {}, { shortcode: { content: '' } } ) ).toEqual( '' );
expect( stripFirstImage( {}, { shortcode: { content: 'Tucson' } } ) ).toEqual( 'Tucson' );
expect( stripFirstImage( {}, { shortcode: { content: '<em>Tucson</em>' } } ) ).toEqual( '<em>Tucson</em>' );
} );

test( 'should strip out image when leading as expected', () => {
expect( stripFirstImage( {}, { shortcode: { content: '<img>' } } ) ).toEqual( '' );
expect( stripFirstImage( {}, { shortcode: { content: '<img>Image!' } } ) ).toEqual( 'Image!' );
expect( stripFirstImage( {}, { shortcode: { content: '<img src="image.png">Image!' } } ) ).toEqual( 'Image!' );
} );

test( 'should strip out image when not in leading position as expected', () => {
expect( stripFirstImage( {}, { shortcode: { content: 'Before<img>' } } ) ).toEqual( 'Before' );
expect( stripFirstImage( {}, { shortcode: { content: 'Before<img>Image!' } } ) ).toEqual( 'BeforeImage!' );
expect( stripFirstImage( {}, { shortcode: { content: 'Before<img src="image.png">Image!' } } ) ).toEqual( 'BeforeImage!' );
} );

test( 'should strip out only the first of many images', () => {
expect( stripFirstImage( {}, { shortcode: { content: '<img><img>' } } ) ).toEqual( '<img>' );
} );
} );