Skip to content

Commit

Permalink
Fix catalyst#234 pick up images from inline style background image
Browse files Browse the repository at this point in the history
  • Loading branch information
John Yao committed Feb 12, 2021
1 parent 8f7d38f commit c865848
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions classes/local/controllers/maintenance_static_page_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) == '') {
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit c865848

Please sign in to comment.