From 6bda9a41179c80e67332d69633aa6f253a954bca Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Mon, 12 Aug 2024 09:24:09 +0200 Subject: [PATCH] MathML Reader : Support for mrow in mfrac --- docs/changes/0.2.0.md | 4 +-- src/Math/Reader/MathML.php | 19 ++++++----- tests/Math/Reader/MathMLTest.php | 54 ++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 10 deletions(-) diff --git a/docs/changes/0.2.0.md b/docs/changes/0.2.0.md index b22d0b3..f7b72fc 100644 --- a/docs/changes/0.2.0.md +++ b/docs/changes/0.2.0.md @@ -6,7 +6,7 @@ ## Bug fixes -- N/A +- MathML Reader : Support for mrow in mfrac by [@Progi1984](https://github/Progi1984) in [#16](https://github.com/PHPOffice/Math/pull/16) ## Miscellaneous @@ -14,4 +14,4 @@ - Bump actions/checkout from 2 to 4 by [@dependabot](https://github/dependabot) in [#10](https://github.com/PHPOffice/Math/pull/10) - Bump actions/setup-python from 2 to 4 by [@dependabot](https://github/dependabot) in [#11](https://github.com/PHPOffice/Math/pull/11) - Bump actions/setup-python from 4 to 5 by [@dependabot](https://github/dependabot) in [#12](https://github.com/PHPOffice/Math/pull/12) -- Bump peaceiris/actions-gh-pages from 3 to 4 by [@dependabot](https://github/dependabot) in [#12](https://github.com/PHPOffice/Math/pull/13) \ No newline at end of file +- Bump peaceiris/actions-gh-pages from 3 to 4 by [@dependabot](https://github/dependabot) in [#13](https://github.com/PHPOffice/Math/pull/13) \ No newline at end of file diff --git a/src/Math/Reader/MathML.php b/src/Math/Reader/MathML.php index 653a452..58d9393 100644 --- a/src/Math/Reader/MathML.php +++ b/src/Math/Reader/MathML.php @@ -61,12 +61,7 @@ protected function parseNode(?DOMNode $nodeRowElement, $parent): void continue; } - $element = $this->getElement($nodeElement); - $parent->add($element); - - if ($element instanceof Element\AbstractGroupElement) { - $this->parseNode($nodeElement, $element); - } + $parent->add($this->getElement($nodeElement)); } } @@ -108,7 +103,11 @@ protected function getElement(DOMNode $nodeElement): Element\AbstractElement return new Element\Operator($nodeValue); case 'mrow': - return new Element\Row(); + $mrow = new Element\Row(); + + $this->parseNode($nodeElement, $mrow); + + return $mrow; case 'msup': $nodeList = $this->xpath->query('*', $nodeElement); if ($nodeList && $nodeList->length == 2) { @@ -124,7 +123,11 @@ protected function getElement(DOMNode $nodeElement): Element\AbstractElement $nodeElement->nodeName )); case 'semantics': - return new Element\Semantics(); + $semantics = new Element\Semantics(); + + $this->parseNode($nodeElement, $semantics); + + return $semantics; default: throw new NotImplementedException(sprintf( '%s : The tag `%s` is not implemented', diff --git a/tests/Math/Reader/MathMLTest.php b/tests/Math/Reader/MathMLTest.php index cd1ab0d..db9b174 100644 --- a/tests/Math/Reader/MathMLTest.php +++ b/tests/Math/Reader/MathMLTest.php @@ -165,6 +165,60 @@ public function testReadFractionInvalid(): void $math = $reader->read($content); } + public function testReadFractionWithRow(): void + { + $content = ' + + + + + 3 + - + x + + 2 + + '; + + $reader = new MathML(); + $math = $reader->read($content); + $this->assertInstanceOf(Math::class, $math); + + $elements = $math->getElements(); + $this->assertCount(1, $elements); + $this->assertInstanceOf(Element\Fraction::class, $elements[0]); + + /** @var Element\Fraction $element */ + $element = $elements[0]; + + $this->assertInstanceOf(Element\Row::class, $element->getNumerator()); + /** @var Element\Row $subElement */ + $subElement = $element->getNumerator(); + + $subsubElements = $subElement->getElements(); + $this->assertCount(3, $subsubElements); + + /** @var Element\Numeric $subsubElement */ + $subsubElement = $subsubElements[0]; + $this->assertInstanceOf(Element\Numeric::class, $subsubElement); + $this->assertEquals('3', $subsubElement->getValue()); + + /** @var Element\Operator $subsubElement */ + $subsubElement = $subsubElements[1]; + $this->assertInstanceOf(Element\Operator::class, $subsubElement); + $this->assertEquals('-', $subsubElement->getValue()); + + /** @var Element\Identifier $subsubElement */ + $subsubElement = $subsubElements[2]; + $this->assertInstanceOf(Element\Identifier::class, $subsubElement); + $this->assertEquals('x', $subsubElement->getValue()); + + $this->assertInstanceOf(Element\Numeric::class, $element->getDenominator()); + /** @var Element\Numeric $subElement */ + $subElement = $element->getDenominator(); + $this->assertEquals('2', $subElement->getValue()); + } + public function testReadSuperscriptInvalid(): void { $this->expectException(InvalidInputException::class);