diff --git a/packages/block-library/src/image/index.js b/packages/block-library/src/image/index.js index 92b9af7ec9a42c..ace6b55f31636b 100644 --- a/packages/block-library/src/image/index.js +++ b/packages/block-library/src/image/index.js @@ -123,6 +123,25 @@ function getFirstAnchorAttributeFormHTML( html, attributeName ) { } } +export function stripFirstImage( attributes, { shortcode } ) { + const { body } = document.implementation.createHTMLDocument( '' ); + + body.innerHTML = shortcode.content; + + let nodeToRemove = body.querySelector( 'img' ); + + // if an image has parents, find the topmost node to remove + while ( nodeToRemove && nodeToRemove.parentNode && nodeToRemove.parentNode !== body ) { + nodeToRemove = nodeToRemove.parentNode; + } + + if ( nodeToRemove ) { + nodeToRemove.parentNode.removeChild( nodeToRemove ); + } + + return body.innerHTML.trim(); +} + export const settings = { title: __( 'Image' ), @@ -196,14 +215,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 } ) => { diff --git a/packages/block-library/src/image/test/index.js b/packages/block-library/src/image/test/index.js new file mode 100644 index 00000000000000..5b21dcefdf6ebd --- /dev/null +++ b/packages/block-library/src/image/test/index.js @@ -0,0 +1,32 @@ +/** + * 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: 'Tucson' } } ) ).toEqual( 'Tucson' ); + } ); + + test( 'should strip out image when leading as expected', () => { + expect( stripFirstImage( {}, { shortcode: { content: '' } } ) ).toEqual( '' ); + expect( stripFirstImage( {}, { shortcode: { content: 'Image!' } } ) ).toEqual( 'Image!' ); + expect( stripFirstImage( {}, { shortcode: { content: 'Image!' } } ) ).toEqual( 'Image!' ); + } ); + + test( 'should strip out image when not in leading position as expected', () => { + expect( stripFirstImage( {}, { shortcode: { content: 'Before' } } ) ).toEqual( 'Before' ); + expect( stripFirstImage( {}, { shortcode: { content: 'BeforeImage!' } } ) ).toEqual( 'BeforeImage!' ); + expect( stripFirstImage( {}, { shortcode: { content: 'BeforeImage!' } } ) ).toEqual( 'BeforeImage!' ); + } ); + + test( 'should strip out only the first of many images', () => { + expect( stripFirstImage( {}, { shortcode: { content: '' } } ) ).toEqual( '' ); + } ); + + test( 'should strip out the first image and its wrapping parents', () => { + expect( stripFirstImage( {}, { shortcode: { content: '

' } } ) ).toEqual( '

' ); + } ); +} );