Skip to content

Commit 0359543

Browse files
committed
Improve scientific notation parsing
1 parent 1ecfb80 commit 0359543

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

lib/Sabberworm/CSS/Value/Size.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ public static function parse(ParserState $oParserState, $bIsColorComponent = fal
2828
if ($oParserState->comes('-')) {
2929
$sSize .= $oParserState->consume('-');
3030
}
31-
while (is_numeric($oParserState->peek()) || $oParserState->comes('.') || $oParserState->comes('e+', true) || $oParserState->comes('e-', true)) {
31+
while (is_numeric($oParserState->peek()) || $oParserState->comes('.') || $oParserState->comes('e', true)) {
3232
if ($oParserState->comes('.')) {
3333
$sSize .= $oParserState->consume('.');
3434
} else if ($oParserState->comes('e', true)) {
35-
$sSize .= $oParserState->consume(2);
35+
$sLookahead = $oParserState->peek(1, 1);
36+
if (is_numeric($sLookahead) || $sLookahead === '+' || $sLookahead === '-') {
37+
$sSize .= $oParserState->consume(2);
38+
} else {
39+
break; // Reached the unit part of the number like "em" or "ex"
40+
}
3641
} else {
3742
$sSize .= $oParserState->consume(1);
3843
}

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ function testLargeSizeValuesInFile() {
763763

764764
function testScientificNotationSizeValuesInFile() {
765765
$oDoc = $this->parsedStructureForFile('scientific-notation-numbers', Settings::create()->withMultibyteSupport(false));
766-
$sExpected = 'body {background-color: rgba(62,174,151,3041820656523200167936);z-index: .030418206565232;}';
766+
$sExpected = 'body {background-color: rgba(62,174,151,3041820656523200167936);z-index: .030418206565232;font-size: 1em;top: 192.3478px;}';
767767
$this->assertSame($sExpected, $oDoc->render());
768768
}
769769

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
body {
22
background-color: rgba(62,174,151,3.0418206565232E+21);
3-
z-index: 3.0418206565232E-2
3+
z-index: 3.0418206565232E-2;
4+
font-size: 1em;
5+
top: 1.923478e2px;
46
}

0 commit comments

Comments
 (0)