-
Notifications
You must be signed in to change notification settings - Fork 144
Add support for grid line names #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
use Sabberworm\CSS\Value\Color; | ||
use Sabberworm\CSS\Value\URL; | ||
use Sabberworm\CSS\Value\CSSString; | ||
use Sabberworm\CSS\Value\LineName; | ||
use Sabberworm\CSS\Rule\Rule; | ||
use Sabberworm\CSS\Parsing\UnexpectedTokenException; | ||
use Sabberworm\CSS\Comment\Comment; | ||
|
@@ -447,6 +448,8 @@ private function parsePrimitiveValue() { | |
$oValue = $this->parseStringValue(); | ||
} else if ($this->comes("progid:") && $this->oParserSettings->bLenientParsing) { | ||
$oValue = $this->parseMicrosoftFilter(); | ||
} else if ($this->comes("[")) { | ||
$oValue = $this->parseLineNameValue(); | ||
} else { | ||
$oValue = $this->parseIdentifier(true, false); | ||
} | ||
|
@@ -480,6 +483,23 @@ private function parseNumericValue($bForColor = false) { | |
return new Size(floatval($sSize), $sUnit, $bForColor, $this->iLineNo); | ||
} | ||
|
||
private function parseLineNameValue() { | ||
$this->consume('['); | ||
$sName = ''; | ||
try { | ||
do { | ||
$this->consumeWhiteSpace(); | ||
$sName .= $this->parseIdentifier(false, true) . ' '; | ||
} while (!$this->comes(']')); | ||
} catch (UnexpectedTokenException $e) { | ||
if(!$this->oParserSettings->bLenientParsing && (!$sName || !$this->comes(']'))) {// This handles constructs like this [ linename ] in non lenient mode | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lenient mode has always tried to mimic what the browser does when parsing. I think if the browser encounters an unparseable value, it throws away the whole rule so we should do the same. Since there is a handler for Then again I’m not sure what the other half of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On the second iteration of the loop, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, you have convinced me with the lenient mode exception handling. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think using exception handling for control flow should be avoided. Having read your comments, shouldn’t the following also work?: private function parseLineNameValue() {
$this->consume('[');
$this->consumeWhiteSpace();
$sName = '';
do {
if($this->oParserSettings->bLenientParsing) {
try {
$sName .= $this->parseIdentifier(false, true) . ' ';
} catch(UnexpectedTokenException $e) {}
} else {
$sName .= $this->parseIdentifier(false, true) . ' ';
}
$this->consumeWhiteSpace();
} while (!$this->comes(']'));
$this->consume(']');
return new LineName(rtrim($sName), $this->iLineNo);
} Having said that: now that I know there can be multiple identifiers in line name value brackets, I think private function parseLineNameValue() {
$this->consume('[');
$this->consumeWhiteSpace();
$aNames = array();
do {
if($this->oParserSettings->bLenientParsing) {
try {
$aNames[] = $this->parseIdentifier(false, true);
} catch(UnexpectedTokenException $e) {}
} else {
$aNames[] = $this->parseIdentifier(false, true);
}
$this->consumeWhiteSpace();
} while (!$this->comes(']'));
$this->consume(']');
return new LineName($aNames, $this->iLineNo);
} which would allow you to shorten class LineName extends ValueList {
public function __construct($aComponents = array(), $iLineNo = 0) {
parent::__construct($aComponents, ' ', $iLineNo);
}
public function __toString() {
return $this->render(new \Sabberworm\CSS\OutputFormat());
}
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
return '['.parent::render(\Sabberworm\CSS\OutputFormat::createCompact()).']';
}
} Optionally, you could create OutputFormat options on whether or not white-space should be output after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love it ❤️ |
||
throw $e; | ||
} | ||
} | ||
$this->consume(']'); | ||
return new LineName(rtrim($sName), $this->iLineNo); | ||
} | ||
|
||
private function parseColorValue() { | ||
$aColor = array(); | ||
if ($this->comes('#')) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Sabberworm\CSS\Value; | ||
|
||
class LineName extends PrimitiveValue { | ||
private $sName; | ||
|
||
public function __construct($sName, $iLineNo = 0) { | ||
parent::__construct($iLineNo); | ||
$this->sName = $sName; | ||
} | ||
|
||
public function setName($sName) { | ||
$this->sName = $sName; | ||
} | ||
|
||
public function getName() { | ||
return $this->sName; | ||
} | ||
|
||
public function __toString() { | ||
return $this->render(new \Sabberworm\CSS\OutputFormat()); | ||
} | ||
|
||
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) { | ||
return "[{$this->sName}]"; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.test { grid-template-columns: [] 100px; } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.test { grid-template-columns: [] 100px; } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
div { grid-template-columns: [ linename ] 100px; } | ||
span { grid-template-columns: [ linename1 linename2 ] 100px; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
consumeWhitespace
after[
and before]
.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be okay now. Please check.