From 5f7b6c8a44bb6ce90e57a40339104d4df189ea96 Mon Sep 17 00:00:00 2001 From: Ivailo Hristov Date: Fri, 6 Nov 2020 22:27:00 +0200 Subject: [PATCH 1/4] Treat rgba() and hsla() as aliases to rgb() and hsl() --- lib/Sabberworm/CSS/Value/Color.php | 22 ++++++++++++++++++---- tests/Sabberworm/CSS/ParserTest.php | 3 ++- tests/files/colortest.css | 4 ++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/Sabberworm/CSS/Value/Color.php b/lib/Sabberworm/CSS/Value/Color.php index 5ff44699..0eeeae84 100644 --- a/lib/Sabberworm/CSS/Value/Color.php +++ b/lib/Sabberworm/CSS/Value/Color.php @@ -41,14 +41,21 @@ public static function parse(ParserState $oParserState) { $oParserState->consume('('); $bContainsVar = false; - $iLength = $oParserState->strlen($sColorMode); + if (strpos($sColorMode, 'rgb') !== false) { + $sColorTarget = 'rgba'; + } else if (strpos($sColorMode, 'hsl') !== false) { + $sColorTarget = 'hsla'; + } else { + $sColorTarget = $sColorMode; + } + $iLength = $oParserState->strlen($sColorTarget); for ($i = 0; $i < $iLength; ++$i) { $oParserState->consumeWhiteSpace(); if ($oParserState->comes('var')) { - $aColor[$sColorMode[$i]] = CSSFunction::parseIdentifierOrFunction($oParserState); + $aColor[$sColorTarget[$i]] = CSSFunction::parseIdentifierOrFunction($oParserState); $bContainsVar = true; } else { - $aColor[$sColorMode[$i]] = Size::parse($oParserState, true); + $aColor[$sColorTarget[$i]] = Size::parse($oParserState, true); } if ($bContainsVar && $oParserState->comes(')')) { @@ -58,7 +65,14 @@ public static function parse(ParserState $oParserState) { $oParserState->consumeWhiteSpace(); if ($i < ($iLength - 1)) { - $oParserState->consume(','); + if ($oParserState->comes(',')) { + $oParserState->consume(','); + } else if ($oParserState->comes('/')) { + $oParserState->consume('/'); + } else if ($oParserState->comes(')')) { + // No alpha channel information + break; + } } } $oParserState->consume(')'); diff --git a/tests/Sabberworm/CSS/ParserTest.php b/tests/Sabberworm/CSS/ParserTest.php index 4a690196..572a764e 100644 --- a/tests/Sabberworm/CSS/ParserTest.php +++ b/tests/Sabberworm/CSS/ParserTest.php @@ -78,7 +78,8 @@ function testColorParsing() { $this->assertSame('#mine {color: red;border-color: #0a64e6;border-color: rgba(10,100,231,.3);outline-color: #222;background-color: #232323;} #yours {background-color: hsl(220,10%,220%);background-color: hsla(220,10%,220%,.3);} #variables {background-color: rgb(var(--some-rgb));background-color: rgb(var(--r),var(--g),var(--b));background-color: rgb(255,var(--g),var(--b));background-color: rgb(255,255,var(--b));background-color: rgb(255,var(--rg));background-color: hsl(var(--some-hsl));} -#variables-alpha {background-color: rgba(var(--some-rgb),.1);background-color: rgba(var(--some-rg),255,.1);background-color: hsla(var(--some-hsl),.1);}', $oDoc->render()); +#variables-alpha {background-color: rgba(var(--some-rgb),.1);background-color: rgba(var(--some-rg),255,.1);background-color: hsla(var(--some-hsl),.1);} +#css4-rgba {background-color: rgba(242,245,249,45%);}', $oDoc->render()); } function testUnicodeParsing() { diff --git a/tests/files/colortest.css b/tests/files/colortest.css index 1c89cf41..a882b6ac 100644 --- a/tests/files/colortest.css +++ b/tests/files/colortest.css @@ -26,3 +26,7 @@ background-color: rgba(var(--some-rg), 255, 0.1); background-color: hsla(var(--some-hsl), 0.1); } + +#css4-rgba { + background-color: rgb(242 245 249 / 45%); +} From 4c432caea42682d11a7bdfde3f37111a5b801144 Mon Sep 17 00:00:00 2001 From: Ivailo Hristov Date: Fri, 6 Nov 2020 22:34:07 +0200 Subject: [PATCH 2/4] Add test case for parsing rgba without an alpha argument --- tests/Sabberworm/CSS/ParserTest.php | 2 +- tests/files/colortest.css | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Sabberworm/CSS/ParserTest.php b/tests/Sabberworm/CSS/ParserTest.php index 572a764e..f25d7f34 100644 --- a/tests/Sabberworm/CSS/ParserTest.php +++ b/tests/Sabberworm/CSS/ParserTest.php @@ -79,7 +79,7 @@ function testColorParsing() { #yours {background-color: hsl(220,10%,220%);background-color: hsla(220,10%,220%,.3);} #variables {background-color: rgb(var(--some-rgb));background-color: rgb(var(--r),var(--g),var(--b));background-color: rgb(255,var(--g),var(--b));background-color: rgb(255,255,var(--b));background-color: rgb(255,var(--rg));background-color: hsl(var(--some-hsl));} #variables-alpha {background-color: rgba(var(--some-rgb),.1);background-color: rgba(var(--some-rg),255,.1);background-color: hsla(var(--some-hsl),.1);} -#css4-rgba {background-color: rgba(242,245,249,45%);}', $oDoc->render()); +#css4-rgba {background-color: rgba(242,245,249,45%);background-color: #f2f5f9;}', $oDoc->render()); } function testUnicodeParsing() { diff --git a/tests/files/colortest.css b/tests/files/colortest.css index a882b6ac..52040084 100644 --- a/tests/files/colortest.css +++ b/tests/files/colortest.css @@ -29,4 +29,5 @@ #css4-rgba { background-color: rgb(242 245 249 / 45%); + background-color: rgba(242 245 249); } From 693430173bf6202dd92e5fa1d8746ce99e74508a Mon Sep 17 00:00:00 2001 From: Ivailo Hristov Date: Sat, 17 Sep 2022 21:36:01 +0300 Subject: [PATCH 3/4] Fix a few errors after catchup merges --- src/Value/Color.php | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Value/Color.php b/src/Value/Color.php index 68500c8f..0653466f 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -66,21 +66,21 @@ public static function parse(ParserState $oParserState) $oParserState->consume('('); $bContainsVar = false; - if (strpos($sColorMode, 'rgb') !== false) { - $sColorTarget = 'rgba'; - } elseif (strpos($sColorMode, 'hsl') !== false) { - $sColorTarget = 'hsla'; - } else { - $sColorTarget = $sColorMode; - } - $iLength = $oParserState->strlen($sColorMode); + if (strpos($sColorMode, 'rgb') !== false) { + $sColorTarget = 'rgba'; + } elseif (strpos($sColorMode, 'hsl') !== false) { + $sColorTarget = 'hsla'; + } else { + $sColorTarget = $sColorMode; + } + $iLength = $oParserState->strlen($sColorTarget); for ($i = 0; $i < $iLength; ++$i) { $oParserState->consumeWhiteSpace(); if ($oParserState->comes('var')) { - $aColor[$sColorMode[$i]] = CSSFunction::parseIdentifierOrFunction($oParserState); + $aColor[$sColorTarget[$i]] = CSSFunction::parseIdentifierOrFunction($oParserState); $bContainsVar = true; } else { - $aColor[$sColorMode[$i]] = Size::parse($oParserState, true); + $aColor[$sColorTarget[$i]] = Size::parse($oParserState, true); } if ($bContainsVar && $oParserState->comes(')')) { @@ -91,13 +91,13 @@ public static function parse(ParserState $oParserState) $oParserState->consumeWhiteSpace(); if ($i < ($iLength - 1)) { if ($oParserState->comes(',')) { - $oParserState->consume(','); - } elseif ($oParserState->comes('/')) { - $oParserState->consume('/'); - } else if ($oParserState->comes(')')) { - // No alpha channel information - break; - } + $oParserState->consume(','); + } elseif ($oParserState->comes('/')) { + $oParserState->consume('/'); + } elseif ($oParserState->comes(')')) { + // No alpha channel information + break; + } } } $oParserState->consume(')'); @@ -177,7 +177,7 @@ public function render(OutputFormat $oOutputFormat) $this->aComponents['b']->getSize() ); return '#' . (($sResult[0] == $sResult[1]) && ($sResult[2] == $sResult[3]) && ($sResult[4] == $sResult[5]) - ? "$sResult[0]$sResult[2]$sResult[4]" : $sResult); + ? "$sResult[0]$sResult[2]$sResult[4]" : $sResult); } return parent::render($oOutputFormat); } From 7a4641b741e1cf796ce5dfeaaa60f6c663800950 Mon Sep 17 00:00:00 2001 From: raxbg Date: Wed, 10 Jul 2024 17:22:45 +0300 Subject: [PATCH 4/4] Stricter checks for rgb and hsl color modes --- src/Value/Color.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Value/Color.php b/src/Value/Color.php index d3525fcf..5b54ce45 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -71,9 +71,9 @@ public static function parse(ParserState $oParserState, bool $bIgnoreCase = fals $oParserState->consume('('); $bContainsVar = false; - if (strpos($sColorMode, 'rgb') !== false) { + if ($sColorMode === 'rgb') { $sColorTarget = 'rgba'; - } elseif (strpos($sColorMode, 'hsl') !== false) { + } elseif ($sColorMode === 'hsl') { $sColorTarget = 'hsla'; } else { $sColorTarget = $sColorMode;