#483. Output escaping for ODF.
This commit is contained in:
parent
a2d307936d
commit
ebbb3a525e
|
|
@ -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.
|
||||
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
$xmlWriter->writeRaw($element->getText());
|
||||
if (Settings::isOutputEscapingEnabled()) {
|
||||
$xmlWriter->text($element->getText());
|
||||
} else {
|
||||
$xmlWriter->writeRaw($element->getText());
|
||||
}
|
||||
$xmlWriter->endElement(); // text:a
|
||||
|
||||
if (!$this->withoutP) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
$xmlWriter->writeRaw($element->getText());
|
||||
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);
|
||||
}
|
||||
$xmlWriter->writeRaw($element->getText());
|
||||
if (Settings::isOutputEscapingEnabled()) {
|
||||
$xmlWriter->text($element->getText());
|
||||
} else {
|
||||
$xmlWriter->writeRaw($element->getText());
|
||||
}
|
||||
$xmlWriter->endElement();
|
||||
}
|
||||
if (!$this->withoutP) {
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
$xmlWriter->writeRaw($element->getText());
|
||||
if (Settings::isOutputEscapingEnabled()) {
|
||||
$xmlWriter->text($element->getText());
|
||||
} else {
|
||||
$xmlWriter->writeRaw($element->getText());
|
||||
}
|
||||
$xmlWriter->endElement(); // text:h
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
// }
|
||||
$xmlWriter->writeRaw($value);
|
||||
if (Settings::isOutputEscapingEnabled()) {
|
||||
$xmlWriter->text($value);
|
||||
} else {
|
||||
$xmlWriter->writeRaw($value);
|
||||
}
|
||||
$xmlWriter->endElement(); // meta:user-defined
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue