From 1ecfb8027a9b2d88acc272da51d0e7db7d547481 Mon Sep 17 00:00:00 2001 From: raxbg Date: Wed, 13 Nov 2019 15:43:43 +0200 Subject: [PATCH 1/4] Handle scientific notation when parsing sizes --- lib/Sabberworm/CSS/Value/Size.php | 4 +++- tests/Sabberworm/CSS/ParserTest.php | 6 ++++++ tests/files/scientific-notation-numbers.css | 4 ++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/files/scientific-notation-numbers.css diff --git a/lib/Sabberworm/CSS/Value/Size.php b/lib/Sabberworm/CSS/Value/Size.php index 648af4a8..b665377e 100644 --- a/lib/Sabberworm/CSS/Value/Size.php +++ b/lib/Sabberworm/CSS/Value/Size.php @@ -28,9 +28,11 @@ public static function parse(ParserState $oParserState, $bIsColorComponent = fal if ($oParserState->comes('-')) { $sSize .= $oParserState->consume('-'); } - while (is_numeric($oParserState->peek()) || $oParserState->comes('.')) { + while (is_numeric($oParserState->peek()) || $oParserState->comes('.') || $oParserState->comes('e+', true) || $oParserState->comes('e-', true)) { if ($oParserState->comes('.')) { $sSize .= $oParserState->consume('.'); + } else if ($oParserState->comes('e', true)) { + $sSize .= $oParserState->consume(2); } else { $sSize .= $oParserState->consume(1); } diff --git a/tests/Sabberworm/CSS/ParserTest.php b/tests/Sabberworm/CSS/ParserTest.php index ea34f2e7..b59bed18 100644 --- a/tests/Sabberworm/CSS/ParserTest.php +++ b/tests/Sabberworm/CSS/ParserTest.php @@ -761,6 +761,12 @@ function testLargeSizeValuesInFile() { $this->assertSame($sExpected, $oDoc->render()); } + function testScientificNotationSizeValuesInFile() { + $oDoc = $this->parsedStructureForFile('scientific-notation-numbers', Settings::create()->withMultibyteSupport(false)); + $sExpected = 'body {background-color: rgba(62,174,151,3041820656523200167936);z-index: .030418206565232;}'; + $this->assertSame($sExpected, $oDoc->render()); + } + function testLonelyImport() { $oDoc = $this->parsedStructureForFile('lonely-import'); $sExpected = "@import url(\"example.css\") only screen and (max-width: 600px);"; diff --git a/tests/files/scientific-notation-numbers.css b/tests/files/scientific-notation-numbers.css new file mode 100644 index 00000000..22476e45 --- /dev/null +++ b/tests/files/scientific-notation-numbers.css @@ -0,0 +1,4 @@ +body { + background-color: rgba(62,174,151,3.0418206565232E+21); + z-index: 3.0418206565232E-2 +} From 0359543d9c3b2fb68a11af59b510ca59843a5fdf Mon Sep 17 00:00:00 2001 From: raxbg Date: Wed, 13 Nov 2019 18:19:44 +0200 Subject: [PATCH 2/4] Improve scientific notation parsing --- lib/Sabberworm/CSS/Value/Size.php | 9 +++++++-- tests/Sabberworm/CSS/ParserTest.php | 2 +- tests/files/scientific-notation-numbers.css | 4 +++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/Sabberworm/CSS/Value/Size.php b/lib/Sabberworm/CSS/Value/Size.php index b665377e..04cb8c19 100644 --- a/lib/Sabberworm/CSS/Value/Size.php +++ b/lib/Sabberworm/CSS/Value/Size.php @@ -28,11 +28,16 @@ public static function parse(ParserState $oParserState, $bIsColorComponent = fal if ($oParserState->comes('-')) { $sSize .= $oParserState->consume('-'); } - while (is_numeric($oParserState->peek()) || $oParserState->comes('.') || $oParserState->comes('e+', true) || $oParserState->comes('e-', true)) { + while (is_numeric($oParserState->peek()) || $oParserState->comes('.') || $oParserState->comes('e', true)) { if ($oParserState->comes('.')) { $sSize .= $oParserState->consume('.'); } else if ($oParserState->comes('e', true)) { - $sSize .= $oParserState->consume(2); + $sLookahead = $oParserState->peek(1, 1); + if (is_numeric($sLookahead) || $sLookahead === '+' || $sLookahead === '-') { + $sSize .= $oParserState->consume(2); + } else { + break; // Reached the unit part of the number like "em" or "ex" + } } else { $sSize .= $oParserState->consume(1); } diff --git a/tests/Sabberworm/CSS/ParserTest.php b/tests/Sabberworm/CSS/ParserTest.php index b59bed18..f8be9ab1 100644 --- a/tests/Sabberworm/CSS/ParserTest.php +++ b/tests/Sabberworm/CSS/ParserTest.php @@ -763,7 +763,7 @@ function testLargeSizeValuesInFile() { function testScientificNotationSizeValuesInFile() { $oDoc = $this->parsedStructureForFile('scientific-notation-numbers', Settings::create()->withMultibyteSupport(false)); - $sExpected = 'body {background-color: rgba(62,174,151,3041820656523200167936);z-index: .030418206565232;}'; + $sExpected = 'body {background-color: rgba(62,174,151,3041820656523200167936);z-index: .030418206565232;font-size: 1em;top: 192.3478px;}'; $this->assertSame($sExpected, $oDoc->render()); } diff --git a/tests/files/scientific-notation-numbers.css b/tests/files/scientific-notation-numbers.css index 22476e45..cbed2337 100644 --- a/tests/files/scientific-notation-numbers.css +++ b/tests/files/scientific-notation-numbers.css @@ -1,4 +1,6 @@ body { background-color: rgba(62,174,151,3.0418206565232E+21); - z-index: 3.0418206565232E-2 + z-index: 3.0418206565232E-2; + font-size: 1em; + top: 1.923478e2px; } From 765e712f268533d0f6b072f23f7803731487baef Mon Sep 17 00:00:00 2001 From: Ivailo Hristov Date: Sat, 17 Sep 2022 21:58:15 +0300 Subject: [PATCH 3/4] Add missing code after catchup merge --- src/Value/Size.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Value/Size.php b/src/Value/Size.php index 23de17c9..1098605c 100644 --- a/src/Value/Size.php +++ b/src/Value/Size.php @@ -77,7 +77,7 @@ public static function parse(ParserState $oParserState, $bIsColorComponent = fal if ($oParserState->comes('-')) { $sSize .= $oParserState->consume('-'); } - while (is_numeric($oParserState->peek()) || $oParserState->comes('.')) { + while (is_numeric($oParserState->peek()) || $oParserState->comes('.') || $oParserState->comes('e', true)) { if ($oParserState->comes('.')) { $sSize .= $oParserState->consume('.'); } elseif ($oParserState->comes('e', true)) { From c97b614de984a95b31a03986e0d2367b23c83bdb Mon Sep 17 00:00:00 2001 From: Ivailo Hristov Date: Sat, 17 Sep 2022 21:58:28 +0300 Subject: [PATCH 4/4] Fixes for CI --- src/Value/Size.php | 14 +++++++------- tests/ParserTest.php | 12 +++++++++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Value/Size.php b/src/Value/Size.php index 1098605c..36a32381 100644 --- a/src/Value/Size.php +++ b/src/Value/Size.php @@ -80,13 +80,13 @@ public static function parse(ParserState $oParserState, $bIsColorComponent = fal while (is_numeric($oParserState->peek()) || $oParserState->comes('.') || $oParserState->comes('e', true)) { if ($oParserState->comes('.')) { $sSize .= $oParserState->consume('.'); - } elseif ($oParserState->comes('e', true)) { - $sLookahead = $oParserState->peek(1, 1); - if (is_numeric($sLookahead) || $sLookahead === '+' || $sLookahead === '-') { - $sSize .= $oParserState->consume(2); - } else { - break; // Reached the unit part of the number like "em" or "ex" - } + } elseif ($oParserState->comes('e', true)) { + $sLookahead = $oParserState->peek(1, 1); + if (is_numeric($sLookahead) || $sLookahead === '+' || $sLookahead === '-') { + $sSize .= $oParserState->consume(2); + } else { + break; // Reached the unit part of the number like "em" or "ex" + } } else { $sSize .= $oParserState->consume(1); } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index c9efbc4e..012ab667 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -1174,9 +1174,15 @@ public function largeSizeValuesInFile() /** * @test */ - public function scientificNotationSizeValuesInFile() { - $oDoc = $this->parsedStructureForFile('scientific-notation-numbers', Settings::create()->withMultibyteSupport(false)); - $sExpected = 'body {background-color: rgba(62,174,151,3041820656523200167936);z-index: .030418206565232;font-size: 1em;top: 192.3478px;}'; + public function scientificNotationSizeValuesInFile() + { + $oDoc = $this->parsedStructureForFile( + 'scientific-notation-numbers', + Settings::create()->withMultibyteSupport(false) + ); + $sExpected = '' + . 'body {background-color: rgba(62,174,151,3041820656523200167936);' + . 'z-index: .030418206565232;font-size: 1em;top: 192.3478px;}'; self::assertSame($sExpected, $oDoc->render()); }