Skip to content

Commit

Permalink
hotfix : use appropriate tags for css and js (#58)
Browse files Browse the repository at this point in the history
Co-authored-by: Julio Motol <[email protected]>
  • Loading branch information
juliomotol and Julio Motol authored Jul 21, 2020
1 parent 1e5e121 commit 606477a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Sri.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ private function generateHtmlTag(string $href, string $path, bool $useCredential
$integrity = $this->html($path, $useCredentials);

if (Str::endsWith($path, 'css')) {
return "<script src='{$href}' {$integrity} {$attributes}></script>";
} elseif (Str::endsWith($path, 'js')) {
return "<link href='{$href}' rel='stylesheet' {$integrity} {$attributes}>";
} elseif (Str::endsWith($path, 'js')) {
return "<script src='{$href}' {$integrity} {$attributes}></script>";
} else {
throw new \Exception('Invalid file');
}
Expand Down
16 changes: 14 additions & 2 deletions tests/GenerateSriAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public function it_generates_css_with_integrity()
$hash = hash('sha256', file_get_contents('./tests/files/app.css'), true);
$base64Hash = base64_encode($hash);

$this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', Sri::asset('app.css'));
$asset_string = Sri::asset('app.css');

$this->assertStringContainsString('link', $asset_string);
$this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string);
}

/** @test */
Expand All @@ -35,6 +38,7 @@ public function it_generates_css_with_integrity_and_credentials()

$this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string);
$this->assertStringContainsString('crossorigin="use-credentials"', $asset_string);
$this->assertStringContainsString('link', $asset_string);
}

/** @test */
Expand All @@ -45,6 +49,7 @@ public function it_generates_css_with_integrity_and_attributes()

$asset_string = Sri::asset('app.css', false, 'rel="stylesheet"');

$this->assertStringContainsString('link', $asset_string);
$this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string);
$this->assertStringContainsString('rel="stylesheet"', $asset_string);
}
Expand All @@ -57,6 +62,7 @@ public function it_generates_css_with_integrity_credentials_and_attributes()

$asset_string = Sri::asset('app.css', true, 'rel="stylesheet"');

$this->assertStringContainsString('link', $asset_string);
$this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string);
$this->assertStringContainsString('crossorigin="use-credentials"', $asset_string);
$this->assertStringContainsString('rel="stylesheet"', $asset_string);
Expand All @@ -68,7 +74,10 @@ public function it_generates_js_with_integrity()
$hash = hash('sha256', file_get_contents('./tests/files/app.js'), true);
$base64Hash = base64_encode($hash);

$this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', Sri::asset('app.js'));
$asset_string = Sri::asset('app.js');

$this->assertStringContainsString('script', $asset_string);
$this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string);
}

/** @test */
Expand All @@ -79,6 +88,7 @@ public function it_generates_js_with_integrity_and_credentials()

$asset_string = Sri::asset('app.js', true);

$this->assertStringContainsString('script', $asset_string);
$this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string);
$this->assertStringContainsString('crossorigin="use-credentials"', $asset_string);
}
Expand All @@ -91,6 +101,7 @@ public function it_generates_js_with_integrity_and_attributes()

$asset_string = Sri::asset('app.js', false, 'type="application/javascript" async');

$this->assertStringContainsString('script', $asset_string);
$this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string);
$this->assertStringContainsString('type="application/javascript" async', $asset_string);
}
Expand All @@ -103,6 +114,7 @@ public function it_generates_js_with_integrity_credentials_and_attributes()

$asset_string = Sri::asset('app.js', true, 'type="application/javascript" async');

$this->assertStringContainsString('script', $asset_string);
$this->assertStringContainsString('integrity="sha256-'.$base64Hash.'"', $asset_string);
$this->assertStringContainsString('crossorigin="use-credentials"', $asset_string);
$this->assertStringContainsString('type="application/javascript" async', $asset_string);
Expand Down
16 changes: 14 additions & 2 deletions tests/GenerateSriMixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ protected function setUp(): void
/** @test */
public function it_generates_css_with_integrity()
{
$this->assertStringContainsString('this-hash-is-valid', Sri::mix('css/app.css'));
$mix_string = Sri::mix('css/app.css');

$this->assertStringContainsString('link', $mix_string);
$this->assertStringContainsString('this-hash-is-valid', $mix_string);
}

/** @test */
public function it_generates_css_with_integrity_and_credentials()
{
$mix_string = Sri::mix('css/app.css', true);

$this->assertStringContainsString('link', $mix_string);
$this->assertStringContainsString('this-hash-is-valid', $mix_string);
$this->assertStringContainsString('crossorigin="use-credentials"', $mix_string);
}
Expand All @@ -38,6 +42,7 @@ public function it_generates_css_with_integrity_and_attributes()
{
$mix_string = Sri::mix('css/app.css', false, 'rel="stylesheet"');

$this->assertStringContainsString('link', $mix_string);
$this->assertStringContainsString('this-hash-is-valid', $mix_string);
$this->assertStringContainsString('rel="stylesheet"', $mix_string);
}
Expand All @@ -47,6 +52,7 @@ public function it_generates_css_with_integrity_credentials_and_attributes()
{
$mix_string = Sri::mix('css/app.css', true, 'rel="stylesheet"');

$this->assertStringContainsString('link', $mix_string);
$this->assertStringContainsString('this-hash-is-valid', $mix_string);
$this->assertStringContainsString('crossorigin="use-credentials"', $mix_string);
$this->assertStringContainsString('rel="stylesheet"', $mix_string);
Expand All @@ -55,14 +61,18 @@ public function it_generates_css_with_integrity_credentials_and_attributes()
/** @test */
public function it_generates_js_with_integrity()
{
$this->assertStringContainsString('this-hash-is-valid', Sri::mix('js/app.js'));
$mix_string = Sri::mix('js/app.js');

$this->assertStringContainsString('script', $mix_string);
$this->assertStringContainsString('this-hash-is-valid', $mix_string);
}

/** @test */
public function it_generates_js_with_integrity_and_credentials()
{
$mix_string = Sri::mix('js/app.js', true);

$this->assertStringContainsString('script', $mix_string);
$this->assertStringContainsString('this-hash-is-valid', $mix_string);
$this->assertStringContainsString('crossorigin="use-credentials"', $mix_string);
}
Expand All @@ -72,6 +82,7 @@ public function it_generates_js_with_integrity_and_attributes()
{
$mix_string = Sri::mix('js/app.js', false, 'type="application/javascript" async');

$this->assertStringContainsString('script', $mix_string);
$this->assertStringContainsString('this-hash-is-valid', $mix_string);
$this->assertStringContainsString('type="application/javascript" async', $mix_string);
}
Expand All @@ -81,6 +92,7 @@ public function it_generates_js_with_integrity_credentials_and_attributes()
{
$mix_string = Sri::mix('js/app.js', true, 'type="application/javascript" async');

$this->assertStringContainsString('script', $mix_string);
$this->assertStringContainsString('this-hash-is-valid', $mix_string);
$this->assertStringContainsString('crossorigin="use-credentials"', $mix_string);
$this->assertStringContainsString('type="application/javascript" async', $mix_string);
Expand Down

0 comments on commit 606477a

Please sign in to comment.