Skip to content

Commit 89a202e

Browse files
Progi1984Noé
andauthored
Word2007 Writer : Fixed StrikeThrough property (#2661)
Co-authored-by: Noé <noe@scopen.fr>
1 parent 39e806b commit 89a202e

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

docs/changes/1.x/1.3.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- 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)
2929
- 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)
3030
- HTML Writer : Fixed rowspan for tables by [@andomiell](https://github.com/andomiell) in [#2659](https://github.com/PHPOffice/PHPWord/pull/2659)
31+
- 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)
3132

3233
### Miscellaneous
3334

src/PhpWord/Style/Font.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,8 @@ public function setSubScript($value = true)
565565

566566
/**
567567
* Get strikethrough.
568-
*
569-
* @return bool
570568
*/
571-
public function isStrikethrough()
569+
public function isStrikethrough(): ?bool
572570
{
573571
return $this->strikethrough;
574572
}
@@ -577,20 +575,16 @@ public function isStrikethrough()
577575
* Set strikethrough.
578576
*
579577
* @param bool $value
580-
*
581-
* @return self
582578
*/
583-
public function setStrikethrough($value = true)
579+
public function setStrikethrough($value = true): self
584580
{
585581
return $this->setPairedVal($this->strikethrough, $this->doubleStrikethrough, $value);
586582
}
587583

588584
/**
589585
* Get double strikethrough.
590-
*
591-
* @return bool
592586
*/
593-
public function isDoubleStrikethrough()
587+
public function isDoubleStrikethrough(): ?bool
594588
{
595589
return $this->doubleStrikethrough;
596590
}
@@ -599,10 +593,8 @@ public function isDoubleStrikethrough()
599593
* Set double strikethrough.
600594
*
601595
* @param bool $value
602-
*
603-
* @return self
604596
*/
605-
public function setDoubleStrikethrough($value = true)
597+
public function setDoubleStrikethrough($value = true): self
606598
{
607599
return $this->setPairedVal($this->doubleStrikethrough, $this->strikethrough, $value);
608600
}

src/PhpWord/Writer/Word2007/Style/Font.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ private function writeStyle(): void
117117
$xmlWriter->writeElementIf($style->isItalic() !== null, 'w:iCs', 'w:val', $this->writeOnOf($style->isItalic()));
118118

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

123123
// Small caps, all caps
124124
$xmlWriter->writeElementIf($style->isSmallCaps() !== null, 'w:smallCaps', 'w:val', $this->writeOnOf($style->isSmallCaps()));

tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,47 @@ public function testWriteFontStyle(): void
533533
self::assertTrue($doc->elementExists("{$parent}/w:i"));
534534
self::assertEquals($styles['underline'], $doc->getElementAttribute("{$parent}/w:u", 'w:val'));
535535
self::assertTrue($doc->elementExists("{$parent}/w:strike"));
536+
self::assertFalse($doc->elementExists("{$parent}/w:dstrike"));
536537
self::assertEquals('superscript', $doc->getElementAttribute("{$parent}/w:vertAlign", 'w:val'));
537538
self::assertEquals($styles['color'], $doc->getElementAttribute("{$parent}/w:color", 'w:val'));
538539
self::assertEquals($styles['fgColor'], $doc->getElementAttribute("{$parent}/w:highlight", 'w:val'));
539540
self::assertTrue($doc->elementExists("{$parent}/w:smallCaps"));
540541
}
541542

543+
/**
544+
* covers ::_writeTextStyle.
545+
*
546+
* @dataProvider providerFontStyleStrikethrough
547+
*/
548+
public function testWriteFontStyleStrikethrough(
549+
bool $isStrikethrough,
550+
bool $isDoubleStrikethrough,
551+
bool $expectedStrikethrough,
552+
bool $expectedDoubleStrikethrough
553+
): void {
554+
$phpWord = new PhpWord();
555+
$styles['strikethrough'] = $isStrikethrough;
556+
$styles['doublestrikethrough'] = $isDoubleStrikethrough;
557+
558+
$section = $phpWord->addSection();
559+
$section->addText('Test', $styles);
560+
$doc = TestHelperDOCX::getDocument($phpWord);
561+
562+
$parent = '/w:document/w:body/w:p/w:r/w:rPr';
563+
self::assertSame($expectedStrikethrough, $doc->elementExists("{$parent}/w:strike"));
564+
self::assertSame($expectedDoubleStrikethrough, $doc->elementExists("{$parent}/w:dstrike"));
565+
}
566+
567+
public static function providerFontStyleStrikethrough(): iterable
568+
{
569+
return [
570+
[true, true, false, true],
571+
[true, false, true, false],
572+
[false, true, false, true],
573+
[false, false, false, false],
574+
];
575+
}
576+
542577
/**
543578
* Tests that if no color is set on a cell a border gets writen with the default color.
544579
*/

0 commit comments

Comments
 (0)