New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Raw handling: preserve empty paragraphs #59476
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,58 +23,20 @@ export default function specialCommentConverter( node, doc ) { | |
return; | ||
} | ||
|
||
if ( node.nodeValue === 'nextpage' ) { | ||
replace( node, createNextpage( doc ) ); | ||
if ( | ||
node.nodeValue !== 'nextpage' && | ||
node.nodeValue.indexOf( 'more' ) !== 0 | ||
) { | ||
return; | ||
} | ||
|
||
if ( node.nodeValue.indexOf( 'more' ) === 0 ) { | ||
moreCommentConverter( node, doc ); | ||
} | ||
} | ||
|
||
/** | ||
* Convert `<!--more-->` as well as the `<!--more Some text-->` variant | ||
* and its `<!--noteaser-->` companion into the custom element | ||
* described in `specialCommentConverter()`. | ||
* | ||
* @param {Node} node The node to be processed. | ||
* @param {Document} doc The document of the node. | ||
* @return {void} | ||
*/ | ||
function moreCommentConverter( node, doc ) { | ||
// Grab any custom text in the comment. | ||
const customText = node.nodeValue.slice( 4 ).trim(); | ||
|
||
/* | ||
* When a `<!--more-->` comment is found, we need to look for any | ||
* `<!--noteaser-->` sibling, but it may not be a direct sibling | ||
* (whitespace typically lies in between) | ||
*/ | ||
let sibling = node; | ||
let noTeaser = false; | ||
while ( ( sibling = sibling.nextSibling ) ) { | ||
if ( | ||
sibling.nodeType === sibling.COMMENT_NODE && | ||
sibling.nodeValue === 'noteaser' | ||
) { | ||
noTeaser = true; | ||
remove( sibling ); | ||
break; | ||
} | ||
} | ||
|
||
const moreBlock = createMore( customText, noTeaser, doc ); | ||
const block = createBlock( node, doc ); | ||
|
||
// If our `<!--more-->` comment is in the middle of a paragraph, we should | ||
// split the paragraph in two and insert the more block in between. If not, | ||
// the more block will eventually end up being inserted after the paragraph. | ||
if ( | ||
! node.parentNode || | ||
node.parentNode.nodeName !== 'P' || | ||
node.parentNode.childNodes.length === 1 | ||
) { | ||
replace( node, moreBlock ); | ||
if ( ! node.parentNode || node.parentNode.nodeName !== 'P' ) { | ||
replace( node, block ); | ||
} else { | ||
const childNodes = Array.from( node.parentNode.childNodes ); | ||
const nodeIndex = childNodes.indexOf( node ); | ||
|
@@ -93,7 +55,7 @@ function moreCommentConverter( node, doc ) { | |
// Split the original parent node and insert our more block | ||
[ | ||
childNodes.slice( 0, nodeIndex ).reduce( paragraphBuilder, null ), | ||
moreBlock, | ||
block, | ||
childNodes.slice( nodeIndex + 1 ).reduce( paragraphBuilder, null ), | ||
].forEach( | ||
( element ) => | ||
|
@@ -105,7 +67,35 @@ function moreCommentConverter( node, doc ) { | |
} | ||
} | ||
|
||
function createMore( customText, noTeaser, doc ) { | ||
function createBlock( commentNode, doc ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand the convenience of folding everything into a single function. However, the resulting function is more difficult to understand for someone who doesn't have our context. Can we add a comment explaining that this function handles both If we wanted to go the extra mile, we could even make that explicit in code, either with a switch: switch ( commentNode.nodeValue.match(/\w+/)?.[0] ) {
case 'nextpage':
// do stuff
break;
case 'more':
// do stuff
break;
} Or simply via function delegation: function createBlock( commentNode, doc ) {
if ( commentNode.nodeValue === 'nextpage' ) {
return createNextPage( doc );
}
return createMore( commentNode, doc );
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Restored the original functions :) |
||
if ( commentNode.nodeValue === 'nextpage' ) { | ||
const node = doc.createElement( 'wp-block' ); | ||
node.dataset.block = 'core/nextpage'; | ||
|
||
return node; | ||
} | ||
|
||
// Grab any custom text in the comment. | ||
const customText = commentNode.nodeValue.slice( 4 ).trim(); | ||
|
||
/* | ||
* When a `<!--more-->` comment is found, we need to look for any | ||
* `<!--noteaser-->` sibling, but it may not be a direct sibling | ||
* (whitespace typically lies in between) | ||
*/ | ||
let sibling = commentNode; | ||
let noTeaser = false; | ||
while ( ( sibling = sibling.nextSibling ) ) { | ||
if ( | ||
sibling.nodeType === sibling.COMMENT_NODE && | ||
sibling.nodeValue === 'noteaser' | ||
) { | ||
noTeaser = true; | ||
remove( sibling ); | ||
break; | ||
} | ||
} | ||
|
||
const node = doc.createElement( 'wp-block' ); | ||
node.dataset.block = 'core/more'; | ||
if ( customText ) { | ||
|
@@ -117,10 +107,3 @@ function createMore( customText, noTeaser, doc ) { | |
} | ||
return node; | ||
} | ||
|
||
function createNextpage( doc ) { | ||
const node = doc.createElement( 'wp-block' ); | ||
node.dataset.block = 'core/nextpage'; | ||
|
||
return node; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ellatrix: can you explain this change? The code no longer matches the comment. If you can confirm my intuition here:
else
branch will be run whenever the comment node's parent is a paragraph, even if it only has one child (the comment node).paragraphBuilder
walk, ensuring that paragraphs that only contained the comment (and would become<p></p>
) are destroyed.If the above is right, some follow-up questions:
packages/blocks/src/api/raw-handling/test/special-comment-converter.js
, there is now some inconsistency between the handling of<!--more-->
and<!--nextpage-->
— with the latter, tests still expect empty paragraph nodes to be produced. Should we fix that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not adjacent, it was required to update this because some of the integration tests started failing with extra empty paragraphs that the original change was no longer removing.
I didn't think the comment no longer matched, but I reworded it.
I also fixed it for next page.