Skip to content

Commit 52e9057

Browse files
authored
Merge pull request #132 from raxbg/grid_syntax_support
Add support for grid line names
2 parents 0c74522 + 46e80a9 commit 52e9057

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Sabberworm\CSS\Value\Color;
2222
use Sabberworm\CSS\Value\URL;
2323
use Sabberworm\CSS\Value\CSSString;
24+
use Sabberworm\CSS\Value\LineName;
2425
use Sabberworm\CSS\Rule\Rule;
2526
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
2627
use Sabberworm\CSS\Comment\Comment;
@@ -447,6 +448,8 @@ private function parsePrimitiveValue() {
447448
$oValue = $this->parseStringValue();
448449
} else if ($this->comes("progid:") && $this->oParserSettings->bLenientParsing) {
449450
$oValue = $this->parseMicrosoftFilter();
451+
} else if ($this->comes("[")) {
452+
$oValue = $this->parseLineNameValue();
450453
} else {
451454
$oValue = $this->parseIdentifier(true, false);
452455
}
@@ -480,6 +483,24 @@ private function parseNumericValue($bForColor = false) {
480483
return new Size(floatval($sSize), $sUnit, $bForColor, $this->iLineNo);
481484
}
482485

486+
private function parseLineNameValue() {
487+
$this->consume('[');
488+
$this->consumeWhiteSpace();
489+
$aNames = array();
490+
do {
491+
if($this->oParserSettings->bLenientParsing) {
492+
try {
493+
$aNames[] = $this->parseIdentifier(false, true);
494+
} catch(UnexpectedTokenException $e) {}
495+
} else {
496+
$aNames[] = $this->parseIdentifier(false, true);
497+
}
498+
$this->consumeWhiteSpace();
499+
} while (!$this->comes(']'));
500+
$this->consume(']');
501+
return new LineName($aNames, $this->iLineNo);
502+
}
503+
483504
private function parseColorValue() {
484505
$aColor = array();
485506
if ($this->comes('#')) {

lib/Sabberworm/CSS/Value/LineName.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Sabberworm\CSS\Value;
4+
5+
class LineName extends ValueList {
6+
public function __construct($aComponents = array(), $iLineNo = 0) {
7+
parent::__construct($aComponents, ' ', $iLineNo);
8+
}
9+
10+
public function __toString() {
11+
return $this->render(new \Sabberworm\CSS\OutputFormat());
12+
}
13+
14+
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
15+
return '[' . parent::render(\Sabberworm\CSS\OutputFormat::createCompact()) . ']';
16+
}
17+
18+
}

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,25 @@ function testCalcInFile() {
394394
$this->assertSame($sExpected, $oDoc->render());
395395
}
396396

397+
function testGridLineNameInFile() {
398+
$oDoc = $this->parsedStructureForFile('grid-linename', Settings::create()->withMultibyteSupport(true));
399+
$sExpected = "div {grid-template-columns: [linename] 100px;}\nspan {grid-template-columns: [linename1 linename2] 100px;}";
400+
$this->assertSame($sExpected, $oDoc->render());
401+
}
402+
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+
397416
/**
398417
* @expectedException Sabberworm\CSS\Parsing\UnexpectedTokenException
399418
*/

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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
div { grid-template-columns: [ linename ] 100px; }
2+
span { grid-template-columns: [ linename1 linename2 ] 100px; }

0 commit comments

Comments
 (0)