-
Notifications
You must be signed in to change notification settings - Fork 146
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
Fix/invalid calc parsing #169
Conversation
tests/files/calc-invalid.css
Outdated
} | ||
|
||
div{ | ||
height: calc |
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.
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;
}
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.
Or maybe I misunderstood… is the error caused by the missing semicolon at the end?
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.
public function setAnchor() { | ||
$this->iAnchor = $this->iCurrentPosition; | ||
} | ||
|
||
public function backtrackToAnchor() { | ||
if ($this->iAnchor !== null) { | ||
$this->iCurrentPosition = $this->iAnchor; | ||
} | ||
} | ||
|
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.
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);
}
[…]
}
lib/Sabberworm/CSS/Value/Value.php
Outdated
$oParserState->consume(')'); | ||
if ($oParserState->streql('url', $mResult)) { | ||
$oParserState->backtrackToAnchor(); | ||
$mResult = URL::parse($oParserState); |
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.
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.
lib/Sabberworm/CSS/Value/URL.php
Outdated
@@ -14,11 +14,21 @@ public function __construct(CSSString $oURL, $iLineNo = 0) { | |||
} | |||
|
|||
public static function parse(ParserState $oParserState) { | |||
$bUseUrl = $oParserState->comes('url', true); |
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.
I’m not sure why comes
isn’t good… Can you explain?
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 |
tests/files/calc-invalid.css
Outdated
|
||
div{ | ||
height: calc | ||
(25% - 1em); |
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.
Issue found: Expected RBRACE at line 7, col 3.
lib/Sabberworm/CSS/Value/URL.php
Outdated
$oParserState->consumeWhiteSpace(); | ||
$oParserState->consume('('); | ||
} else { |
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.
tests/files/calc-invalid.css
Outdated
} | ||
|
||
div{ | ||
height: calc |
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.
Issue found: Missed semicolon (CssSyntaxError)
lib/Sabberworm/CSS/Value/Value.php
Outdated
$oParserState->consume(')'); | ||
if ($oParserState->streql('url', $mResult)) { | ||
$oParserState->backtrackToAnchor(); | ||
$mResult = URL::parse($oParserState); |
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.
tests/files/calc-invalid.css
Outdated
|
||
div{ | ||
height: calc | ||
width: 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.
Issue found: Expected RBRACE at line 12, col 8.
tests/files/calc-invalid.css
Outdated
@@ -0,0 +1,22 @@ | |||
div{ | |||
height: calc (25% - 1em); |
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.
Issue found: Expected RBRACE at line 2, col 16.
lib/Sabberworm/CSS/Value/Value.php
Outdated
$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); |
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.
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.
Thank you very much, looks fantastic!
This PR adds better handling of invalid
calc
definitions.