Skip to content

Commit c41921a

Browse files
NoéProgi1984
authored andcommitted
Word2007 Writer : Fixed StrikeThrough property
1 parent 39e806b commit c41921a

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
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 [#2659](https://github.com/PHPOffice/PHPWord/pull/2659)
3132

3233
### Miscellaneous
3334

src/PhpWord/Style/Font.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,9 @@ public function setSubScript($value = true)
566566
/**
567567
* Get strikethrough.
568568
*
569-
* @return bool
569+
* @return bool|null
570570
*/
571-
public function isStrikethrough()
571+
public function isStrikethrough(): ?bool
572572
{
573573
return $this->strikethrough;
574574
}
@@ -580,17 +580,17 @@ public function isStrikethrough()
580580
*
581581
* @return self
582582
*/
583-
public function setStrikethrough($value = true)
583+
public function setStrikethrough($value = true): self
584584
{
585585
return $this->setPairedVal($this->strikethrough, $this->doubleStrikethrough, $value);
586586
}
587587

588588
/**
589589
* Get double strikethrough.
590590
*
591-
* @return bool
591+
* @return bool|null
592592
*/
593-
public function isDoubleStrikethrough()
593+
public function isDoubleStrikethrough(): ?bool
594594
{
595595
return $this->doubleStrikethrough;
596596
}
@@ -602,7 +602,7 @@ public function isDoubleStrikethrough()
602602
*
603603
* @return self
604604
*/
605-
public function setDoubleStrikethrough($value = true)
605+
public function setDoubleStrikethrough($value = true): self
606606
{
607607
return $this->setPairedVal($this->doubleStrikethrough, $this->strikethrough, $value);
608608
}

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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,48 @@ 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+
{
555+
$phpWord = new PhpWord();
556+
$styles['strikethrough'] = $isStrikethrough;
557+
$styles['doublestrikethrough'] = $isDoubleStrikethrough;
558+
559+
$section = $phpWord->addSection();
560+
$section->addText('Test', $styles);
561+
$doc = TestHelperDOCX::getDocument($phpWord);
562+
563+
$parent = '/w:document/w:body/w:p/w:r/w:rPr';
564+
self::assertSame($expectedStrikethrough,$doc->elementExists("{$parent}/w:strike"));
565+
self::assertSame($expectedDoubleStrikethrough, $doc->elementExists("{$parent}/w:dstrike"));
566+
}
567+
568+
public static function providerFontStyleStrikethrough(): iterable
569+
{
570+
return [
571+
[true, true, false, true],
572+
[true, false, true, false],
573+
[false, true, false, true],
574+
[false, false, false, false],
575+
];
576+
}
577+
542578
/**
543579
* Tests that if no color is set on a cell a border gets writen with the default color.
544580
*/

0 commit comments

Comments
 (0)