Skip to content

Commit 9f755a4

Browse files
HTML Reader : Fixed link without href (#2663)
Co-authored-by: Åsmund Stavdahl <[email protected]>
1 parent 249412b commit 9f755a4

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

docs/changes/1.x/1.3.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- 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)
3131
- HTML Writer : Fixed rowspan for tables by [@andomiell](https://github.com/andomiell) in [#2659](https://github.com/PHPOffice/PHPWord/pull/2659)
3232
- 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)
33+
- HTML Reader : Fixed link without href by [@asmundstavdahl](https://github.com/asmundstavdahl) fixing [#1562](https://github.com/PHPOffice/PHPWord/issues/1562) in [#2663](https://github.com/PHPOffice/PHPWord/pull/2663)
3334

3435
### Miscellaneous
3536

src/PhpWord/Shared/Html.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,11 @@ protected static function parseLink($node, $element, &$styles)
11561156
}
11571157
$styles['font'] = self::parseInlineStyle($node, $styles['font']);
11581158

1159-
if (strpos($target, '#') === 0) {
1159+
if (empty($target)) {
1160+
$target = '#';
1161+
}
1162+
1163+
if (strpos($target, '#') === 0 && strlen($target) > 1) {
11601164
return $element->addLink(substr($target, 1), $node->textContent, $styles['font'], $styles['paragraph'], true);
11611165
}
11621166

tests/PhpWordTests/Shared/HtmlTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,27 @@ public function testParseLink2(): void
984984
self::assertEquals('bookmark', $doc->getElement('/w:document/w:body/w:p/w:hyperlink')->getAttribute('w:anchor'));
985985
}
986986

987+
public function testParseLinkAllowsAbsenceOfHref(): void
988+
{
989+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
990+
$section = $phpWord->addSection();
991+
$html = '<p><a>text of href-less link</a></p>';
992+
Html::addHtml($section, $html);
993+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
994+
995+
self::assertTrue($doc->elementExists('/w:document/w:body/w:p/w:hyperlink'));
996+
self::assertEquals('text of href-less link', $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t')->nodeValue);
997+
998+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
999+
$section = $phpWord->addSection();
1000+
$html = '<p><a href="">text of empty-href link</a></p>';
1001+
Html::addHtml($section, $html);
1002+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
1003+
1004+
self::assertTrue($doc->elementExists('/w:document/w:body/w:p/w:hyperlink'));
1005+
self::assertEquals('text of empty-href link', $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t')->nodeValue);
1006+
}
1007+
9871008
public function testParseMalformedStyleIsIgnored(): void
9881009
{
9891010
$phpWord = new PhpWord();

0 commit comments

Comments
 (0)