Skip to content

Commit

Permalink
Parser: Apply autop to fallback block content
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jan 5, 2018
1 parent 9e6ea32 commit 344a0e7
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 47 deletions.
72 changes: 43 additions & 29 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import { parse as hpqParse } from 'hpq';
import { mapValues, omit } from 'lodash';

/**
* WordPress dependencies
*/
import { autop } from '@wordpress/autop';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -175,7 +180,16 @@ export function createBlockWithFallback( name, innerHTML, attributes ) {

// Try finding type for known block name, else fall back again.
let blockType = getBlockType( name );

const fallbackBlock = getUnknownTypeHandlerName();

// Fallback content may be upgraded from classic editor expecting implicit
// automatic paragraphs, so preserve them. Assumes wpautop is idempotent,
// meaning there are no negative consequences to repeated autop calls.
if ( name === fallbackBlock ) {
innerHTML = autop( innerHTML ).trim();
}

if ( ! blockType ) {
// If detected as a block which is not registered, preserve comment
// delimiters in content of unknown type handler.
Expand All @@ -188,40 +202,40 @@ export function createBlockWithFallback( name, innerHTML, attributes ) {
}

// Include in set only if type were determined.
// TODO do we ever expect there to not be an unknown type handler?
if ( blockType && ( innerHTML || name !== fallbackBlock ) ) {
// TODO allow blocks to opt-in to receiving a tree instead of a string.
// Gradually convert all blocks to this new format, then remove the
// string serialization.
const block = createBlock(
name,
getBlockAttributes( blockType, innerHTML, attributes )
);
if ( ! blockType || ( ! innerHTML && name === fallbackBlock ) ) {
return;
}

const block = createBlock(
name,
getBlockAttributes( blockType, innerHTML, 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.
// 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.
if ( name !== fallbackBlock ) {
block.isValid = isValidBlock( innerHTML, blockType, block.attributes );
}

// Preserve original content for future use in case the block is parsed
// as invalid, or future serialization attempt results in an error
block.originalContent = innerHTML;

// When a block is invalid, attempt to parse it using a supplied `deprecated` definition.
// This allows blocks to modify their attribute and markup structure without invalidating
// content written in previous formats.
if ( ! block.isValid ) {
const attributesParsedWithDeprecatedVersion = getAttributesFromDeprecatedVersion(
blockType, innerHTML, attributes
);
if ( attributesParsedWithDeprecatedVersion ) {
block.isValid = true;
block.attributes = attributesParsedWithDeprecatedVersion;
}
}
// Preserve original content for future use in case the block is parsed
// as invalid, or future serialization attempt results in an error
block.originalContent = innerHTML;

return block;
// When a block is invalid, attempt to parse it using a supplied `deprecated` definition.
// This allows blocks to modify their attribute and markup structure without invalidating
// content written in previous formats.
if ( ! block.isValid ) {
const attributesParsedWithDeprecatedVersion = getAttributesFromDeprecatedVersion(
blockType, innerHTML, attributes
);
if ( attributesParsedWithDeprecatedVersion ) {
block.isValid = true;
block.attributes = attributesParsedWithDeprecatedVersion;
}
}

return block;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion blocks/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ describe( 'block parser', () => {

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

it( 'should not create a block if no unknown type handler', () => {
Expand Down
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__4-invalid-starting-letter.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"name": "core/freeform",
"isValid": true,
"attributes": {
"content": "<!-- wp:core/4-invalid /-->"
"content": "<p><!-- wp:core/4-invalid /--></p>"
},
"originalContent": "<!-- wp:core/4-invalid /-->"
"originalContent": "<p><!-- wp:core/4-invalid /--></p>"
}
]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!-- wp:core/4-invalid /-->
<p><!-- wp:core/4-invalid /--></p>
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__freeform.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"name": "core/freeform",
"isValid": true,
"attributes": {
"content": "Testing freeform block with some\n<div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
"content": "<p>Testing freeform block with some\n</p><div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
},
"originalContent": "Testing freeform block with some\n<div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
"originalContent": "<p>Testing freeform block with some\n<div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
}
]
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__freeform.serialized.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Testing freeform block with some
<div class="wp-some-class">
<p>Testing freeform block with some
</p><div class="wp-some-class">
HTML <span style="color: red;">content</span>
</div>
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__freeform__undelimited.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"name": "core/freeform",
"isValid": true,
"attributes": {
"content": "Testing freeform block with some\n<div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
"content": "<p>Testing freeform block with some\n</p><div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
},
"originalContent": "Testing freeform block with some\n<div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
"originalContent": "<p>Testing freeform block with some\n<div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
}
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Testing freeform block with some
<div class="wp-some-class">
<p>Testing freeform block with some
</p><div class="wp-some-class">
HTML <span style="color: red;">content</span>
</div>
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__invalid-Capitals.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"name": "core/freeform",
"isValid": true,
"attributes": {
"content": "<!-- wp:core/invalid-Capitals /-->"
"content": "<p><!-- wp:core/invalid-Capitals /--></p>"
},
"originalContent": "<!-- wp:core/invalid-Capitals /-->"
"originalContent": "<p><!-- wp:core/invalid-Capitals /--></p>"
}
]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!-- wp:core/invalid-Capitals /-->
<p><!-- wp:core/invalid-Capitals /--></p>
4 changes: 2 additions & 2 deletions blocks/test/fixtures/core__invalid-special.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"name": "core/freeform",
"isValid": true,
"attributes": {
"content": "<!-- wp:core/invalid-$special /-->"
"content": "<p><!-- wp:core/invalid-$special /--></p>"
},
"originalContent": "<!-- wp:core/invalid-$special /-->"
"originalContent": "<p><!-- wp:core/invalid-$special /--></p>"
}
]
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core__invalid-special.serialized.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!-- wp:core/invalid-$special /-->
<p><!-- wp:core/invalid-$special /--></p>
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"dependencies": {
"@wordpress/a11y": "0.1.0-beta.1",
"@wordpress/autop": "1.0.0",
"@wordpress/hooks": "1.0.1",
"@wordpress/url": "0.1.0-beta.1",
"classnames": "2.2.5",
Expand Down

0 comments on commit 344a0e7

Please sign in to comment.