Skip to content

Commit 6ba2df4

Browse files
authored
Merge pull request #1336 from lalop/addhtpl-bookmark
support internal link in addHtml method
2 parents 3380aa8 + ca82e19 commit 6ba2df4

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
@@ -723,6 +723,10 @@ private static function parseLink($node, $element, &$styles)
723723
}
724724
self::parseInlineStyle($node, $styles['font']);
725725

726-
return $element->addLink($target, $node->textContent, $styles['font'], $styles['paragraph']);
726+
if(strpos($target, '#') === 0) {
727+
return $element->addLink(substr($target, 1), $node->textContent, $styles['font'], $styles['paragraph'], true);
728+
} else {
729+
return $element->addLink($target, $node->textContent, $styles['font'], $styles['paragraph']);
730+
}
727731
}
728732
}

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,18 @@ public function testParseLink()
453453

454454
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:hyperlink'));
455455
$this->assertEquals('link text', $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t')->nodeValue);
456+
457+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
458+
$section = $phpWord->addSection();
459+
$section->addBookmark('bookmark');
460+
$html = '<p><a href="#bookmark">internal link text</a></p>';
461+
Html::addHtml($section, $html);
462+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
463+
464+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:hyperlink'));
465+
$this->assertTrue($doc->getElement('/w:document/w:body/w:p/w:hyperlink')->hasAttribute('w:anchor'));
466+
$this->assertEquals('bookmark', $doc->getElement('/w:document/w:body/w:p/w:hyperlink')->getAttribute('w:anchor'));
467+
456468
}
457469

458470
public function testParseMalformedStyleIsIgnored()

0 commit comments

Comments
 (0)