-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get rid of csp-module and implement secure-renderer
- Loading branch information
1 parent
11da598
commit bb6cbb6
Showing
6 changed files
with
54 additions
and
78 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 33 additions & 13 deletions
46
view/frontend/templates/hyva/add-webp-class-to-body.phtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,35 @@ | ||
<?php declare(strict_types=1); ?> <script> | ||
function hasWebP() { | ||
var elem = document.createElement('canvas'); | ||
<?php | ||
|
||
if (!!(elem.getContext && elem.getContext('2d'))) { | ||
return elem.toDataURL('image/webp').indexOf('data:image/webp') === 0; | ||
declare(strict_types=1); | ||
|
||
/** | ||
* @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer | ||
*/ | ||
|
||
$script = <<<JS | ||
(() => { | ||
let _hasWebP = null; | ||
window.hasWebP = () => { | ||
if (_hasWebP !== null) { | ||
return _hasWebP; | ||
} | ||
var elem = document.createElement('canvas'); | ||
if (!!(elem.getContext && elem.getContext('2d'))) { | ||
_hasWebP = elem.toDataURL('image/webp').indexOf('data:image/webp') === 0; | ||
} else { | ||
_hasWebP = false; | ||
} | ||
return _hasWebP; | ||
} | ||
return false; | ||
} | ||
|
||
if (hasWebP()) { | ||
document.body.classList.add("webp"); | ||
} else { | ||
document.body.classList.add("no-webp"); | ||
}</script> | ||
if (window.hasWebP()) { | ||
document.body.classList.add("webp"); | ||
} else { | ||
document.body.classList.add("no-webp"); | ||
} | ||
})(); | ||
JS; | ||
|
||
echo $secureRenderer->renderTag('script', [], $script, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,27 @@ | ||
<script> | ||
document.addEventListener('gallery-init', ({detail: galleryData}) => { | ||
if (typeof hasWebP != 'function') { | ||
console.error('[Yireo_WebP2] JavaScript function "hasWebP" does not exist'); | ||
return; | ||
} | ||
<?php | ||
|
||
function replaceImage(image) { | ||
if (image.full && image.full_webp) { | ||
image.full = image.full_webp; | ||
delete image.full_webp; | ||
} | ||
declare(strict_types=1); | ||
|
||
if (image.img && image.img_webp) { | ||
image.img = image.img_webp; | ||
delete image.img_webp; | ||
} | ||
/** | ||
* @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer | ||
*/ | ||
|
||
if (image.thumb && image.thumb_webp) { | ||
image.thumb = image.thumb_webp; | ||
delete image.thumb_webp; | ||
} | ||
$script = <<<JS | ||
document.addEventListener('gallery-init', ({detail: galleryData}) => { | ||
const keys = ['full', 'img', 'thumb']; | ||
function replaceImage(image) { | ||
keys.forEach((key) => { | ||
const webpKey = key + '_webp'; | ||
if (key in image && webpKey in image) { | ||
image[key] = image[webpKey]; | ||
delete image[webpKey]; | ||
} | ||
}) | ||
} | ||
if (galleryData.images) { | ||
galleryData.images.forEach(function(image) { | ||
replaceImage(image); | ||
}); | ||
} | ||
galleryData.images?.forEach(replaceImage); | ||
galleryData.initialImages?.forEach(replaceImage); | ||
}); | ||
JS; | ||
|
||
if (galleryData.initialImages) { | ||
galleryData.initialImages.forEach(function(image) { | ||
replaceImage(image); | ||
}); | ||
} | ||
});</script> | ||
echo $secureRenderer->renderTag('script', [], $script, false); |