Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Keep sources for onlyWebp option #64

Merged
merged 13 commits into from
Jul 4, 2024
6 changes: 3 additions & 3 deletions Classes/Domain/Model/PictureConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ public function sourcesShouldBeAdded(): bool

public function webpShouldBeAddedBeforeSrcset(): bool
{
return $this->addWebp && !$this->addSources;
return $this->addWebp && !$this->onlyWebp && !$this->addSources;
}

public function webpShouldBeAddedAfterSrcset(): bool
{
return $this->addWebp && $this->addSources;
return $this->addWebp && !$this->onlyWebp && $this->addSources;
}

public function webpShouldBeAdded(): bool
{
return $this->addWebp;
return $this->addWebp && !$this->onlyWebp;
}

public function webpShouldBeAddedOnly(): bool
Expand Down
19 changes: 9 additions & 10 deletions Classes/ViewHelpers/ImageViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,11 @@ public function render(): string
$this->pictureConfiguration = GeneralUtility::makeInstance(PictureConfiguration::class, $this->arguments, $settings, $image);

// build the image tag
if (!$this->pictureConfiguration->webpShouldBeAddedOnly()) {
$tag = $this->buildSingleTag('img', $this->arguments, $image);
$imageTag = $tag->render();
} else {
if ($this->pictureConfiguration->webpShouldBeAddedOnly()) {
$this->arguments['fileExtension'] = 'webp';
$tag = $this->buildSingleTag('img', $this->arguments, $image);
$imageTag = $tag->render();
}
$tag = $this->buildSingleTag('img', $this->arguments, $image);
$imageTag = $tag->render();

// Add a webp source tag and activate nesting within a picture element only if no sources are set.
if ($this->pictureConfiguration->webpShouldBeAddedBeforeSrcset()) {
Expand All @@ -172,11 +169,13 @@ public function render(): string
} else {
$imageSrc = $image;
}
// Build source tag if onlyWebp is not set
if (!$this->pictureConfiguration->webpShouldBeAddedOnly()) {
$tag = $this->buildSingleTag('source', $sourceConfiguration, $imageSrc);
$sourceOutputs[] = $tag->render();

// Force webp rendering if onlyWebp is set
if ($this->pictureConfiguration->webpShouldBeAddedOnly()) {
$sourceConfiguration['fileExtension'] = 'webp';
}
$tag = $this->buildSingleTag('source', $sourceConfiguration, $imageSrc);
$sourceOutputs[] = $tag->render();

// Build additional source with type webp if attribute addWebp is set and previously build tag is not type of webp already.
$type = htmlspecialchars_decode($tag->getAttribute('type') ?? '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
data-namespace-typo3-fluid="true"
>
<f:spaceless>
<i:image src="EXT:picture/Resources/Public/Test/Picture.png" onlyWebp="1" width="400" alt="Testimage 400px width" />
<i:image src="EXT:picture/Resources/Public/Test/Picture.png" onlyWebp="1" width="400" alt="Testimage 400px width"
sources="{0: {width: 800, media: 'min-width: 1024px'}}" />
</f:spaceless>
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ page.config.disableAllHeaderCode = 1
page.10 = FLUIDTEMPLATE
page.10.templateRootPaths.10 = EXT:picture/Tests/Functional/Frontend/Fixtures/Templates
page.10.templateName = SimpleImageWithOnlyWebpOption.html
config.absRefPrefix = /
"
5 changes: 3 additions & 2 deletions Tests/Functional/Frontend/TagRenderingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ public function simpleImageAsWebp(): void
public function simpleImageWithOnlyWebp(): void
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/simple_image_with_only_webp_option.csv');
$response = $this->executeFrontendRequestWrapper(new InternalRequest('http://localhost/'));
$response = $this->executeFrontendSubRequest(new InternalRequest('http://localhost/'));
$body = (string)$response->getBody();
$expected = '<img alt="Testimage 400px width" src="/typo3temp/assets/_processed_/a/2/csm_Picture_cfb567934c.webp" width="400" height="200" loading="lazy" />';
$expected = '<picture><source srcset="/typo3temp/assets/_processed_/a/2/csm_Picture_xxx.webp" media="(min-width: 1024px)" /><img alt="Testimage 400px width" src="/typo3temp/assets/_processed_/a/2/csm_Picture_xxx.webp" width="400" height="200" loading="lazy" /></picture>';
$expected = implode('', GeneralUtility::trimExplode("\n", $expected));
self::assertStringContainsString($this->anonymouseProcessdImage($expected), $this->anonymouseProcessdImage($body));
}

Expand Down
Loading