Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Assets/AudioAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(
public readonly ?string $file = null,
?string $mimeType = null,
?float $duration = null,
public readonly string|bool|null $crossorigin = null,
) {
$this->mimeType = $mimeType ?? Helpers::guessMimeTypeFromExtension($file ?? $url);
$this->lazyLoad(compact('duration'), function () {
Expand All @@ -38,10 +39,11 @@ public function __toString(): string

public function getImportElement(): Html
{
return Html::el('audio', [
return Html::el('audio', array_filter([
'src' => $this->url,
'type' => $this->mimeType,
]);
'crossorigin' => $this->crossorigin,
], fn($value) => $value !== null));
}


Expand All @@ -52,6 +54,7 @@ public function getPreloadElement(): Html
'href' => $this->url,
'as' => 'audio',
'type' => $this->mimeType,
'crossorigin' => $this->crossorigin,
], fn($value) => $value !== null));
}
}
1 change: 1 addition & 0 deletions src/Assets/GenericAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(
public readonly ?string $file = null,
public readonly ?string $media = null,
public readonly ?string $integrity = null,
public readonly string|bool|null $crossorigin = null,
) {
$this->lazyLoad(compact('mimeType'), function () {
$this->mimeType = $this->file ? (mime_content_type($this->file) ?: null) : null;
Expand Down
3 changes: 3 additions & 0 deletions src/Assets/VideoAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(
/** Poster image URL */
public readonly ?string $poster = null,
public readonly bool $autoPlay = false,
public readonly string|bool|null $crossorigin = null,
) {
}

Expand All @@ -40,6 +41,7 @@ public function getImportElement(): Html
'type' => $this->mimeType,
'poster' => $this->poster,
'autoplay' => $this->autoPlay ? true : null,
'crossorigin' => $this->crossorigin,
], fn($value) => $value !== null));
}

Expand All @@ -51,6 +53,7 @@ public function getPreloadElement(): Html
'href' => $this->url,
'as' => 'video',
'type' => $this->mimeType,
'crossorigin' => $this->crossorigin,
], fn($value) => $value !== null));
}
}
6 changes: 4 additions & 2 deletions tests/Assets/AudioAsset.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,24 @@ test('Invalid MP3 file throws exception', function () {


test('getImportElement()', function () {
$asset = new AudioAsset('/audio/sound.mp3', mimeType: 'audio/mpeg', duration: 120.5);
$asset = new AudioAsset('/audio/sound.mp3', mimeType: 'audio/mpeg', duration: 120.5, crossorigin: true);

Assert::equal(Html::el('audio', [
'src' => '/audio/sound.mp3',
'type' => 'audio/mpeg',
'crossorigin' => true,
]), $asset->getImportElement());
});


test('getHtmlPreloadElement()', function () {
$asset = new AudioAsset('/audio/sound.mp3', mimeType: 'audio/mpeg');
$asset = new AudioAsset('/audio/sound.mp3', mimeType: 'audio/mpeg', crossorigin: true);

Assert::equal(Html::el('link', [
'rel' => 'preload',
'href' => '/audio/sound.mp3',
'as' => 'audio',
'type' => 'audio/mpeg',
'crossorigin' => true,
]), $asset->getPreloadElement());
});
5 changes: 5 additions & 0 deletions tests/Assets/GenericAsset.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ test('MIME type is null without file', function () {
$asset = new GenericAsset('');
Assert::null($asset->mimeType);
});

test('crossorigin is stored', function () {
$asset = new GenericAsset('', crossorigin: true);
Assert::true($asset->crossorigin);
});
5 changes: 4 additions & 1 deletion tests/Assets/VideoAsset.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ test('getImportElement()', function () {
duration: 120.5,
poster: '/img/poster.jpg',
autoPlay: true,
crossorigin: true,
);

Assert::equal(Html::el('video', [
Expand All @@ -26,17 +27,19 @@ test('getImportElement()', function () {
'type' => 'video/mp4',
'poster' => '/img/poster.jpg',
'autoplay' => true,
'crossorigin' => true,
]), $asset->getImportElement());
});


test('getHtmlPreloadElement()', function () {
$asset = new VideoAsset('/video/movie.mp4', mimeType: 'video/mp4');
$asset = new VideoAsset('/video/movie.mp4', mimeType: 'video/mp4', crossorigin: true);

Assert::equal(Html::el('link', [
'rel' => 'preload',
'href' => '/video/movie.mp4',
'as' => 'video',
'type' => 'video/mp4',
'crossorigin' => true,
]), $asset->getPreloadElement());
});
11 changes: 11 additions & 0 deletions tests/Assets/ViteMapper.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use Nette\Assets\EntryAsset;
use Nette\Assets\FilesystemMapper;
use Nette\Assets\ScriptAsset;
use Nette\Assets\StyleAsset;
use Nette\Assets\VideoAsset;
use Nette\Assets\ViteMapper;
use Tester\Assert;

Expand Down Expand Up @@ -36,6 +37,16 @@ test('Production mode - CSS entry point', function (): void {
});


test('Production mode - video entry point without dependencies', function (): void {
$mapper = new ViteMapper('https://example.com', __DIR__ . '/fixtures', __DIR__ . '/fixtures/manifest.json');
$asset = $mapper->getAsset('src/video.mp4');

Assert::type(VideoAsset::class, $asset);
Assert::same('https://example.com/assets/video-3m4n5o6p.mp4', $asset->url);
Assert::true($asset->crossorigin);
});


test('Fallback to filesystem when asset not found in manifest', function (): void {
$filesystemMapper = new FilesystemMapper('https://example.com', __DIR__ . '/fixtures');
$mapper = new ViteMapper('https://example.com', __DIR__ . '/fixtures', __DIR__ . '/fixtures/manifest.json', publicMapper: $filesystemMapper);
Expand Down
5 changes: 5 additions & 0 deletions tests/Assets/fixtures/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
"file": "assets/styles-9i0j1k2l.css",
"src": "src/styles.css",
"isEntry": true
},
"src/video.mp4": {
"file": "assets/video-3m4n5o6p.mp4",
"src": "src/video.mp4",
"isEntry": true
}
}
3 changes: 3 additions & 0 deletions tests/types/assets-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function testAudioAssetProperties(AudioAsset $asset): void
assertType('string|null', $asset->file);
assertType('float|null', $asset->duration);
assertType('string|null', $asset->mimeType);
assertType('bool|string|null', $asset->crossorigin);
}


Expand All @@ -134,6 +135,7 @@ function testVideoAssetProperties(VideoAsset $asset): void
assertType('float|null', $asset->duration);
assertType('string|null', $asset->poster);
assertType('bool', $asset->autoPlay);
assertType('bool|string|null', $asset->crossorigin);
}


Expand All @@ -154,6 +156,7 @@ function testGenericAssetProperties(GenericAsset $asset): void
assertType('string|null', $asset->mimeType);
assertType('string|null', $asset->media);
assertType('string|null', $asset->integrity);
assertType('bool|string|null', $asset->crossorigin);
}


Expand Down