TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Html\Srcset;
|
||||
|
||||
/**
|
||||
* Represents a source candidate for the HTML srcset attribute
|
||||
* using the "x" unit (relative pixel density, like "1x" or "2x")
|
||||
*/
|
||||
final class DensitySrcsetCandidate extends SrcsetCandidate
|
||||
{
|
||||
public function __construct(
|
||||
protected float $density,
|
||||
protected ?int $referenceWidth = null
|
||||
) {}
|
||||
|
||||
public function getDensity(): float
|
||||
{
|
||||
return $this->density;
|
||||
}
|
||||
|
||||
public function setDensity(float $density): static
|
||||
{
|
||||
$this->density = $density;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getReferenceWidth(): ?int
|
||||
{
|
||||
return $this->referenceWidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* The absolute $referenceWidth will be used as "1x" width.
|
||||
*/
|
||||
public function setReferenceWidth(int $referenceWidth): static
|
||||
{
|
||||
$this->referenceWidth = $referenceWidth;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDescriptor(): string
|
||||
{
|
||||
return $this->density . static::DENSITY_UNIT;
|
||||
}
|
||||
|
||||
public function getCalculatedWidth(): int
|
||||
{
|
||||
if ($this->referenceWidth === null) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Reference width needs to be specified if pixel density descriptors (e. g. 2x) are used in srcset: %s',
|
||||
$this->getDescriptor()
|
||||
), 1697743145);
|
||||
}
|
||||
return (int)($this->density * $this->referenceWidth);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Html\Srcset;
|
||||
|
||||
/**
|
||||
* Generates a HTML srcset attribute for responsive images
|
||||
*/
|
||||
final class SrcsetAttribute
|
||||
{
|
||||
/** @var SrcsetCandidate[] */
|
||||
private array $candidates = [];
|
||||
private string $candidateType;
|
||||
|
||||
/**
|
||||
* @param string[] $descriptors Array of srcset width (like "300w, 500w") or density (like "1x, 2x")
|
||||
* descriptors.
|
||||
* @param int|null $referenceWidth Needs to be provided if $descriptors contains density descriptors
|
||||
* (like "2x") to be able to resolve the relative image dimensions.
|
||||
* The absolute $referenceWidth will be used as "1x" width.
|
||||
*/
|
||||
public static function createFromDescriptors(array $descriptors, ?int $referenceWidth = null): SrcsetAttribute
|
||||
{
|
||||
$generator = new SrcsetAttribute();
|
||||
foreach ($descriptors as $descriptor) {
|
||||
$generator->addCandidate(SrcsetCandidate::createFromDescriptor((string)$descriptor, $referenceWidth));
|
||||
}
|
||||
return $generator;
|
||||
}
|
||||
|
||||
public function addCandidate(SrcsetCandidate $candidate): static
|
||||
{
|
||||
if (!$this->isValidCandidate($candidate)) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Invalid mix of w and x descriptors in srcset: %s, ..., %s',
|
||||
$this->candidates[0]->generateSrcset(),
|
||||
$candidate->generateSrcset()
|
||||
), 1697745459);
|
||||
}
|
||||
|
||||
$this->candidates[] = $candidate;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SrcsetCandidate[]
|
||||
*/
|
||||
public function getCandidates(): array
|
||||
{
|
||||
return $this->candidates;
|
||||
}
|
||||
|
||||
public function generateSrcset(): string
|
||||
{
|
||||
$uniqueSrcset = [];
|
||||
foreach ($this->candidates as $candidate) {
|
||||
$uniqueSrcset[$candidate->getDescriptor()] = $candidate->generateSrcset();
|
||||
}
|
||||
return implode(', ', $uniqueSrcset);
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->generateSrcset();
|
||||
}
|
||||
|
||||
private function isValidCandidate(SrcsetCandidate $candidate): bool
|
||||
{
|
||||
if (!isset($this->candidateType)) {
|
||||
$this->candidateType = ($candidate instanceof DensitySrcsetCandidate)
|
||||
? DensitySrcsetCandidate::class
|
||||
: WidthSrcsetCandidate::class;
|
||||
return true;
|
||||
}
|
||||
|
||||
return $candidate instanceof $this->candidateType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Html\Srcset;
|
||||
|
||||
/**
|
||||
* Represents a source candidate for the HTML srcset attribute
|
||||
*/
|
||||
abstract class SrcsetCandidate
|
||||
{
|
||||
public const WIDTH_UNIT = 'w';
|
||||
public const DENSITY_UNIT = 'x';
|
||||
|
||||
protected ?string $uri = null;
|
||||
|
||||
/**
|
||||
* @param string $descriptor srcset width (like "300w") or density (like "2x") descriptor.
|
||||
* @param int|null $referenceWidth Needs to be provided if $descriptor is a density descriptor
|
||||
* (like "2x") to be able to resolve the relative image dimensions.
|
||||
* The absolute $referenceWidth will be used as "1x" width.
|
||||
*/
|
||||
public static function createFromDescriptor(string $descriptor, ?int $referenceWidth = null): SrcsetCandidate
|
||||
{
|
||||
$mode = substr($descriptor, -1);
|
||||
$value = substr($descriptor, 0, -1);
|
||||
if (is_numeric($value)) {
|
||||
if ($mode === static::DENSITY_UNIT) {
|
||||
// '1.5x'
|
||||
return new DensitySrcsetCandidate((float)$value, $referenceWidth);
|
||||
}
|
||||
if ($mode === static::WIDTH_UNIT) {
|
||||
// '200w'
|
||||
return new WidthSrcsetCandidate((int)$value);
|
||||
}
|
||||
}
|
||||
throw new \InvalidArgumentException(
|
||||
'Invalid srcset descriptor provided, must be a numeric value that ends with "w" or "x": ' . $descriptor,
|
||||
1774527269,
|
||||
);
|
||||
}
|
||||
|
||||
abstract public function getDescriptor(): string;
|
||||
abstract public function getCalculatedWidth(): int;
|
||||
|
||||
public function setUri(string $uri): static
|
||||
{
|
||||
$this->uri = $uri;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUri(): ?string
|
||||
{
|
||||
return $this->uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the provided URI can be used safely in a srcset attribute
|
||||
*/
|
||||
public function getSanitizedUri(): ?string
|
||||
{
|
||||
if ($this->uri === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return strtr($this->uri, [
|
||||
' ' => '%20',
|
||||
',' => '%2C',
|
||||
]);
|
||||
}
|
||||
|
||||
public function generateSrcset(): string
|
||||
{
|
||||
return $this->getSanitizedUri() . ' ' . $this->getDescriptor();
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->generateSrcset();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Html\Srcset;
|
||||
|
||||
/**
|
||||
* Represents a source candidate for the HTML srcset attribute
|
||||
* using the "w" unit (absolute width in image pixels, like "200w")
|
||||
*/
|
||||
final class WidthSrcsetCandidate extends SrcsetCandidate
|
||||
{
|
||||
public function __construct(protected int $width) {}
|
||||
|
||||
public function setWidth(int $width): static
|
||||
{
|
||||
$this->width = $width;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCalculatedWidth(): int
|
||||
{
|
||||
return $this->width;
|
||||
}
|
||||
|
||||
public function getDescriptor(): string
|
||||
{
|
||||
return $this->width . static::WIDTH_UNIT;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user