Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
Separating component's and kirby-sri's options
Browse files Browse the repository at this point in the history
This fixes 'undefined' errors if file doesn't exist, thanks @mrunkel
  • Loading branch information
S1SYPHOS committed Nov 17, 2017
1 parent 8bd37c2 commit bbbca2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
16 changes: 12 additions & 4 deletions core/css.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function tag($url, $media = null) {

$url = ltrim($url, '/');

if (file_exists($url)) {
if(file_exists($url)) {
// generate sri hash for css files
$cssInput = (new Asset($url))->content();
$cssIntegrity = sri_checksum($cssInput);
Expand All @@ -46,16 +46,24 @@ public function tag($url, $media = null) {
$filename = f::name($url) . '.' . $modified . '.' . f::extension($url);
$dirname = f::dirname($url);
$url = ($dirname === '.') ? $filename : $dirname . '/' . $filename;

// build an array of SRI-related attributes
$cssOptions = array(
'integrity' => $cssIntegrity, // generated SRI hash
'crossorigin' => c::get('plugin.kirby-sri.use-credentials') ? 'use-credentials' : 'anonymous' // user-defined 'crossorigin' attribute
);
}

// build the array of HTML attributes
$attr = array(
'rel' => 'stylesheet',
'href' => url($url),
'integrity' => $cssIntegrity, // inject generated sri hash
'crossorigin' => c::get('plugin.kirby-sri.use-credentials') ? 'use-credentials' : 'anonymous' // set user-defined 'crossorigin' attribute
'href' => url($url)
);

if(file_exists($url)) {
$attr = array_merge($attr, $cssOptions);
}

if(is_array($media)) {
// merge array with custom options
$attr = array_merge($attr, $media);
Expand Down
18 changes: 12 additions & 6 deletions core/js.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function tag($src, $async = false) {

$src = ltrim($src, '/');

if (file_exists($src)) {
if(file_exists($src)) {
// generate sri hash for css files
$jsInput = (new Asset($src))->content();
$jsIntegrity = sri_checksum($jsInput);
Expand All @@ -46,14 +46,20 @@ public function tag($src, $async = false) {
$filename = f::name($src) . '.' . $modified . '.' . f::extension($src);
$dirname = f::dirname($src);
$src = ($dirname === '.') ? $filename : $dirname . '/' . $filename;

// build an array of SRI-related attributes
$jsOptions = array(
'integrity' => $jsIntegrity, // generated SRI hash
'crossorigin' => c::get('plugin.kirby-sri.use-credentials') ? 'use-credentials' : 'anonymous' // user-defined 'crossorigin' attribute
);
}

// build the array of HTML attributes
$attr = array(
'src' => url($src),
'integrity' => $jsIntegrity, // inject generated sri hash
'crossorigin' => c::get('plugin.kirby-sri.use-credentials') ? 'use-credentials' : 'anonymous' // set user-defined 'crossorigin' attribute
);
$attr = array('src' => url($src));

if(file_exists($src)) {
$attr = array_merge($attr, $jsOptions);
}

if(is_array($async)) {
// merge array with custom options
Expand Down

0 comments on commit bbbca2f

Please sign in to comment.