Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #234 pick up images from inline style background image #254

Open
wants to merge 1 commit into
base: MOODLE_39_STABLE
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -218,6 +219,23 @@ private function update_images() {
}
}

/**
* Fetch and fixes all inline background images.
*/
private function update_inline_background_images() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might work for this one use case but there will be a bunch of other similar use cases such as

background: url()

I'm wondering if it would be better to only match on the url() in the regex.

I'd also want some negative unit tests, eg make sure a url('data:xxxx') url isn not rewritten

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