Skip to content

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

Merged
merged 4 commits into from
Jul 13, 2018

Conversation

raxbg
Copy link
Contributor

@raxbg raxbg commented Jul 11, 2018

Fix proposal for #131

@@ -480,6 +483,16 @@ private function parseNumericValue($bForColor = false) {
return new Size(floatval($sSize), $sUnit, $bForColor, $this->iLineNo);
}

private function parseLineNameValue() {
$this->consume('[');
$sName = '';
Copy link
Collaborator

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 ].

Copy link
Contributor Author

@raxbg raxbg Jul 12, 2018

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.

$this->consume('[');
$sName = '';
while(!$this->comes(']')) {
$sName .= $this->consume(1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading https://developer.mozilla.org/en-US/docs/Web/CSS/custom-ident it seems we should support certain escape sequences. We could use parseCharacter(true) for this.

Copy link
Contributor Author

@raxbg raxbg Jul 12, 2018

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.

$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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 UnexpectedTokenException already in place that does that, we don’t need to catch it in lenient mode.

Then again I’m not sure what the other half of the if (!$sName || !$this->comes(']')) is for. The comment does not enlighten me much. Shouldn’t [ linename ] parse correctly without throwing an exception? There’s something I don’t understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

[] is parsed as [] in Chrome. If the UnexpectedTokenException is not handled here the parser will drop the value for the rule.

Then again I’m not sure what the other half of the if (!$sName || !$this->comes(']')) is for.

On the second iteration of the loop, parseIdentifier() will try to parse the ], so it will throw an exception. However this should not be considered an error because $sName will not be empty (it will have the value of 'linename '). The exception should be forwarded only if $sName is empty and lenient mode is disabled. This will be the case if parsing [].

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, you have convinced me with the lenient mode exception handling.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 LineName should extend ValueList with $sSeparator = ' ' and thus the code would become:

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 LineName to the following:

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 [ and before ].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it ❤️

@sabberworm sabberworm merged commit 52e9057 into MyIntervals:master Jul 13, 2018
@sabberworm
Copy link
Collaborator

Thanks a lot @raxbg !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants