Skip to content

Commit c95610b

Browse files
committed
User parseIdentifier() for line name parsing
1 parent 92abffa commit c95610b

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,19 @@ private function parseNumericValue($bForColor = false) {
485485

486486
private function parseLineNameValue() {
487487
$this->consume('[');
488-
$this->consumeWhiteSpace();
489488
$sName = '';
490-
while(!$this->comes(']')) {
491-
$sName .= $this->parseCharacter(true);
489+
try {
490+
do {
491+
$this->consumeWhiteSpace();
492+
$sName .= $this->parseIdentifier(false, true) . ' ';
493+
} while (!$this->comes(']'));
494+
} catch (UnexpectedTokenException $e) {
495+
if(!$this->oParserSettings->bLenientParsing && (!$sName || !$this->comes(']'))) {// This handles constructs like this [ linename ] in non lenient mode
496+
throw $e;
497+
}
492498
}
493-
$this->consumeWhiteSpace();
494499
$this->consume(']');
495-
return new LineName($sName, $this->iLineNo);
500+
return new LineName(rtrim($sName), $this->iLineNo);
496501
}
497502

498503
private function parseColorValue() {

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,23 @@ function testCalcInFile() {
396396

397397
function testGridLineNameInFile() {
398398
$oDoc = $this->parsedStructureForFile('grid-linename', Settings::create()->withMultibyteSupport(true));
399-
$sExpected = '.test {grid-template-columns: [linename] 100px;}';
399+
$sExpected = "div {grid-template-columns: [linename] 100px;}\nspan {grid-template-columns: [linename1 linename2] 100px;}";
400400
$this->assertSame($sExpected, $oDoc->render());
401401
}
402402

403+
function testEmptyGridLineNameLenientInFile() {
404+
$oDoc = $this->parsedStructureForFile('empty-grid-linename');
405+
$sExpected = '.test {grid-template-columns: [] 100px;}';
406+
$this->assertSame($sExpected, $oDoc->render());
407+
}
408+
409+
/**
410+
* @expectedException Sabberworm\CSS\Parsing\UnexpectedTokenException
411+
*/
412+
function testLineNameFailure() {
413+
$this->parsedStructureForFile('-empty-grid-linename', Settings::create()->withLenientParsing(false));
414+
}
415+
403416
/**
404417
* @expectedException Sabberworm\CSS\Parsing\UnexpectedTokenException
405418
*/

tests/files/-empty-grid-linename.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.test { grid-template-columns: [] 100px; }

tests/files/empty-grid-linename.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.test { grid-template-columns: [] 100px; }

tests/files/grid-linename.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.test { grid-template-columns: [linename] 100px; }
1+
div { grid-template-columns: [ linename ] 100px; }
2+
span { grid-template-columns: [ linename1 linename2 ] 100px; }

0 commit comments

Comments
 (0)