Skip to content

Commit

Permalink
Parser: Test both parsing strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed May 4, 2017
1 parent ef3e4e1 commit 6331eb3
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 105 deletions.
12 changes: 8 additions & 4 deletions blocks/api/post.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ WP_Block_Type

HTML_Attribute_List
= as:(_+ a:HTML_Attribute_Item { return a })*
{ return as.reduce( ( attrs, [ name, value ] ) => Object.assign(
attrs,
{ [ name ]: value }
), {} ) }
{ return as.reduce( function( attrs, currentAttribute ) {
var currentAttrs = {};
currentAttrs[ currentAttribute[ 0 ] ] = currentAttribute[ 1 ];
return Object.assign(
attrs,
currentAttrs
);
}, {} ) }

HTML_Attribute_Item
= HTML_Attribute_Quoted
Expand Down
211 changes: 110 additions & 101 deletions blocks/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
getBlockAttributes,
parseBlockAttributes,
createBlockWithFallback,
default as parse,
parseWithGrammar,
parseWithTinyMCE
} from '../parser';
import {
registerBlock,
Expand Down Expand Up @@ -139,107 +140,115 @@ describe( 'block parser', () => {
} );

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

const parsed = parse(
'<!-- wp:core/test-block smoked="yes" url="http://google.com" -->' +
'Brisket' +
'<!-- /wp:core/test-block -->'
);

expect( parsed ).to.have.lengthOf( 1 );
expect( parsed[ 0 ].blockType ).to.equal( 'core/test-block' );
expect( parsed[ 0 ].attributes ).to.eql( {
content: 'Brisket',
smoked: 'yes',
url: 'http://google.com'
} );
expect( parsed[ 0 ].uid ).to.be.a( 'string' );
} );

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

const parsed = parse(
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->'
);

expect( parsed ).to.have.lengthOf( 1 );
expect( parsed[ 0 ].blockType ).to.equal( 'core/test-block' );
expect( parsed[ 0 ].attributes ).to.eql( {
content: 'Ribs & Chicken'
} );
expect( parsed[ 0 ].uid ).to.be.a( 'string' );
} );

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

setUnknownTypeHandler( 'core/unknown-block' );

const parsed = parse(
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->'
);

expect( parsed ).to.have.lengthOf( 3 );
expect( parsed.map( ( { blockType } ) => blockType ) ).to.eql( [
'core/test-block',
'core/unknown-block',
'core/unknown-block',
] );
} );

it( 'should parse the post content, including raw HTML at each end', () => {
registerBlock( 'core/test-block', {} );
registerBlock( 'core/unknown-block', {
// Currently this is the only way to test block content parsing?
attributes: function( rawContent ) {
return {
content: rawContent,
};
}
const parsers = { parseWithTinyMCE, parseWithGrammar };
Object.keys( parsers ).forEach( ( parser ) => {
const parse = parsers[ parser ];
describe( parser, () => {
it( 'should parse the post content, including block attributes', () => {
registerBlock( 'core/test-block', {
// Currently this is the only way to test block content parsing?
attributes: function( rawContent ) {
return {
content: rawContent,
};
}
} );

const parsed = parse(
'<!-- wp:core/test-block smoked="yes" url="http://google.com" chicken="ribs & \'wings\'" checked -->' +
'Brisket' +
'<!-- /wp:core/test-block -->'
);

expect( parsed ).to.have.lengthOf( 1 );
expect( parsed[ 0 ].blockType ).to.equal( 'core/test-block' );
expect( parsed[ 0 ].attributes ).to.eql( {
content: 'Brisket',
smoked: 'yes',
url: 'http://google.com',
chicken: 'ribs & \'wings\'',
checked: true
} );
expect( parsed[ 0 ].uid ).to.be.a( 'string' );
} );

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

const parsed = parse(
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->'
);

expect( parsed ).to.have.lengthOf( 1 );
expect( parsed[ 0 ].blockType ).to.equal( 'core/test-block' );
expect( parsed[ 0 ].attributes ).to.eql( {
content: 'Ribs & Chicken'
} );
expect( parsed[ 0 ].uid ).to.be.a( 'string' );
} );

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

setUnknownTypeHandler( 'core/unknown-block' );

const parsed = parse(
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->'
);

expect( parsed ).to.have.lengthOf( 3 );
expect( parsed.map( ( { blockType } ) => blockType ) ).to.eql( [
'core/test-block',
'core/unknown-block',
'core/unknown-block',
] );
} );

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

setUnknownTypeHandler( 'core/unknown-block' );

const parsed = parse(
'<p>Cauliflower</p>' +
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Romanesco</p>'
);

expect( parsed ).to.have.lengthOf( 5 );
expect( parsed.map( ( { blockType } ) => blockType ) ).to.eql( [
'core/unknown-block',
'core/test-block',
'core/unknown-block',
'core/test-block',
'core/unknown-block',
] );
expect( parsed[ 0 ].attributes.content ).to.eql( '<p>Cauliflower</p>' );
expect( parsed[ 2 ].attributes.content ).to.eql( '<p>Broccoli</p>' );
expect( parsed[ 4 ].attributes.content ).to.eql( '<p>Romanesco</p>' );
} );
} );

setUnknownTypeHandler( 'core/unknown-block' );

const parsed = parse(
'<p>Cauliflower</p>' +
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Romanesco</p>'
);

expect( parsed ).to.have.lengthOf( 5 );
expect( parsed.map( ( { blockType } ) => blockType ) ).to.eql( [
'core/unknown-block',
'core/test-block',
'core/unknown-block',
'core/test-block',
'core/unknown-block',
] );
expect( parsed[ 0 ].attributes.content ).to.eql( '<p>Cauliflower</p>' );
expect( parsed[ 2 ].attributes.content ).to.eql( '<p>Broccoli</p>' );
expect( parsed[ 4 ].attributes.content ).to.eql( '<p>Romanesco</p>' );
} );
} );
} );

0 comments on commit 6331eb3

Please sign in to comment.