Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Send content in between block delimiters to the "unknown" block type
  • Loading branch information
nylen committed Apr 18, 2017
commit e1988dab79e55882de822c88d7837dbedfaeaf8d
36 changes: 30 additions & 6 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,20 @@ export default function parse( content ) {
const blocks = [];

// Store markup we found in between blocks
// let betweenBlocks = new tinymce.html.Node( 'body', 11 );
let contentBetweenBlocks = null;
function flushContentBetweenBlocks() {
if ( contentBetweenBlocks && contentBetweenBlocks.firstChild ) {
const blockType = getUnknownTypeHandler();
const settings = getBlockSettings( blockType );
if ( settings ) {
const rawContent = serializer.serialize( contentBetweenBlocks );
const blockNode = { rawContent, attrs: {} };
const block = createBlock( blockType, getBlockAttributes( blockNode, settings ) );
blocks.push( block );
}
}
contentBetweenBlocks = new tinymce.html.Node( 'body', 11 );
}

let currentNode = tree.firstChild;
do {
Expand All @@ -137,7 +150,12 @@ export default function parse( content ) {
}

// Include in set only if settings were determined
// TODO when would this fail? error handling?
if ( settings ) {
// If we have any pending content outside of block delimiters,
// add it as a block now.
flushContentBetweenBlocks();

const rawContent = serializer.serialize( currentNode );
const blockAttributes = htmlUnescape( nodeAttributes.attributes || '' )
.split( /\s+/ )
Expand All @@ -156,14 +174,20 @@ export default function parse( content ) {
const block = createBlock( blockType, getBlockAttributes( blockNode, settings ) );
blocks.push( block );
}

currentNode = currentNode.next;
} else {
// TODO: store HTML outside of blocks and pass it off to a "freeform" block
// TODO: later on, match these nodes against block markup
console.log( 'root-level node not wp-block', currentNode ); // eslint-disable-line no-console
// We have some HTML content outside of block delimiters. Save it
// so that we can initialize it using `getUnknownTypeHandler`.
const toAppend = currentNode;
// Advance the DOM tree pointer before calling `append` because
// this is a destructive operation.
currentNode = currentNode.next;
contentBetweenBlocks.append( toAppend );
}

currentNode = currentNode.next;
} while ( currentNode );

flushContentBetweenBlocks();

return blocks;
}
24 changes: 23 additions & 1 deletion blocks/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,29 @@ describe( 'block parser', () => {
expect( parsed.map( ( { blockType } ) => blockType ) ).to.eql( [
'core/test-block',
'core/unknown-block',
'core/unknown-block'
'core/unknown-block',
] );
} );

it( 'should parse the post content, using unknown block handler at the end of the block', () => {
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/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Romanesco</p>'
);

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