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-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' );

View file

@ -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()
) );
}
}
}

View file

@ -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 <categorytree> tag
$html = $this->parserHook( $cat, $argv, $parser );