Simplify by using Html::element and Html::rawElement

Use element/rawElement instead of openElement and closeElement.

Change-Id: I7229200f073326e66e6bc3c0907da6817777110a
This commit is contained in:
Fomafix 2021-08-31 11:18:30 +00:00 committed by Krinkle
parent a887e226e0
commit 509745aaaa
3 changed files with 54 additions and 62 deletions

View file

@ -375,26 +375,21 @@ class CategoryTree {
$attr['data-ct-mode'] = $this->mOptions['mode']; $attr['data-ct-mode'] = $this->mOptions['mode'];
$attr['data-ct-options'] = $this->getOptionsAsJsStructure(); $attr['data-ct-options'] = $this->getOptionsAsJsStructure();
$html = '';
$html .= Html::openElement( 'div', $attr );
if ( !$allowMissing && !$title->getArticleID() ) { if ( !$allowMissing && !$title->getArticleID() ) {
$html .= Html::openElement( 'span', [ 'class' => 'CategoryTreeNotice' ] ); $html = Html::rawElement( 'span', [ 'class' => 'CategoryTreeNotice' ],
$html .= wfMessage( 'categorytree-not-found' ) wfMessage( 'categorytree-not-found' )
->plaintextParams( $category ) ->plaintextParams( $category )
->parse(); ->parse()
$html .= Html::closeElement( 'span' ); );
} else { } else {
if ( !$hideroot ) { if ( !$hideroot ) {
$html .= $this->renderNode( $title, $depth ); $html = $this->renderNode( $title, $depth );
} else { } else {
$html .= $this->renderChildren( $title, $depth ); $html = $this->renderChildren( $title, $depth );
} }
} }
$html .= Html::closeElement( 'div' ); return Html::rawElement( 'div', $attr, $html );
return $html;
} }
/** /**
@ -550,26 +545,22 @@ class CategoryTree {
$special = SpecialPage::getTitleFor( 'CategoryTree' ); $special = SpecialPage::getTitleFor( 'CategoryTree' );
$s = ''; $s = [];
foreach ( $res as $row ) { foreach ( $res as $row ) {
$t = Title::makeTitle( NS_CATEGORY, $row->cl_to ); $t = Title::makeTitle( NS_CATEGORY, $row->cl_to );
if ( $s !== '' ) { $s[] = Html::rawElement( 'span', [ 'class' => 'CategoryTreeItem' ],
$s .= wfMessage( 'pipe-separator' )->escaped(); $this->linkRenderer->makeLink(
}
$s .= Html::openElement( 'span', [ 'class' => 'CategoryTreeItem' ] );
$s .= $this->linkRenderer->makeLink(
$special, $special,
$t->getText(), $t->getText(),
[ 'class' => 'CategoryTreeLabel' ], [ 'class' => 'CategoryTreeLabel' ],
[ 'target' => $t->getDBkey() ] + $this->mOptions [ 'target' => $t->getDBkey() ] + $this->mOptions
)
); );
$s .= Html::closeElement( 'span' );
} }
return $s; return implode( wfMessage( 'pipe-separator' )->escaped(), $s );
} }
/** /**
@ -693,21 +684,26 @@ class CategoryTree {
if ( $ns == NS_CATEGORY && $children > 0 ) { if ( $ns == NS_CATEGORY && $children > 0 ) {
$children = $this->renderChildren( $title, $children ); $children = $this->renderChildren( $title, $children );
if ( $children == '' ) { if ( $children == '' ) {
$s .= Html::openElement( 'i', [ 'class' => 'CategoryTreeNotice' ] ); switch ( $mode ) {
if ( $mode == CategoryTreeMode::CATEGORIES ) { case CategoryTreeMode::CATEGORIES:
$s .= wfMessage( 'categorytree-no-subcategories' )->escaped(); $msg = 'categorytree-no-subcategories';
} elseif ( $mode == CategoryTreeMode::PAGES ) { break;
$s .= wfMessage( 'categorytree-no-pages' )->escaped(); case CategoryTreeMode::PAGES:
} elseif ( $mode == CategoryTreeMode::PARENTS ) { $msg = 'categorytree-no-pages';
$s .= wfMessage( 'categorytree-no-parent-categories' )->escaped(); break;
} else { case CategoryTreeMode::PARENTS:
$s .= wfMessage( 'categorytree-nothing-found' )->escaped(); $msg = 'categorytree-no-parent-categories';
break;
default:
$msg = 'categorytree-nothing-found';
break;
}
$children = Html::element( 'i', [ 'class' => 'CategoryTreeNotice' ],
wfMessage( $msg )->text()
);
} }
$s .= Html::closeElement( 'i' );
} else {
$s .= $children; $s .= $children;
} }
}
$s .= Html::closeElement( 'div' ) . Html::closeElement( 'div' ); $s .= Html::closeElement( 'div' ) . Html::closeElement( 'div' );

View file

@ -114,34 +114,31 @@ class CategoryTreePage extends SpecialPage {
$title = CategoryTree::makeTitle( $this->target ); $title = CategoryTree::makeTitle( $this->target );
if ( $title && $title->getArticleID() ) { 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 ); $parents = $this->tree->renderParents( $title );
if ( $parents == '' ) { if ( $parents == '' ) {
$output->addHTML( $this->msg( 'categorytree-no-parent-categories' )->parse() ); $parents = $this->msg( 'categorytree-no-parent-categories' )->parse();
} else {
$output->addHTML( $parents );
} }
$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', [ $output->addHTML( Html::rawElement( 'div',
[
'class' => 'CategoryTreeResult CategoryTreeTag', 'class' => 'CategoryTreeResult CategoryTreeTag',
'data-ct-mode' => $this->tree->getOption( 'mode' ), 'data-ct-mode' => $this->tree->getOption( 'mode' ),
'data-ct-options' => $this->tree->getOptionsAsJsStructure(), 'data-ct-options' => $this->tree->getOptionsAsJsStructure(),
] ) ); ],
$output->addHTML( $this->tree->renderNode( $title, 1 ) ); $this->tree->renderNode( $title, 1 )
$output->addHTML( Html::closeElement( 'div' ) ); ) );
} else { } else {
$output->addHTML( Html::openElement( 'div', [ 'class' => 'CategoryTreeNotice' ] ) ); $output->addHTML( Html::rawElement( 'div', [ 'class' => 'CategoryTreeNotice' ],
$output->addHTML( $this->msg( 'categorytree-not-found' ) $this->msg( 'categorytree-not-found' )
->plaintextParams( $this->target ) ->plaintextParams( $this->target )
->parse() ->parse()
); ) );
$output->addHTML( Html::closeElement( 'div' ) );
} }
} }
} }

View file

@ -123,8 +123,7 @@ class Hooks implements
} }
if ( $parser->getOutputType() === Parser::OT_PREPROCESS ) { if ( $parser->getOutputType() === Parser::OT_PREPROCESS ) {
return Html::openElement( 'categorytree', $argv ) . return Html::rawElement( 'categorytree', $argv, $cat );
$cat . Html::closeElement( 'categorytree' );
} else { } else {
// now handle just like a <categorytree> tag // now handle just like a <categorytree> tag
$html = $this->parserHook( $cat, $argv, $parser ); $html = $this->parserHook( $cat, $argv, $parser );