Skip to content

Commit ebbb3a5

Browse files
author
Roman Syroeshko
committed
#483. Output escaping for ODF.
1 parent a2d3079 commit ebbb3a5

File tree

7 files changed

+49
-25
lines changed

7 files changed

+49
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ $objWriter->save('helloWorld.html');
149149
/* Note: we skip RTF, because it's not XML-based and requires a different example. */
150150
/* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */
151151
```
152-
:warning: Escape any string you pass to ODF/HTML document, otherwise it may get broken.
152+
:warning: Escape any string you pass to HTML document, otherwise it may get broken.
153153

154154
More examples are provided in the [samples folder](samples/). You can also read the [Developers' Documentation](http://phpword.readthedocs.org/) and the [API Documentation](http://phpoffice.github.io/PHPWord/docs/master/) for more detail.
155155

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
namespace PhpOffice\PhpWord\Writer\ODText\Element;
19+
use PhpOffice\PhpWord\Settings;
1920

2021
/**
2122
* Text element writer
@@ -42,7 +43,11 @@ public function write()
4243
$xmlWriter->startElement('text:a');
4344
$xmlWriter->writeAttribute('xlink:type', 'simple');
4445
$xmlWriter->writeAttribute('xlink:href', $element->getSource());
45-
$xmlWriter->writeRaw($element->getText());
46+
if (Settings::isOutputEscapingEnabled()) {
47+
$xmlWriter->text($element->getText());
48+
} else {
49+
$xmlWriter->writeRaw($element->getText());
50+
}
4651
$xmlWriter->endElement(); // text:a
4752

4853
if (!$this->withoutP) {

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PhpOffice\PhpWord\Writer\ODText\Element;
1919

2020
use PhpOffice\PhpWord\Exception\Exception;
21+
use PhpOffice\PhpWord\Settings;
2122

2223
/**
2324
* Text element writer
@@ -56,7 +57,11 @@ public function write()
5657
} elseif (is_string($paragraphStyle)) {
5758
$xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
5859
}
59-
$xmlWriter->writeRaw($element->getText());
60+
if (Settings::isOutputEscapingEnabled()) {
61+
$xmlWriter->text($element->getText());
62+
} else {
63+
$xmlWriter->writeRaw($element->getText());
64+
}
6065
} else {
6166
if (empty($paragraphStyle)) {
6267
$xmlWriter->writeAttribute('text:style-name', 'Standard');
@@ -68,7 +73,11 @@ public function write()
6873
if (is_string($fontStyle)) {
6974
$xmlWriter->writeAttribute('text:style-name', $fontStyle);
7075
}
71-
$xmlWriter->writeRaw($element->getText());
76+
if (Settings::isOutputEscapingEnabled()) {
77+
$xmlWriter->text($element->getText());
78+
} else {
79+
$xmlWriter->writeRaw($element->getText());
80+
}
7281
$xmlWriter->endElement();
7382
}
7483
if (!$this->withoutP) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
namespace PhpOffice\PhpWord\Writer\ODText\Element;
19+
use PhpOffice\PhpWord\Settings;
1920

2021
/**
2122
* Title element writer
@@ -37,7 +38,11 @@ public function write()
3738

3839
$xmlWriter->startElement('text:h');
3940
$xmlWriter->writeAttribute('text:outline-level', $element->getDepth());
40-
$xmlWriter->writeRaw($element->getText());
41+
if (Settings::isOutputEscapingEnabled()) {
42+
$xmlWriter->text($element->getText());
43+
} else {
44+
$xmlWriter->writeRaw($element->getText());
45+
}
4146
$xmlWriter->endElement(); // text:h
4247
}
4348
}

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

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

2020
use PhpOffice\Common\XMLWriter;
21+
use PhpOffice\PhpWord\Settings;
2122

2223
/**
2324
* ODText meta part writer: meta.xml
@@ -100,7 +101,11 @@ private function writeCustomProperty(XMLWriter $xmlWriter, $property, $value)
100101
// if ($type !== null) {
101102
// $xmlWriter->writeAttribute('meta:value-type', $type);
102103
// }
103-
$xmlWriter->writeRaw($value);
104+
if (Settings::isOutputEscapingEnabled()) {
105+
$xmlWriter->text($value);
106+
} else {
107+
$xmlWriter->writeRaw($value);
108+
}
104109
$xmlWriter->endElement(); // meta:user-defined
105110
}
106111
}

tests/PhpWord/Writer/ODText/Part/ContentTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,35 @@ public function testWriteContent()
5656
$phpWord->addTableStyle('tblStyle', array('width' => 100));
5757

5858
$section = $phpWord->addSection(array('colsNum' => 2));
59-
$section->addText(htmlspecialchars($expected, ENT_COMPAT, 'UTF-8'));
60-
$section->addText(htmlspecialchars('Test font style', ENT_COMPAT, 'UTF-8'), 'Font');
61-
$section->addText(htmlspecialchars('Test paragraph style', ENT_COMPAT, 'UTF-8'), null, 'Paragraph');
62-
$section->addLink('https://github.com/PHPOffice/PHPWord', htmlspecialchars('PHPWord on GitHub', ENT_COMPAT, 'UTF-8'));
63-
$section->addTitle(htmlspecialchars('Test title', ENT_COMPAT, 'UTF-8'), 1);
59+
$section->addText($expected);
60+
$section->addText('Test font style', 'Font');
61+
$section->addText('Test paragraph style', null, 'Paragraph');
62+
$section->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub');
63+
$section->addTitle('Test title', 1);
6464
$section->addTextBreak();
6565
$section->addPageBreak();
66-
$section->addListItem(htmlspecialchars('Test list item', ENT_COMPAT, 'UTF-8'));
66+
$section->addListItem('Test list item');
6767
$section->addImage($imageSrc, array('width' => 50));
6868
$section->addObject($objectSrc);
6969
$section->addTOC();
7070

7171
$textrun = $section->addTextRun();
72-
$textrun->addText(htmlspecialchars('Test text run', ENT_COMPAT, 'UTF-8'));
72+
$textrun->addText('Test text run');
7373

7474
$table = $section->addTable(array('width' => 50));
7575
$cell = $table->addRow()->addCell();
7676
$cell = $table->addRow()->addCell();
77-
$cell->addText(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'));
78-
$cell->addLink('https://github.com/PHPOffice/PHPWord', htmlspecialchars('PHPWord on GitHub', ENT_COMPAT, 'UTF-8'));
77+
$cell->addText('Test');
78+
$cell->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub');
7979
$cell->addTextBreak();
80-
$cell->addListItem(htmlspecialchars('Test list item', ENT_COMPAT, 'UTF-8'));
80+
$cell->addListItem('Test list item');
8181
$cell->addImage($imageSrc);
8282
$cell->addObject($objectSrc);
8383
$textrun = $cell->addTextRun();
84-
$textrun->addText(htmlspecialchars('Test text run', ENT_COMPAT, 'UTF-8'));
84+
$textrun->addText('Test text run');
8585

8686
$footer = $section->addFooter();
87-
$footer->addPreserveText(htmlspecialchars('{PAGE}', ENT_COMPAT, 'UTF-8'));
87+
$footer->addPreserveText('{PAGE}');
8888

8989
$table = $section->addTable('tblStyle')->addRow()->addCell();
9090

tests/PhpWord/Writer/ODTextTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,20 @@ public function testSave()
7373
$phpWord->addFontStyle('Font', array('size' => 11));
7474
$phpWord->addParagraphStyle('Paragraph', array('alignment' => Jc::CENTER));
7575
$section = $phpWord->addSection();
76-
$section->addText(htmlspecialchars('Test 1', ENT_COMPAT, 'UTF-8'), 'Font');
76+
$section->addText('Test 1', 'Font');
7777
$section->addTextBreak();
78-
$section->addText(htmlspecialchars('Test 2', ENT_COMPAT, 'UTF-8'), null, 'Paragraph');
78+
$section->addText('Test 2', null, 'Paragraph');
7979
$section->addLink('https://github.com/PHPOffice/PHPWord');
80-
$section->addTitle(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'), 1);
80+
$section->addTitle('Test', 1);
8181
$section->addPageBreak();
82-
$section->addTable()->addRow()->addCell()->addText(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'));
83-
$section->addListItem(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'));
82+
$section->addTable()->addRow()->addCell()->addText('Test');
83+
$section->addListItem('Test');
8484
$section->addImage($imageSrc);
8585
$section->addObject($objectSrc);
8686
$section->addTOC();
8787
$section = $phpWord->addSection();
8888
$textrun = $section->addTextRun();
89-
$textrun->addText(htmlspecialchars('Test 3', ENT_COMPAT, 'UTF-8'));
89+
$textrun->addText('Test 3');
9090
$writer = new ODText($phpWord);
9191
$writer->save($file);
9292

@@ -104,7 +104,7 @@ public function testSavePhpOutput()
104104
{
105105
$phpWord = new PhpWord();
106106
$section = $phpWord->addSection();
107-
$section->addText(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'));
107+
$section->addText('Test');
108108
$writer = new ODText($phpWord);
109109
$writer->save('php://output');
110110
}

0 commit comments

Comments
 (0)