Skip to content

Commit

Permalink
MathML Reader : Support for mrow in mfrac
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Aug 12, 2024
1 parent d1f0d1f commit 6bda9a4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docs/changes/0.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

## 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

- Github Action : Roave BC Check by [@Progi1984](https://github/Progi1984) in [#9](https://github.com/PHPOffice/Math/pull/9)
- 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)
- Bump peaceiris/actions-gh-pages from 3 to 4 by [@dependabot](https://github/dependabot) in [#13](https://github.com/PHPOffice/Math/pull/13)
19 changes: 11 additions & 8 deletions src/Math/Reader/MathML.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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',
Expand Down
54 changes: 54 additions & 0 deletions tests/Math/Reader/MathMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,60 @@ public function testReadFractionInvalid(): void
$math = $reader->read($content);
}

public function testReadFractionWithRow(): void
{
$content = '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mfrac>
<mrow>
<mn>3</mn>
<mo>-</mo>
<mi>x</mi>
</mrow>
<mn>2</mn>
</mfrac>
</math>';

$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);
Expand Down

0 comments on commit 6bda9a4

Please sign in to comment.