Skip to content

Commit f9f8edc

Browse files
author
David Monllao
committed
Merge branch 'MDL-61052-35' of git://github.com/damyon/moodle into MOODLE_35_STABLE
2 parents a9354db + 3bea0fa commit f9f8edc

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

mod/assign/feedback/editpdf/classes/document_services.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,22 @@ private static function hash($assignment, $userid, $attemptnumber) {
120120
* @return string New html with no image tags.
121121
*/
122122
protected static function strip_images($html) {
123+
// Load HTML and suppress any parsing errors (DOMDocument->loadHTML() does not current support HTML5 tags).
123124
$dom = new DOMDocument();
124-
$dom->loadHTML("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" . $html);
125-
$images = $dom->getElementsByTagName('img');
126-
$i = 0;
127-
128-
for ($i = ($images->length - 1); $i >= 0; $i--) {
129-
$node = $images->item($i);
130-
131-
if ($node->hasAttribute('alt')) {
132-
$replacement = ' [ ' . $node->getAttribute('alt') . ' ] ';
133-
} else {
134-
$replacement = ' ';
125+
libxml_use_internal_errors(true);
126+
$dom->loadHTML('<?xml version="1.0" encoding="UTF-8" ?>' . $html);
127+
libxml_clear_errors();
128+
129+
// Find all img tags.
130+
if ($imgnodes = $dom->getElementsByTagName('img')) {
131+
// Replace img nodes with the img alt text without overriding DOM elements.
132+
for ($i = ($imgnodes->length - 1); $i >= 0; $i--) {
133+
$imgnode = $imgnodes->item($i);
134+
$alt = ($imgnode->hasAttribute('alt')) ? ' [ ' . $imgnode->getAttribute('alt') . ' ] ' : ' ';
135+
$textnode = $dom->createTextNode($alt);
136+
137+
$imgnode->parentNode->replaceChild($textnode, $imgnode);
135138
}
136-
137-
$text = $dom->createTextNode($replacement);
138-
$node->parentNode->replaceChild($text, $node);
139139
}
140140
$count = 1;
141141
return str_replace("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>", "", $dom->saveHTML(), $count);

mod/assign/submission/onlinetext/locallib.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,9 @@ public function get_files(stdClass $submission, stdClass $user) {
398398
// Note that this check is the same logic as the result from the is_empty function but we do
399399
// not call it directly because we already have the submission record.
400400
if ($onlinetextsubmission && !empty($onlinetextsubmission->onlinetext)) {
401-
$finaltext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this);
402-
$formattedtext = format_text($finaltext,
403-
$onlinetextsubmission->onlineformat,
404-
array('context'=>$this->assignment->get_context()));
401+
// Do not pass the text through format_text. The result may not be displayed in Moodle and
402+
// may be passed to external services such as document conversion or portfolios.
403+
$formattedtext = $this->assignment->download_rewrite_pluginfile_urls($onlinetextsubmission->onlinetext, $user, $this);
405404
$head = '<head><meta charset="UTF-8"></head>';
406405
$submissioncontent = '<!DOCTYPE html><html>' . $head . '<body>'. $formattedtext . '</body></html>';
407406

0 commit comments

Comments
 (0)