Skip to content

Commit ca82e19

Browse files
committed
support internal link in addHtml method
1 parent 9a91d54 commit ca82e19

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,10 @@ private static function parseLink($node, $element, &$styles)
664664
}
665665
self::parseInlineStyle($node, $styles['font']);
666666

667-
return $element->addLink($target, $node->textContent, $styles['font'], $styles['paragraph']);
667+
if(strpos($target, '#') === 0) {
668+
return $element->addLink(substr($target, 1), $node->textContent, $styles['font'], $styles['paragraph'], true);
669+
} else {
670+
return $element->addLink($target, $node->textContent, $styles['font'], $styles['paragraph']);
671+
}
668672
}
669673
}

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,5 +353,17 @@ public function testParseLink()
353353

354354
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:hyperlink'));
355355
$this->assertEquals('link text', $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t')->nodeValue);
356+
357+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
358+
$section = $phpWord->addSection();
359+
$section->addBookmark('bookmark');
360+
$html = '<p><a href="#bookmark">internal link text</a></p>';
361+
Html::addHtml($section, $html);
362+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
363+
364+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:hyperlink'));
365+
$this->assertTrue($doc->getElement('/w:document/w:body/w:p/w:hyperlink')->hasAttribute('w:anchor'));
366+
$this->assertEquals('bookmark', $doc->getElement('/w:document/w:body/w:p/w:hyperlink')->getAttribute('w:anchor'));
367+
356368
}
357369
}

0 commit comments

Comments
 (0)