diff --git a/filter.php b/filter.php index 5e5fbff..4f68aaa 100644 --- a/filter.php +++ b/filter.php @@ -465,7 +465,8 @@ private function replace($target, $fulltext) : string { $replacedlink = $fulltext; } - return $replacedlink; + // Wrap just smartmedia content inside a wrapper div for styling targeting. + return '
' . $replacedlink . '
'; } /** @@ -558,22 +559,20 @@ public function filter($text, array $options = array()) { // Open that as a new doc to pull the video node out. $tempdom = new DOMDocument('1.0', 'UTF-8'); @$tempdom->loadHTML($newtext, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); - $newvideo = $tempdom->getElementsByTagName('video')[0]; + $xpath = new DomXPath($tempdom); + $query = $xpath->query("//*[contains(@class, 'local-smartmedia-wrapper')]"); - if (is_null($newvideo)) { - // We must have replaced it with a placeholder. Use that as the new "video". - $newvideo = $tempdom->firstChild; + if (count($query) === 0) { + // Something went real wrong. + continue; } + $newvideo = $query->item(0); // Import that video node into the original DOM, and replace the original node. $imported = $originaldom->importNode($newvideo, true); - $video->parentNode->replaceChild($imported, $video); - - // If the tempdom has a form, then there is a button to transfer over. - if (count($tempdom->getElementsByTagName('form')) > 0) { - $element = $originaldom->importNode($tempdom->getElementsByTagName('form')[0], true); - $imported->parentNode->insertBefore($element, $imported->nextSibling); - } + // Replace target is the mediaplugin div 2 levels above the video. This swaps the whole block. + $replacetarget = $video->parentNode->parentNode; + $replacetarget->parentNode->replaceChild($imported, $replacetarget); } // We now need to check every in the Dom. @@ -614,30 +613,23 @@ public function filter($text, array $options = array()) { // Open that as a new doc to pull the video node out. $tempdom = new DOMDocument('1.0', 'UTF-8'); @$tempdom->loadHTML($newtext, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); - $newvideo = $tempdom->getElementsByTagName('video')[0]; + $xpath = new DomXPath($tempdom); + $query = $xpath->query("//*[contains(@class, 'local-smartmedia-wrapper')]"); - // If we haven't had a code replacement, just continue. - if (empty($newvideo)) { + if (count($query) === 0) { + // If we haven't had a code replacement, just continue. continue; } + $newvideo = $query->item(0); // Import that video node into the original DOM, and replace the original node. $imported = $originaldom->importNode($newvideo, true); $link->parentNode->replaceChild($imported, $link); - - // If the tempdom has a form, then there is a button to transfer over. - if (count($tempdom->getElementsByTagName('form')) > 0) { - $element = $originaldom->importNode($tempdom->getElementsByTagName('form')[0], true); - $imported->parentNode->insertBefore($element, $imported->nextSibling); - } } // The raw html minus the wrapping div. $html = substr(trim($originaldom->saveHTML()), 5, -6); - // Add a wrapper class to all smart media video content, so it can be styled further later on. - $html = preg_replace('/(mediaplugin mediaplugin_videojs)/', '$1 local-smartmedia-wrapper', $html); - return $html; } diff --git a/tests/filter_test.php b/tests/filter_test.php index fcc4dff..e4d5fdb 100644 --- a/tests/filter_test.php +++ b/tests/filter_test.php @@ -288,6 +288,8 @@ public function test_get_placeholder_markkup() { public function test_filter_replace_dataprovider() { // Return [text, regex to match in output, match count]. + // All , Legit video link. [ @@ -323,32 +325,32 @@ public function test_filter_replace_dataprovider() { ], // Test , 2 legit elements. [ - '' . + '
' . html_writer::link('url.com/pluginfile.php/fake.mp4', 'My Fake Video'), '~pluginfile\.php.*?fakename\.mp4?~', 2 @@ -356,15 +358,15 @@ public function test_filter_replace_dataprovider() { // Test
then then