-
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
Parse simple expressions #389
Open
raxbg
wants to merge
13
commits into
MyIntervals:main
Choose a base branch
from
raxbg:fix/expression-parsing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
908bc46
Parse simple expressions
raxbg 093c527
Support arithmetic ops (+,-,*,/) in functions
raxbg f734920
Add tests for arithmetic in functions
raxbg 7da6868
Merge branch 'fix/arithmetic-in-functions' into fix/expression-parsing
raxbg 8627ef6
Use CSSFunction::parseArgs when parsing expression arguments
raxbg 184b610
Handle arithmetic operators in Value
raxbg 9386fe9
Merge branch 'fix/arithmetic-in-functions' into fix/expression-parsing
raxbg d9f89cc
Fix tests
raxbg db4d18b
Merge branch 'upstream_master' into fix/expression-parsing
raxbg ca89a71
Make the check for arithmetic operators strict
raxbg 7aaebb2
Merge branch 'main' into fix/expression-parsing
raxbg abbcc8f
Catch up with upstream/main
raxbg 14118a2
Add class-specific tests for CSS\Value\Expression
raxbg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Sabberworm\CSS\Value; | ||
|
||
use Sabberworm\CSS\OutputFormat; | ||
use Sabberworm\CSS\Parsing\ParserState; | ||
|
||
/** | ||
* An `Expression` represents a special kind of value that is comprised of multiple components wrapped in parenthesis. | ||
* Examle `height: (vh - 10);`. | ||
*/ | ||
class Expression extends CSSFunction | ||
{ | ||
/** | ||
* @throws SourceException | ||
* @throws UnexpectedEOFException | ||
* @throws UnexpectedTokenException | ||
*/ | ||
public static function parse(ParserState $oParserState, bool $bIgnoreCase = false): Expression | ||
{ | ||
$oParserState->consume('('); | ||
$aArguments = parent::parseArguments($oParserState); | ||
$mResult = new Expression("", $aArguments, ',', $oParserState->currentLine()); | ||
$oParserState->consume(')'); | ||
return $mResult; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Sabberworm\CSS\Tests\Value; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sabberworm\CSS\Parsing\ParserState; | ||
use Sabberworm\CSS\Settings; | ||
use Sabberworm\CSS\Value\Value; | ||
use Sabberworm\CSS\Value\ValueList; | ||
use Sabberworm\CSS\Value\Expression; | ||
use Sabberworm\CSS\Rule\Rule; | ||
|
||
/** | ||
* @covers \Sabberworm\CSS\Value\Expression | ||
*/ | ||
final class ExpressionTest extends TestCase | ||
{ | ||
/** | ||
* @return array<0, array{string: string}> | ||
*/ | ||
public static function provideExpressions(): array | ||
{ | ||
return [ | ||
[ | ||
'input' => '(vh - 10) / 2', | ||
'expected_output' => '(vh - 10)/2', | ||
'expression_index' => 0, | ||
], | ||
[ | ||
'input' => 'max(5, (vh - 10))', | ||
'expected_output' => 'max(5,(vh - 10))', | ||
'expression_index' => 1 | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @dataProvider provideExpressions | ||
*/ | ||
public function parseExpressions(string $input, string $expected, int $expression_index): void | ||
{ | ||
$val = Value::parseValue( | ||
new ParserState($input, Settings::create()), | ||
$this->getDelimiters('height') | ||
); | ||
|
||
self::assertInstanceOf(ValueList::class, $val); | ||
self::assertInstanceOf(Expression::class, $val->getListComponents()[$expression_index]); | ||
self::assertSame($expected, (string) $val); | ||
} | ||
|
||
private function getDelimiters(string $rule): array | ||
{ | ||
$closure = function($rule) { | ||
return self::listDelimiterForRule($rule); | ||
}; | ||
|
||
$getter = $closure->bindTo(null, Rule::class); | ||
return $getter($rule); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
div { | ||
height: (vh - 10); | ||
} | ||
|
||
div { | ||
height: (vh - 10) / 2; | ||
} | ||
|
||
div { | ||
height: max(5, (vh - 10)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
div { | ||
height: max(300, vh + 10); | ||
} | ||
div { | ||
height: max(300, vh - 10); | ||
} | ||
div { | ||
height: max(300, vh * 10); | ||
} | ||
div { | ||
height: max(300, vh / 10); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
AFAICT, vh without a number in front is still not valid and I’d prefer it would throw an error in strict mode.
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.
Indeed, it is invalid, however this seems to be outside of the scope of these changes. I guess we need a separate issue for this. Also is it a concern of the parser to validate the semantic meaning of identifiers it encounters?
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 agree it's not.
Moving forwards, we've agreed to replace strict mode parsing with a logging mechanism. And try to replicate what most browsers do when encountering invalid syntax. A unit with no number in front should therefore probably be parsed as zero of that unit. And, as a bonus, a separate PR would log the lack of a number.
So, for now, we don't need to worry about strict mode, but should create issues for things that could be logged in future.