From 509745aaaa83c1953b7ba54c2a1a396e73c8a445 Mon Sep 17 00:00:00 2001 From: Fomafix Date: Tue, 31 Aug 2021 11:18:30 +0000 Subject: [PATCH] Simplify by using Html::element and Html::rawElement Use element/rawElement instead of openElement and closeElement. Change-Id: I7229200f073326e66e6bc3c0907da6817777110a --- includes/CategoryTree.php | 72 +++++++++++++++++------------------ includes/CategoryTreePage.php | 41 +++++++++----------- includes/Hooks.php | 3 +- 3 files changed, 54 insertions(+), 62 deletions(-) diff --git a/includes/CategoryTree.php b/includes/CategoryTree.php index d5e4bb4b..6ef0738c 100644 --- a/includes/CategoryTree.php +++ b/includes/CategoryTree.php @@ -375,26 +375,21 @@ class CategoryTree { $attr['data-ct-mode'] = $this->mOptions['mode']; $attr['data-ct-options'] = $this->getOptionsAsJsStructure(); - $html = ''; - $html .= Html::openElement( 'div', $attr ); - if ( !$allowMissing && !$title->getArticleID() ) { - $html .= Html::openElement( 'span', [ 'class' => 'CategoryTreeNotice' ] ); - $html .= wfMessage( 'categorytree-not-found' ) - ->plaintextParams( $category ) - ->parse(); - $html .= Html::closeElement( 'span' ); + $html = Html::rawElement( 'span', [ 'class' => 'CategoryTreeNotice' ], + wfMessage( 'categorytree-not-found' ) + ->plaintextParams( $category ) + ->parse() + ); } else { if ( !$hideroot ) { - $html .= $this->renderNode( $title, $depth ); + $html = $this->renderNode( $title, $depth ); } else { - $html .= $this->renderChildren( $title, $depth ); + $html = $this->renderChildren( $title, $depth ); } } - $html .= Html::closeElement( 'div' ); - - return $html; + return Html::rawElement( 'div', $attr, $html ); } /** @@ -550,26 +545,22 @@ class CategoryTree { $special = SpecialPage::getTitleFor( 'CategoryTree' ); - $s = ''; + $s = []; foreach ( $res as $row ) { $t = Title::makeTitle( NS_CATEGORY, $row->cl_to ); - if ( $s !== '' ) { - $s .= wfMessage( 'pipe-separator' )->escaped(); - } - - $s .= Html::openElement( 'span', [ 'class' => 'CategoryTreeItem' ] ); - $s .= $this->linkRenderer->makeLink( - $special, - $t->getText(), - [ 'class' => 'CategoryTreeLabel' ], - [ 'target' => $t->getDBkey() ] + $this->mOptions + $s[] = Html::rawElement( 'span', [ 'class' => 'CategoryTreeItem' ], + $this->linkRenderer->makeLink( + $special, + $t->getText(), + [ 'class' => 'CategoryTreeLabel' ], + [ 'target' => $t->getDBkey() ] + $this->mOptions + ) ); - $s .= Html::closeElement( 'span' ); } - return $s; + return implode( wfMessage( 'pipe-separator' )->escaped(), $s ); } /** @@ -693,20 +684,25 @@ class CategoryTree { if ( $ns == NS_CATEGORY && $children > 0 ) { $children = $this->renderChildren( $title, $children ); if ( $children == '' ) { - $s .= Html::openElement( 'i', [ 'class' => 'CategoryTreeNotice' ] ); - if ( $mode == CategoryTreeMode::CATEGORIES ) { - $s .= wfMessage( 'categorytree-no-subcategories' )->escaped(); - } elseif ( $mode == CategoryTreeMode::PAGES ) { - $s .= wfMessage( 'categorytree-no-pages' )->escaped(); - } elseif ( $mode == CategoryTreeMode::PARENTS ) { - $s .= wfMessage( 'categorytree-no-parent-categories' )->escaped(); - } else { - $s .= wfMessage( 'categorytree-nothing-found' )->escaped(); + switch ( $mode ) { + case CategoryTreeMode::CATEGORIES: + $msg = 'categorytree-no-subcategories'; + break; + case CategoryTreeMode::PAGES: + $msg = 'categorytree-no-pages'; + break; + case CategoryTreeMode::PARENTS: + $msg = 'categorytree-no-parent-categories'; + break; + default: + $msg = 'categorytree-nothing-found'; + break; } - $s .= Html::closeElement( 'i' ); - } else { - $s .= $children; + $children = Html::element( 'i', [ 'class' => 'CategoryTreeNotice' ], + wfMessage( $msg )->text() + ); } + $s .= $children; } $s .= Html::closeElement( 'div' ) . Html::closeElement( 'div' ); diff --git a/includes/CategoryTreePage.php b/includes/CategoryTreePage.php index cfbc6765..411398b6 100644 --- a/includes/CategoryTreePage.php +++ b/includes/CategoryTreePage.php @@ -114,34 +114,31 @@ class CategoryTreePage extends SpecialPage { $title = CategoryTree::makeTitle( $this->target ); if ( $title && $title->getArticleID() ) { - $output->addHTML( Html::openElement( 'div', [ 'class' => 'CategoryTreeParents' ] ) ); - $output->addHTML( $this->msg( 'categorytree-parents' )->parse() ); - $output->addHTML( $this->msg( 'colon-separator' )->escaped() ); - $parents = $this->tree->renderParents( $title ); - if ( $parents == '' ) { - $output->addHTML( $this->msg( 'categorytree-no-parent-categories' )->parse() ); - } else { - $output->addHTML( $parents ); + $parents = $this->msg( 'categorytree-no-parent-categories' )->parse(); } - $output->addHTML( Html::closeElement( 'div' ) ); + $output->addHTML( Html::rawElement( 'div', [ 'class' => 'CategoryTreeParents' ], + $this->msg( 'categorytree-parents' )->parse() . + $this->msg( 'colon-separator' )->escaped() . + $parents + ) ); - $output->addHTML( Html::openElement( 'div', [ - 'class' => 'CategoryTreeResult CategoryTreeTag', - 'data-ct-mode' => $this->tree->getOption( 'mode' ), - 'data-ct-options' => $this->tree->getOptionsAsJsStructure(), - ] ) ); - $output->addHTML( $this->tree->renderNode( $title, 1 ) ); - $output->addHTML( Html::closeElement( 'div' ) ); + $output->addHTML( Html::rawElement( 'div', + [ + 'class' => 'CategoryTreeResult CategoryTreeTag', + 'data-ct-mode' => $this->tree->getOption( 'mode' ), + 'data-ct-options' => $this->tree->getOptionsAsJsStructure(), + ], + $this->tree->renderNode( $title, 1 ) + ) ); } else { - $output->addHTML( Html::openElement( 'div', [ 'class' => 'CategoryTreeNotice' ] ) ); - $output->addHTML( $this->msg( 'categorytree-not-found' ) - ->plaintextParams( $this->target ) - ->parse() - ); - $output->addHTML( Html::closeElement( 'div' ) ); + $output->addHTML( Html::rawElement( 'div', [ 'class' => 'CategoryTreeNotice' ], + $this->msg( 'categorytree-not-found' ) + ->plaintextParams( $this->target ) + ->parse() + ) ); } } } diff --git a/includes/Hooks.php b/includes/Hooks.php index 8a5642fa..c88d1702 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -123,8 +123,7 @@ class Hooks implements } if ( $parser->getOutputType() === Parser::OT_PREPROCESS ) { - return Html::openElement( 'categorytree', $argv ) . - $cat . Html::closeElement( 'categorytree' ); + return Html::rawElement( 'categorytree', $argv, $cat ); } else { // now handle just like a tag $html = $this->parserHook( $cat, $argv, $parser );