Skip to content

Commit d17a806

Browse files
committed
Small fixes for addHtml.
Adding some HTML Entities to the test for addHTML
1 parent 4c04071 commit d17a806

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ public static function addHtml($element, $html, $fullHTML = false)
4242
* which could be applied when such an element occurs in the parseNode function.
4343
*/
4444

45-
// Preprocess: remove all line ends, decode HTML entity, and add body tag for HTML fragments
45+
// Preprocess: remove all line ends, decode HTML entity,
46+
// fix ampersand and angle brackets and add body tag for HTML fragments
4647
$html = str_replace(array("\n", "\r"), '', $html);
47-
$html = html_entity_decode($html);
48+
$html = str_replace(array('<', '>', '&'), array('_lt_', '_gt_', '_amp_'), $html);
49+
$html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
50+
$html = str_replace('&', '&', $html);
51+
$html = str_replace(array('_lt_', '_gt_', '_amp_'), array('<', '>', '&'), $html);
52+
4853
if ($fullHTML === false) {
4954
$html = '<body>' . $html . '</body>';
5055
}

tests/PhpWord/Tests/Shared/HtmlTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ public function testAddHtml()
6060
$content .= '<table><tr><th>Header</th><td>Content</td></tr></table>';
6161
$content .= '<ul><li>Bullet</li><ul><li>Bullet</li></ul></ul>';
6262
$content .= '<ol><li>Bullet</li></ol>';
63+
$content .= "'Single Quoted Text'";
64+
$content .= '"Double Quoted Text"';
65+
$content .= '& Ampersand';
66+
$content .= '&lt;&gt;&ldquo;&lsquo;&rsquo;&laquo;&raquo;&lsaquo;&rsaquo;';
67+
$content .= '&amp;&bull;&deg;&hellip;&trade;&copy;&reg;&mdash;';
68+
$content .= '&ndash;&nbsp;&emsp;&ensp;&sup2;&sup3;&frac14;&frac12;&frac34;';
6369
Html::addHtml($section, $content);
6470
}
6571
}

0 commit comments

Comments
 (0)