diff --git a/projects/packages/jetpack-mu-wpcom/changelog/fix-write-empty-gb-blocks b/projects/packages/jetpack-mu-wpcom/changelog/fix-write-empty-gb-blocks new file mode 100644 index 00000000000..144c86f43cd --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/changelog/fix-write-empty-gb-blocks @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Write: strip lone br placeholders in convertToBlocks to fix empty heading and blockquote block markup diff --git a/projects/packages/jetpack-mu-wpcom/src/features/write/view.js b/projects/packages/jetpack-mu-wpcom/src/features/write/view.js index bedcd294025..e9a0de3a831 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/write/view.js +++ b/projects/packages/jetpack-mu-wpcom/src/features/write/view.js @@ -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
placeholders left by contentEditable so empty + // blocks are not serialized with stale markup. + const inner = node.innerHTML.trim().replace( /^$/, '' ); - 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; @@ -347,11 +354,15 @@ function convertToBlocks( html ) { ); } else if ( tag === 'ul' || tag === 'ol' ) { // Wrap each
  • in wp:list-item block comments. - const listItems = Array.from( node.querySelectorAll( ':scope > li' ) ) - .map( - li => `\n
  • ${ li.innerHTML.trim() }
  • \n` - ) - .join( '\n' ); + const liNodes = Array.from( node.querySelectorAll( ':scope > li' ) ); + const listItems = liNodes.length + ? liNodes + .map( + li => + `\n
  • ${ li.innerHTML.trim() }
  • \n` + ) + .join( '\n' ) + : '\n
  • \n'; const listTag = tag === 'ol' ? 'ol' : 'ul'; const attrs = tag === 'ol' ? ' {"ordered":true}' : ''; blocks.push(