Skip to content

Commit 90b9dec

Browse files
authored
Xlsx Reader Better Namespace Handling Phase 1 Second Bugfix (#2303)
* Xlsx Reader Better Namespace Handling Phase 1 Second Bugfix See issue #2301. The main problem in that issue had been introduced with 18.0 and had already ben fixed in master. However there was a subsequent problem that had been introduced in master, an undotted i uncrossed t with namespace handling. When using namespaces, need to call attributes() to access the attributes before trying to access them directly. Failure to do so in parseRichText caused fonts declared in Rich Text elements to be ignored. * Add An Assertion Addresses problem in 2301 that had already been fixed.
1 parent f2cd62a commit 90b9dec

File tree

4 files changed

+76
-30
lines changed

4 files changed

+76
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2121
- Phase 1 of better namespace handling for Xlsx, resolving many open issues.
2222
[PR #2173](https://github.com/PHPOffice/PhpSpreadsheet/pull/2173)
2323
[PR #2204](https://github.com/PHPOffice/PhpSpreadsheet/pull/2204)
24+
[PR #2303](https://github.com/PHPOffice/PhpSpreadsheet/pull/2303)
2425
- Add ability to extract images if source is a URL. [Issue #1997](https://github.com/PHPOffice/PhpSpreadsheet/issues/1997) [PR #2072](https://github.com/PHPOffice/PhpSpreadsheet/pull/2072)
2526
- Support for passing flags in the Reader `load()` and Writer `save()`methods, and through the IOFactory, to set behaviours. [PR #2136](https://github.com/PHPOffice/PhpSpreadsheet/pull/2136)
2627
- See [documentation](https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/) for details

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,46 +1675,63 @@ private function parseRichText(?SimpleXMLElement $is)
16751675
} else {
16761676
$objText = $value->createTextRun(StringHelper::controlCharacterOOXML2PHP((string) $run->t));
16771677

1678-
if (isset($run->rPr->rFont['val'])) {
1679-
$objText->getFont()->setName((string) $run->rPr->rFont['val']);
1678+
$attr = $run->rPr->rFont->attributes();
1679+
if (isset($attr['val'])) {
1680+
$objText->getFont()->setName((string) $attr['val']);
16801681
}
1681-
if (isset($run->rPr->sz['val'])) {
1682-
$objText->getFont()->setSize((float) $run->rPr->sz['val']);
1682+
$attr = $run->rPr->sz->attributes();
1683+
if (isset($attr['val'])) {
1684+
$objText->getFont()->setSize((float) $attr['val']);
16831685
}
16841686
if (isset($run->rPr->color)) {
16851687
$objText->getFont()->setColor(new Color(Styles::readColor($run->rPr->color)));
16861688
}
1687-
if (
1688-
(isset($run->rPr->b['val']) && self::boolean((string) $run->rPr->b['val'])) ||
1689-
(isset($run->rPr->b) && !isset($run->rPr->b['val']))
1690-
) {
1691-
$objText->getFont()->setBold(true);
1692-
}
1693-
if (
1694-
(isset($run->rPr->i['val']) && self::boolean((string) $run->rPr->i['val'])) ||
1695-
(isset($run->rPr->i) && !isset($run->rPr->i['val']))
1696-
) {
1697-
$objText->getFont()->setItalic(true);
1689+
if (isset($run->rPr->b)) {
1690+
$attr = $run->rPr->b->attributes();
1691+
if (
1692+
(isset($attr['val']) && self::boolean((string) $attr['val'])) ||
1693+
(!isset($attr['val']))
1694+
) {
1695+
$objText->getFont()->setBold(true);
1696+
}
16981697
}
1699-
if (isset($run->rPr->vertAlign, $run->rPr->vertAlign['val'])) {
1700-
$vertAlign = strtolower((string) $run->rPr->vertAlign['val']);
1701-
if ($vertAlign == 'superscript') {
1702-
$objText->getFont()->setSuperscript(true);
1698+
if (isset($run->rPr->i)) {
1699+
$attr = $run->rPr->i->attributes();
1700+
if (
1701+
(isset($attr['val']) && self::boolean((string) $attr['val'])) ||
1702+
(!isset($attr['val']))
1703+
) {
1704+
$objText->getFont()->setItalic(true);
17031705
}
1704-
if ($vertAlign == 'subscript') {
1705-
$objText->getFont()->setSubscript(true);
1706+
}
1707+
if (isset($run->rPr->vertAlign)) {
1708+
$attr = $run->rPr->vertAlign->attributes();
1709+
if (isset($attr['val'])) {
1710+
$vertAlign = strtolower((string) $attr['val']);
1711+
if ($vertAlign == 'superscript') {
1712+
$objText->getFont()->setSuperscript(true);
1713+
}
1714+
if ($vertAlign == 'subscript') {
1715+
$objText->getFont()->setSubscript(true);
1716+
}
17061717
}
17071718
}
1708-
if (isset($run->rPr->u) && !isset($run->rPr->u['val'])) {
1709-
$objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE);
1710-
} elseif (isset($run->rPr->u, $run->rPr->u['val'])) {
1711-
$objText->getFont()->setUnderline((string) $run->rPr->u['val']);
1719+
if (isset($run->rPr->u)) {
1720+
$attr = $run->rPr->u->attributes();
1721+
if (!isset($attr['val'])) {
1722+
$objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE);
1723+
} else {
1724+
$objText->getFont()->setUnderline((string) $attr['val']);
1725+
}
17121726
}
1713-
if (
1714-
(isset($run->rPr->strike['val']) && self::boolean((string) $run->rPr->strike['val'])) ||
1715-
(isset($run->rPr->strike) && !isset($run->rPr->strike['val']))
1716-
) {
1717-
$objText->getFont()->setStrikethrough(true);
1727+
if (isset($run->rPr->strike)) {
1728+
$attr = $run->rPr->strike->attributes();
1729+
if (
1730+
(isset($attr['val']) && self::boolean((string) $attr['val'])) ||
1731+
(!isset($attr['val']))
1732+
) {
1733+
$objText->getFont()->setStrikethrough(true);
1734+
}
17181735
}
17191736
}
17201737
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\IOFactory;
6+
use PhpOffice\PhpSpreadsheet\RichText\RichText;
7+
8+
class Issue2301Test extends \PHPUnit\Framework\TestCase
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private static $testbook = 'tests/data/Reader/XLSX/issue.2301.xlsx';
14+
15+
public static function testReadRichText(): void
16+
{
17+
$spreadsheet = IOFactory::load(self::$testbook);
18+
$sheet = $spreadsheet->getActiveSheet();
19+
$value = $sheet->getCell('B45')->getValue();
20+
self::assertInstanceOf(RichText::class, $value);
21+
$richtext = $value->getRichTextElements();
22+
$font = $richtext[1]->getFont();
23+
self::assertNotNull($font);
24+
self::assertSame('Arial CE', $font->getName());
25+
self::assertSame(9.0, $font->getSize());
26+
self::assertSame('protected', $sheet->getCell('BT10')->getStyle()->getProtection()->getHidden());
27+
}
28+
}
83.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)