Add HTML writer for Bookmarks + tests

This commit is contained in:
troosan 2018-02-10 23:43:15 +01:00
parent 874c6d6fb6
commit 377fb99fbc
5 changed files with 54 additions and 6 deletions

View File

@ -15,7 +15,8 @@ v0.15.0 (?? ??? 2018)
- Fix the size unit of when parsing html images - @troosan #1254
- Fixed HTML parsing of nested lists - @troosan #1265
- Save PNG alpha information when using remote images. @samsullivan #779
- fix parsing of `<w:br/>` tag. @troosan #1274
- Fix parsing of `<w:br/>` tag. @troosan #1274
- Bookmark are not writton as internal link in html writer @troosan #1263

View File

@ -0,0 +1,45 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2017 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\HTML\Element;
/**
* Bookmark element HTML writer
*
* @since 0.15.0
*/
class Bookmark extends Text
{
/**
* Write bookmark
*
* @return string
*/
public function write()
{
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Bookmark) {
return '';
}
$content = '';
$content .= $this->writeOpening();
$content .= "<a name=\"{$this->element->getName()}\"/>";
$content .= $this->writeClosing();
return $content;
}
}

View File

@ -37,12 +37,12 @@ class Link extends Text
return '';
}
$content = '';
$content .= $this->writeOpening();
$prefix = $this->element->isInternal() ? '#' : '';
$content = $this->writeOpening();
if (Settings::isOutputEscapingEnabled()) {
$content .= "<a href=\"{$this->escaper->escapeHtmlAttr($this->element->getSource())}\">{$this->escaper->escapeHtml($this->element->getText())}</a>";
$content .= "<a href=\"{$prefix}{$this->escaper->escapeHtmlAttr($this->element->getSource())}\">{$this->escaper->escapeHtml($this->element->getText())}</a>";
} else {
$content .= "<a href=\"{$this->element->getSource()}\">{$this->element->getText()}</a>";
$content .= "<a href=\"{$prefix}{$this->element->getSource()}\">{$this->element->getText()}</a>";
}
$content .= $this->writeClosing();

View File

@ -31,7 +31,7 @@ class ElementTest extends \PHPUnit\Framework\TestCase
*/
public function testUnmatchedElements()
{
$elements = array('Container', 'Footnote', 'Image', 'Link', 'ListItem', 'Table', 'Title');
$elements = array('Container', 'Footnote', 'Image', 'Link', 'ListItem', 'Table', 'Title', 'Bookmark');
foreach ($elements as $element) {
$objectClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element\\' . $element;
$parentWriter = new HTML();

View File

@ -73,6 +73,7 @@ class HTMLTest extends \PHPUnit\Framework\TestCase
);
$phpWord->addParagraphStyle('Paragraph', array('alignment' => Jc::CENTER, 'spaceAfter' => 20, 'spaceBefore' => 20));
$section = $phpWord->addSection();
$section->addBookmark('top');
$section->addText(htmlspecialchars('Test 1', ENT_COMPAT, 'UTF-8'), 'Font', 'Paragraph');
$section->addTextBreak();
$section->addText(
@ -128,6 +129,7 @@ class HTMLTest extends \PHPUnit\Framework\TestCase
$cell->addFootnote();
$cell->addEndnote();
$cell = $table->addRow()->addCell();
$section->addLink('top', 'back to top', null, null, true);
$writer = new HTML($phpWord);