Skip to content
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

Fix/invalid calc parsing #169

Merged
merged 15 commits into from
Sep 20, 2022
Merged

Conversation

raxbg
Copy link
Contributor

@raxbg raxbg commented Sep 18, 2019

This PR adds better handling of invalid calc definitions.

}

div{
height: calc
Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t think we should disallow calc used as a non-function keyword (I realize your change didn’t break this and simply improved the error reporting) but to my knowledge the following is valid:

#outer {
  grid-template-areas: "top calc" "bottom calc";
}
#inner {
  grid-area: calc;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Or maybe I misunderstood… is the error caused by the missing semicolon at the end?

Copy link
Contributor Author

@raxbg raxbg Nov 13, 2019

Choose a reason for hiding this comment

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

It appears that

#inner {
  grid-area: url;
}

is as valid. I pushed an update that handles this and as a side effect it also fixes #170. If this gets merged PR #174 can be closed.

Comment on lines 53 to 62
public function setAnchor() {
$this->iAnchor = $this->iCurrentPosition;
}

public function backtrackToAnchor() {
if ($this->iAnchor !== null) {
$this->iCurrentPosition = $this->iAnchor;
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I kinda like the idea with the anchor but I’d prefer it to be implemented as a separate class:

class Anchor {
	private $iPosition;
	private $oParserState; 
[…]
	function backtrack() {
		$this->oParserState->iCurrentPosition = $this->iPosition; // Or using setter
	}
}

class ParserState {
[…]
	function anchor() {
		return new Anchor($this->iCurrentPosition, $this);
	}
[…]
}

$oParserState->consume(')');
if ($oParserState->streql('url', $mResult)) {
$oParserState->backtrackToAnchor();
$mResult = URL::parse($oParserState);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we use URL::parse and CalcFunction::parse also in other places? Otherwise we could just change them to not parse the function name (or pass it as argument in case of CalcFunction).

Also maybe we should just add a CSSFunction::parse method so that all three cases have the same structure.

@@ -14,11 +14,21 @@ public function __construct(CSSString $oURL, $iLineNo = 0) {
}

public static function parse(ParserState $oParserState) {
$bUseUrl = $oParserState->comes('url', true);
Copy link
Contributor

Choose a reason for hiding this comment

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

I’m not sure why comes isn’t good… Can you explain?

@sabberworm
Copy link
Contributor

Codacy Here is an overview of what got changed by this pull request:

Issues
======
+ Solved 2
- Added 7
           

Complexity increasing per file
==============================
- lib/Sabberworm/CSS/Value/URL.php  2
- lib/Sabberworm/CSS/Value/CalcFunction.php  4
         

See the complete overview on Codacy


div{
height: calc
(25% - 1em);
Copy link
Contributor

Choose a reason for hiding this comment

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

$oParserState->consumeWhiteSpace();
$oParserState->consume('(');
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

}

div{
height: calc
Copy link
Contributor

Choose a reason for hiding this comment

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

$oParserState->consume(')');
if ($oParserState->streql('url', $mResult)) {
$oParserState->backtrackToAnchor();
$mResult = URL::parse($oParserState);
Copy link
Contributor

Choose a reason for hiding this comment

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


div{
height: calc
width: 100px
Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -0,0 +1,22 @@
div{
height: calc (25% - 1em);
Copy link
Contributor

Choose a reason for hiding this comment

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

$mResult = URL::parse($oParserState);
} else if ($oParserState->streql('calc', $mResult) || $oParserState->streql('-webkit-calc', $mResult) || $oParserState->streql('-moz-calc', $mResult)) {
$oParserState->backtrackToAnchor();
$mResult = CalcFunction::parse($oParserState);
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

@sabberworm sabberworm left a comment

Choose a reason for hiding this comment

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

Thank you very much, looks fantastic!

@sabberworm sabberworm merged commit bbece12 into MyIntervals:master Sep 20, 2022
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