Skip to content

Commit 4f6fa50

Browse files
authored
Merge pull request #1258 from troosan/parse_html_links
Add parsing of HTML links
2 parents 4c68ebb + 0425a25 commit 4f6fa50

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

samples/Sample_26_Html.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
$html .= '<p>Some well formed HTML snippet needs to be used</p>';
1111
$html .= '<p>With for example <strong>some<sup>1</sup> <em>inline</em> formatting</strong><sub>1</sub></p>';
1212

13+
$html .= '<p>A link to <a href="http://phpword.readthedocs.io/">Read the docs</a></p>';
14+
1315
$html .= '<p style="margin-top: 240pt;">Unordered (bulleted) list:</p>';
1416
$html .= '<ul><li>Item 1</li><li>Item 2</li><ul><li>Item 2.1</li><li>Item 2.1</li></ul></ul>';
1517

src/PhpWord/Shared/Html.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ protected static function parseNode($node, $element, $styles = array(), $data =
142142
'li' => array('ListItem', $node, $element, $styles, $data, null, null),
143143
'img' => array('Image', $node, $element, $styles, null, null, null),
144144
'br' => array('LineBreak', null, $element, $styles, null, null, null),
145+
'a' => array('Link', $node, $element, $styles, null, null, null),
145146
);
146147

147148
$newElement = null;
@@ -643,4 +644,26 @@ private static function parseLineBreak($element)
643644
{
644645
$element->addTextBreak();
645646
}
647+
648+
/**
649+
* Parse link node
650+
*
651+
* @param \DOMNode $node
652+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
653+
* @param array $styles
654+
*/
655+
private static function parseLink($node, $element, &$styles)
656+
{
657+
$target = null;
658+
foreach ($node->attributes as $attribute) {
659+
switch ($attribute->name) {
660+
case 'href':
661+
$target = $attribute->value;
662+
break;
663+
}
664+
}
665+
self::parseInlineStyle($node, $styles['font']);
666+
667+
return $element->addLink($target, $node->textContent, $styles['font'], $styles['paragraph']);
668+
}
646669
}

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,17 @@ public function testParseImage()
341341
$this->assertStringMatchesFormat('%Smso-position-horizontal:right%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
342342
$this->assertStringMatchesFormat('%Smso-position-horizontal:left%S', $doc->getElementAttribute($baseXpath . '[2]/w:pict/v:shape', 'style'));
343343
}
344+
345+
public function testParseLink()
346+
{
347+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
348+
$section = $phpWord->addSection();
349+
$html = '<p><a href="http://phpword.readthedocs.io/" style="text-decoration: underline">link text</a></p>';
350+
Html::addHtml($section, $html);
351+
352+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
353+
354+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:hyperlink'));
355+
$this->assertEquals('link text', $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t')->nodeValue);
356+
}
344357
}

0 commit comments

Comments
 (0)