-
Notifications
You must be signed in to change notification settings - Fork 147
[BUGFIX] Do not escape characters that do not need escaping in CSS string #1444
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
base: main
Are you sure you want to change the base?
Changes from all commits
676dd8c
07504ba
84e938d
2c201fc
234f5da
3855f91
90d1602
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |||||||||||||||||||||||||||
| namespace Sabberworm\CSS\Tests\Unit\Value; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| use PHPUnit\Framework\TestCase; | ||||||||||||||||||||||||||||
| use Sabberworm\CSS\OutputFormat; | ||||||||||||||||||||||||||||
| use Sabberworm\CSS\Value\CSSString; | ||||||||||||||||||||||||||||
| use Sabberworm\CSS\Value\PrimitiveValue; | ||||||||||||||||||||||||||||
| use Sabberworm\CSS\Value\Value; | ||||||||||||||||||||||||||||
|
|
@@ -107,4 +108,44 @@ public function getArrayRepresentationIncludesContents(): void | |||||||||||||||||||||||||||
| self::assertArrayHasKey('contents', $result); | ||||||||||||||||||||||||||||
| self::assertSame($contents, $result['contents']); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * @test | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| public function doesNotEscapeCharactersThatDoNotNeedToBeEscaped(): void | ||||||||||||||||||||||||||||
|
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. ...and
Suggested change
|
||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| $input = "data:image/svg+xml,%3Csvg width='24' height='24' viewBox='0 0 24 24' fill='none'" . | ||||||||||||||||||||||||||||
| " xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd'" . | ||||||||||||||||||||||||||||
| " d='M14.3145 2 3V11.9987H17.5687L18 8.20761H14.3145L14.32 6.31012C14.32 5.32134 14.4207" . | ||||||||||||||||||||||||||||
| ' 4.79153 15.9426 4.79153H17.977V1H14.7223C10.8129 1 9.43687 2.83909 9.43687 ' . | ||||||||||||||||||||||||||||
| " 5.93187V8.20804H7V11.9991H9.43687V23H14.3145Z' fill='black'/%3E%3C/svg%3E%0A"; | ||||||||||||||||||||||||||||
|
Comment on lines
+117
to
+121
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. Autoformatting may apply some indentation, but I suggest leaving it as is for now. @oliverklee can run the autoformatter he uses as a post-PR (I think it's part of PHPStorm, which I don't have, and even if you have it, you may not have the same configuration set-up). |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| $outputFormat = OutputFormat::createPretty(); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| self::assertSame("\"{$input}\"", (new CSSString($input))->render($outputFormat)); | ||||||||||||||||||||||||||||
|
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. I think we are generally favouring using string contactenation rather than string interpolation, but I'm not sure of the rationale. All I can gather from some threads on StackOverflow is that it is a matter of personal preference. @oliverklee, are you able to expand on this?
Contributor
Author
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. If you regex the project with
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. Yes, for new code, please use string concatenation instead of interpolation.
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.
I've opened #1464, because I'm not sure that's best. Apologies for the conflicting reviewer requests. I'd be happy to merge with string interpolation for now. We can revisit this later pending the outcome of that discussion. @oliverklee, would you be happy to merge this with the strings as are, pending further revisitation?
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. Yes, I'd be fine to keep the string interpolation when we merge this PR. I'd still like to get rid of the addslash-first-and-then-partially-undo part, though. |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| $outputFormat->setStringQuotingType("'"); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| $expected = str_replace("'", "\\'", $input); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| self::assertSame("'{$expected}'", (new CSSString($input))->render($outputFormat)); | ||||||||||||||||||||||||||||
|
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. Ditto. |
||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * @test | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| public function doesNotEscapeCharactersThatDoNotNeedToBeEscaped2(): void | ||||||||||||||||||||||||||||
|
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. Now there are two test methods, rather than appending the number
Suggested change
|
||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| $outputFormat = OutputFormat::createPretty(); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| $input = "'Hello World'"; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| self::assertSame("\"{$input}\"", (new CSSString($input))->render($outputFormat)); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| $input = '"Hello World"'; | ||||||||||||||||||||||||||||
|
Comment on lines
+139
to
+145
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. The first test checks that single quote ( I'd suggest the second test does likewise for double quote (
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| $outputFormat->setStringQuotingType("'"); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| self::assertSame("'{$input}'", (new CSSString($input))->render($outputFormat)); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
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.
Instead of first escaping unnecessarily and then undoing part of the work, I'd prefer to only escape what we need to escape in the first place, i.e., directly use
str_replaceand avoidaddslashes.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 suggested the current approach in my original review - see #1444 (review)
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.
Hm … couldn't we use just the
str_replaceand skip theaddslashes(without using a loop)?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 use
addcslashesand provide the characters to escape?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.
We could probably use
addcslashesto escape the backslash and quote.The only other character that
addslashesescapes is the null character (U+0000), which I don't think we need to worry about (there are no tests for this, it is highly unlikely to be used in the real world, and it was probably just an inconsequential side-effect ofaddslashesthat that would be the behaviour - other control characters aren't handled, apart from newline which is currentlystr_replaced separately by the calling method).Perhaps we could move the
str_replacefor newline to the new method as part of this change.