From f69885e7b92e4e149c7a535aaa6dabf5c4d57117 Mon Sep 17 00:00:00 2001 From: lubosdz Date: Wed, 22 Jul 2020 10:04:12 +0200 Subject: [PATCH] fix bug - don't decode double quotes inside double quoted string --- src/PhpWord/Shared/Html.php | 4 ++-- tests/PhpWord/Shared/HtmlTest.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index c8a7fa69..d3e452e4 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -62,10 +62,10 @@ class Html // Preprocess: remove all line ends, decode HTML entity, // fix ampersand and angle brackets and add body tag for HTML fragments $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 = 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) { $html = '' . $html . ''; diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index 93df9337..7a806c26 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -884,4 +884,22 @@ HTML; $this->assertTrue($doc->elementExists($xpath)); $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 = <<This would crash if inline quotes also decoded at loading XML into DOMDocument! +HTML; + + Html::addHtml($section, $html); + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + $this->assertTrue(is_object($doc)); + } }