From 93bee733497a098b65daf5910f1c435d347860a4 Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 27 Jun 2024 21:12:06 +0200 Subject: [PATCH] feat: Add support for CSS aspect-ratio (#408) --- library/HTMLPurifier/AttrDef/CSS/Ratio.php | 46 ++++++++++++++++++++ library/HTMLPurifier/CSSDefinition.php | 7 +++ tests/HTMLPurifier/AttrDef/CSS/RatioTest.php | 24 ++++++++++ tests/HTMLPurifier/AttrDef/CSSTest.php | 4 ++ 4 files changed, 81 insertions(+) create mode 100644 library/HTMLPurifier/AttrDef/CSS/Ratio.php create mode 100644 tests/HTMLPurifier/AttrDef/CSS/RatioTest.php diff --git a/library/HTMLPurifier/AttrDef/CSS/Ratio.php b/library/HTMLPurifier/AttrDef/CSS/Ratio.php new file mode 100644 index 00000000..e08e2c49 --- /dev/null +++ b/library/HTMLPurifier/AttrDef/CSS/Ratio.php @@ -0,0 +1,46 @@ +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 diff --git a/library/HTMLPurifier/CSSDefinition.php b/library/HTMLPurifier/CSSDefinition.php index 1bc419c5..09321fd2 100644 --- a/library/HTMLPurifier/CSSDefinition.php +++ b/library/HTMLPurifier/CSSDefinition.php @@ -304,6 +304,13 @@ class HTMLPurifier_CSSDefinition extends HTMLPurifier_Definition $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(); diff --git a/tests/HTMLPurifier/AttrDef/CSS/RatioTest.php b/tests/HTMLPurifier/AttrDef/CSS/RatioTest.php new file mode 100644 index 00000000..f1d27af5 --- /dev/null +++ b/tests/HTMLPurifier/AttrDef/CSS/RatioTest.php @@ -0,0 +1,24 @@ +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 diff --git a/tests/HTMLPurifier/AttrDef/CSSTest.php b/tests/HTMLPurifier/AttrDef/CSSTest.php index 2367c187..cff044ec 100644 --- a/tests/HTMLPurifier/AttrDef/CSSTest.php +++ b/tests/HTMLPurifier/AttrDef/CSSTest.php @@ -72,6 +72,10 @@ class HTMLPurifier_AttrDef_CSSTest extends HTMLPurifier_AttrDefHarness $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;');