Skip to content

Commit

Permalink
Adds check for attributes in block settings (#15959)
Browse files Browse the repository at this point in the history
* Adds check for attributes in block settings

* Adds unit test

* Addresses PR feedback
  • Loading branch information
tellthemachines authored and gziolo committed Jun 4, 2019
1 parent f7cf606 commit 6d3269e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/block-editor/src/hooks/anchor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { assign } from 'lodash';
import { assign, has } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -33,6 +33,10 @@ const ANCHOR_REGEX = /[\s#]/g;
* @return {Object} Filtered block settings.
*/
export function addAttribute( settings ) {
// allow blocks to specify their own attribute definition with default values if needed.
if ( has( settings.attributes, [ 'anchor', 'type' ] ) ) {
return settings;
}
if ( hasBlockSupport( settings, 'anchor' ) ) {
// Use Lodash's assign to gracefully handle if attributes are undefined
settings.attributes = assign( settings.attributes, {
Expand Down
17 changes: 17 additions & 0 deletions packages/block-editor/src/hooks/test/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ describe( 'anchor', () => {

expect( settings.attributes ).toHaveProperty( 'anchor' );
} );

it( 'should not override attributes defined in settings', () => {
const settings = registerBlockType( {
...blockSettings,
supports: {
anchor: true,
},
attributes: {
anchor: {
type: 'string',
default: 'testAnchor',
},
},
} );

expect( settings.attributes.anchor ).toEqual( { type: 'string', default: 'testAnchor' } );
} );
} );

describe( 'addSaveProps', () => {
Expand Down

0 comments on commit 6d3269e

Please sign in to comment.