Skip to content

Commit

Permalink
Word2007 Writer : Fixed StrikeThrough property (#2661)
Browse files Browse the repository at this point in the history
Co-authored-by: Noé <[email protected]>
  • Loading branch information
Progi1984 and Noé authored Aug 27, 2024
1 parent 39e806b commit 89a202e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/changes/1.x/1.3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- Word2007 Writer : Fix first footnote appearing as separator [#2634](https://github.com/PHPOffice/PHPWord/issues/2634) by [@jacksleight](https://github.com/jacksleight) in [#2635](https://github.com/PHPOffice/PHPWord/pull/2635)
- Template Processor : Fixed images with transparent backgrounds displaying a white background by [@ElwynVdb](https://github.com/ElwynVdb) in [#2638](https://github.com/PHPOffice/PHPWord/pull/2638)
- HTML Writer : Fixed rowspan for tables by [@andomiell](https://github.com/andomiell) in [#2659](https://github.com/PHPOffice/PHPWord/pull/2659)
- Word2007 Writer : Fixed StrikeThrough property by [@noec764](https://github.com/noec764) fixing [#1722](https://github.com/PHPOffice/PHPWord/issues/1722) & [#1693](https://github.com/PHPOffice/PHPWord/issues/1693) in [#2661](https://github.com/PHPOffice/PHPWord/pull/2661)

### Miscellaneous

Expand Down
16 changes: 4 additions & 12 deletions src/PhpWord/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,8 @@ public function setSubScript($value = true)

/**
* Get strikethrough.
*
* @return bool
*/
public function isStrikethrough()
public function isStrikethrough(): ?bool
{
return $this->strikethrough;
}
Expand All @@ -577,20 +575,16 @@ public function isStrikethrough()
* Set strikethrough.
*
* @param bool $value
*
* @return self
*/
public function setStrikethrough($value = true)
public function setStrikethrough($value = true): self
{
return $this->setPairedVal($this->strikethrough, $this->doubleStrikethrough, $value);
}

/**
* Get double strikethrough.
*
* @return bool
*/
public function isDoubleStrikethrough()
public function isDoubleStrikethrough(): ?bool
{
return $this->doubleStrikethrough;
}
Expand All @@ -599,10 +593,8 @@ public function isDoubleStrikethrough()
* Set double strikethrough.
*
* @param bool $value
*
* @return self
*/
public function setDoubleStrikethrough($value = true)
public function setDoubleStrikethrough($value = true): self
{
return $this->setPairedVal($this->doubleStrikethrough, $this->strikethrough, $value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Writer/Word2007/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ private function writeStyle(): void
$xmlWriter->writeElementIf($style->isItalic() !== null, 'w:iCs', 'w:val', $this->writeOnOf($style->isItalic()));

// Strikethrough, double strikethrough
$xmlWriter->writeElementIf($style->isStrikethrough() !== null, 'w:strike', 'w:val', $this->writeOnOf($style->isStrikethrough()));
$xmlWriter->writeElementIf($style->isDoubleStrikethrough() !== null, 'w:dstrike', 'w:val', $this->writeOnOf($style->isDoubleStrikethrough()));
$xmlWriter->writeElementIf($style->isStrikethrough(), 'w:strike', 'w:val', $this->writeOnOf($style->isStrikethrough()));
$xmlWriter->writeElementIf($style->isDoubleStrikethrough(), 'w:dstrike', 'w:val', $this->writeOnOf($style->isDoubleStrikethrough()));

// Small caps, all caps
$xmlWriter->writeElementIf($style->isSmallCaps() !== null, 'w:smallCaps', 'w:val', $this->writeOnOf($style->isSmallCaps()));
Expand Down
35 changes: 35 additions & 0 deletions tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,12 +533,47 @@ public function testWriteFontStyle(): void
self::assertTrue($doc->elementExists("{$parent}/w:i"));
self::assertEquals($styles['underline'], $doc->getElementAttribute("{$parent}/w:u", 'w:val'));
self::assertTrue($doc->elementExists("{$parent}/w:strike"));
self::assertFalse($doc->elementExists("{$parent}/w:dstrike"));
self::assertEquals('superscript', $doc->getElementAttribute("{$parent}/w:vertAlign", 'w:val'));
self::assertEquals($styles['color'], $doc->getElementAttribute("{$parent}/w:color", 'w:val'));
self::assertEquals($styles['fgColor'], $doc->getElementAttribute("{$parent}/w:highlight", 'w:val'));
self::assertTrue($doc->elementExists("{$parent}/w:smallCaps"));
}

/**
* covers ::_writeTextStyle.
*
* @dataProvider providerFontStyleStrikethrough
*/
public function testWriteFontStyleStrikethrough(
bool $isStrikethrough,
bool $isDoubleStrikethrough,
bool $expectedStrikethrough,
bool $expectedDoubleStrikethrough
): void {
$phpWord = new PhpWord();
$styles['strikethrough'] = $isStrikethrough;
$styles['doublestrikethrough'] = $isDoubleStrikethrough;

$section = $phpWord->addSection();
$section->addText('Test', $styles);
$doc = TestHelperDOCX::getDocument($phpWord);

$parent = '/w:document/w:body/w:p/w:r/w:rPr';
self::assertSame($expectedStrikethrough, $doc->elementExists("{$parent}/w:strike"));
self::assertSame($expectedDoubleStrikethrough, $doc->elementExists("{$parent}/w:dstrike"));
}

public static function providerFontStyleStrikethrough(): iterable
{
return [
[true, true, false, true],
[true, false, true, false],
[false, true, false, true],
[false, false, false, false],
];
}

/**
* Tests that if no color is set on a cell a border gets writen with the default color.
*/
Expand Down

0 comments on commit 89a202e

Please sign in to comment.