Skip to content

Commit 344a0e7

Browse files
committed
Parser: Apply autop to fallback block content
1 parent 9e6ea32 commit 344a0e7

14 files changed

+67
-47
lines changed

blocks/api/parser.js

+43-29
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
import { parse as hpqParse } from 'hpq';
55
import { mapValues, omit } from 'lodash';
66

7+
/**
8+
* WordPress dependencies
9+
*/
10+
import { autop } from '@wordpress/autop';
11+
712
/**
813
* Internal dependencies
914
*/
@@ -175,7 +180,16 @@ export function createBlockWithFallback( name, innerHTML, attributes ) {
175180

176181
// Try finding type for known block name, else fall back again.
177182
let blockType = getBlockType( name );
183+
178184
const fallbackBlock = getUnknownTypeHandlerName();
185+
186+
// Fallback content may be upgraded from classic editor expecting implicit
187+
// automatic paragraphs, so preserve them. Assumes wpautop is idempotent,
188+
// meaning there are no negative consequences to repeated autop calls.
189+
if ( name === fallbackBlock ) {
190+
innerHTML = autop( innerHTML ).trim();
191+
}
192+
179193
if ( ! blockType ) {
180194
// If detected as a block which is not registered, preserve comment
181195
// delimiters in content of unknown type handler.
@@ -188,40 +202,40 @@ export function createBlockWithFallback( name, innerHTML, attributes ) {
188202
}
189203

190204
// Include in set only if type were determined.
191-
// TODO do we ever expect there to not be an unknown type handler?
192-
if ( blockType && ( innerHTML || name !== fallbackBlock ) ) {
193-
// TODO allow blocks to opt-in to receiving a tree instead of a string.
194-
// Gradually convert all blocks to this new format, then remove the
195-
// string serialization.
196-
const block = createBlock(
197-
name,
198-
getBlockAttributes( blockType, innerHTML, attributes )
199-
);
205+
if ( ! blockType || ( ! innerHTML && name === fallbackBlock ) ) {
206+
return;
207+
}
208+
209+
const block = createBlock(
210+
name,
211+
getBlockAttributes( blockType, innerHTML, attributes )
212+
);
200213

201-
// Validate that the parsed block is valid, meaning that if we were to
202-
// reserialize it given the assumed attributes, the markup matches the
203-
// original value.
214+
// Validate that the parsed block is valid, meaning that if we were to
215+
// reserialize it given the assumed attributes, the markup matches the
216+
// original value.
217+
if ( name !== fallbackBlock ) {
204218
block.isValid = isValidBlock( innerHTML, blockType, block.attributes );
219+
}
205220

206-
// Preserve original content for future use in case the block is parsed
207-
// as invalid, or future serialization attempt results in an error
208-
block.originalContent = innerHTML;
209-
210-
// When a block is invalid, attempt to parse it using a supplied `deprecated` definition.
211-
// This allows blocks to modify their attribute and markup structure without invalidating
212-
// content written in previous formats.
213-
if ( ! block.isValid ) {
214-
const attributesParsedWithDeprecatedVersion = getAttributesFromDeprecatedVersion(
215-
blockType, innerHTML, attributes
216-
);
217-
if ( attributesParsedWithDeprecatedVersion ) {
218-
block.isValid = true;
219-
block.attributes = attributesParsedWithDeprecatedVersion;
220-
}
221-
}
221+
// Preserve original content for future use in case the block is parsed
222+
// as invalid, or future serialization attempt results in an error
223+
block.originalContent = innerHTML;
222224

223-
return block;
225+
// When a block is invalid, attempt to parse it using a supplied `deprecated` definition.
226+
// This allows blocks to modify their attribute and markup structure without invalidating
227+
// content written in previous formats.
228+
if ( ! block.isValid ) {
229+
const attributesParsedWithDeprecatedVersion = getAttributesFromDeprecatedVersion(
230+
blockType, innerHTML, attributes
231+
);
232+
if ( attributesParsedWithDeprecatedVersion ) {
233+
block.isValid = true;
234+
block.attributes = attributesParsedWithDeprecatedVersion;
235+
}
224236
}
237+
238+
return block;
225239
}
226240

227241
/**

blocks/api/test/parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ describe( 'block parser', () => {
267267

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

273273
it( 'should not create a block if no unknown type handler', () => {

blocks/test/fixtures/core__4-invalid-starting-letter.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"name": "core/freeform",
55
"isValid": true,
66
"attributes": {
7-
"content": "<!-- wp:core/4-invalid /-->"
7+
"content": "<p><!-- wp:core/4-invalid /--></p>"
88
},
9-
"originalContent": "<!-- wp:core/4-invalid /-->"
9+
"originalContent": "<p><!-- wp:core/4-invalid /--></p>"
1010
}
1111
]
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!-- wp:core/4-invalid /-->
1+
<p><!-- wp:core/4-invalid /--></p>

blocks/test/fixtures/core__freeform.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"name": "core/freeform",
55
"isValid": true,
66
"attributes": {
7-
"content": "Testing freeform block with some\n<div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
7+
"content": "<p>Testing freeform block with some\n</p><div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
88
},
9-
"originalContent": "Testing freeform block with some\n<div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
9+
"originalContent": "<p>Testing freeform block with some\n<div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
1010
}
1111
]
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Testing freeform block with some
2-
<div class="wp-some-class">
1+
<p>Testing freeform block with some
2+
</p><div class="wp-some-class">
33
HTML <span style="color: red;">content</span>
44
</div>

blocks/test/fixtures/core__freeform__undelimited.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"name": "core/freeform",
55
"isValid": true,
66
"attributes": {
7-
"content": "Testing freeform block with some\n<div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
7+
"content": "<p>Testing freeform block with some\n</p><div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
88
},
9-
"originalContent": "Testing freeform block with some\n<div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
9+
"originalContent": "<p>Testing freeform block with some\n<div class=\"wp-some-class\">\n\tHTML <span style=\"color: red;\">content</span>\n</div>"
1010
}
1111
]
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Testing freeform block with some
2-
<div class="wp-some-class">
1+
<p>Testing freeform block with some
2+
</p><div class="wp-some-class">
33
HTML <span style="color: red;">content</span>
44
</div>

blocks/test/fixtures/core__invalid-Capitals.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"name": "core/freeform",
55
"isValid": true,
66
"attributes": {
7-
"content": "<!-- wp:core/invalid-Capitals /-->"
7+
"content": "<p><!-- wp:core/invalid-Capitals /--></p>"
88
},
9-
"originalContent": "<!-- wp:core/invalid-Capitals /-->"
9+
"originalContent": "<p><!-- wp:core/invalid-Capitals /--></p>"
1010
}
1111
]
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!-- wp:core/invalid-Capitals /-->
1+
<p><!-- wp:core/invalid-Capitals /--></p>

blocks/test/fixtures/core__invalid-special.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"name": "core/freeform",
55
"isValid": true,
66
"attributes": {
7-
"content": "<!-- wp:core/invalid-$special /-->"
7+
"content": "<p><!-- wp:core/invalid-$special /--></p>"
88
},
9-
"originalContent": "<!-- wp:core/invalid-$special /-->"
9+
"originalContent": "<p><!-- wp:core/invalid-$special /--></p>"
1010
}
1111
]
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!-- wp:core/invalid-$special /-->
1+
<p><!-- wp:core/invalid-$special /--></p>

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
},
1717
"dependencies": {
1818
"@wordpress/a11y": "0.1.0-beta.1",
19+
"@wordpress/autop": "1.0.0",
1920
"@wordpress/hooks": "1.0.1",
2021
"@wordpress/url": "0.1.0-beta.1",
2122
"classnames": "2.2.5",

0 commit comments

Comments
 (0)