#483. Output escaping for ODF.

This commit is contained in:
Roman Syroeshko 2016-06-13 20:14:54 +04:00
parent a2d307936d
commit ebbb3a525e
7 changed files with 49 additions and 25 deletions

View File

@ -149,7 +149,7 @@ $objWriter->save('helloWorld.html');
/* Note: we skip RTF, because it's not XML-based and requires a different example. */
/* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */
```
:warning: Escape any string you pass to ODF/HTML document, otherwise it may get broken.
:warning: Escape any string you pass to HTML document, otherwise it may get broken.
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.

View File

@ -16,6 +16,7 @@
*/
namespace PhpOffice\PhpWord\Writer\ODText\Element;
use PhpOffice\PhpWord\Settings;
/**
* Text element writer
@ -42,7 +43,11 @@ class Link extends AbstractElement
$xmlWriter->startElement('text:a');
$xmlWriter->writeAttribute('xlink:type', 'simple');
$xmlWriter->writeAttribute('xlink:href', $element->getSource());
if (Settings::isOutputEscapingEnabled()) {
$xmlWriter->text($element->getText());
} else {
$xmlWriter->writeRaw($element->getText());
}
$xmlWriter->endElement(); // text:a
if (!$this->withoutP) {

View File

@ -18,6 +18,7 @@
namespace PhpOffice\PhpWord\Writer\ODText\Element;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Settings;
/**
* Text element writer
@ -56,7 +57,11 @@ class Text extends AbstractElement
} elseif (is_string($paragraphStyle)) {
$xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
}
if (Settings::isOutputEscapingEnabled()) {
$xmlWriter->text($element->getText());
} else {
$xmlWriter->writeRaw($element->getText());
}
} else {
if (empty($paragraphStyle)) {
$xmlWriter->writeAttribute('text:style-name', 'Standard');
@ -68,7 +73,11 @@ class Text extends AbstractElement
if (is_string($fontStyle)) {
$xmlWriter->writeAttribute('text:style-name', $fontStyle);
}
if (Settings::isOutputEscapingEnabled()) {
$xmlWriter->text($element->getText());
} else {
$xmlWriter->writeRaw($element->getText());
}
$xmlWriter->endElement();
}
if (!$this->withoutP) {

View File

@ -16,6 +16,7 @@
*/
namespace PhpOffice\PhpWord\Writer\ODText\Element;
use PhpOffice\PhpWord\Settings;
/**
* Title element writer
@ -37,7 +38,11 @@ class Title extends AbstractElement
$xmlWriter->startElement('text:h');
$xmlWriter->writeAttribute('text:outline-level', $element->getDepth());
if (Settings::isOutputEscapingEnabled()) {
$xmlWriter->text($element->getText());
} else {
$xmlWriter->writeRaw($element->getText());
}
$xmlWriter->endElement(); // text:h
}
}

View File

@ -18,6 +18,7 @@
namespace PhpOffice\PhpWord\Writer\ODText\Part;
use PhpOffice\Common\XMLWriter;
use PhpOffice\PhpWord\Settings;
/**
* ODText meta part writer: meta.xml
@ -100,7 +101,11 @@ class Meta extends AbstractPart
// if ($type !== null) {
// $xmlWriter->writeAttribute('meta:value-type', $type);
// }
if (Settings::isOutputEscapingEnabled()) {
$xmlWriter->text($value);
} else {
$xmlWriter->writeRaw($value);
}
$xmlWriter->endElement(); // meta:user-defined
}
}

View File

@ -56,35 +56,35 @@ class ContentTest extends \PHPUnit_Framework_TestCase
$phpWord->addTableStyle('tblStyle', array('width' => 100));
$section = $phpWord->addSection(array('colsNum' => 2));
$section->addText(htmlspecialchars($expected, ENT_COMPAT, 'UTF-8'));
$section->addText(htmlspecialchars('Test font style', ENT_COMPAT, 'UTF-8'), 'Font');
$section->addText(htmlspecialchars('Test paragraph style', ENT_COMPAT, 'UTF-8'), null, 'Paragraph');
$section->addLink('https://github.com/PHPOffice/PHPWord', htmlspecialchars('PHPWord on GitHub', ENT_COMPAT, 'UTF-8'));
$section->addTitle(htmlspecialchars('Test title', ENT_COMPAT, 'UTF-8'), 1);
$section->addText($expected);
$section->addText('Test font style', 'Font');
$section->addText('Test paragraph style', null, 'Paragraph');
$section->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub');
$section->addTitle('Test title', 1);
$section->addTextBreak();
$section->addPageBreak();
$section->addListItem(htmlspecialchars('Test list item', ENT_COMPAT, 'UTF-8'));
$section->addListItem('Test list item');
$section->addImage($imageSrc, array('width' => 50));
$section->addObject($objectSrc);
$section->addTOC();
$textrun = $section->addTextRun();
$textrun->addText(htmlspecialchars('Test text run', ENT_COMPAT, 'UTF-8'));
$textrun->addText('Test text run');
$table = $section->addTable(array('width' => 50));
$cell = $table->addRow()->addCell();
$cell = $table->addRow()->addCell();
$cell->addText(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'));
$cell->addLink('https://github.com/PHPOffice/PHPWord', htmlspecialchars('PHPWord on GitHub', ENT_COMPAT, 'UTF-8'));
$cell->addText('Test');
$cell->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub');
$cell->addTextBreak();
$cell->addListItem(htmlspecialchars('Test list item', ENT_COMPAT, 'UTF-8'));
$cell->addListItem('Test list item');
$cell->addImage($imageSrc);
$cell->addObject($objectSrc);
$textrun = $cell->addTextRun();
$textrun->addText(htmlspecialchars('Test text run', ENT_COMPAT, 'UTF-8'));
$textrun->addText('Test text run');
$footer = $section->addFooter();
$footer->addPreserveText(htmlspecialchars('{PAGE}', ENT_COMPAT, 'UTF-8'));
$footer->addPreserveText('{PAGE}');
$table = $section->addTable('tblStyle')->addRow()->addCell();

View File

@ -73,20 +73,20 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
$phpWord->addFontStyle('Font', array('size' => 11));
$phpWord->addParagraphStyle('Paragraph', array('alignment' => Jc::CENTER));
$section = $phpWord->addSection();
$section->addText(htmlspecialchars('Test 1', ENT_COMPAT, 'UTF-8'), 'Font');
$section->addText('Test 1', 'Font');
$section->addTextBreak();
$section->addText(htmlspecialchars('Test 2', ENT_COMPAT, 'UTF-8'), null, 'Paragraph');
$section->addText('Test 2', null, 'Paragraph');
$section->addLink('https://github.com/PHPOffice/PHPWord');
$section->addTitle(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'), 1);
$section->addTitle('Test', 1);
$section->addPageBreak();
$section->addTable()->addRow()->addCell()->addText(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'));
$section->addListItem(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'));
$section->addTable()->addRow()->addCell()->addText('Test');
$section->addListItem('Test');
$section->addImage($imageSrc);
$section->addObject($objectSrc);
$section->addTOC();
$section = $phpWord->addSection();
$textrun = $section->addTextRun();
$textrun->addText(htmlspecialchars('Test 3', ENT_COMPAT, 'UTF-8'));
$textrun->addText('Test 3');
$writer = new ODText($phpWord);
$writer->save($file);
@ -104,7 +104,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'));
$section->addText('Test');
$writer = new ODText($phpWord);
$writer->save('php://output');
}