diff --git a/MathMathML.php b/MathMathML.php index 64fe5d7da..2dd091f69 100644 --- a/MathMathML.php +++ b/MathMathML.php @@ -339,11 +339,10 @@ class MathMathML extends MathRenderer { /** * Helper function to correct the style information for a * linked SVG image. - * @param string $svg SVG-image data * @param string $style current style information to be updated */ - public function correctSvgStyle( $svg, &$style ) { - if ( preg_match( '/style="([^"]*)"/', $svg, $styles ) ) { + public function correctSvgStyle( &$style ) { + if ( preg_match( '/style="([^"]*)"/', $this->getSvg(), $styles ) ) { $style .= ' ' . $styles[1]; // merge styles if ( $this->getMathStyle() === 'display' ) { // TODO: Improve style cleaning @@ -351,7 +350,9 @@ class MathMathML extends MathRenderer { '/margin\-(left|right)\:\s*\d+(\%|in|cm|mm|em|ex|pt|pc|px)\;/', '', $style ); } - $style = preg_replace( '/position:\s*absolute;\s*left:\s*0px;/', '', $style ); + $style = trim( preg_replace( '/position:\s*absolute;\s*left:\s*0px;/', '', $style ), + "; \t\n\r\0\x0B" ) .'; '; + } // TODO: Figure out if there is a way to construct // a SVGReader from a string that represents the SVG @@ -382,7 +383,7 @@ class MathMathML extends MathRenderer { $class = $classOverride; } if ( ! $this->mathoidStyle ) { - $this->correctSvgStyle( $this->getSvg(), $this->mathoidStyle ); + $this->correctSvgStyle( $this->mathoidStyle ); } // TODO: move the common styles to the global stylesheet! $style = 'background-image: url(\''. $url . diff --git a/tests/MathMathMLTest.php b/tests/MathMathMLTest.php index 3283da5b5..745c449e9 100644 --- a/tests/MathMathMLTest.php +++ b/tests/MathMathMLTest.php @@ -120,6 +120,19 @@ class MathMathMLTest extends MediaWikiTestCase { $this->assertContains( '.png', $res ); } + /** + * @covers MathMathML::correctSvgStyle + * @see https://phabricator.wikimedia.org/T132563 + */ + public function testMathMLStyle() { + $m = new MathMathML(); + $m->setSvg( 'style="vertical-align:-.505ex" height="2.843ex" width="28.527ex"' ); + $style = ''; + $m->correctSvgStyle( $style ); + $this->assertEquals( 'vertical-align:-.505ex; height: 2.843ex; width: 28.527ex;', $style ); + $m->setSvg( 'style=" vertical-align:-.505ex; \n" height="2.843ex" width="28.527ex"' ); + $this->assertEquals( 'vertical-align:-.505ex; height: 2.843ex; width: 28.527ex;', $style ); + } } /**