Skip to content

Commit

Permalink
HTML Reader : Fixed link without href (#2663)
Browse files Browse the repository at this point in the history
Co-authored-by: Åsmund Stavdahl <[email protected]>
  • Loading branch information
Progi1984 and asmundstavdahl authored Aug 30, 2024
1 parent 249412b commit 9f755a4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
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 @@ -30,6 +30,7 @@
- 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)
- 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)

### Miscellaneous

Expand Down
6 changes: 5 additions & 1 deletion src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,11 @@ protected static function parseLink($node, $element, &$styles)
}
$styles['font'] = self::parseInlineStyle($node, $styles['font']);

if (strpos($target, '#') === 0) {
if (empty($target)) {
$target = '#';
}

if (strpos($target, '#') === 0 && strlen($target) > 1) {
return $element->addLink(substr($target, 1), $node->textContent, $styles['font'], $styles['paragraph'], true);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/PhpWordTests/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,27 @@ public function testParseLink2(): void
self::assertEquals('bookmark', $doc->getElement('/w:document/w:body/w:p/w:hyperlink')->getAttribute('w:anchor'));
}

public function testParseLinkAllowsAbsenceOfHref(): void
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p><a>text of href-less link</a></p>';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

self::assertTrue($doc->elementExists('/w:document/w:body/w:p/w:hyperlink'));
self::assertEquals('text of href-less link', $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t')->nodeValue);

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p><a href="">text of empty-href link</a></p>';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

self::assertTrue($doc->elementExists('/w:document/w:body/w:p/w:hyperlink'));
self::assertEquals('text of empty-href link', $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t')->nodeValue);
}

public function testParseMalformedStyleIsIgnored(): void
{
$phpWord = new PhpWord();
Expand Down

0 comments on commit 9f755a4

Please sign in to comment.