Skip to content

Commit

Permalink
Raw handling: fix for mixed blocks and freeform content (#62545)
Browse files Browse the repository at this point in the history
Unlinked contributors: vvdc@vvdcs-MacBook-Pro.local, saulyz.
  • Loading branch information
saulyz authored and ellatrix committed Jun 18, 2024
1 parent 580c1fe commit d3f0e25
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/blocks/src/api/raw-handling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ export function deprecatedGetPhrasingContentSchema( context ) {
export function rawHandler( { HTML = '' } ) {
// If we detect block delimiters, parse entirely as blocks.
if ( HTML.indexOf( '<!-- wp:' ) !== -1 ) {
return parse( HTML );
const parseResult = parse( HTML );
const isSingleFreeFormBlock =
parseResult.length === 1 &&
parseResult[ 0 ].name === 'core/freeform';
if ( ! isSingleFreeFormBlock ) {
return parseResult;
}
}

// An array of HTML strings and block objects. The blocks replace matched
Expand Down
8 changes: 7 additions & 1 deletion packages/blocks/src/api/raw-handling/paste-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ export function pasteHandler( {
const content = HTML ? HTML : plainText;

if ( content.indexOf( '<!-- wp:' ) !== -1 ) {
return parse( content );
const parseResult = parse( content );
const isSingleFreeFormBlock =
parseResult.length === 1 &&
parseResult[ 0 ].name === 'core/freeform';
if ( ! isSingleFreeFormBlock ) {
return parseResult;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ exports[`Blocks raw handling pasteHandler iframe-embed 1`] = `""`;

exports[`Blocks raw handling pasteHandler markdown 1`] = `"This is a heading with <em>italic</em><br>This is a paragraph with a <a href="https://w.org/">link</a>, <strong>bold</strong>, and <del>strikethrough</del>.<br>Preserve<br>line breaks please.<br>Lists<br>A<br>Bulleted Indented<br>List<br>One<br>Two<br>Three<br>Table<br>First Header<br>Second Header<br>Content from cell 1<br>Content from cell 2<br>Content in the first column<br>Content in the second column<br><br><br><br>Table with empty cells.<br>Quote<br>First<br>Second<br>Code<br>Inline <code>code</code> tags should work.<br><code>This is a code block.</code>"`;

exports[`Blocks raw handling pasteHandler mixed-content 1`] = `"Some heading<br><br>Content we need to preserve"`;

exports[`Blocks raw handling pasteHandler ms-word 1`] = `"This is a title<br>&nbsp;<br>This is a subtitle<br>&nbsp;<br>This is a heading level 1<br>&nbsp;<br>This is a heading level 2<br>&nbsp;<br>This is a <strong>paragraph</strong> with a <a href="https://w.org/">link</a>.<br>&nbsp;<br>A<br>Bulleted<br>Indented<br>List<br>&nbsp;<br>One<br>Two<br>Three<br>&nbsp;<br>One<br>Two<br>Three<br>1<br>2<br>3<br>I<br>II<br>III<br>&nbsp;<br>An image:<br>&nbsp;<br><img width="451" height="338" src="file:LOW-RES.png"><br><a href="#anchor">This is an anchor link</a> that leads to the next paragraph.<br><a id="anchor">This is the paragraph with the anchor.</a><br><a href="#nowhere">This is an anchor link</a> that leads nowhere.<br><a>This is a paragraph with an anchor with no link pointing to it.</a><br>This is a reference to a footnote<a href="#_ftn1" id="_ftnref1">[1]</a>.<br>This is a reference to an endnote<a href="#_edn1" id="_ednref1">[i]</a>.<br><br><br><a href="#_ftnref1" id="_ftn1">[1]</a> This is a footnote.<br><br><br><a href="#_ednref1" id="_edn1">[i]</a> This is an endnote."`;

exports[`Blocks raw handling pasteHandler ms-word-list 1`] = `"<a>This is a headline?</a><br>This is a text:<br>One<br>Two<br>Three<br><a>Lorem Ipsum.</a><br>&nbsp;"`;
Expand Down
1 change: 1 addition & 0 deletions test/integration/blocks-raw-handling.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ describe( 'Blocks raw handling', () => {
'shortcode-matching',
'slack-quote',
'slack-paragraphs',
'mixed-content',
].forEach( ( type ) => {
// eslint-disable-next-line jest/valid-title
it( type, () => {
Expand Down
7 changes: 7 additions & 0 deletions test/integration/fixtures/documents/mixed-content-in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h3>Some heading</h3>
<ul>
<li style="list-style-type: none;">
<ul><!-- /wp:list-item --> <!-- wp:list-item --></ul>
</li>
<li>Content we need to preserve</li>
</ul>
3 changes: 3 additions & 0 deletions test/integration/fixtures/documents/mixed-content-out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Some heading</h3>
<!-- /wp:heading -->

0 comments on commit d3f0e25

Please sign in to comment.