Skip to content

Commit

Permalink
Remove data-src and data-srcset from original attributes in sources
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Feb 27, 2024
1 parent 59a56c4 commit 4a799cb
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Block/Picture.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use DOMDocument;
use DOMNode;
use Exception;
use Magento\Framework\View\Element\Template;
use Yireo\NextGenImages\Image\Image;

Expand Down Expand Up @@ -263,7 +262,7 @@ public function getOriginalAttributes(): array

foreach ($originalNode->attributes as $attribute) {
$name = $attribute->nodeName;
if (in_array($name, ['img', 'src', ':src', 'srcset', ':srcset', 'class'])) {
if (in_array($name, ['img', 'data-src', 'src', ':data-src', ':src', 'data-srcset', 'srcset', ':srcset', ':data-srcset', 'class'])) {
continue;
}

Expand Down Expand Up @@ -380,6 +379,14 @@ public function setSrcAttribute(string $srcAttribute): Picture
return $this;
}

/**
* @return string
*/
public function toHtml()
{
return $this->stripWhitespaces((string)parent::toHtml());
}

/**
* @param string $html
* @return DomNode|null
Expand Down Expand Up @@ -407,4 +414,14 @@ private function getDomElementFromHtmlTag(string $html): ?DomNode

return $document->getElementsByTagName('*')->item(0);
}

private function stripWhitespaces(string $html): string
{
$html = preg_replace('/(\s+)</m', '<', $html);
$html = preg_replace('/(\s+)>/m', '>', $html);
$html = preg_replace('/(\s+)/m', ' ', $html);
$html = str_replace("\n", "", $html);
$html = trim($html);
return $html;
}
}

0 comments on commit 4a799cb

Please sign in to comment.