From a5afba306af262785686182869a83e2b491670c0 Mon Sep 17 00:00:00 2001 From: thiemowmde Date: Fri, 19 Apr 2024 21:02:49 +0200 Subject: [PATCH] Stop using the Xml class in this codebase There was only a single caller. Maybe a mistake or just old code that was never updated. As well as: * Avoid the separate open/closeElement when possible, usually in favor of a single rawElement. * Note this patch also fixes one place where a message was added to the HTML output without escaping. Bug: T341775 Change-Id: I51a29b47fbd8e0269c065e4277ef775f1d659ff6 --- src/MathMathML.php | 18 +++++++++--------- src/MathWikibaseInfo.php | 34 +++++++++++++++------------------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/MathMathML.php b/src/MathMathML.php index 6dde98d4e..010a46832 100644 --- a/src/MathMathML.php +++ b/src/MathMathML.php @@ -18,7 +18,6 @@ use Psr\Log\LoggerInterface; use StatusValue; use stdClass; use Throwable; -use Xml; use XmlTypeCheck; /** @@ -468,10 +467,7 @@ class MathMathML extends MathRenderer { $titleObj = SpecialPage::getTitleFor( 'MathWikibase' ); $hyperlink = $titleObj->getLocalURL( [ 'qid' => $this->params['qid'] ] ); } - $output = Html::openElement( $element, $attribs ); - if ( $hyperlink && $enableLinks ) { - $output .= Html::openElement( 'a', [ 'href' => $hyperlink, 'style' => 'color:inherit;' ] ); - } + $output = ''; // MathML has to be wrapped into a div or span in order to be able to hide it. // Remove displayStyle attributes set by the MathML converter // (Beginning from Mathoid 0.2.5 block is the default layout.) @@ -481,15 +477,19 @@ class MathMathML extends MathRenderer { if ( $this->getMathStyle() == 'display' ) { $mml = preg_replace( '/ $this->getClassName(), 'style' => 'display: none;' ], $mml ); $output .= $this->getFallbackImage(); + if ( $hyperlink && $enableLinks ) { - $output .= Html::closeElement( 'a' ); + $output = Html::rawElement( 'a', + [ 'href' => $hyperlink, 'style' => 'color:inherit;' ], + $output + ); } - $output .= Html::closeElement( $element ); - return $output; + + return Html::rawElement( $element, $attribs, $output ); } protected function dbOutArray() { diff --git a/src/MathWikibaseInfo.php b/src/MathWikibaseInfo.php index 4076e2fd4..f48b21b53 100644 --- a/src/MathWikibaseInfo.php +++ b/src/MathWikibaseInfo.php @@ -175,8 +175,7 @@ class MathWikibaseInfo { $labelAlign = $lang->isRTL() ? 'left' : 'right'; $labelAlignOpposite = !$lang->isRTL() ? 'left' : 'right'; - $output = Html::openElement( "table", [ "style" => "padding: 5px" ] ); - $output .= Html::openElement( "tbody" ); + $output = ''; foreach ( $this->hasParts as $part ) { $output .= Html::openElement( "tr" ); @@ -193,7 +192,7 @@ class MathWikibaseInfo { $part->getLabel() ); } else { - $output .= $part->getLabel(); + $output .= htmlspecialchars( $part->getLabel() ); } $output .= Html::closeElement( "td" ); @@ -221,10 +220,9 @@ class MathWikibaseInfo { $output .= Html::closeElement( "tr" ); } - $output .= Html::closeElement( "tbody" ); - $output .= Html::closeElement( "table" ); - - return $output; + return Html::rawElement( 'table', [ 'style' => 'padding: 5px' ], + Html::rawElement( 'tbody', [], $output ) + ); } /** @@ -232,22 +230,20 @@ class MathWikibaseInfo { * @return string */ public function generateSmallTableOfParts() { - $output = Html::openElement( "table" ); - $output .= Html::openElement( "tbody" ); + $output = ''; foreach ( $this->hasParts as $part ) { - $output .= Html::openElement( "tr" ); - $output .= Html::rawElement( - "td", - [ "style" => "text-align: center; padding-right: 5px;" ], - $part->getFormattedSymbol() + $output .= Html::rawElement( 'tr', [], + Html::rawElement( 'td', + [ 'style' => 'text-align: center; padding-right: 5px;' ], + $part->getFormattedSymbol() + ) . + Html::element( 'td', [ 'style' => 'text-align:left;' ], $part->getLabel() ) ); - $output .= Html::element( "td", [ "style" => "text-align:left;" ], $part->getLabel() ); - $output .= Html::closeElement( "tr" ); } - $output .= Html::closeElement( "tbody" ); - $output .= Html::closeElement( "table" ); - return $output; + return Html::rawElement( 'table', [], + Html::rawElement( 'tbody', [], $output ) + ); } }