-
Notifications
You must be signed in to change notification settings - Fork 154
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
[BUGFIX] Remove uppercase !important from attributes #911
Changes from all commits
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 |
---|---|---|
|
@@ -2752,40 +2752,98 @@ private function buildSubjectWithBoilerplateHtml(string $style = ''): CssInliner | |
return $this->buildDebugSubject($html); | ||
} | ||
|
||
/** | ||
* @return string[][] | ||
*/ | ||
public function provideImportantDeclarationMarker(): array | ||
{ | ||
return [ | ||
'lower case' => [' !important'], | ||
'mixed case' => [' !ImPorTant'], | ||
'upper case' => [' !IMPORTANT'], | ||
'with space within' => [' ! important'], | ||
'without space preceding' => ['!important'], | ||
]; | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @param string $importantMarker | ||
* | ||
* @dataProvider provideImportantDeclarationMarker | ||
*/ | ||
public function importantInExternalCssOverwritesInlineCss(): void | ||
public function inlineCssRemovesImportantFromStyleAttribute(string $importantMarker): void | ||
{ | ||
$subject = $this->buildSubjectWithBoilerplateHtml('margin: 2px;'); | ||
$subject = $this->buildSubjectWithBoilerplateHtml('margin: 1px' . $importantMarker . ';'); | ||
|
||
$subject->inlineCss('p { margin: 1px !important; }'); | ||
$subject->inlineCss(); | ||
|
||
self::assertStringContainsString('<p style="margin: 1px;">', $subject->renderBodyContent()); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @param string $importantMarker | ||
* | ||
* @dataProvider provideImportantDeclarationMarker | ||
*/ | ||
public function importantInExternalCssKeepsInlineCssForOtherAttributes(): void | ||
public function inlineCssRemovesImportantFromInlinedStyleAttribute(string $importantMarker): void | ||
{ | ||
$subject = $this->buildSubjectWithBoilerplateHtml('margin: 2px; text-align: center;'); | ||
$subject = $this->buildSubjectWithBoilerplateHtml(); | ||
|
||
$subject->inlineCss('p { margin: 1px !important; }'); | ||
$subject->inlineCss('p { margin: 1px' . $importantMarker . '; }'); | ||
|
||
self::assertStringContainsString('<p style="text-align: center; margin: 1px;">', $subject->renderBodyContent()); | ||
self::assertStringContainsString('<p style="margin: 1px;">', $subject->renderBodyContent()); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @param string $importantMarker | ||
* | ||
* @dataProvider provideImportantDeclarationMarker | ||
*/ | ||
public function importantIsCaseInsensitive(): void | ||
public function importantInExternalCssOverwritesInlineCss(string $importantMarker): void | ||
{ | ||
$subject = $this->buildSubjectWithBoilerplateHtml('margin: 2px;'); | ||
|
||
$subject->inlineCss('p { margin: 1px !ImPorTant; }'); | ||
$subject->inlineCss('p { margin: 1px' . $importantMarker . '; }'); | ||
|
||
self::assertStringContainsString('<p style="margin: 1px;">', $subject->renderBodyContent()); | ||
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 then also should rename the test. 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 maybe we can use a data provider for the the 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.
Good idea. I have done that, and adjusted the tests. I also then found another bug - The final fix involves changing the Do some of the other Psalm issues included in the 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. Seems I also need to disable a PHPCS rule, because I declared the extra exception that could be thrown. See squizlabs/PHP_CodeSniffer#2683. |
||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @param string $importantMarker | ||
* | ||
* @dataProvider provideImportantDeclarationMarker | ||
*/ | ||
public function importantInExternalCssNotOverwritesImportantInInlineCss(string $importantMarker): void | ||
{ | ||
$subject = $this->buildSubjectWithBoilerplateHtml('margin: 2px' . $importantMarker . ';'); | ||
|
||
$subject->inlineCss('p { margin: 1px' . $importantMarker . '; }'); | ||
|
||
self::assertStringContainsString('<p style="margin: 2px;">', $subject->renderBodyContent()); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @param string $importantMarker | ||
* | ||
* @dataProvider provideImportantDeclarationMarker | ||
*/ | ||
public function importantInExternalCssKeepsInlineCssForOtherAttributes(string $importantMarker): void | ||
{ | ||
$subject = $this->buildSubjectWithBoilerplateHtml('margin: 2px; text-align: center;'); | ||
|
||
self::assertStringContainsString('<p style="margin: 1px !ImPorTant;">', $subject->renderBodyContent()); | ||
$subject->inlineCss('p { margin: 1px' . $importantMarker . '; }'); | ||
|
||
self::assertStringContainsString('<p style="text-align: center; margin: 1px;">', $subject->renderBodyContent()); | ||
} | ||
|
||
/** | ||
|
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.
Psalm says that
preg_replace
might returnnull
. I think it's more likely that the server will crash and it won't return anything at all, but Psalm doesn't seem to cover that eventuality.Please reject this change and either remove Psalm from the project or tell me how I'm supposed to actually make code changes that satisfy Psalm in some kind-of semi-positive way.
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.
In this case, Psalm is right.
preg_replace
will returnnull
if there is an error.For doing this in a cleaner way, we could do something like this:
… = \preg_replace(…) ?? ''
This will return an empty string in case of an error.
Or we could throw an exception if
null
is returned to make the problem visible.