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

Parser (Fix): Output freeform content before void blocks #9984

Merged
merged 6 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions packages/block-serialization-default-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.0.1
- Fix: Include freeform content preceding void blocks (#9984)

## 1.0.0

- Initial release.
2 changes: 1 addition & 1 deletion packages/block-serialization-default-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wordpress/block-serialization-default-parser",
"version": "1.0.0-rc.0",
"version": "1.0.1",
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the idea is that we don't touch the package.json ourselves, we let lerna do so at the moment of releasing. What we need to do though is to just add a not in the changelog and add an ( unreleased ) next to the version, that way the person that will publish the packages to npm will know what version to pick using lerna.

you can check CHANGELOG files from other packages

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks @youknowriad! I had no idea but TIL

"description": "Block serialization specification parser for WordPress posts.",
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
Expand Down
20 changes: 16 additions & 4 deletions packages/block-serialization-default-parser/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,13 @@ function parse( $document ) {
* @return bool
*/
function proceed() {
list( $token_type, $block_name, $attrs, $start_offset, $token_length ) = $this->next_token();
$next_token = $this->next_token();
list( $token_type, $block_name, $attrs, $start_offset, $token_length ) = $next_token;
$stack_depth = count( $this->stack );

// we may have some HTML soup before the next block
$leading_html_start = $start_offset > $this->offset ? $this->offset : null;

switch ( $token_type ) {
case 'no-more-tokens':
// if not in a block then flush output
Expand Down Expand Up @@ -239,6 +243,17 @@ function proceed() {
* in the top-level of the document
*/
if ( 0 === $stack_depth ) {
if ( isset( $leading_html_start ) ) {
$this->output[] = array(
'attrs' => array(),
'innerHTML' => substr(
$this->document,
$leading_html_start,
$start_offset - $leading_html_start
),
);
}

$this->output[] = new WP_Block_Parser_Block( $block_name, $attrs, array(), '' );
$this->offset = $start_offset + $token_length;
return true;
Expand All @@ -254,9 +269,6 @@ function proceed() {
return true;

case 'block-opener':
// we may have some HTML soup before the next block
$leading_html_start = $start_offset > $this->offset ? $this->offset : null;

// track all newly-opened blocks on the stack
array_push( $this->stack, new WP_Block_Parser_Frame(
new WP_Block_Parser_Block( $block_name, $attrs, array(), '' ),
Expand Down
12 changes: 9 additions & 3 deletions packages/block-serialization-default-parser/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ function proceed() {
const [ tokenType, blockName, attrs, startOffset, tokenLength ] = next;
const stackDepth = stack.length;

// we may have some HTML soup before the next block
const leadingHtmlStart = ( startOffset > offset ) ? offset : null;

switch ( tokenType ) {
case 'no-more-tokens':
// if not in a block then flush output
Expand Down Expand Up @@ -74,6 +77,12 @@ function proceed() {
// easy case is if we stumbled upon a void block
// in the top-level of the document
if ( 0 === stackDepth ) {
if ( null !== leadingHtmlStart ) {
output.push( {
attrs: {},
innerHTML: document.substr( leadingHtmlStart, startOffset - leadingHtmlStart ),
} );
}
output.push( Block( blockName, attrs, [], '' ) );
offset = startOffset + tokenLength;
return true;
Expand All @@ -89,9 +98,6 @@ function proceed() {
return true;

case 'block-opener':
// we may have some HTML soup before the next block
const leadingHtmlStart = ( startOffset > offset ) ? offset : null;

// track all newly-opened blocks on the stack
stack.push(
Frame(
Expand Down
46 changes: 46 additions & 0 deletions packages/block-serialization-default-parser/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,50 @@ describe( 'block-serialization-spec-parser', () => {
},
] );
} );

test( 'treats void blocks and empty blocks identically', () => {
expect( parse(
'<!-- wp:block /-->'
) ).toEqual( parse(
'<!-- wp:block --><!-- /wp:block -->'
) );

expect( parse(
'<!-- wp:my/bus { "is": "fast" } /-->'
) ).toEqual( parse(
'<!-- wp:my/bus { "is": "fast" } --><!-- /wp:my/bus -->'
) );
} );

test( 'should grab HTML soup before block openers', () => {
[
'<p>Break me</p><!-- wp:block /-->',
'<p>Break me</p><!-- wp:block --><!-- /wp:block -->',
].forEach( ( input ) => expect( parse( input ) ).toEqual( [
expect.objectContaining( { innerHTML: '<p>Break me</p>' } ),
expect.objectContaining( { blockName: 'core/block', innerHTML: '' } ),
] ) );
} );

test( 'should grab HTML soup before inner block openers', () => {
[
'<!-- wp:outer --><p>Break me</p><!-- wp:block /--><!-- /wp:outer -->',
'<!-- wp:outer --><p>Break me</p><!-- wp:block --><!-- /wp:block --><!-- /wp:outer -->',
].forEach( ( input ) => expect( parse( input ) ).toEqual( [
expect.objectContaining( {
innerBlocks: [ expect.objectContaining( { blockName: 'core/block', innerHTML: '' } ) ],
innerHTML: '<p>Break me</p>',
} ),
] ) );
} );

test( 'should grab HTML soup after blocks', () => {
[
'<!-- wp:block /--><p>Break me</p>',
'<!-- wp:block --><!-- /wp:block --><p>Break me</p>',
].forEach( ( input ) => expect( parse( input ) ).toEqual( [
expect.objectContaining( { blockName: 'core/block', innerHTML: '' } ),
expect.objectContaining( { innerHTML: '<p>Break me</p>' } ),
] ) );
} );
} );