Avoid error building threaditemshtml if there's an empty final section

If the final content on a page is a heading, there would be an error as
we tried to access nextSibling on a non-existent node.

Also tidies up the case where there's an empty section that's not the
final section. It would have `othercontent` set to an empty string,
which was pointless -- the empty `replies` field is sufficient.

Bug: T321317
Change-Id: Ia58e97214e715c1f6b02c2e045d13f2df7393b80
This commit is contained in:
David Lynch 2022-12-01 11:44:28 -06:00
parent 64e5c91970
commit 51ed8a21f6

View file

@ -167,8 +167,15 @@ class ApiDiscussionToolsPageInfo extends ApiBase {
$startOffset = $startRange->startOffset;
} else {
$startNode = CommentUtils::closestElementWithSibling( $startRange->endContainer, 'next' );
$startNode = $startNode->nextSibling;
$startOffset = 0;
if ( !$startNode ) {
// If there's no siblings here this means we're on a
// heading that is the final heading on a page and
// which has no contents at all. We can skip the rest.
continue;
} else {
$startNode = $startNode->nextSibling;
$startOffset = 0;
}
}
if ( !$startNode ) {
@ -206,7 +213,13 @@ class ApiDiscussionToolsPageInfo extends ApiBase {
}
$fragment = $betweenRange->cloneContents();
CommentModifier::unwrapFragment( $fragment );
$output[$index]['othercontent'] = trim( DOMUtils::getFragmentInnerHTML( $fragment ) );
$otherContent = trim( DOMUtils::getFragmentInnerHTML( $fragment ) );
if ( $otherContent ) {
// A completely empty section will result in otherContent
// being an empty string. In this case we should just not include it.
$output[$index]['othercontent'] = $otherContent;
}
}
}
return $output;