From ef997a0c8e54c4f8d7604fe2c69d1e97ce68a537 Mon Sep 17 00:00:00 2001 From: oleibman Date: Sun, 4 Oct 2020 18:21:40 -0700 Subject: [PATCH] Bug setting Superscript/Subscript to false (#1567) If font style Superscript is set to true, Subscript is set to false. Likewise, setting Subscript to true sets Superscript to false. Both of these are working as they should. However, setting Superscript to false causes Subscript to be set to true, and setting Subscript to false causes Superscript to be set to true. I believe that is an error in both cases. This change fixes it. There seem to be no existing tests for Font styles. I added the tests necessary to validate this change. I will put adding more on my to-do list. --- src/PhpSpreadsheet/Style/Font.php | 22 ++++------ tests/PhpSpreadsheetTests/Style/FontTest.php | 42 ++++++++++++++++++++ 2 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Style/FontTest.php diff --git a/src/PhpSpreadsheet/Style/Font.php b/src/PhpSpreadsheet/Style/Font.php index eee7df04..a062a38f 100644 --- a/src/PhpSpreadsheet/Style/Font.php +++ b/src/PhpSpreadsheet/Style/Font.php @@ -357,21 +357,18 @@ class Font extends Supervisor /** * Set Superscript. * - * @param bool $pValue - * * @return $this */ - public function setSuperscript($pValue) + public function setSuperscript(bool $pValue) { - if ($pValue == '') { - $pValue = false; - } if ($this->isSupervisor) { $styleArray = $this->getStyleArray(['superscript' => $pValue]); $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); } else { $this->superscript = $pValue; - $this->subscript = !$pValue; + if ($this->superscript) { + $this->subscript = false; + } } return $this; @@ -394,21 +391,18 @@ class Font extends Supervisor /** * Set Subscript. * - * @param bool $pValue - * * @return $this */ - public function setSubscript($pValue) + public function setSubscript(bool $pValue) { - if ($pValue == '') { - $pValue = false; - } if ($this->isSupervisor) { $styleArray = $this->getStyleArray(['subscript' => $pValue]); $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); } else { $this->subscript = $pValue; - $this->superscript = !$pValue; + if ($this->subscript) { + $this->superscript = false; + } } return $this; diff --git a/tests/PhpSpreadsheetTests/Style/FontTest.php b/tests/PhpSpreadsheetTests/Style/FontTest.php new file mode 100644 index 00000000..c014a4b6 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Style/FontTest.php @@ -0,0 +1,42 @@ +getActiveSheet(); + $cell = $sheet->getCell('A1'); + $cell->setValue('Cell A1'); + $font = $cell->getStyle()->getFont(); + $font->setSuperscript(true); + $font->setSubscript(true); + self::assertFalse($font->getSuperscript(), 'Earlier set true loses'); + self::assertTrue($font->getSubscript(), 'Last set true wins'); + $font->setSubscript(true); + $font->setSuperscript(true); + self::assertTrue($font->getSuperscript(), 'Last set true wins'); + self::assertFalse($font->getSubscript(), 'Earlier set true loses'); + $font->setSuperscript(false); + $font->setSubscript(false); + self::assertFalse($font->getSuperscript(), 'False remains unchanged'); + self::assertFalse($font->getSubscript(), 'False remains unchanged'); + $font->setSubscript(false); + $font->setSuperscript(false); + self::assertFalse($font->getSuperscript(), 'False remains unchanged'); + self::assertFalse($font->getSubscript(), 'False remains unchanged'); + $font->setSubscript(true); + $font->setSuperscript(false); + self::assertFalse($font->getSuperscript(), 'False remains unchanged'); + self::assertTrue($font->getSubscript(), 'True remains unchanged'); + $font->setSubscript(false); + $font->setSuperscript(true); + self::assertTrue($font->getSuperscript()); + self::assertFalse($font->getSubscript(), 'False remains unchanged'); + } +}