From 6d03e5e5540a5196db48e1e4350de45f4e9bc619 Mon Sep 17 00:00:00 2001 From: Zig Tan Date: Fri, 29 Jun 2018 12:25:41 +0800 Subject: [PATCH 1/2] MDL-61052 assign: Improve strip_images() to avoid HTML5 tags errors --- .../editpdf/classes/document_services.php | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/mod/assign/feedback/editpdf/classes/document_services.php b/mod/assign/feedback/editpdf/classes/document_services.php index fa6dbfbb598..71477b7c435 100644 --- a/mod/assign/feedback/editpdf/classes/document_services.php +++ b/mod/assign/feedback/editpdf/classes/document_services.php @@ -120,25 +120,24 @@ EOD; * @return string New html with no image tags. */ protected static function strip_images($html) { + // Load HTML and suppress any parsing errors (DOMDocument->loadHTML() does not current support HTML5 tags). $dom = new DOMDocument(); - $dom->loadHTML("" . $html); - $images = $dom->getElementsByTagName('img'); - $i = 0; + libxml_use_internal_errors(true); + $dom->loadHTML('' . $html); + libxml_clear_errors(); - for ($i = ($images->length - 1); $i >= 0; $i--) { - $node = $images->item($i); + // Find all img tags. + if ($imgnodes = $dom->getElementsByTagName('img')) { + // Replace img nodes with the img alt text without overriding DOM elements. + for ($i = ($imgnodes->length - 1); $i >= 0; $i--) { + $imgnode = $imgnodes->item($i); + $alt = ($imgnode->hasAttribute('alt')) ? ' [ ' . $imgnode->getAttribute('alt') . ' ] ' : ' '; + $textnode = $dom->createTextNode($alt); - if ($node->hasAttribute('alt')) { - $replacement = ' [ ' . $node->getAttribute('alt') . ' ] '; - } else { - $replacement = ' '; + $imgnode->parentNode->replaceChild($textnode, $imgnode); } - - $text = $dom->createTextNode($replacement); - $node->parentNode->replaceChild($text, $node); } - $count = 1; - return str_replace("", "", $dom->saveHTML(), $count); + return preg_replace('/^<\?xml[^>[]*(\[[^]]*\])?>/', '', $dom->saveHTML()); } /** From 3bea0fa387ba507165028450d658473645647853 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Mon, 2 Jul 2018 14:35:39 +0800 Subject: [PATCH 2/2] MDL-61052 assign: Do not queue conversions Identical submissions do not require individual conversions from the document converter. Text passing through format_text cannot be trusted and is likely to be unique each time, even for the same text. --- mod/assign/feedback/editpdf/classes/document_services.php | 3 ++- mod/assign/submission/onlinetext/locallib.php | 7 +++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mod/assign/feedback/editpdf/classes/document_services.php b/mod/assign/feedback/editpdf/classes/document_services.php index 71477b7c435..384ccdd2bdd 100644 --- a/mod/assign/feedback/editpdf/classes/document_services.php +++ b/mod/assign/feedback/editpdf/classes/document_services.php @@ -137,7 +137,8 @@ EOD; $imgnode->parentNode->replaceChild($textnode, $imgnode); } } - return preg_replace('/^<\?xml[^>[]*(\[[^]]*\])?>/', '', $dom->saveHTML()); + $count = 1; + return str_replace("", "", $dom->saveHTML(), $count); } /** diff --git a/mod/assign/submission/onlinetext/locallib.php b/mod/assign/submission/onlinetext/locallib.php index b2365b31192..e5e8e1933cb 100644 --- a/mod/assign/submission/onlinetext/locallib.php +++ b/mod/assign/submission/onlinetext/locallib.php @@ -398,10 +398,9 @@ class assign_submission_onlinetext extends assign_submission_plugin { // Note that this check is the same logic as the result from the is_empty function but we do // not call it directly because we already have the submission record. if ($onlinetextsubmission && !empty($onlinetextsubmission->onlinetext)) { - $finaltext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this); - $formattedtext = format_text($finaltext, - $onlinetextsubmission->onlineformat, - array('context'=>$this->assignment->get_context())); + // Do not pass the text through format_text. The result may not be displayed in Moodle and + // may be passed to external services such as document conversion or portfolios. + $formattedtext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this); $head = ''; $submissioncontent = '' . $head . ''. $formattedtext . '';