-
Notifications
You must be signed in to change notification settings - Fork 148
[BUGFIX] Parse calc split over multiple lines #1399
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
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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,206 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sabberworm\CSS\Tests\Unit\Value; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Sabberworm\CSS\OutputFormat; | ||
| use Sabberworm\CSS\Parsing\ParserState; | ||
| use Sabberworm\CSS\Parsing\UnexpectedTokenException; | ||
| use Sabberworm\CSS\Settings; | ||
| use Sabberworm\CSS\Value\CalcFunction; | ||
| use Sabberworm\CSS\Value\CalcRuleValueList; | ||
| use Sabberworm\CSS\Value\Size; | ||
|
|
||
| /** | ||
| * @covers \Sabberworm\CSS\Value\CalcFunction | ||
| */ | ||
| final class CalcFunctionTest extends TestCase | ||
| { | ||
| /** | ||
| * @test | ||
| */ | ||
| public function parseSimpleCalc(): void | ||
| { | ||
| $css = 'calc(100% - 20px)'; | ||
| $calcFunction = $this->parse($css); | ||
|
|
||
| self::assertInstanceOf(CalcFunction::class, $calcFunction); | ||
| self::assertSame('calc', $calcFunction->getName()); | ||
|
|
||
| $args = $calcFunction->getArguments(); | ||
| self::assertCount(1, $args); | ||
| self::assertInstanceOf(CalcRuleValueList::class, $args[0]); | ||
|
|
||
| /** @var CalcRuleValueList $value */ | ||
| $value = $args[0]; | ||
| $components = $value->getListComponents(); | ||
| self::assertCount(3, $components); // 100%, -, 20px | ||
|
|
||
| self::assertInstanceOf(Size::class, $components[0]); | ||
| self::assertSame(100.0, $components[0]->getSize()); | ||
| self::assertSame('%', $components[0]->getUnit()); | ||
|
|
||
| self::assertSame('-', $components[1]); | ||
|
|
||
| self::assertInstanceOf(Size::class, $components[2]); | ||
| self::assertSame(20.0, $components[2]->getSize()); | ||
| self::assertSame('px', $components[2]->getUnit()); | ||
| } | ||
| /** | ||
| * @test | ||
| */ | ||
| public function parseNestedCalc(): void | ||
| { | ||
| $css = 'calc(100% - calc(20px + 1em))'; | ||
| $calcFunction = $this->parse($css); | ||
|
|
||
| /** @var CalcRuleValueList $value */ | ||
| $value = $calcFunction->getArguments()[0]; | ||
| $components = $value->getListComponents(); | ||
|
|
||
| self::assertCount(3, $components); | ||
| self::assertSame('-', $components[1]); | ||
|
|
||
| /** @var CalcFunction */ | ||
JakeQZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $nestedCalc = $components[2]; | ||
| self::assertInstanceOf(CalcFunction::class, $nestedCalc); | ||
|
|
||
| /** @var CalcRuleValueList $nestedValue */ | ||
| $nestedValue = $nestedCalc->getArguments()[0]; | ||
| self::assertInstanceOf(CalcRuleValueList::class, $nestedValue); | ||
| $nestedComponents = $nestedValue->getListComponents(); | ||
|
|
||
| self::assertCount(3, $nestedComponents); | ||
| self::assertSame('+', $nestedComponents[1]); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function parseWithParentheses(): void | ||
| { | ||
| $css = 'calc((100% - 20px) * 2)'; | ||
| $calcFunction = $this->parse($css); | ||
|
|
||
| /** @var CalcRuleValueList $value */ | ||
| $value = $calcFunction->getArguments()[0]; | ||
| $components = $value->getListComponents(); | ||
|
|
||
| self::assertCount(7, $components); | ||
| self::assertSame('(', $components[0]); | ||
| self::assertInstanceOf(Size::class, $components[1]); // 100% | ||
| self::assertSame('-', $components[2]); | ||
| self::assertInstanceOf(Size::class, $components[3]); // 20px | ||
| self::assertSame(')', $components[4]); | ||
| self::assertSame('*', $components[5]); | ||
| self::assertInstanceOf(Size::class, $components[6]); // 2 | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, array{0: string, 1: string}> | ||
JakeQZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| public function provideValidOperatorSyntax(): array | ||
| { | ||
| return [ | ||
| '+ op' => ['calc(100% + 20px)', 'calc(100% + 20px)'], | ||
| '- op' => ['calc(100% - 20px)', 'calc(100% - 20px)'], | ||
| '* op' => ['calc(100% * 20)', 'calc(100% * 20)'], | ||
| '* op no space' => ['calc(100%*20)', 'calc(100% * 20)'], | ||
| '/ op' => ['calc(100% / 20)', 'calc(100% / 20)'], | ||
| '/ op no space' => ['calc(100%/20)', 'calc(100% / 20)'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| * | ||
| * @dataProvider provideValidOperatorSyntax | ||
| */ | ||
| public function parseValidOperators(string $css, string $rendered): void | ||
| { | ||
| $calcFunction = $this->parse($css); | ||
| $output = $calcFunction->render(OutputFormat::create()); | ||
| self::assertSame($rendered, $output); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, array{0: string, 1: string}> | ||
JakeQZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| public function provideMultiline(): array | ||
| { | ||
| return [ | ||
| 'right newline' => ["calc(100% +\n20px)", 'calc(100% + 20px)'], | ||
| 'right and outer newline' => ["calc(\n100% +\n20px\n)", 'calc(100% + 20px)'], | ||
| 'left newline' => ["calc(100%\n+ 20px)", 'calc(100% + 20px)'], | ||
| 'both newline' => ["calc(100%\n+\n20px)", 'calc(100% + 20px)'], | ||
| 'tab whitespace' => ["calc(100%\t+\t20px)", 'calc(100% + 20px)'], | ||
| '- op' => ["calc(100%\n-\n20px)", 'calc(100% - 20px)'], | ||
| '/ op' => ["calc(100% /\n20)", 'calc(100% / 20)'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| * | ||
| * @dataProvider provideMultiline | ||
| */ | ||
| public function parseMultiline(string $css, string $rendered): void | ||
| { | ||
| $calcFunction = $this->parse($css); | ||
| $output = $calcFunction->render(OutputFormat::create()); | ||
| self::assertSame($rendered, $output); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, array{0: string}> | ||
JakeQZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| public function provideInvalidSyntax(): array | ||
| { | ||
| return [ | ||
| 'missing space around -' => ['calc(100%-20px)'], | ||
| 'missing space around +' => ['calc(100%+20px)'], | ||
| 'invalid operator' => ['calc(100% ^ 20px)'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| * | ||
| * @dataProvider provideInvalidSyntax | ||
| */ | ||
| public function parseThrowsExceptionForInvalidSyntax(string $css): void | ||
| { | ||
| $this->expectException(UnexpectedTokenException::class); | ||
| $this->parse($css); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function parseThrowsExceptionIfCalledWithWrongFunctionName(): void | ||
| { | ||
| $css = 'wrong(100% - 20px)'; | ||
| $parserState = new ParserState($css, Settings::create()); | ||
|
|
||
| $this->expectException(UnexpectedTokenException::class); | ||
| $this->expectExceptionMessage('calc'); | ||
| CalcFunction::parse($parserState); | ||
| } | ||
|
|
||
| /** | ||
| * Parse provided CSS as a CalcFunction | ||
| * | ||
| * @param string $css | ||
| * @return CalcFunction | ||
|
Comment on lines
+195
to
+196
Collaborator
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. These PHPDoc annotations are actually redundant, since the types are in the method signature. |
||
| */ | ||
| private function parse(string $css): CalcFunction | ||
| { | ||
| $parserState = new ParserState($css, Settings::create()); | ||
|
|
||
| $function = CalcFunction::parse($parserState); | ||
| self::assertInstanceOf(CalcFunction::class, $function); | ||
| return $function; | ||
| } | ||
| } | ||
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.
I forgot to mention that we put the changelog entries in reverse chronological order (though I'm not sure why).