The start of something beautiful

This commit is contained in:
2024-09-11 22:48:07 -06:00
parent 45acea47f3
commit f5997ee5ec
5614 changed files with 630696 additions and 0 deletions
@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Extension\HtmlSanitizer;
use Psr\Container\ContainerInterface;
use Symfony\Component\Form\AbstractExtension;
/**
* Integrates the HtmlSanitizer component with the Form library.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class HtmlSanitizerExtension extends AbstractExtension
{
public function __construct(
private ContainerInterface $sanitizers,
private string $defaultSanitizer = 'default',
) {
}
protected function loadTypeExtensions(): array
{
return [
new Type\TextTypeHtmlSanitizerExtension($this->sanitizers, $this->defaultSanitizer),
];
}
}
@@ -0,0 +1,66 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Extension\HtmlSanitizer\Type;
use Psr\Container\ContainerInterface;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @author Titouan Galopin <galopintitouan@gmail.com>
*/
class TextTypeHtmlSanitizerExtension extends AbstractTypeExtension
{
public function __construct(
private ContainerInterface $sanitizers,
private string $defaultSanitizer = 'default',
) {
}
public static function getExtendedTypes(): iterable
{
return [TextType::class];
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults(['sanitize_html' => false, 'sanitizer' => null])
->setAllowedTypes('sanitize_html', 'bool')
->setAllowedTypes('sanitizer', ['string', 'null'])
;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
if (!$options['sanitize_html']) {
return;
}
$sanitizers = $this->sanitizers;
$sanitizer = $options['sanitizer'] ?? $this->defaultSanitizer;
$builder->addEventListener(
FormEvents::PRE_SUBMIT,
static function (FormEvent $event) use ($sanitizers, $sanitizer) {
if (\is_scalar($data = $event->getData()) && '' !== trim($data)) {
$event->setData($sanitizers->get($sanitizer)->sanitize($data));
}
},
10000 /* as soon as possible */
);
}
}