0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 16:31:53 +00:00

feat: Add support for CSS aspect-ratio (#408)

This commit is contained in:
Erik 2024-06-27 21:12:06 +02:00 committed by GitHub
parent d9fbef8e27
commit 93bee73349
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,46 @@
<?php
/**
* Validates a ratio as defined by the CSS spec.
*/
class HTMLPurifier_AttrDef_CSS_Ratio extends HTMLPurifier_AttrDef
{
/**
* @param string $ratio Ratio to validate
* @param HTMLPurifier_Config $config Configuration options
* @param HTMLPurifier_Context $context Context
*
* @return string|boolean
*
* @warning Some contexts do not pass $config, $context. These
* variables should not be used without checking HTMLPurifier_Length
*/
public function validate($ratio, $config, $context)
{
$ratio = $this->parseCDATA($ratio);
$parts = explode('/', $ratio, 2);
$length = count($parts);
if ($length < 1 || $length > 2) {
return false;
}
$num = new \HTMLPurifier_AttrDef_CSS_Number();
if ($length === 1) {
return $num->validate($parts[0], $config, $context);
}
$num1 = $num->validate($parts[0], $config, $context);
$num2 = $num->validate($parts[1], $config, $context);
if ($num1 === false || $num2 === false) {
return false;
}
return $num1 . '/' . $num2;
}
}
// vim: et sw=4 sts=4

View File

@ -304,6 +304,13 @@ class HTMLPurifier_CSSDefinition extends HTMLPurifier_Definition
$trusted_max_wh $trusted_max_wh
); );
$this->info['aspect-ratio'] = new HTMLPurifier_AttrDef_CSS_Multiple(
new HTMLPurifier_AttrDef_CSS_Composite([
new HTMLPurifier_AttrDef_CSS_Ratio(),
new HTMLPurifier_AttrDef_Enum(['auto']),
])
);
// text-decoration and related shorthands // text-decoration and related shorthands
$this->info['text-decoration'] = new HTMLPurifier_AttrDef_CSS_TextDecoration(); $this->info['text-decoration'] = new HTMLPurifier_AttrDef_CSS_TextDecoration();

View File

@ -0,0 +1,24 @@
<?php
class HTMLPurifier_AttrDef_CSS_RatioTest extends HTMLPurifier_AttrDefHarness
{
public function test()
{
$this->def = new HTMLPurifier_AttrDef_CSS_Ratio();
$this->assertDef('1/2');
$this->assertDef('1 / 2', '1/2');
$this->assertDef('1');
$this->assertDef('1/0');
$this->assertDef('0/1');
$this->assertDef('1/2/3', false);
$this->assertDef('/2/3', false);
$this->assertDef('/12', false);
$this->assertDef('1/', false);
$this->assertDef('asdf', false);
}
}
// vim: et sw=4 sts=4

View File

@ -72,6 +72,10 @@ class HTMLPurifier_AttrDef_CSSTest extends HTMLPurifier_AttrDefHarness
$this->assertDef('min-width:50rem;'); $this->assertDef('min-width:50rem;');
$this->assertDef('min-width:50vw;'); $this->assertDef('min-width:50vw;');
$this->assertDef('min-width:-50vw;', false); $this->assertDef('min-width:-50vw;', false);
$this->assertDef('aspect-ratio:16/9;');
$this->assertDef('aspect-ratio:auto;');
$this->assertDef('aspect-ratio:16/9 auto;');
$this->assertDef('aspect-ratio:auto 16/9;');
$this->assertDef('text-decoration:underline;'); $this->assertDef('text-decoration:underline;');
$this->assertDef('text-decoration-line:overline;'); $this->assertDef('text-decoration-line:overline;');
$this->assertDef('text-decoration-style:dashed;'); $this->assertDef('text-decoration-style:dashed;');