withAttributeSanitizer example

public function getSupportedAttributes(): ?array
            {
                return null;
            }

            public function sanitizeAttribute(string $element, string $attribute, string $value, HtmlSanitizerConfig $config): ?string
            {
                return '';
            }
        };

        $config = $config->withAttributeSanitizer($sanitizer);
        $this->assertContains($sanitizer$config->getAttributeSanitizers());

        $config = $config->withoutAttributeSanitizer($sanitizer);
        $this->assertNotContains($sanitizer$config->getAttributeSanitizers());
    }
}
$this->assertSame(
            '<img src="https://trusted.com" />',
            $this->sanitize($config, '<img src="https://trusted.com" />')
        );
    }

    public function testCustomAttributeSanitizer()
    {
        $config = (new HtmlSanitizerConfig())
            ->allowElement('div', ['data-attr'])
            ->withAttributeSanitizer(new class() implements AttributeSanitizerInterface {
                public function getSupportedElements(): ?array
                {
                    return ['div'];
                }

                public function getSupportedAttributes(): ?array
                {
                    return ['data-attr'];
                }

                public function sanitizeAttribute(string $element, string $attribute, string $value, HtmlSanitizerConfig $config): ?string
                {
Home | Imprint | This part of the site doesn't use cookies.