Skip to content

Commit 13cdd0f

Browse files
authored
Merge pull request #42 from catalyst/fix-domelement-exception
fix: anchor tag handling
2 parents d5c48fa + 74a3b86 commit 13cdd0f

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

filter.php

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class filter_smartmedia extends moodle_text_filter {
7171
*
7272
* @var array
7373
*/
74-
private $mediatypes = array(
74+
private $mediatypes = [
7575
'aac', // Type: audio/aac.
7676
'au', // Type: audio/au.
7777
'mp3', // Type: audio/mp3.
@@ -104,19 +104,19 @@ class filter_smartmedia extends moodle_text_filter {
104104
'asf', // Type: video/x-ms-asf.
105105
'avi', // Type: video/x-ms-wm.
106106
'wmv', // Type: video/x-ms-wmv.
107-
);
107+
];
108108

109109
/**
110110
* Types of media that most browsers will play natively.
111111
*
112112
* @var array
113113
*/
114-
private $browsernative = array(
114+
private $browsernative = [
115115
'.mp3', // Type: audio/mp3.
116116
'.ogg', // Type: audio/ogg.
117117
'.mp4', // Type: video/mp4.
118118
'.webm', // Type: video/webm.
119-
);
119+
];
120120

121121
/**
122122
* Conversion controller used for filtering.
@@ -207,9 +207,9 @@ private function get_browser_native_types() : string {
207207
* @return array $elements The smart media elements to embed.
208208
*/
209209
private function get_smart_elements(string $linkhref) : array {
210-
$urls = array();
211-
$options = array();
212-
$elements = array();
210+
$urls = [];
211+
$options = [];
212+
$elements = [];
213213
$moodleurl = new \moodle_url($linkhref);
214214

215215
$smartmedia = $this->conversion->get_smart_media($moodleurl);
@@ -223,12 +223,12 @@ private function get_smart_elements(string $linkhref) : array {
223223
$options['height'] = core_media_player_native::get_attribute($linkhref, 'height', PARAM_INT);
224224
$options['name'] = core_media_player_native::get_attribute($linkhref, 'title');
225225

226-
$elements = array(
226+
$elements = [
227227
'urls' => $urls,
228228
'options' => $options,
229229
'download' => $smartmedia['download'],
230-
'metadata' => $smartmedia['data']
231-
);
230+
'metadata' => $smartmedia['data'],
231+
];
232232
}
233233

234234
return [$smartmedia['context'], $elements];
@@ -267,7 +267,7 @@ private function get_embed_markup(string $linkhref, array $urls, array $options,
267267
$name = $options['name'];
268268
$width = $options['width'];
269269
$height = $options['height'];
270-
$embedoptions = array();
270+
$embedoptions = [];
271271
$downloaddata = '<video ';
272272

273273
$videojs = new \media_videojs_plugin();
@@ -290,8 +290,8 @@ private function get_embed_markup(string $linkhref, array $urls, array $options,
290290
$decoded = json_decode(htmlspecialchars_decode($originalvalue));
291291
$decoded->html5 = [
292292
'hls' => [
293-
'enableLowInitialPlaylist' => true
294-
]
293+
'enableLowInitialPlaylist' => true,
294+
],
295295
];
296296

297297
$newvalue = htmlspecialchars(json_encode($decoded));
@@ -302,9 +302,9 @@ private function get_embed_markup(string $linkhref, array $urls, array $options,
302302
if (!empty($download)) {
303303
foreach ($download as $url) {
304304
if ($this->string_ends_with($url->out(), '.mp4')) {
305-
$downloaddata .= 'data-download-video="' . $url->out(true, array('forcedownload' => true)). '" ';
305+
$downloaddata .= 'data-download-video="' . $url->out(true, ['forcedownload' => true]). '" ';
306306
} else if ($this->string_ends_with($url->out(), '.mp3')) {
307-
$downloaddata .= 'data-download-audio="' . $url->out(true, array('forcedownload' => true)). '" ';
307+
$downloaddata .= 'data-download-audio="' . $url->out(true, ['forcedownload' => true]). '" ';
308308
}
309309
}
310310
$newtext = preg_replace('/\<video /', $downloaddata, $newtext);
@@ -318,7 +318,7 @@ private function get_embed_markup(string $linkhref, array $urls, array $options,
318318
new moodle_url('/filter/smartmedia/download_metadata.php', [
319319
'sesskey' => sesskey(),
320320
'conv' => base64_encode($linkhref),
321-
'title' => base64_encode(end($components))
321+
'title' => base64_encode(end($components)),
322322
]),
323323
get_string('downloadmetadata', 'filter_smartmedia')
324324
) . '';
@@ -475,7 +475,7 @@ private function replace($target, $fulltext) : array {
475475
$replacedlink = \html_writer::div($this->get_placeholder_markup($target, $fulltext), 'local-smartmedia-wrapper');
476476
$replaced = true;
477477
} else {
478-
// Do nothing, no replacement candidate
478+
// Do nothing, no replacement candidate.
479479
$replacedlink = $fulltext;
480480
$replaced = false;
481481
}
@@ -535,7 +535,7 @@ private function url_from_context($context) {
535535
* @param array $options Extra options.
536536
* @return string $newtext The filtered Text.
537537
*/
538-
public function filter($text, array $options = array()) {
538+
public function filter($text, array $options = []) {
539539
global $SESSION;
540540

541541
// First check the page URL's for flags. Prevents Ajax load missing them.
@@ -565,7 +565,7 @@ public function filter($text, array $options = array()) {
565565
return $text;
566566
}
567567

568-
if (!is_string($text) or empty($text)) {
568+
if (!is_string($text) || empty($text)) {
569569
// Non string data can not be filtered anyway.
570570
return $text;
571571
}
@@ -653,8 +653,13 @@ public function filter($text, array $options = array()) {
653653
// Check if this link node still exists. That's the ones we want.
654654
$exists = false;
655655
foreach ($newlinks as $newlink) {
656-
if ($link->isSameNode($newlink)) {
657-
$exists = true;
656+
try {
657+
if ($link->isSameNode($newlink)) {
658+
$exists = true;
659+
}
660+
} catch (\Throwable $e) {
661+
// Some error, likely when the $link is no longer a valid DOMElement.
662+
continue;
658663
}
659664
}
660665
if (!$exists) {

0 commit comments

Comments
 (0)