From 794332c1a1a97fcdcbf873fae44e47809749e848 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Tue, 23 Feb 2021 22:50:22 +0000 Subject: [PATCH] Add stripTrailingSeparator to getBodyText as well as getBodyHTML Change-Id: I33163bb291b4ee5bb6a1d602e518796057983d55 --- includes/CommentItem.php | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/includes/CommentItem.php b/includes/CommentItem.php index f98f32514..61adb0d99 100644 --- a/includes/CommentItem.php +++ b/includes/CommentItem.php @@ -2,6 +2,7 @@ namespace MediaWiki\Extension\DiscussionTools; +use DOMDocumentFragment; use DOMText; use DOMXPath; use MWException; @@ -46,12 +47,11 @@ class CommentItem extends ThreadItem { /** * Get the HTML of this comment's body * - * * @param bool $stripTrailingSeparator Strip a trailing separator between the body and * the signature which consists of whitespace and hyphens e.g. ' --' - * @return string HTML + * @return DOMDocumentFragment Cloned fragment of the body content */ - public function getBodyHTML( bool $stripTrailingSeparator = false ) : string { + private function getBodyFragment( bool $stripTrailingSeparator = false ) : DOMDocumentFragment { $fragment = $this->getBodyRange()->cloneContents(); CommentModifier::unwrapFragment( $fragment ); @@ -72,6 +72,18 @@ class CommentItem extends ThreadItem { substr( $lastChild->nodeValue, 0, -strlen( $matches[0] ) ); } } + return $fragment; + } + + /** + * Get the HTML of this comment's body + * + * + * @param bool $stripTrailingSeparator See getBodyFragment + * @return string HTML + */ + public function getBodyHTML( bool $stripTrailingSeparator = false ) : string { + $fragment = $this->getBodyFragment( $stripTrailingSeparator ); $container = $fragment->ownerDocument->createElement( 'div' ); $container->appendChild( $fragment ); return DOMCompat::getInnerHTML( $container ); @@ -80,10 +92,11 @@ class CommentItem extends ThreadItem { /** * Get the text of this comment's body * + * @param bool $stripTrailingSeparator See getBodyFragment * @return string Text */ - public function getBodyText() : string { - $fragment = $this->getBodyRange()->cloneContents(); + public function getBodyText( bool $stripTrailingSeparator = false ) : string { + $fragment = $this->getBodyFragment( $stripTrailingSeparator ); return $fragment->textContent; }