Skip to content

Commit c7a03a3

Browse files
committed
Handle EOF when parsing calc functions
1 parent 6ac546d commit c7a03a3

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

lib/Sabberworm/CSS/Value/CalcFunction.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CalcFunction extends CSSFunction {
1111

1212
public static function parse(ParserState $oParserState) {
1313
$aOperators = array('+', '-', '*', '/');
14-
$aTerminators = array('(', ';', "\n", "\r");
14+
$aTerminators = array('(', ';', "\n", "\r", ParserState::EOF);
1515
$sFunction = $oParserState->consumeUntil($aTerminators, false, true);
1616
if ($oParserState->peek(1, -1) != '(') {
1717
// Found ; or end of line before an opening bracket
@@ -25,6 +25,7 @@ public static function parse(ParserState $oParserState) {
2525
$iNestingLevel = 0;
2626
$iLastComponentType = NULL;
2727
while(!$oParserState->comes(')') || $iNestingLevel > 0) {
28+
if ($oParserState->isEnd() && $iNestingLevel === 0) break;
2829
$oParserState->consumeWhiteSpace();
2930
if ($oParserState->comes('(')) {
3031
$iNestingLevel++;
@@ -66,7 +67,9 @@ public static function parse(ParserState $oParserState) {
6667
$oParserState->consumeWhiteSpace();
6768
}
6869
$oList->addListComponent($oCalcList);
69-
$oParserState->consume(')');
70+
if (!$oParserState->isEnd()) {
71+
$oParserState->consume(')');
72+
}
7073
return new CalcFunction($sFunction, $oList, ',', $oParserState->currentLine());
7174
}
7275

lib/Sabberworm/CSS/Value/Value.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static function parseValue(ParserState $oParserState, $aListDelimiters =
1717
$aStack = array();
1818
$oParserState->consumeWhiteSpace();
1919
//Build a list of delimiters and parsed values
20-
while (!($oParserState->comes('}') || $oParserState->comes(';') || $oParserState->comes('!') || $oParserState->comes(')') || $oParserState->comes('\\'))) {
20+
while (!($oParserState->comes('}') || $oParserState->comes(';') || $oParserState->comes('!') || $oParserState->comes(')') || $oParserState->comes('\\') || $oParserState->isEnd())) {
2121
if (count($aStack) > 0) {
2222
$bFoundDelimiter = false;
2323
foreach ($aListDelimiters as $sDelimiter) {

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,32 @@ function testInvalidCalcInFile() {
425425
$this->assertSame($sExpected, $oDoc->render());
426426
}
427427

428+
function testInvalidCalc() {
429+
$parser = new Parser('div { height: calc(100px');
430+
$oDoc = $parser->parse();
431+
$this->assertSame('div {height: calc(100px);}', $oDoc->render());
432+
433+
$parser = new Parser('div { height: calc(100px)');
434+
$oDoc = $parser->parse();
435+
$this->assertSame('div {height: calc(100px);}', $oDoc->render());
436+
437+
$parser = new Parser('div { height: calc(100px);');
438+
$oDoc = $parser->parse();
439+
$this->assertSame('div {height: calc(100px);}', $oDoc->render());
440+
441+
$parser = new Parser('div { height: calc(100px}');
442+
$oDoc = $parser->parse();
443+
$this->assertSame('div {}', $oDoc->render());
444+
445+
$parser = new Parser('div { height: calc(100px;');
446+
$oDoc = $parser->parse();
447+
$this->assertSame('div {}', $oDoc->render());
448+
449+
$parser = new Parser('div { height: calc(100px;}');
450+
$oDoc = $parser->parse();
451+
$this->assertSame('div {}', $oDoc->render());
452+
}
453+
428454
function testGridLineNameInFile() {
429455
$oDoc = $this->parsedStructureForFile('grid-linename', Settings::create()->withMultibyteSupport(true));
430456
$sExpected = "div {grid-template-columns: [linename] 100px;}\nspan {grid-template-columns: [linename1 linename2] 100px;}";

0 commit comments

Comments
 (0)