@@ -120,22 +120,22 @@ private static function hash($assignment, $userid, $attemptnumber) {
120
120
* @return string New html with no image tags.
121
121
*/
122
122
protected static function strip_images ($ html ) {
123
+ // Load HTML and suppress any parsing errors (DOMDocument->loadHTML() does not current support HTML5 tags).
123
124
$ 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 );
135
138
}
136
-
137
- $ text = $ dom ->createTextNode ($ replacement );
138
- $ node ->parentNode ->replaceChild ($ text , $ node );
139
139
}
140
140
$ count = 1 ;
141
141
return str_replace ("<?xml version= \"1.0 \" encoding= \"UTF-8 \" ?> " , "" , $ dom ->saveHTML (), $ count );
0 commit comments