Skip to content

Commit

Permalink
Bail early in createUpgradedEmbedBlock for invalid block types (#15885)
Browse files Browse the repository at this point in the history
* Bail early in createUpgradedEmbedBlock if block type does not exists

* Add changelog entry

* Add tests
  • Loading branch information
swissspidy authored May 31, 2019
1 parent 42afdbf commit 1d2d1fc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Master

- Fixed an issue with creating upgraded embed blocks that are not registered ([#15883](https://github.com/WordPress/gutenberg/issues/15883)).

## 2.5.0 (2019-05-21)

- Add vertical alignment controls to Columns Block ([#13899](https://github.com/WordPress/gutenberg/pull/13899/)).
Expand Down
32 changes: 31 additions & 1 deletion packages/block-library/src/embed/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
*/
import { render } from 'enzyme';

/**
* WordPress dependencies
*/
import { registerBlockType, unregisterBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { getEmbedEditComponent } from '../edit';
import { findBlock, getClassNames } from '../util';
import { findBlock, getClassNames, createUpgradedEmbedBlock } from '../util';

describe( 'core/embed', () => {
test( 'block edit matches snapshot', () => {
Expand Down Expand Up @@ -44,4 +49,29 @@ describe( 'core/embed', () => {
const expected = 'lovely';
expect( getClassNames( html, 'lovely wp-embed-aspect-16-9 wp-has-aspect-ratio', false ) ).toEqual( expected );
} );

test( 'createUpgradedEmbedBlock bails early when block type does not exist', () => {
const youtubeURL = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';

expect( createUpgradedEmbedBlock( { attributes: { url: youtubeURL } }, {} ) ).toBeUndefined();
} );

test( 'createUpgradedEmbedBlock returns a YouTube embed block when given a YouTube URL', () => {
const youtubeURL = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';

registerBlockType(
'core-embed/youtube',
{
title: 'YouTube',
category: 'embed',
}
);

const result = createUpgradedEmbedBlock( { attributes: { url: youtubeURL } }, {} );

unregisterBlockType( 'core-embed/youtube' );

expect( result ).not.toBeUndefined();
expect( result.name ).toBe( 'core-embed/youtube' );
} );
} );
6 changes: 5 additions & 1 deletion packages/block-library/src/embed/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import memoize from 'memize';
* WordPress dependencies
*/
import { renderToString } from '@wordpress/element';
import { createBlock } from '@wordpress/blocks';
import { createBlock, getBlockType } from '@wordpress/blocks';

/**
* Returns true if any of the regular expressions match the URL.
Expand Down Expand Up @@ -82,6 +82,10 @@ export const createUpgradedEmbedBlock = ( props, attributesFromPreview ) => {

const matchingBlock = findBlock( url );

if ( ! getBlockType( matchingBlock ) ) {
return;
}

// WordPress blocks can work on multiple sites, and so don't have patterns,
// so if we're in a WordPress block, assume the user has chosen it for a WordPress URL.
if ( WORDPRESS_EMBED_BLOCK !== name && DEFAULT_EMBED_BLOCK !== matchingBlock ) {
Expand Down

0 comments on commit 1d2d1fc

Please sign in to comment.