Skip to content

Commit

Permalink
Block versioning: Introduce a migration function
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jan 3, 2018
1 parent b82e4c0 commit c79bbcb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
14 changes: 10 additions & 4 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,16 @@ export function getAttributesFromDeprecatedVersion( blockType, innerHTML, attrib
...omit( blockType, [ 'attributes', 'save', 'supports' ] ), // Parsing/Serialization properties
...blockType.deprecated[ i ],
};
const deprecatedBlockAttributes = getBlockAttributes( deprecatedBlockType, innerHTML, attributes );
const isValid = isValidBlock( innerHTML, deprecatedBlockType, deprecatedBlockAttributes );
if ( isValid ) {
return deprecatedBlockAttributes;

try {
const deprecatedBlockAttributes = getBlockAttributes( deprecatedBlockType, innerHTML, attributes );
const migratedBlockAttributes = deprecatedBlockType.migrate ? deprecatedBlockType.migrate( deprecatedBlockAttributes ) : deprecatedBlockAttributes;
const isValid = isValidBlock( innerHTML, deprecatedBlockType, deprecatedBlockAttributes );
if ( isValid ) {
return migratedBlockAttributes;
}
} catch ( error ) {
// ignore error, it means this deprecated version is invalid
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion blocks/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ describe( 'block parser', () => {
},
},
save: ( { attributes } ) => <span>{ attributes.fruit }</span>,
migrate: ( attributes ) => ( { fruit: 'Big ' + attributes.fruit } ),
},
],
} );
Expand All @@ -314,7 +315,7 @@ describe( 'block parser', () => {
{ fruit: 'Bananas' }
);
expect( block.name ).toEqual( 'core/test-block' );
expect( block.attributes ).toEqual( { fruit: 'Bananas' } );
expect( block.attributes ).toEqual( { fruit: 'Big Bananas' } );
expect( block.isValid ).toBe( true );
expectFailingBlockValidation();
} );
Expand Down

0 comments on commit c79bbcb

Please sign in to comment.