Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
50 changes: 41 additions & 9 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ export function createBlockWithFallback( name, rawContent, attributes ) {
// Validate that the parsed block is valid, meaning that if we were to
// reserialize it given the assumed attributes, the markup matches the
// original value. Otherwise, preserve original to avoid destruction.
block.isValid = isValidBlock( rawContent, blockType, block.attributes );
if ( ! block.isValid ) {
try {
block.isValid = isValidBlock( rawContent, blockType, block.attributes );
} catch ( error ) {
block.isValid = false;
block.error = error;
block.originalContent = rawContent;
}

Expand Down Expand Up @@ -129,13 +132,42 @@ export function isValidBlock( rawContent, blockType, attributes ) {

const isValid = ( actual === expected );

if ( ! isValid && 'development' === process.env.NODE_ENV ) {
// eslint-disable-next-line no-console
console.error(
'Invalid block parse\n' +
'\tExpected: ' + expected + '\n' +
'\tActual: ' + actual
);
if ( ! isValid ) {
// NOTE: DO NOT COLLAPSE THESE CONDITIONS OR CONVERT COMMONJS TO ES2015
// IMPORTS. They are intentionally written as such because minification
// will drop this unreachable block in production environments.
if ( 'production' !== process.env.NODE_ENV ) {
const chalk = require( 'chalk' );
let message = 'Invalid block parse for ';

// Chalk automatically detects its environment to determine whether
// ANSI codes are supported. In these environments we provide a nicely
// colored error message for invalid parse.
//
// TODO: Teach browsers to ANSI

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

... or teach chalk to output HTML. ANSI escape sequences are quite convoluted, and it seems like what we really want is an intermediate representation that can be outputted in either form.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

... or teach chalk to output HTML. ANSI escape sequences are quite convoluted, and it seems like what we really want is an intermediate representation that can be outputted in either form.

I did run across this: https://www.npmjs.com/package/ansi_up

I'd also toyed with ideas for converting ANSI automatically to CSS-styled console.log depending on the environment.

const { diffLines } = require( 'diff' );
const diff = diffLines( actual, expected );
message += chalk.bold( blockType.name ) + ':\n\n';
message += chalk.bgGreen( '- Expected' ) + '\n' + chalk.bgRed( '+ Actual' ) + '\n\n';
message += diff.reduce( ( result, part ) => {
let { value } = part;
if ( part.removed ) {
value = value.replace( /\n$/, '' );
value = value.replace( /^/gm, '- ' );
value = chalk.red( value );
} else if ( part.added ) {
value = value.replace( /\n$/, '' );
value = value.replace( /^/gm, '+ ' );
value = '\n' + value;
value = chalk.green( value );
value += '\n';
}

return result + value;
}, '' );

throw new Error( message );
}
}

return isValid;
Expand Down
76 changes: 30 additions & 46 deletions blocks/api/test/parser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { noop } from 'lodash';

/**
* Internal dependencies
*/
Expand All @@ -26,6 +21,12 @@ describe( 'block parser', () => {
const defaultBlockSettings = {
save: ( { attributes } ) => attributes.fruit,
};
const identityBlock = {
attributes( rawContent ) {
return { content: rawContent };
},
save: ( { attributes } ) => attributes.content,
};

afterEach( () => {
setUnknownTypeHandler( undefined );
Expand Down Expand Up @@ -97,11 +98,11 @@ describe( 'block parser', () => {
it( 'returns false is block is not valid', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );

expect( isValidBlock(
expect( () => isValidBlock(
'Apples',
getBlockType( 'core/test-block' ),
{ fruit: 'Bananas' }
) ).toBe( false );
) ).toThrow();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we test a specific error message here, or save this for later when we're ready to expose this info to the UI?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is a pain to test with the ANSI escapes, unless we were to disable that for testing, or just test the pattern.

when we're ready to expose this info to the UI?

Which I'm starting to wonder, do we want the error message exposed in the UI anywhere? Currently we're only generating and throwing the error in development modes, which means it's not something generally accessible in the UI (in distributed plugin anyways).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To clarify, I think what should be shown in the UI is more like a text-mode diff and a prompt to confirm that the changes are acceptable.

} );

it( 'returns true is block is valid', () => {
Expand All @@ -117,27 +118,30 @@ describe( 'block parser', () => {

describe( 'createBlockWithFallback', () => {
it( 'should create the requested block if it exists', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );
registerBlockType( 'core/test-block', identityBlock );

const block = createBlockWithFallback(
'core/test-block',
'content',
{ attr: 'value' }
);
expect( block.name ).toEqual( 'core/test-block' );
expect( block.attributes ).toEqual( { attr: 'value' } );
expect( block.attributes ).toEqual( {
attr: 'value',
content: 'content',
} );
} );

it( 'should create the requested block with no attributes if it exists', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );
registerBlockType( 'core/test-block', identityBlock );

const block = createBlockWithFallback( 'core/test-block', 'content' );
expect( block.name ).toEqual( 'core/test-block' );
expect( block.attributes ).toEqual( {} );
expect( block.attributes ).toEqual( { content: 'content' } );
} );

it( 'should fall back to the unknown type handler for unknown blocks if present', () => {
registerBlockType( 'core/unknown-block', defaultBlockSettings );
registerBlockType( 'core/unknown-block', identityBlock );
setUnknownTypeHandler( 'core/unknown-block' );

const block = createBlockWithFallback(
Expand All @@ -146,16 +150,19 @@ describe( 'block parser', () => {
{ attr: 'value' }
);
expect( block.name ).toEqual( 'core/unknown-block' );
expect( block.attributes ).toEqual( { attr: 'value' } );
expect( block.attributes ).toEqual( {
attr: 'value',
content: 'content',
} );
} );

it( 'should fall back to the unknown type handler if block type not specified', () => {
registerBlockType( 'core/unknown-block', defaultBlockSettings );
registerBlockType( 'core/unknown-block', identityBlock );
setUnknownTypeHandler( 'core/unknown-block' );

const block = createBlockWithFallback( null, 'content' );
expect( block.name ).toEqual( 'core/unknown-block' );
expect( block.attributes ).toEqual( {} );
expect( block.attributes ).toEqual( { content: 'content' } );
} );

it( 'should not create a block if no unknown type handler', () => {
Expand All @@ -166,15 +173,7 @@ describe( 'block parser', () => {

describe( 'parse()', () => {
it( 'should parse the post content, including block attributes', () => {
registerBlockType( 'core/test-block', {
// Currently this is the only way to test block content parsing?
attributes: function( rawContent ) {
return {
content: rawContent,
};
},
save: noop,
} );
registerBlockType( 'core/test-block', identityBlock );

const parsed = parse(
'<!-- wp:core/test-block {"smoked":"yes","url":"http://google.com","chicken":"ribs & \'wings\'"} -->' +
Expand All @@ -200,17 +199,10 @@ describe( 'block parser', () => {
} );

it( 'should parse the post content, ignoring unknown blocks', () => {
registerBlockType( 'core/test-block', {
attributes: function( rawContent ) {
return {
content: rawContent + ' & Chicken',
};
},
save: noop,
} );
registerBlockType( 'core/test-block', identityBlock );

const parsed = parse(
'<!-- wp:core/test-block -->\nRibs\n<!-- /wp:core/test-block -->' +
'<!-- wp:core/test-block -->Ribs & Chicken<!-- /wp:core/test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->'
);
Expand All @@ -225,12 +217,12 @@ describe( 'block parser', () => {

it( 'should parse the post content, using unknown block handler', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );
registerBlockType( 'core/unknown-block', defaultBlockSettings );
registerBlockType( 'core/unknown-block', identityBlock );

setUnknownTypeHandler( 'core/unknown-block' );

const parsed = parse(
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<!-- wp:core/test-block {"fruit":"Bananas"} -->Bananas<!-- /wp:core/test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->'
);
Expand All @@ -245,23 +237,15 @@ describe( 'block parser', () => {

it( 'should parse the post content, including raw HTML at each end', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );
registerBlockType( 'core/unknown-block', {
// Currently this is the only way to test block content parsing?
attributes: function( rawContent ) {
return {
content: rawContent,
};
},
save: noop,
} );
registerBlockType( 'core/unknown-block', identityBlock );

setUnknownTypeHandler( 'core/unknown-block' );

const parsed = parse(
'<p>Cauliflower</p>' +
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<!-- wp:core/test-block {"fruit":"Bananas"} -->Bananas<!-- /wp:core/test-block -->' +
'\n<p>Broccoli</p>\n' +
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<!-- wp:core/test-block {"fruit":"Bananas"} -->Bananas<!-- /wp:core/test-block -->' +
'<p>Romanesco</p>'
);

Expand Down
11 changes: 8 additions & 3 deletions blocks/library/gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ registerBlockType( 'core/gallery', {
icon: 'format-gallery',
category: 'common',

defaultAttributes: {
images: [],
align: 'none',
imageCrop: true,
},

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) {
Expand All @@ -78,10 +84,9 @@ registerBlockType( 'core/gallery', {
},

edit( { attributes, setAttributes, focus, className } ) {
const { images = [], columns = defaultColumnsNumber( attributes ), align = 'none' } = attributes;
const { images, columns = defaultColumnsNumber( attributes ), align, imageCrop } = attributes;
const setColumnsNumber = ( event ) => setAttributes( { columns: event.target.value } );
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const { imageCrop = true } = attributes;
const toggleImageCrop = () => setAttributes( { imageCrop: ! imageCrop } );

const controls = (
Expand Down Expand Up @@ -158,7 +163,7 @@ registerBlockType( 'core/gallery', {
},

save( { attributes } ) {
const { images, columns = defaultColumnsNumber( attributes ), align = 'none', imageCrop = true } = attributes;
const { images, columns = defaultColumnsNumber( attributes ), align, imageCrop } = attributes;
return (
<div className={ `align${ align } columns-${ columns } ${ imageCrop ? 'is-cropped' : '' }` } >
{ images.map( ( img ) => (
Expand Down
6 changes: 5 additions & 1 deletion blocks/library/pullquote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ registerBlockType( 'core/pullquote', {
citation: children( 'footer' ),
},

defaultAttributes: {
align: 'none',
},

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) {
Expand Down Expand Up @@ -81,7 +85,7 @@ registerBlockType( 'core/pullquote', {
},

save( { attributes } ) {
const { value, citation, align = 'none' } = attributes;
const { value, citation, align } = attributes;

return (
<blockquote className={ `align${ align }` }>
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core__cover-text.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:core/cover-text {"backgroundColor":"#fcb900"} -->
<div style="background-color:#fcb900"><p>Text with color.</p></div>
<div style="background-color:#fcb900;" class="wp-block-cover-text"><p>Text with color.</p></div>
<!-- /wp:core/cover-text -->
5 changes: 2 additions & 3 deletions blocks/test/fixtures/core__cover-text.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
{
"uid": "_uid_0",
"name": "core/cover-text",
"isValid": false,
"isValid": true,
"attributes": {
"backgroundColor": "#fcb900",
"content": [
[
"Text with color."
]
]
},
"originalContent": "<div style=\"background-color:#fcb900\"><p>Text with color.</p></div>"
}
}
]
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core__cover-text.parsed.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"attrs": {
"backgroundColor": "#fcb900"
},
"rawContent": "\n<div style=\"background-color:#fcb900\"><p>Text with color.</p></div>\n"
"rawContent": "\n<div style=\"background-color:#fcb900;\" class=\"wp-block-cover-text\"><p>Text with color.</p></div>\n"
},
{
"attrs": {},
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core__cover-text.serialized.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:core/cover-text {"backgroundColor":"#fcb900"} -->
<div style="background-color:#fcb900">
<div style="background-color:#fcb900;" class="wp-block-cover-text">
<p>Text with color.</p>
</div>
<!-- /wp:core/cover-text -->
3 changes: 3 additions & 0 deletions blocks/test/fixtures/core__cover-text__invalid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:core/cover-text {"backgroundColor":"#fcb900"} -->
<div style="background-color:#fcb900"><p>Text with color.</p></div>
<!-- /wp:core/cover-text -->
17 changes: 17 additions & 0 deletions blocks/test/fixtures/core__cover-text__invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"uid": "_uid_0",
"name": "core/cover-text",
"isValid": false,
"attributes": {
"backgroundColor": "#fcb900",
"content": [
[
"Text with color."
]
]
},
"error": {},
"originalContent": "<div style=\"background-color:#fcb900\"><p>Text with color.</p></div>"
}
]
13 changes: 13 additions & 0 deletions blocks/test/fixtures/core__cover-text__invalid.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"blockName": "core/cover-text",
"attrs": {
"backgroundColor": "#fcb900"
},
"rawContent": "\n<div style=\"background-color:#fcb900\"><p>Text with color.</p></div>\n"
},
{
"attrs": {},
"rawContent": "\n"
}
]
10 changes: 3 additions & 7 deletions blocks/test/fixtures/core__gallery.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<!-- wp:core/gallery {"images":[{"sizes":{"full":{"url":"https://cldup.com/uuUqE_dXzy.jpg","height":1080,"width":810,"orientation":"portrait"}},"mime":"image/jpeg","type":"image","subtype":"jpeg","id":1,"url":"https://cldup.com/uuUqE_dXzy.jpg","alt":"title"},{"sizes":{"full":{"url":"http://google.com/hi.png","height":1080,"width":1440,"orientation":"landscape"}},"mime":"image/jpeg","type":"image","subtype":"jpeg","id":2,"url":"http://google.com/hi.png","alt":"title"}]} -->
<div class="wp-block-gallery">
<figure class="blocks-gallery-image">
<img src="https://cldup.com/uuUqE_dXzy.jpg" alt="title" />
</figure>
<figure class="blocks-gallery-image">
<img src="http://google.com/hi.png" alt="title" />
</figure>
<div class="wp-block-gallery alignnone columns-2 is-cropped">
<figure class="blocks-gallery-image"><img src="https://cldup.com/uuUqE_dXzy.jpg" alt="title" /></figure>
<figure class="blocks-gallery-image"><img src="http://google.com/hi.png" alt="title" /></figure>
</div>
<!-- /wp:core/gallery -->
Loading