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 block validation error message #13499

Merged
merged 4 commits into from
Mar 12, 2019
Merged
Changes from 3 commits
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
20 changes: 10 additions & 10 deletions packages/blocks/src/api/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,30 +620,30 @@ export function isEquivalentHTML( actual, expected ) {
*
* Logs to console in development environments when invalid.
*
* @param {string|Object} blockTypeOrName Block type.
* @param {Object} attributes Parsed block attributes.
* @param {string} innerHTML Original block content.
* @param {string|Object} blockTypeOrName Block type.
* @param {Object} attributes Parsed block attributes.
* @param {string} originalBlockContent Original block content.
*
* @return {boolean} Whether block is valid.
*/
export function isValidBlockContent( blockTypeOrName, attributes, innerHTML ) {
export function isValidBlockContent( blockTypeOrName, attributes, originalBlockContent ) {
const blockType = normalizeBlockType( blockTypeOrName );
let saveContent;
let generatedBlockContent;
try {
saveContent = getSaveContent( blockType, attributes );
generatedBlockContent = getSaveContent( blockType, attributes );
} catch ( error ) {
log.error( 'Block validation failed because an error occurred while generating block content:\n\n%s', error.toString() );
return false;
}

const isValid = isEquivalentHTML( innerHTML, saveContent );
const isValid = isEquivalentHTML( originalBlockContent, generatedBlockContent );
if ( ! isValid ) {
log.error(
'Block validation failed for `%s` (%o).\n\nExpected:\n\n%s\n\nActual:\n\n%s',
'Block validation failed for `%s` (%o).\n\nContent generated by `save` function:\n\n%s\n\nContent retrieved from post body:\n\n%s',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There shouldn't be a concept of a "post" in this context. This logic applies for the post editor, but also any blocks parsing (e.g. upcoming widgets screen blocks editor). I realize the desire to improve messaging to developers. There may be alternatives which serve as an adequate compromise:

Markup produced by `save` function:

...

Markup parsed from content:

...

blockType.name,
blockType,
saveContent,
innerHTML
generatedBlockContent,
originalBlockContent
);
}

Expand Down