Skip to content

Commit fc2eb6d

Browse files
authored
Merge pull request #16 from PHPOffice/mathmlMFracMRow
MathML Reader : Support for mrow in mfrac
2 parents d1f0d1f + 6bda9a4 commit fc2eb6d

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

docs/changes/0.2.0.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
## Bug fixes
88

9-
- N/A
9+
- MathML Reader : Support for mrow in mfrac by [@Progi1984](https://github/Progi1984) in [#16](https://github.com/PHPOffice/Math/pull/16)
1010

1111
## Miscellaneous
1212

1313
- Github Action : Roave BC Check by [@Progi1984](https://github/Progi1984) in [#9](https://github.com/PHPOffice/Math/pull/9)
1414
- Bump actions/checkout from 2 to 4 by [@dependabot](https://github/dependabot) in [#10](https://github.com/PHPOffice/Math/pull/10)
1515
- Bump actions/setup-python from 2 to 4 by [@dependabot](https://github/dependabot) in [#11](https://github.com/PHPOffice/Math/pull/11)
1616
- Bump actions/setup-python from 4 to 5 by [@dependabot](https://github/dependabot) in [#12](https://github.com/PHPOffice/Math/pull/12)
17-
- Bump peaceiris/actions-gh-pages from 3 to 4 by [@dependabot](https://github/dependabot) in [#12](https://github.com/PHPOffice/Math/pull/13)
17+
- Bump peaceiris/actions-gh-pages from 3 to 4 by [@dependabot](https://github/dependabot) in [#13](https://github.com/PHPOffice/Math/pull/13)

src/Math/Reader/MathML.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,7 @@ protected function parseNode(?DOMNode $nodeRowElement, $parent): void
6161
continue;
6262
}
6363

64-
$element = $this->getElement($nodeElement);
65-
$parent->add($element);
66-
67-
if ($element instanceof Element\AbstractGroupElement) {
68-
$this->parseNode($nodeElement, $element);
69-
}
64+
$parent->add($this->getElement($nodeElement));
7065
}
7166
}
7267

@@ -108,7 +103,11 @@ protected function getElement(DOMNode $nodeElement): Element\AbstractElement
108103

109104
return new Element\Operator($nodeValue);
110105
case 'mrow':
111-
return new Element\Row();
106+
$mrow = new Element\Row();
107+
108+
$this->parseNode($nodeElement, $mrow);
109+
110+
return $mrow;
112111
case 'msup':
113112
$nodeList = $this->xpath->query('*', $nodeElement);
114113
if ($nodeList && $nodeList->length == 2) {
@@ -124,7 +123,11 @@ protected function getElement(DOMNode $nodeElement): Element\AbstractElement
124123
$nodeElement->nodeName
125124
));
126125
case 'semantics':
127-
return new Element\Semantics();
126+
$semantics = new Element\Semantics();
127+
128+
$this->parseNode($nodeElement, $semantics);
129+
130+
return $semantics;
128131
default:
129132
throw new NotImplementedException(sprintf(
130133
'%s : The tag `%s` is not implemented',

tests/Math/Reader/MathMLTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,60 @@ public function testReadFractionInvalid(): void
165165
$math = $reader->read($content);
166166
}
167167

168+
public function testReadFractionWithRow(): void
169+
{
170+
$content = '<?xml version="1.0" encoding="UTF-8"?>
171+
<!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">
172+
<math xmlns="http://www.w3.org/1998/Math/MathML">
173+
<mfrac>
174+
<mrow>
175+
<mn>3</mn>
176+
<mo>-</mo>
177+
<mi>x</mi>
178+
</mrow>
179+
<mn>2</mn>
180+
</mfrac>
181+
</math>';
182+
183+
$reader = new MathML();
184+
$math = $reader->read($content);
185+
$this->assertInstanceOf(Math::class, $math);
186+
187+
$elements = $math->getElements();
188+
$this->assertCount(1, $elements);
189+
$this->assertInstanceOf(Element\Fraction::class, $elements[0]);
190+
191+
/** @var Element\Fraction $element */
192+
$element = $elements[0];
193+
194+
$this->assertInstanceOf(Element\Row::class, $element->getNumerator());
195+
/** @var Element\Row $subElement */
196+
$subElement = $element->getNumerator();
197+
198+
$subsubElements = $subElement->getElements();
199+
$this->assertCount(3, $subsubElements);
200+
201+
/** @var Element\Numeric $subsubElement */
202+
$subsubElement = $subsubElements[0];
203+
$this->assertInstanceOf(Element\Numeric::class, $subsubElement);
204+
$this->assertEquals('3', $subsubElement->getValue());
205+
206+
/** @var Element\Operator $subsubElement */
207+
$subsubElement = $subsubElements[1];
208+
$this->assertInstanceOf(Element\Operator::class, $subsubElement);
209+
$this->assertEquals('-', $subsubElement->getValue());
210+
211+
/** @var Element\Identifier $subsubElement */
212+
$subsubElement = $subsubElements[2];
213+
$this->assertInstanceOf(Element\Identifier::class, $subsubElement);
214+
$this->assertEquals('x', $subsubElement->getValue());
215+
216+
$this->assertInstanceOf(Element\Numeric::class, $element->getDenominator());
217+
/** @var Element\Numeric $subElement */
218+
$subElement = $element->getDenominator();
219+
$this->assertEquals('2', $subElement->getValue());
220+
}
221+
168222
public function testReadSuperscriptInvalid(): void
169223
{
170224
$this->expectException(InvalidInputException::class);

0 commit comments

Comments
 (0)