Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions samples/Sample_26_Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
$html .= '<p>Some well formed HTML snippet needs to be used</p>';
$html .= '<p>With for example <strong>some<sup>1</sup> <em>inline</em> formatting</strong><sub>1</sub></p>';

$html .= '<p>A link to <a href="http://phpword.readthedocs.io/">Read the docs</a></p>';

$html .= '<p style="margin-top: 240pt;">Unordered (bulleted) list:</p>';
$html .= '<ul><li>Item 1</li><li>Item 2</li><ul><li>Item 2.1</li><li>Item 2.1</li></ul></ul>';

Expand Down
23 changes: 23 additions & 0 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ protected static function parseNode($node, $element, $styles = array(), $data =
'li' => array('ListItem', $node, $element, $styles, $data, null, null),
'img' => array('Image', $node, $element, $styles, null, null, null),
'br' => array('LineBreak', null, $element, $styles, null, null, null),
'a' => array('Link', $node, $element, $styles, null, null, null),
);

$newElement = null;
Expand Down Expand Up @@ -643,4 +644,26 @@ private static function parseLineBreak($element)
{
$element->addTextBreak();
}

/**
* Parse link node
*
* @param \DOMNode $node
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
* @param array $styles
*/
private static function parseLink($node, $element, &$styles)
{
$target = null;
foreach ($node->attributes as $attribute) {
switch ($attribute->name) {
case 'href':
$target = $attribute->value;
break;
}
}
self::parseInlineStyle($node, $styles['font']);

return $element->addLink($target, $node->textContent, $styles['font'], $styles['paragraph']);
}
}
13 changes: 13 additions & 0 deletions tests/PhpWord/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,17 @@ public function testParseImage()
$this->assertStringMatchesFormat('%Smso-position-horizontal:right%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
$this->assertStringMatchesFormat('%Smso-position-horizontal:left%S', $doc->getElementAttribute($baseXpath . '[2]/w:pict/v:shape', 'style'));
}

public function testParseLink()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p><a href="http://phpword.readthedocs.io/" style="text-decoration: underline">link text</a></p>';
Html::addHtml($section, $html);

$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

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