From c8658480a7df345b2c132d6309509c768a0fb566 Mon Sep 17 00:00:00 2001 From: John Yao Date: Fri, 12 Feb 2021 16:25:52 +1100 Subject: [PATCH] Fix #234 pick up images from inline style background image --- .../maintenance_static_page_generator.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/classes/local/controllers/maintenance_static_page_generator.php b/classes/local/controllers/maintenance_static_page_generator.php index 986750a..b15a7db 100644 --- a/classes/local/controllers/maintenance_static_page_generator.php +++ b/classes/local/controllers/maintenance_static_page_generator.php @@ -83,6 +83,7 @@ public function generate() { $this->update_link_favicon(); $this->update_images(); $this->remove_configured_css_selectors(); + $this->update_inline_background_images(); $html = $this->dom->saveHTML(); if (trim($html) == '') { @@ -217,6 +218,23 @@ private function update_images() { } } + /** + * Fetch and fixes all inline background images. + */ + private function update_inline_background_images() { + $xpath = new \DOMXPath($this->dom); + $elements = $xpath->query("//*[@style]"); + + foreach ($elements as $element) { + $style = $element->getAttribute("style"); + preg_match('/(?<=background-image)\s*:\s*url\s*\(\s*(\S+)\s*\);/', $style, $matches); + if (isset($matches[1])) { + $updated = preg_replace("/(?<=background-image)\s*:\s*url\s*\(\s*(\S+)\s*\);/", ': url(' . $this->io->generate_file_url($matches[1]) . '); ', $style); + $element->setAttribute('style', $updated); + } + } + } + /** * Remove from DOM the CSS selectores defined in the plugin settings. */