fix bug - don't decode double quotes inside double quoted string

This commit is contained in:
lubosdz 2020-07-22 10:04:12 +02:00
parent 4448bda721
commit f69885e7b9
2 changed files with 20 additions and 2 deletions

View File

@ -62,10 +62,10 @@ class Html
// Preprocess: remove all line ends, decode HTML entity, // Preprocess: remove all line ends, decode HTML entity,
// fix ampersand and angle brackets and add body tag for HTML fragments // fix ampersand and angle brackets and add body tag for HTML fragments
$html = str_replace(array("\n", "\r"), '', $html); $html = str_replace(array("\n", "\r"), '', $html);
$html = str_replace(array('<', '>', '&'), array('_lt_', '_gt_', '_amp_'), $html); $html = str_replace(array('<', '>', '&', '"'), array('_lt_', '_gt_', '_amp_', '_quot_'), $html);
$html = html_entity_decode($html, ENT_QUOTES, 'UTF-8'); $html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
$html = str_replace('&', '&', $html); $html = str_replace('&', '&', $html);
$html = str_replace(array('_lt_', '_gt_', '_amp_'), array('<', '>', '&'), $html); $html = str_replace(array('_lt_', '_gt_', '_amp_', '_quot_'), array('<', '>', '&', '"'), $html);
if (false === $fullHTML) { if (false === $fullHTML) {
$html = '<body>' . $html . '</body>'; $html = '<body>' . $html . '</body>';

View File

@ -884,4 +884,22 @@ HTML;
$this->assertTrue($doc->elementExists($xpath)); $this->assertTrue($doc->elementExists($xpath));
$this->assertEquals('bottom', $doc->getElement($xpath)->getAttribute('w:val')); $this->assertEquals('bottom', $doc->getElement($xpath)->getAttribute('w:val'));
} }
/**
* Fix bug - don't decode double quotes inside double quoted string
*/
public function testDontDecodeAlreadyEncodedDoubleQuotes()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
// borders & backgrounds are here just for better visual comparison
$html = <<<HTML
<div style="font-family: Arial, &quot;Helvetice Neue&quot;">This would crash if inline quotes also decoded at loading XML into DOMDocument!</div>
HTML;
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue(is_object($doc));
}
} }