Merge "Remove use of DOMXPath to remove Phan suppressions"

This commit is contained in:
jenkins-bot 2021-08-06 03:05:13 +00:00 committed by Gerrit Code Review
commit 02c09106ab
3 changed files with 25 additions and 27 deletions

View file

@ -2,7 +2,6 @@
namespace MediaWiki\Extension\DiscussionTools; namespace MediaWiki\Extension\DiscussionTools;
use DOMXPath;
use MWException; use MWException;
use Title; use Title;
use Wikimedia\Parsoid\DOM\DocumentFragment; use Wikimedia\Parsoid\DOM\DocumentFragment;
@ -107,17 +106,17 @@ class CommentItem extends ThreadItem {
*/ */
public function getMentions(): array { public function getMentions(): array {
$fragment = $this->getBodyRange()->cloneContents(); $fragment = $this->getBodyRange()->cloneContents();
// XXX use DOMCompat::querySelectorAll('a[href]') perhaps // Note: DOMCompat::getElementsByTagName() doesn't take a DocumentFragment argument
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal Nonstandard DOM $links = DOMCompat::querySelectorAll( $fragment, 'a' );
$xPath = new DOMXPath( $fragment->ownerDocument );
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal Nonstandard DOM
$links = $xPath->query( './/a', $fragment );
$users = []; $users = [];
foreach ( $links as $link ) { foreach ( $links as $link ) {
$title = CommentUtils::getTitleFromUrl( $link->getAttribute( 'href' ) ); $href = $link->getAttribute( 'href' );
if ( $title && $title->getNamespace() === NS_USER ) { if ( $href ) {
// TODO: Consider returning User objects $title = CommentUtils::getTitleFromUrl( $href );
$users[] = $title; if ( $title && $title->getNamespace() === NS_USER ) {
// TODO: Consider returning User objects
$users[] = $title;
}
} }
} }
return array_unique( $users ); return array_unique( $users );

View file

@ -2,7 +2,6 @@
namespace MediaWiki\Extension\DiscussionTools; namespace MediaWiki\Extension\DiscussionTools;
use DOMXPath;
use MWException; use MWException;
use Wikimedia\Parsoid\DOM\Document; use Wikimedia\Parsoid\DOM\Document;
use Wikimedia\Parsoid\DOM\DocumentFragment; use Wikimedia\Parsoid\DOM\DocumentFragment;
@ -449,16 +448,18 @@ class CommentModifier {
* @return bool * @return bool
*/ */
public static function isHtmlSigned( Element $container ): bool { public static function isHtmlSigned( Element $container ): bool {
// XXX use querySelectorAll
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal Nonstandard DOM
$xpath = new DOMXPath( $container->ownerDocument );
// Good enough?… // Good enough?…
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal Nonstandard DOM $matches = DOMCompat::querySelectorAll( $container, 'span[typeof="mw:Transclusion"][data-mw*="~~~~"]' );
$matches = $xpath->query( './/span[@typeof="mw:Transclusion"][contains(@data-mw,"~~~~")]', $container ); // Iterate to get the last item. We don't know if $matches is an array or some iterator,
if ( $matches->length === 0 ) { // and there doesn't seem to be a nicer way to get just the last item.
foreach ( $matches as $match ) {
$lastSig = $match;
}
if ( !isset( $lastSig ) ) {
// List was empty
return false; return false;
} }
$lastSig = $matches->item( $matches->length - 1 );
// Signature must be at the end of the comment - there must be no sibling following this node, or its parents // Signature must be at the end of the comment - there must be no sibling following this node, or its parents
$node = $lastSig; $node = $lastSig;
while ( $node ) { while ( $node ) {
@ -466,7 +467,7 @@ class CommentModifier {
while ( while (
$node->nextSibling && $node->nextSibling &&
$node->nextSibling->nodeType === XML_TEXT_NODE && $node->nextSibling->nodeType === XML_TEXT_NODE &&
CommentUtils::htmlTrim( $node->nextSibling->nodeValue ) === '' CommentUtils::htmlTrim( $node->nextSibling->nodeValue ?? '' ) === ''
) { ) {
$node = $node->nextSibling; $node = $node->nextSibling;
} }

View file

@ -2,9 +2,9 @@
namespace MediaWiki\Extension\DiscussionTools; namespace MediaWiki\Extension\DiscussionTools;
use DOMXPath;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use Title; use Title;
use Wikimedia\Assert\Assert;
use Wikimedia\Parsoid\DOM\Comment; use Wikimedia\Parsoid\DOM\Comment;
use Wikimedia\Parsoid\DOM\Element; use Wikimedia\Parsoid\DOM\Element;
use Wikimedia\Parsoid\DOM\Node; use Wikimedia\Parsoid\DOM\Node;
@ -404,17 +404,15 @@ class CommentUtils {
public static function unwrapParsoidSections( public static function unwrapParsoidSections(
Element $element, string $keepSection = null Element $element, string $keepSection = null
): void { ): void {
// XXX use DOMCompat::querySelectorAll $sections = DOMCompat::querySelectorAll( $element, 'section[data-mw-section-id]' );
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal Nonstandard DOM
$xpath = new DOMXPath( $element->ownerDocument );
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal Nonstandard DOM
$sections = $xpath->query( '//section[@data-mw-section-id]', $element );
foreach ( $sections as $section ) { foreach ( $sections as $section ) {
$parent = $section->parentNode; $parent = $section->parentNode;
$sectionId = $section->getAttribute( 'data-mw-section-id' ); $sectionId = $section->getAttribute( 'data-mw-section-id' );
// Copy section ID to first child (should be a heading) // Copy section ID to first child (should be a heading)
if ( $sectionId !== '' && intval( $sectionId ) > 0 ) { if ( $sectionId !== null && $sectionId !== '' && intval( $sectionId ) > 0 ) {
$section->firstChild->setAttribute( 'data-mw-section-id', $sectionId ); $firstChild = $section->firstChild;
Assert::precondition( $firstChild instanceof Element, 'Section has a heading' );
$firstChild->setAttribute( 'data-mw-section-id', $sectionId );
} }
if ( $keepSection !== null && $sectionId === $keepSection ) { if ( $keepSection !== null && $sectionId === $keepSection ) {
return; return;