Skip to content

Commit a6b1bee

Browse files
bschuilingpdobrescu
authored andcommitted
Fix : PictureController not double the definitions ( both original / webp ) but just use webp if available instead.
1 parent 2411cda commit a6b1bee

File tree

1 file changed

+23
-35
lines changed

1 file changed

+23
-35
lines changed

class/Controller/Front/PictureController.php

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ protected function convert($content)
105105

106106

107107
$pattern = '/<img[^>]*>/i';
108-
$count = 0;
109108
$content = preg_replace_callback($pattern, array($this, 'convertImage'), $content);
110109

111110
// [BS] No callback because we need preg_match_all
@@ -114,36 +113,6 @@ protected function convert($content)
114113
return $content;
115114
}
116115

117-
/** If lazy loading is happening, get source (src) from those values
118-
* Otherwise pass back image data in a regular way.
119-
*/
120-
private function lazyGet($img, $type)
121-
{
122-
123-
$value = false;
124-
$prefix = false;
125-
126-
if (isset($img['data-lazy-' . $type]) && strlen($img['data-lazy-' . $type]) > 0)
127-
{
128-
$value = $img['data-lazy-' . $type];
129-
$prefix = 'data-lazy-';
130-
}
131-
elseif( isset($img['data-' . $type]) && strlen($img['data-' . $type]) > 0)
132-
{
133-
$value = $img['data-' . $type];
134-
$prefix = 'data-';
135-
}
136-
elseif(isset($img[$type]) && strlen($img[$type]) > 0)
137-
{
138-
$value = $img[$type];
139-
$prefix = '';
140-
}
141-
142-
return array(
143-
'value' => $value,
144-
'prefix' => $prefix,
145-
);
146-
}
147116

148117
/* Find image tags within picture definitions and make sure they are converted only by block, */
149118
private function testPictures($content)
@@ -368,10 +337,7 @@ protected function convertInlineStyle($matches, $content)
368337
continue;
369338
}
370339
$url = $match[1];
371-
//$parsed_url = parse_url($url);
372340
$filename = basename($url);
373-
374-
$fileonly = pathinfo($url, PATHINFO_FILENAME);
375341
$ext = pathinfo($url, PATHINFO_EXTENSION);
376342

377343
if (! in_array($ext, $allowed_exts))
@@ -417,10 +383,15 @@ protected function convertInlineStyle($matches, $content)
417383
{
418384
// if webp, then add another URL() def after the targeted one. (str_replace old full URL def, with new one on main match?
419385
$target_urldef = $matches[0][$i];
386+
387+
// The target_urldef should remain original to be picked up by str_replace, but the original_definitions are what goes back of the original stuff and should be filtered.
388+
// $original_definitions = $this->filterForbiddenInline($target_urldef);
389+
420390
if (! isset($converted[$target_urldef])) // if the same image is on multiple elements, this replace might go double. prevent.
421391
{
422392
$converted[] = $target_urldef;
423-
$new_urldef = "url('" . $checkedFile . "') $image_data, " . $target_urldef;
393+
// Fix: The originals are not being put anymore because this would lead to double images and that's not a good thing.
394+
$new_urldef = "url('" . $checkedFile . "') $image_data ";
424395
$content = str_replace($target_urldef, $new_urldef, $content);
425396
}
426397
}
@@ -430,4 +401,21 @@ protected function convertInlineStyle($matches, $content)
430401
return $content;
431402
}
432403

404+
405+
/**
406+
* FilterForbiddenInline
407+
*
408+
* Filter tags like !important for the targetstring that should not be duplicated or causes issues otherwise.
409+
*
410+
* @param [String] $targetString
411+
* @return String
412+
*/
413+
protected function filterForbiddenInline($targetString)
414+
{
415+
$search = ['!important'];
416+
$targetString = str_replace($search, '', $targetString);
417+
418+
return $targetString;
419+
}
420+
433421
} // class

0 commit comments

Comments
 (0)