Skip to content

Commit

Permalink
Introduces optional chaining for yoastseo/parse
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkuu committed Jan 9, 2025
1 parent abf031a commit 9afde4b
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/yoastseo/src/parse/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function build( paper, languageProcessor, shortcodes ) {
html = html.replace( htmlEntitiesRegex, "#$1" );

let tree = adapt( parseFragment( html, { sourceCodeLocationInfo: true } ) );
if ( tree.childNodes && tree.childNodes.length > 0 ) {
if ( tree.childNodes?.length > 0 ) {
parseBlocks( paper, tree );
}

Expand Down
2 changes: 1 addition & 1 deletion packages/yoastseo/src/parse/build/private/adapt.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function isBlockElement( nodeName ) {
function isOverarchingParagraph( nodeName, children ) {
return isParagraph( nodeName ) && children.some( ( node, index, childNodes ) => {
const nextNode = childNodes.length - 1 !== index && childNodes[ index + 1 ];
return node.name === "br" && nextNode && nextNode.name === "br";
return node.name === "br" && nextNode?.name === "br";
} );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function isInterElementWhitespace( node ) {
* @returns {boolean} Whether the node has any children.
*/
function hasChildren( node ) {
return node && node.childNodes.length > 0;
return node?.childNodes.length > 0;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/yoastseo/src/parse/build/private/filterHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function elementHasName( name ) {
*/
export function elementHasClass( className ) {
return ( blockElement ) => {
return !! blockElement.attributes.class && blockElement.attributes.class.has( className );
return !! blockElement.attributes.class?.has( className );
};
}

Expand Down
9 changes: 4 additions & 5 deletions packages/yoastseo/src/parse/build/private/parseBlocks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isUndefined } from "lodash";
const blockTokenizer = /<!--\s+wp:([a-z][a-z0-9_-]*\/)?([a-z][a-z0-9_-]*)\s+({(?:(?=([^}]+|}+(?=})|(?!}\s+\/?-->)[^])*)\5|[^]*?)}\s+)?(\/)?-->/g;

/**
Expand Down Expand Up @@ -67,7 +66,7 @@ export function updateBlocksOffset( blocks, text ) {
currentBlock.contentOffset = startedAt + length + 1;
}

if ( currentBlock.innerBlocks && currentBlock.innerBlocks.length > 0 ) {
if ( currentBlock.innerBlocks?.length > 0 ) {
updateBlocksOffset( currentBlock.innerBlocks, text );
}
} );
Expand All @@ -90,7 +89,7 @@ function updateClientIdAndAttrIdForSubtree( rootNode, blocks, clientId ) {
}

let currentClientId = clientId;
if ( rootNode.sourceCodeLocation && ! isUndefined( rootNode.sourceCodeLocation.startOffset ) ) {
if ( rootNode.sourceCodeLocation?.startOffset ) {
const foundBlock = blocks.find( block => block.contentOffset === rootNode.sourceCodeLocation.startOffset );
if ( foundBlock ) {
currentClientId = foundBlock.clientId;
Expand All @@ -104,7 +103,7 @@ function updateClientIdAndAttrIdForSubtree( rootNode, blocks, clientId ) {

// If the node has children, update the clientId for them.
( rootNode.childNodes || [] ).forEach( ( node ) => {
if ( node.attributes && node.attributes.id ) {
if ( node.attributes?.id ) {
/*
* If the node's child has an attribute with 'id' key, also set this id to the first and third child node.
* This step is specifically for parsing the Yoast blocks.
Expand All @@ -118,7 +117,7 @@ function updateClientIdAndAttrIdForSubtree( rootNode, blocks, clientId ) {
* - For example, in Yoast FAQ block, the second child node would represent the "answer" section.
* 4. The fourth child node of the sub-block, only contains a new line.
*/
if ( node.childNodes && node.childNodes.length > 3 ) {
if ( node.childNodes?.length > 3 ) {
node.childNodes[ 0 ].attributeId = node.attributes.id;
node.childNodes[ 0 ].isFirstSection = true;

Expand Down

0 comments on commit 9afde4b

Please sign in to comment.