Tighten Up Writer Html Logic

Fix #434, which went stale in 2018 and is now reopened. User reported a fatal error in Writer/Html. Regrettably, there is no example code/spreadsheet to illustrate the error. However, the area of code where the error happened is identified. Studying that, it was clear that the error could be avoided through the use of the nullsafe operator `?->` without any performance hit, while making the resulting code a little clearer.
This commit is contained in:
oleibman
2026-06-11 06:54:43 -07:00
parent fd5f72edd7
commit db8917cd4b
+9 -3
View File
@@ -1595,7 +1595,13 @@ class Html extends BaseWriter
$origData2 = $cell->getValueString();
}
}
$formatCode = $worksheet->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode();
$style = $worksheet->getParent()
?->getCellXfByIndexOrNull(
$cell->getXfIndex()
);
$formatCode = $style
?->getNumberFormat()
->getFormatCode();
$cellData = NumberFormat::toFormattedString(
$origData2,
@@ -1606,9 +1612,9 @@ class Html extends BaseWriter
if ($cellData === $origData) {
$cellData = htmlspecialchars($cellData, Settings::htmlEntityFlags());
}
if ($worksheet->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getFont()->getSuperscript()) {
if (true === $style?->getFont()->getSuperscript()) {
$cellData = '<sup>' . $cellData . '</sup>';
} elseif ($worksheet->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getFont()->getSubscript()) {
} elseif (true === $style?->getFont()->getSubscript()) {
$cellData = '<sub>' . $cellData . '</sub>';
}
}