Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for CSS aspect-ratio #408

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions library/HTMLPurifier/AttrDef/CSS/Ratio.php
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions library/HTMLPurifier/CSSDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ protected function doSetup($config)
$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
$this->info['text-decoration'] = new HTMLPurifier_AttrDef_CSS_TextDecoration();

Expand Down
24 changes: 24 additions & 0 deletions tests/HTMLPurifier/AttrDef/CSS/RatioTest.php
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions tests/HTMLPurifier/AttrDef/CSSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public function test()
$this->assertDef('min-width:50rem;');
$this->assertDef('min-width:50vw;');
$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-line:overline;');
$this->assertDef('text-decoration-style:dashed;');
Expand Down
Loading