From 27ee0f0000a582ef0f9455d0a50da7621cd0702c Mon Sep 17 00:00:00 2001 From: Ali Khalili Date: Mon, 19 Feb 2024 22:59:17 +0100 Subject: [PATCH 1/6] Update Color Parsing function Handle incorrect inputs color which have less than 6 chars --- src/Value/Color.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Value/Color.php b/src/Value/Color.php index 1cf00cce..b5bf321f 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -56,12 +56,19 @@ public static function parse(ParserState $oParserState, $bIgnoreCase = false) $oParserState->currentLine() ), ]; - } else { + } else if ($oParserState->strlen($sValue) === 6) { $aColor = [ 'r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()), 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()), 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()), ]; + } else { + // the size of input color string is incorrect, use black color as fallback + $aColor = [ + 'r' => new Size(0, null, true, $oParserState->currentLine()), + 'g' => new Size(0, null, true, $oParserState->currentLine()), + 'b' => new Size(0, null, true, $oParserState->currentLine()), + ]; } } else { $sColorMode = $oParserState->parseIdentifier(true); From 5ab587bcf92f0666afad6b29ce88f4908c273b09 Mon Sep 17 00:00:00 2001 From: Ali Khalili Date: Mon, 19 Feb 2024 23:16:38 +0100 Subject: [PATCH 2/6] adding tests --- tests/ParserTest.php | 9 ++++++++- tests/fixtures/colortest.css | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/ParserTest.php b/tests/ParserTest.php index c5ddeb44..28cce00a 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -146,6 +146,13 @@ public function colorParsing() 'l' => new Size(220.0, '%', true, $oColor->getLineNo()), 'a' => new Size(0000.3, null, true, $oColor->getLineNo()), ], $oColor->getColor()); + $aColorRule = $oRuleSet->getRules('outline-color'); + $oColor = $aColorRule[0]->getValue(); + self::assertEquals([ + 'r' => new Size(0, null, true, $oColor->getLineNo()), + 'g' => new Size(0, null, true, $oColor->getLineNo()), + 'b' => new Size(0, null, true, $oColor->getLineNo()), + ], $oColor->getColor()); } } foreach ($oDoc->getAllValues('color') as $sColor) { @@ -155,7 +162,7 @@ public function colorParsing() '#mine {color: red;border-color: #0a64e6;border-color: rgba(10,100,231,.3);outline-color: #222;' . 'background-color: #232323;}' . "\n" - . '#yours {background-color: hsl(220,10%,220%);background-color: hsla(220,10%,220%,.3);}' + . '#yours {background-color: hsl(220,10%,220%);background-color: hsla(220,10%,220%,.3);outline-color: #000;}' . "\n" . '#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));' diff --git a/tests/fixtures/colortest.css b/tests/fixtures/colortest.css index 1c89cf41..f834aa77 100644 --- a/tests/fixtures/colortest.css +++ b/tests/fixtures/colortest.css @@ -9,6 +9,7 @@ #yours { background-color: hsl(220, 10%, 220%); background-color: hsla(220, 10%, 220%, 0.3); + outline-color: #22; } #variables { From 7ba04108b8fa40afa0688ddb4bcd514bbdbf2b98 Mon Sep 17 00:00:00 2001 From: Ali Khalili Date: Mon, 19 Feb 2024 23:35:15 +0100 Subject: [PATCH 3/6] fix stan --- src/Value/Color.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Value/Color.php b/src/Value/Color.php index b5bf321f..3fd512eb 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -56,7 +56,7 @@ public static function parse(ParserState $oParserState, $bIgnoreCase = false) $oParserState->currentLine() ), ]; - } else if ($oParserState->strlen($sValue) === 6) { + } elseif ($oParserState->strlen($sValue) === 6) { $aColor = [ 'r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()), 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()), From d22c41616bc28f128acac383f4aa753c845e29d8 Mon Sep 17 00:00:00 2001 From: Ali Khalili Date: Tue, 20 Feb 2024 19:35:15 +0100 Subject: [PATCH 4/6] drop the invalid hex color instead of using a fallback --- src/Value/Color.php | 12 ++++++------ tests/ParserTest.php | 9 ++------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/Value/Color.php b/src/Value/Color.php index 3fd512eb..4c456810 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -63,12 +63,12 @@ public static function parse(ParserState $oParserState, $bIgnoreCase = false) 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()), ]; } else { - // the size of input color string is incorrect, use black color as fallback - $aColor = [ - 'r' => new Size(0, null, true, $oParserState->currentLine()), - 'g' => new Size(0, null, true, $oParserState->currentLine()), - 'b' => new Size(0, null, true, $oParserState->currentLine()), - ]; + throw new UnexpectedTokenException( + 'Invalid hex color value', + $sValue, + 'custom', + $oParserState->currentLine() + ); } } else { $sColorMode = $oParserState->parseIdentifier(true); diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 28cce00a..0cf81407 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -147,12 +147,7 @@ public function colorParsing() 'a' => new Size(0000.3, null, true, $oColor->getLineNo()), ], $oColor->getColor()); $aColorRule = $oRuleSet->getRules('outline-color'); - $oColor = $aColorRule[0]->getValue(); - self::assertEquals([ - 'r' => new Size(0, null, true, $oColor->getLineNo()), - 'g' => new Size(0, null, true, $oColor->getLineNo()), - 'b' => new Size(0, null, true, $oColor->getLineNo()), - ], $oColor->getColor()); + self::assertEmpty($aColorRule); } } foreach ($oDoc->getAllValues('color') as $sColor) { @@ -162,7 +157,7 @@ public function colorParsing() '#mine {color: red;border-color: #0a64e6;border-color: rgba(10,100,231,.3);outline-color: #222;' . 'background-color: #232323;}' . "\n" - . '#yours {background-color: hsl(220,10%,220%);background-color: hsla(220,10%,220%,.3);outline-color: #000;}' + . '#yours {background-color: hsl(220,10%,220%);background-color: hsla(220,10%,220%,.3);}' . "\n" . '#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));' From 191572bdadc024df4e0852588f31566918c249c8 Mon Sep 17 00:00:00 2001 From: Ali Khalili Date: Tue, 20 Feb 2024 19:41:48 +0100 Subject: [PATCH 5/6] updating change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60e1e140..97c3bfe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Fixed +- Fix PHP notice caused by parsing invalid color values having less than 6 charachters (#485) - Fix (regression) failure to parse at-rules with strict parsing (#456) ## 8.5.0 From 148ea16879f22ede5ef46ab9de636d393ef4a7aa Mon Sep 17 00:00:00 2001 From: Ali Khalili Date: Tue, 20 Feb 2024 20:22:19 +0100 Subject: [PATCH 6/6] fix lint --- src/Value/Color.php | 6 +++--- tests/ParserTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Value/Color.php b/src/Value/Color.php index 4c456810..5daad412 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -64,9 +64,9 @@ public static function parse(ParserState $oParserState, $bIgnoreCase = false) ]; } else { throw new UnexpectedTokenException( - 'Invalid hex color value', - $sValue, - 'custom', + 'Invalid hex color value', + $sValue, + 'custom', $oParserState->currentLine() ); } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 0cf81407..a48ac0e7 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -147,7 +147,7 @@ public function colorParsing() 'a' => new Size(0000.3, null, true, $oColor->getLineNo()), ], $oColor->getColor()); $aColorRule = $oRuleSet->getRules('outline-color'); - self::assertEmpty($aColorRule); + self::assertEmpty($aColorRule); } } foreach ($oDoc->getAllValues('color') as $sColor) {