Skip to content

Commit 377fb99

Browse files
committed
Add HTML writer for Bookmarks + tests
1 parent 874c6d6 commit 377fb99

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ v0.15.0 (?? ??? 2018)
1515
- Fix the size unit of when parsing html images - @troosan #1254
1616
- Fixed HTML parsing of nested lists - @troosan #1265
1717
- Save PNG alpha information when using remote images. @samsullivan #779
18-
- fix parsing of `<w:br/>` tag. @troosan #1274
18+
- Fix parsing of `<w:br/>` tag. @troosan #1274
19+
- Bookmark are not writton as internal link in html writer @troosan #1263
1920

2021

2122

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2017 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\HTML\Element;
19+
20+
/**
21+
* Bookmark element HTML writer
22+
*
23+
* @since 0.15.0
24+
*/
25+
class Bookmark extends Text
26+
{
27+
/**
28+
* Write bookmark
29+
*
30+
* @return string
31+
*/
32+
public function write()
33+
{
34+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Bookmark) {
35+
return '';
36+
}
37+
38+
$content = '';
39+
$content .= $this->writeOpening();
40+
$content .= "<a name=\"{$this->element->getName()}\"/>";
41+
$content .= $this->writeClosing();
42+
43+
return $content;
44+
}
45+
}

src/PhpWord/Writer/HTML/Element/Link.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public function write()
3737
return '';
3838
}
3939

40-
$content = '';
41-
$content .= $this->writeOpening();
40+
$prefix = $this->element->isInternal() ? '#' : '';
41+
$content = $this->writeOpening();
4242
if (Settings::isOutputEscapingEnabled()) {
43-
$content .= "<a href=\"{$this->escaper->escapeHtmlAttr($this->element->getSource())}\">{$this->escaper->escapeHtml($this->element->getText())}</a>";
43+
$content .= "<a href=\"{$prefix}{$this->escaper->escapeHtmlAttr($this->element->getSource())}\">{$this->escaper->escapeHtml($this->element->getText())}</a>";
4444
} else {
45-
$content .= "<a href=\"{$this->element->getSource()}\">{$this->element->getText()}</a>";
45+
$content .= "<a href=\"{$prefix}{$this->element->getSource()}\">{$this->element->getText()}</a>";
4646
}
4747
$content .= $this->writeClosing();
4848

tests/PhpWord/Writer/HTML/ElementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ElementTest extends \PHPUnit\Framework\TestCase
3131
*/
3232
public function testUnmatchedElements()
3333
{
34-
$elements = array('Container', 'Footnote', 'Image', 'Link', 'ListItem', 'Table', 'Title');
34+
$elements = array('Container', 'Footnote', 'Image', 'Link', 'ListItem', 'Table', 'Title', 'Bookmark');
3535
foreach ($elements as $element) {
3636
$objectClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element\\' . $element;
3737
$parentWriter = new HTML();

tests/PhpWord/Writer/HTMLTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function testSave()
7373
);
7474
$phpWord->addParagraphStyle('Paragraph', array('alignment' => Jc::CENTER, 'spaceAfter' => 20, 'spaceBefore' => 20));
7575
$section = $phpWord->addSection();
76+
$section->addBookmark('top');
7677
$section->addText(htmlspecialchars('Test 1', ENT_COMPAT, 'UTF-8'), 'Font', 'Paragraph');
7778
$section->addTextBreak();
7879
$section->addText(
@@ -128,6 +129,7 @@ public function testSave()
128129
$cell->addFootnote();
129130
$cell->addEndnote();
130131
$cell = $table->addRow()->addCell();
132+
$section->addLink('top', 'back to top', null, null, true);
131133

132134
$writer = new HTML($phpWord);
133135

0 commit comments

Comments
 (0)