Commit d879f296 authored by Son Nguyen's avatar Son Nguyen

Bug 1582778: Allow UTF-8 characters when preparing embedded images

Change-Id: I2057eea5916f1f820bb98765b641320f5b5f4fef
behatnotneeded: manual tests is enough
(cherry picked from commit 6bc4cd43)
parent b57ebf68
...@@ -39,7 +39,10 @@ class EmbeddedImage { ...@@ -39,7 +39,10 @@ class EmbeddedImage {
$dom = new DOMDocument(); $dom = new DOMDocument();
$dom->encoding = 'utf-8'; $dom->encoding = 'utf-8';
$oldval = libxml_use_internal_errors(true); $oldval = libxml_use_internal_errors(true);
$success = $dom->loadHTML(utf8_decode($fieldvalue)); $tmpstr = (mb_detect_encoding($fieldvalue, 'auto') == 'UTF-8')
? '<?xml version="1.0" encoding="utf-8"?>' . $fieldvalue
: $fieldvalue;
$success = $dom->loadHTML($tmpstr);
libxml_use_internal_errors($oldval); libxml_use_internal_errors($oldval);
if ($success) { if ($success) {
$publicimages = array(); $publicimages = array();
...@@ -120,10 +123,12 @@ class EmbeddedImage { ...@@ -120,10 +123,12 @@ class EmbeddedImage {
// we only want the fragments inside the body tag created by new DOMDocument // we only want the fragments inside the body tag created by new DOMDocument
$childnodes = $dom->getElementsByTagName('body')->item(0)->childNodes; $childnodes = $dom->getElementsByTagName('body')->item(0)->childNodes;
$dummydom = new DOMDocument(); $dummydom = new DOMDocument();
$dummydom->loadHTML('<?xml version="1.0" encoding="utf-8"?><div></div>');
$dummydiv = $dummydom->getElementsByTagName('div')->item(0);
foreach ($childnodes as $child) { foreach ($childnodes as $child) {
$dummydom->appendChild($dummydom->importNode($child, true)); $dummydiv->appendChild($dummydom->importNode($child, true));
} }
$fieldvalue = html_entity_decode($dummydom->saveHTML(), ENT_QUOTES, 'UTF-8'); $fieldvalue = substr($dummydom->saveHTML($dummydom->getElementsByTagName('div')->item(0)), strlen('<div>'), -strlen('</div>'));
return $fieldvalue; return $fieldvalue;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment