4
4
import { parse as hpqParse } from 'hpq' ;
5
5
import { mapValues , omit } from 'lodash' ;
6
6
7
+ /**
8
+ * WordPress dependencies
9
+ */
10
+ import { autop } from '@wordpress/autop' ;
11
+
7
12
/**
8
13
* Internal dependencies
9
14
*/
@@ -175,7 +180,16 @@ export function createBlockWithFallback( name, innerHTML, attributes ) {
175
180
176
181
// Try finding type for known block name, else fall back again.
177
182
let blockType = getBlockType ( name ) ;
183
+
178
184
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
+
179
193
if ( ! blockType ) {
180
194
// If detected as a block which is not registered, preserve comment
181
195
// delimiters in content of unknown type handler.
@@ -188,40 +202,40 @@ export function createBlockWithFallback( name, innerHTML, attributes ) {
188
202
}
189
203
190
204
// 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
+ ) ;
200
213
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 ) {
204
218
block . isValid = isValidBlock ( innerHTML , blockType , block . attributes ) ;
219
+ }
205
220
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 ;
222
224
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
+ }
224
236
}
237
+
238
+ return block ;
225
239
}
226
240
227
241
/**
0 commit comments