1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extra\Html;
use Symfony\Component\Mime\MimeTypes;
use Twig\Environment;
use Twig\Error\RuntimeError;
use Twig\Extension\AbstractExtension;
use Twig\Extra\Html\HtmlAttr\AttributeValueInterface;
use Twig\Extra\Html\HtmlAttr\InlineStyle;
use Twig\Extra\Html\HtmlAttr\MergeableInterface;
use Twig\Extra\Html\HtmlAttr\SeparatedTokenList;
use Twig\Markup;
use Twig\Runtime\EscaperRuntime;
use Twig\TwigFilter;
use Twig\TwigFunction;
final class HtmlExtension extends AbstractExtension
{
private $mimeTypes;
public function __construct(?MimeTypes $mimeTypes = null)
{
$this->mimeTypes = $mimeTypes;
}
public function getFilters(): array
{
return [
new TwigFilter('data_uri', [$this, 'dataUri']),
new TwigFilter('html_attr_merge', [self::class, 'htmlAttrMerge']),
new TwigFilter('html_attr_type', [self::class, 'htmlAttrType']),
];
}
public function getFunctions(): array
{
return [
new TwigFunction('html_classes', [self::class, 'htmlClasses']),
new TwigFunction('html_cva', [self::class, 'htmlCva']),
new TwigFunction('html_attr', [self::class, 'htmlAttr'], ['needs_environment' => true, 'is_safe' => ['html']]),
];
}
/**
* Creates a data URI (RFC 2397).
*
* Length validation is not performed on purpose, validation should
* be done before calling this filter.
*
* @return string The generated data URI
*
* @internal
*/
public function dataUri(string $data, ?string $mime = null, array $parameters = []): string
{
$repr = 'data:';
if (null === $mime) {
if (null === $this->mimeTypes) {
$this->mimeTypes = new MimeTypes();
}
$tmp = tempnam(sys_get_temp_dir(), 'mime');
file_put_contents($tmp, $data);
try {
if (null === $mime = $this->mimeTypes->guessMimeType($tmp)) {
$mime = 'text/plain';
}
} finally {
@unlink($tmp);
}
}
$repr .= $mime;
foreach ($parameters as $key => $value) {
$repr .= ';'.$key.'='.rawurlencode($value);
}
if (str_starts_with($mime, 'text/')) {
$repr .= ','.rawurlencode($data);
} else {
$repr .= ';base64,'.base64_encode($data);
}
return $repr;
}
/**
* @internal
*/
public static function htmlClasses(...$args): string
{
$classes = [];
foreach ($args as $i => $arg) {
if (\is_string($arg) || $arg instanceof Markup) {
$classes[] = (string) $arg;
} elseif (\is_array($arg)) {
foreach ($arg as $class => $condition) {
if (!\is_string($class)) {
throw new RuntimeError(\sprintf('The "html_classes" function argument %d (key %d) should be a string, got "%s".', $i, $class, get_debug_type($class)));
}
if (!$condition) {
continue;
}
$classes[] = $class;
}
} else {
throw new RuntimeError(\sprintf('The "html_classes" function argument %d should be either a string or an array, got "%s".', $i, get_debug_type($arg)));
}
}
return implode(' ', array_unique(array_filter($classes, static function ($v) { return '' !== $v; })));
}
/**
* @param string|list<string|null> $base
* @param array<string, array<string, string|array<string>>> $variants
* @param array<array<string, string|array<string>>> $compoundVariants
* @param array<string, string> $defaultVariant
*
* @internal
*/
public static function htmlCva(array|string $base = [], array $variants = [], array $compoundVariants = [], array $defaultVariant = []): Cva
{
return new Cva($base, $variants, $compoundVariants, $defaultVariant);
}
/** @internal */
public static function htmlAttrType(mixed $value, string $type = 'sst'): AttributeValueInterface
{
return match ($type) {
'sst' => new SeparatedTokenList($value, ' '),
'cst' => new SeparatedTokenList($value, ', '),
'style' => new InlineStyle($value),
default => throw new RuntimeError(\sprintf('Unknown attribute type "%s" The only supported types are "sst", "cst" and "style".', $type)),
};
}
/** @internal */
public static function htmlAttrMerge(iterable|string|false|null ...$arrays): array
{
$result = [];
foreach ($arrays as $array) {
if (!$array) {
continue;
}
if (\is_string($array)) {
throw new RuntimeError('Only empty strings may be passed as string arguments to html_attr_merge. This is to support the implicit else clause for ternary operators.');
}
foreach ($array as $key => $value) {
if (!isset($result[$key])) {
$result[$key] = $value;
continue;
}
$existing = $result[$key];
switch (true) {
case $value instanceof MergeableInterface:
$result[$key] = $value->mergeInto($existing);
break;
case $existing instanceof MergeableInterface:
$result[$key] = $existing->appendFrom($value);
break;
case is_iterable($existing) && is_iterable($value):
$result[$key] = [...$existing, ...$value];
break;
case (\is_scalar($existing) || \is_object($existing)) && (\is_scalar($value) || \is_object($value)):
$result[$key] = $value;
break;
default:
throw new RuntimeError(\sprintf('Cannot merge incompatible values for key "%s".', $key));
}
}
}
return $result;
}
/** @internal */
public static function htmlAttr(Environment $env, iterable|string|false|null ...$args): string
{
$attr = self::htmlAttrMerge(...$args);
$result = '';
$runtime = $env->getRuntime(EscaperRuntime::class);
foreach ($attr as $name => $value) {
if (str_starts_with($name, 'aria-')) {
// For aria-*, convert booleans to "true" and "false" strings
if (true === $value) {
$value = 'true';
} elseif (false === $value) {
$value = 'false';
}
}
if (str_starts_with($name, 'data-')) {
if (!$value instanceof AttributeValueInterface && null !== $value && !\is_scalar($value)) {
// ... encode non-null non-scalars as JSON
try {
$value = json_encode($value, \JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new RuntimeError(\sprintf('The "%s" attribute value cannot be JSON encoded.', $name), previous: $e);
}
} elseif (true === $value) {
// ... and convert boolean true to a 'true' string.
$value = 'true';
}
}
// Convert iterable values to token lists
if (!$value instanceof AttributeValueInterface && is_iterable($value)) {
if ('style' === $name) {
$value = new InlineStyle($value);
} else {
$value = new SeparatedTokenList($value);
}
}
if ($value instanceof AttributeValueInterface) {
$value = $value->getValue();
}
// In general, ...
if (true === $value) {
// ... use attribute="" for boolean true,
// which is XHTML compliant and indicates the "empty value default", see
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2 and
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes
$value = '';
}
if (null === $value || false === $value) {
// omit null-valued and false attributes completely (note aria-* has been processed before)
continue;
}
if (\is_object($value) && !$value instanceof \Stringable) {
throw new RuntimeError(\sprintf('The "%s" attribute value should be a scalar, an iterable, or an object implementing "%s", got "%s".', $name, \Stringable::class, get_debug_type($value)));
}
$result .= $runtime->escape($name, 'html_attr_relaxed').'="'.$runtime->escape((string) $value).'" ';
}
return trim($result);
}
}
|