Skip to content

Commit 6a460c2

Browse files
authored
Merge pull request #1206 from troosan/odt_page_break
Implement PageBreak for the ODText writer
2 parents 6faddc6 + def9123 commit 6a460c2

File tree

7 files changed

+62
-31
lines changed

7 files changed

+62
-31
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Change Log
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6-
v0.14.0 (?? ???? 2017)
6+
v0.14.0 (?? Dec 2017)
77
----------------------
88
This release fixes several bugs and adds some new features.
99
This is the last version to support PHP 5.3
@@ -20,6 +20,7 @@ This is the last version to support PHP 5.3
2020
- Add support for HTML underline tag <u> in addHtml - @zNightFalLz #1186
2121
- Allow to change cell width unit - @guillaume-ro-fr #986
2222
- Allow to change the line height rule @troosan
23+
- Implement PageBreak for odt writer @cookiekiller #863 #824
2324
- Allow to force an update of all fields on opening a document - @troosan #951
2425

2526
### Fixed

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\ODText\Element;
1919

20-
use PhpOffice\PhpWord\Settings;
21-
2220
/**
2321
* Text element writer
2422
*
@@ -44,11 +42,7 @@ public function write()
4442
$xmlWriter->startElement('text:a');
4543
$xmlWriter->writeAttribute('xlink:type', 'simple');
4644
$xmlWriter->writeAttribute('xlink:href', $element->getSource());
47-
if (Settings::isOutputEscapingEnabled()) {
48-
$xmlWriter->text($element->getText());
49-
} else {
50-
$xmlWriter->writeRaw($element->getText());
51-
}
45+
$this->writeText($element->getText());
5246
$xmlWriter->endElement(); // text:a
5347

5448
if (!$this->withoutP) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\ODText\Element;
19+
20+
/**
21+
* PageBreak element writer
22+
*/
23+
class PageBreak extends AbstractElement
24+
{
25+
/**
26+
* Write element
27+
*/
28+
public function write()
29+
{
30+
$xmlWriter = $this->getXmlWriter();
31+
32+
$xmlWriter->startElement('text:p');
33+
$xmlWriter->writeAttribute('text:style-name', 'P1');
34+
$xmlWriter->endElement();
35+
}
36+
}

src/PhpWord/Writer/ODText/Element/Text.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ public function write()
5858
} elseif (is_string($paragraphStyle)) {
5959
$xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
6060
}
61-
if (Settings::isOutputEscapingEnabled()) {
62-
$xmlWriter->text($element->getText());
63-
} else {
64-
$xmlWriter->writeRaw($element->getText());
65-
}
61+
$this->writeText($element->getText());
6662
} else {
6763
if (empty($paragraphStyle)) {
6864
$xmlWriter->writeAttribute('text:style-name', 'Standard');
@@ -74,11 +70,7 @@ public function write()
7470
if (is_string($fontStyle)) {
7571
$xmlWriter->writeAttribute('text:style-name', $fontStyle);
7672
}
77-
if (Settings::isOutputEscapingEnabled()) {
78-
$xmlWriter->text($element->getText());
79-
} else {
80-
$xmlWriter->writeRaw($element->getText());
81-
}
73+
$this->writeText($element->getText());
8274
$xmlWriter->endElement();
8375
}
8476
if (!$this->withoutP) {

src/PhpWord/Writer/ODText/Element/Title.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\ODText\Element;
1919

20-
use PhpOffice\PhpWord\Settings;
21-
2220
/**
2321
* Title element writer
2422
*
@@ -39,11 +37,7 @@ public function write()
3937

4038
$xmlWriter->startElement('text:h');
4139
$xmlWriter->writeAttribute('text:outline-level', $element->getDepth());
42-
if (Settings::isOutputEscapingEnabled()) {
43-
$xmlWriter->text($element->getText());
44-
} else {
45-
$xmlWriter->writeRaw($element->getText());
46-
}
40+
$this->writeText($element->getText());
4741
$xmlWriter->endElement(); // text:h
4842
}
4943
}

src/PhpWord/Writer/ODText/Part/Meta.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
namespace PhpOffice\PhpWord\Writer\ODText\Part;
1919

2020
use PhpOffice\Common\XMLWriter;
21-
use PhpOffice\PhpWord\Settings;
2221

2322
/**
2423
* ODText meta part writer: meta.xml
@@ -100,11 +99,7 @@ private function writeCustomProperty(XMLWriter $xmlWriter, $property, $value)
10099
// if ($type !== null) {
101100
// $xmlWriter->writeAttribute('meta:value-type', $type);
102101
// }
103-
if (Settings::isOutputEscapingEnabled()) {
104-
$xmlWriter->text($value);
105-
} else {
106-
$xmlWriter->writeRaw($value);
107-
}
102+
$this->writeText($value);
108103
$xmlWriter->endElement(); // meta:user-defined
109104
}
110105
}

tests/PhpWord/Writer/ODText/ElementTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
namespace PhpOffice\PhpWord\Writer\ODText;
1919

2020
use PhpOffice\Common\XMLWriter;
21+
use PhpOffice\PhpWord\PhpWord;
22+
use PhpOffice\PhpWord\TestHelperDOCX;
2123

2224
/**
2325
* Test class for PhpOffice\PhpWord\Writer\ODText\Element subnamespace
@@ -40,4 +42,21 @@ public function testUnmatchedElements()
4042
$this->assertEquals('', $xmlWriter->getData());
4143
}
4244
}
45+
46+
/**
47+
* Test PageBreak
48+
*/
49+
public function testPageBreak()
50+
{
51+
$phpWord = new PhpWord();
52+
$section = $phpWord->addSection();
53+
$section->addText('test');
54+
$section->addPageBreak();
55+
56+
$doc = TestHelperDOCX::getDocument($phpWord, 'ODText');
57+
58+
$element = '/office:document-content/office:body/office:text/text:section/text:p[2]';
59+
$this->assertTrue($doc->elementExists($element, 'content.xml'));
60+
$this->assertEquals('P1', $doc->getElementAttribute($element, 'text:style-name', 'content.xml'));
61+
}
4362
}

0 commit comments

Comments
 (0)