Add parsing of HTML links
This commit is contained in:
parent
4c68ebbe9d
commit
0425a25cdb
|
|
@ -10,6 +10,8 @@ $html = '<h1>Adding element via HTML</h1>';
|
|||
$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>';
|
||||
|
||||
|
|
|
|||
|
|
@ -142,6 +142,7 @@ class Html
|
|||
'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;
|
||||
|
|
@ -643,4 +644,26 @@ class Html
|
|||
{
|
||||
$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']);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -341,4 +341,17 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
|
|||
$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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue