Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Write: strip lone br placeholders in convertToBlocks to fix empty heading and blockquote block markup
25 changes: 18 additions & 7 deletions projects/packages/jetpack-mu-wpcom/src/features/write/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,16 @@ function convertToBlocks( html ) {
if ( node.nodeType !== Node.ELEMENT_NODE ) continue;

const tag = node.tagName.toLowerCase();
const inner = node.innerHTML.trim();
// Strip lone <br> placeholders left by contentEditable so empty
// blocks are not serialized with stale markup.
const inner = node.innerHTML.trim().replace( /^<br\s*\/?>$/, '' );

if ( ! inner && ! [ 'figure', 'img', 'hr' ].includes( tag ) ) continue;
if (
! inner &&
! [ 'figure', 'img', 'hr', 'blockquote', 'ul', 'ol' ].includes( tag ) &&
! /^h[1-6]$/.test( tag )
)
continue;

// Check for text alignment.
const align = node.style && node.style.textAlign;
Expand Down Expand Up @@ -347,11 +354,15 @@ function convertToBlocks( html ) {
);
} else if ( tag === 'ul' || tag === 'ol' ) {
// Wrap each <li> in wp:list-item block comments.
const listItems = Array.from( node.querySelectorAll( ':scope > li' ) )
.map(
li => `<!-- wp:list-item -->\n<li>${ li.innerHTML.trim() }</li>\n<!-- /wp:list-item -->`
)
.join( '\n' );
const liNodes = Array.from( node.querySelectorAll( ':scope > li' ) );
const listItems = liNodes.length
? liNodes
.map(
li =>
`<!-- wp:list-item -->\n<li>${ li.innerHTML.trim() }</li>\n<!-- /wp:list-item -->`
)
.join( '\n' )
: '<!-- wp:list-item -->\n<li></li>\n<!-- /wp:list-item -->';
const listTag = tag === 'ol' ? 'ol' : 'ul';
const attrs = tag === 'ol' ? ' {"ordered":true}' : '';
blocks.push(
Expand Down
Loading