Use Sanitizer::stripAllTags() when generating notification snippets

It adds white-space between block tags and strips invisible tags.

It may be slightly slower (it takes HTML as input rather than DOM, so
we need to serialize the HTML first and then call it, rather than only
find and concatenate text nodes), but the difference is negligible,
and it seems better to use this method than to try to re-implement it.

Test runtime went from ~9.0s to ~9.5s locally, when testing using:
  php tests/phpunit/phpunit.php \
  extensions/DiscussionTools/tests/phpunit/ThreadItemTest.php \
  --filter getText

Bug: T219138
Change-Id: I0cb89ebd2160e1ef499b78573c6688f493a4c42f
This commit is contained in:
Bartosz Dziewoński 2022-02-10 21:34:05 +01:00
parent 92e3a5bb42
commit 91e1bb15cc
8 changed files with 598 additions and 596 deletions

View file

@ -4,6 +4,7 @@ namespace MediaWiki\Extension\DiscussionTools;
use DateTimeImmutable;
use MWException;
use Sanitizer;
use Title;
use Wikimedia\Parsoid\DOM\DocumentFragment;
use Wikimedia\Parsoid\DOM\Text;
@ -109,8 +110,8 @@ class CommentItem extends ThreadItem {
* @return string Text
*/
public function getBodyText( bool $stripTrailingSeparator = false ): string {
$fragment = $this->getBodyFragment( $stripTrailingSeparator );
return $fragment->textContent ?? '';
$html = $this->getBodyHTML( $stripTrailingSeparator );
return Sanitizer::stripAllTags( $html );
}
/**

View file

@ -4,6 +4,7 @@ namespace MediaWiki\Extension\DiscussionTools;
use JsonSerializable;
use LogicException;
use Sanitizer;
use Title;
use Wikimedia\Parsoid\DOM\Element;
use Wikimedia\Parsoid\DOM\Node;
@ -281,8 +282,8 @@ abstract class ThreadItem implements JsonSerializable {
* @return string Text
*/
public function getText(): string {
$fragment = $this->getRange()->cloneContents();
return $fragment->textContent ?? '';
$html = $this->getHTML();
return Sanitizer::stripAllTags( $html );
}
/**

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long