Skip to content
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

[TASK] Pass debug setting on to CSS parser #1142

Merged
merged 4 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
### Added

### Changed
- Throw exception with invalid CSS in debug mode (#1142)
- Only support up to 69 atomic expressions in a selector (#1113)
- Require `sabberworm/php-css-parser:^8.4.0` (#1134)
- Upgrade to PHPUnit 9 (#1112)
Expand Down
27 changes: 24 additions & 3 deletions src/Css/CssDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Sabberworm\CSS\Renderable as CssRenderable;
use Sabberworm\CSS\RuleSet\DeclarationBlock as CssDeclarationBlock;
use Sabberworm\CSS\RuleSet\RuleSet as CssRuleSet;
use Sabberworm\CSS\Settings as ParserSettings;

/**
* Parses and stores a CSS document from a string of CSS, and provides methods to obtain the CSS in parts or as data
Expand All @@ -36,11 +37,31 @@ class CssDocument
private $isImportRuleAllowed = true;

/**
* @param string $css
* @param string $css {@see https://github.com/squizlabs/PHP_CodeSniffer/issues/3521}
oliverklee marked this conversation as resolved.
Show resolved Hide resolved
* @param bool $debug
* If this is `true`, an exception will be thrown if invalid CSS is encountered.
* Otherwise the parser will try to do the best it can.
*/
public function __construct(string $css)
public function __construct(string $css, bool $debug)
{
$this->sabberwormCssDocument = (new CssParser($css))->parse();
// CSS Parser currently throws exception with nested at-rules (like `@media`) in strict parsing mode
$parserSettings = ParserSettings::create()->withLenientParsing(!$debug || static::hasNestedAtRule($css));

// CSS Parser currently throws exception with non-empty whitespace-only CSS in strict parsing mode, so `trim()`
// @see https://github.com/sabberworm/PHP-CSS-Parser/issues/349
$this->sabberwormCssDocument = (new CssParser(\trim($css), $parserSettings))->parse();
oliverklee marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Tests if a string of CSS appears to contain an at-rule with nested rules
* (`@media`, `@supports`, `@keyframes`, `@document`,
* the latter two additionally with vendor prefixes that may commonly be used).
*
* @see https://github.com/sabberworm/PHP-CSS-Parser/issues/127
*/
private static function hasNestedAtRule(string $css): bool
oliverklee marked this conversation as resolved.
Show resolved Hide resolved
{
return \preg_match('/@(?:media|supports|(?:-webkit-|-moz-|-ms-|-o-)?+(keyframes|document))\\b/', $css) === 1;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/CssInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function inlineCss(string $css = ''): self
if ($this->isStyleBlocksParsingEnabled) {
$combinedCss .= $this->getCssFromAllStyleNodes();
}
$parsedCss = new CssDocument($combinedCss);
$parsedCss = new CssDocument($combinedCss, $this->debug);

$excludedNodes = $this->getNodesToExclude();
$cssRules = $this->collateCssRules($parsedCss);
Expand Down
103 changes: 83 additions & 20 deletions tests/Unit/Css/CssDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Pelago\Emogrifier\Css\CssDocument;
use Pelago\Emogrifier\Tests\Support\Traits\AssertCss;
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;

/**
* @covers \Pelago\Emogrifier\Css\CssDocument
Expand All @@ -22,6 +24,11 @@ final class CssDocumentTest extends TestCase
. ' font-family: "Foo Sans";' . "\n"
. ' src: url("/foo-sans.woff2") format("woff2");' . "\n}";

private static function createDebugSubject(string $css): CssDocument
oliverklee marked this conversation as resolved.
Show resolved Hide resolved
{
return new CssDocument($css, true);
}

/**
* @test
*
Expand All @@ -33,7 +40,7 @@ final class CssDocumentTest extends TestCase
public function parsesSelector(string $selector): void
{
$css = $selector . '{ color: green; }';
$subject = new CssDocument($css);
$subject = self::createDebugSubject($css);

$result = $subject->getStyleRulesData([]);

Expand All @@ -47,7 +54,7 @@ public function parsesSelector(string $selector): void
public function canParseMultipleSelectors(): void
{
$css = 'h1, h2 { color: green; }';
$subject = new CssDocument($css);
$subject = self::createDebugSubject($css);

$result = $subject->getStyleRulesData([]);

Expand Down Expand Up @@ -115,7 +122,7 @@ public function provideSelectorWithVariedWhitespace(): array
public function parsesDeclarations(string $declarations): void
{
$css = 'p {' . $declarations . '}';
$subject = new CssDocument($css);
$subject = self::createDebugSubject($css);

$result = $subject->getStyleRulesData([]);

Expand Down Expand Up @@ -177,7 +184,7 @@ public function parsesAtMediaRule(string $mediaQuery): void
{
$atMediaAndQuery = '@media ' . $mediaQuery;
$css = $atMediaAndQuery . ' { p { color: green; } }';
$subject = new CssDocument($css);
$subject = self::createDebugSubject($css);

$result = $subject->getStyleRulesData(['screen']);

Expand Down Expand Up @@ -218,7 +225,7 @@ public function parsesAtMediaRuleWithVariedWhitespace(
$atMediaAndQuery = '@media' . $whitespaceAfterAtMedia . 'screen';
$css = $atMediaAndQuery . $optionalWhitespaceWithinRule
. '{' . $optionalWhitespaceWithinRule . 'p { color: green; }' . $optionalWhitespaceWithinRule . '}';
$subject = new CssDocument($css);
$subject = self::createDebugSubject($css);

$result = $subject->getStyleRulesData(['screen']);

Expand Down Expand Up @@ -252,7 +259,7 @@ public function provideVariedWhitespaceForAtMediaRule(): array
*/
public function discardsMediaRuleWithTypeNotInAllowlist(string $mediaQuery): void
{
$subject = new CssDocument('@media ' . $mediaQuery . ' { p { color: red; } }');
$subject = self::createDebugSubject('@media ' . $mediaQuery . ' { p { color: red; } }');

$result = $subject->getStyleRulesData(['screen']);

Expand Down Expand Up @@ -296,7 +303,9 @@ public function provideMediaQueryWithTvTypeAndVariedWhitespace(): array
*/
public function parsesMultipleStyleRulesWithOtherCssBetween(string $cssBetween): void
{
$subject = new CssDocument('p { color: green; }' . $cssBetween . '@media screen { h1 { color: green; } }');
$subject = self::createDebugSubject(
'p { color: green; }' . $cssBetween . '@media screen { h1 { color: green; } }'
);

$result = $subject->getStyleRulesData(['screen']);

Expand All @@ -314,7 +323,7 @@ public function parsesMultipleStyleRulesWithOtherCssBetween(string $cssBetween):
*/
public function parsesMultipleStyleRulesWithOtherCssBefore(string $cssBefore): void
{
$subject = new CssDocument(
$subject = self::createDebugSubject(
$cssBefore . 'p { color: green; } @media screen { h1 { color: green; } }'
);

Expand Down Expand Up @@ -362,7 +371,7 @@ public function provideCssThatMustPrecedeStyleRules(): array
*/
public function rendersValidNonConditionalAtRule(string $atRuleCss, string $cssBefore = ''): void
{
$subject = new CssDocument($cssBefore . $atRuleCss);
$subject = self::createDebugSubject($cssBefore . $atRuleCss);

$result = $subject->renderNonConditionalAtRules();

Expand Down Expand Up @@ -403,7 +412,7 @@ public function provideValidNonConditionalAtRule(): array
*/
public function rendersMultipleNonConditionalAtRules(string $cssBetween): void
{
$subject = new CssDocument('@import "foo.css";' . $cssBetween . self::VALID_AT_FONT_FACE_RULE);
$subject = self::createDebugSubject('@import "foo.css";' . $cssBetween . self::VALID_AT_FONT_FACE_RULE);

$result = $subject->renderNonConditionalAtRules();

Expand Down Expand Up @@ -432,20 +441,51 @@ public function provideCssWithoutNonConditionalAtRules(): array
/**
* @test
*
* @param string $css
*
* @dataProvider provideValidAtCharsetRules
* @dataProvider provideInvalidAtCharsetRules
*/
public function discardsValidOrInvalidAtCharsetRule(string $css): void
public function discardsValidOrInvalidAtCharsetRuleNotInDebugMode(string $css): void
{
$subject = new CssDocument($css);
$subject = new CssDocument($css, false);

$result = $subject->renderNonConditionalAtRules();

self::assertSame('', $result);
}

/**
* @test
*
* @dataProvider provideValidAtCharsetRules
*/
public function discardsValidAtCharsetRuleInDebugMode(string $css): void
{
$subject = self::createDebugSubject($css);

$result = $subject->renderNonConditionalAtRules();

self::assertSame('', $result);
}

/**
* @test
*
* @dataProvider provideInvalidAtCharsetRules
*/
public function throwsExceptionForOrDiscardsInvalidAtCharsetRuleInDebugMode(string $css): void
{
try {
$subject = self::createDebugSubject($css);

$result = $subject->renderNonConditionalAtRules();

self::assertSame('', $result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this line ever be reached?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replied at length but it's been lost because GitHub's UI/UX is rubbish and I accidentally clicked a link.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@JakeQZ JakeQZ Dec 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the above-linked change it was possible to split test data up on logical grounds - one set of data syntactically invalid, the other merely with property names/values that don't conform to the spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case there doesn't appear to be any logical split between which test data causes an exception and for which the invalid rules are simply discarded.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually in PHPUnit we can use logicalOr to test for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if one of the conditions is an exception being thrown, AFAIAA, this is not possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hence this construct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We (as the Emogrifier maintainers) have the secret knowledge of what actually happens ;-) - and I think it's fair that we decide on one possible behavior to be the desired one and test for that. Otherwise, this would make the tests really confusing to read (because the desired behavior then is not clear to the reader).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What actually happens here is down to the behaviour of sabberworm/php-css-parser. We only know what happens because we have looked into it. We can influence it and I note you have had a number of PRs accepted. But at the end of the day it is a 3rd party library.

Ultimately we may be able to dispense with this intermediary class and use the Sabberworm classes directly, but we are a little way off that yet.

Some invalid CSS throws an excpetion (in debug mode). Some doesn't. If it throws an excpetion in debug mode, that's a bonus. If it doesn't, it's no big deal. But we don't control that. This relates back to #1135 - to what extent are we testing 3rd party libraries duplicitously?

} catch (UnexpectedEOFException $exception) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let's use $this->expectException, expectExceptionCode and expectExceptionMessage instead.

Ditto for other tests that check for exceptions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

Copy link
Contributor Author

@JakeQZ JakeQZ Dec 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean below. (Now it's above again. I mean see other comment on this.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let's use $this->expectException, expectExceptionCode and expectExceptionMessage instead.

Though if there is a doNotExpectException method the test could be written without the try/catch - i.e. we expect an exception but if we reach some later point we'll make an assertion instead, and cancel the expectation of the exception. Is that possible?

Copy link
Contributor Author

@JakeQZ JakeQZ Dec 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though if there is a doNotExpectException method the test could be written without the try/catch ... Is that possible?

expectException(null) would do the trick, except that the parameter is not documented as nullable and a Psalm error is generated. (Actually it's not nullable at all and a TypeError is generated if null is passed. There doesn't seem to be a way to 'undo' expectException.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To test that an exception is not thrown, we can do one of the following things:

a) state that not throwing the exception is the main statement of the test, and use a @doesNotPerformAssertions annotation
b) state that the return value (or something similar) is the main statement of the test, and assert for that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we can't, it seems, have a 'throws exception' OR 'satisfies assertion' test. Or, at least, not easily.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then please let's test for one of the two. (We can also add the other case as a test and mark it as self::markAsSkipped() (if I remember the method name correctly).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I've change this (and the other similar test) to expect an exception, but if one isn't thrown mark the test as skipped - since in strict (debug) mode the CSS parser only throws an exception with some of the datasets, and we are already testing that the invalid rules from these datasets are dropped in lenient (non-debug) mode.

// Passed test
self::expectNotToPerformAssertions();
}
}

/**
* @return array<string, array{0: string}>
*/
Expand All @@ -472,14 +512,11 @@ public function provideInvalidAtCharsetRules(): array
/**
* @test
*
* @param string $atRuleCss
* @param string $cssBefore
*
* @dataProvider provideInvalidNonConditionalAtRule
*/
public function notRendersInvalidNonConditionalAtRule(string $atRuleCss, string $cssBefore = ''): void
public function discardsInvalidNonConditionalAtRuleNotInDebugMode(string $atRuleCss, string $cssBefore = ''): void
{
$subject = new CssDocument($cssBefore . $atRuleCss);
$subject = new CssDocument($cssBefore . $atRuleCss, false);

$result = $subject->renderNonConditionalAtRules();

Expand All @@ -488,6 +525,32 @@ public function notRendersInvalidNonConditionalAtRule(string $atRuleCss, string
self::assertStringNotContainsString($atAndRuleName, $result);
}

/**
* @test
*
* @param string $atRuleCss
* @param string $cssBefore
oliverklee marked this conversation as resolved.
Show resolved Hide resolved
*
* @dataProvider provideInvalidNonConditionalAtRule
*/
public function throwsExceptionForOrDiscardsInvalidNonConditionalAtRuleInDebugMode(
string $atRuleCss,
string $cssBefore = ''
): void {
try {
$subject = self::createDebugSubject($cssBefore . $atRuleCss);

$result = $subject->renderNonConditionalAtRules();

\preg_match('/@[\\w\\-]++/', $atRuleCss, $atAndRuleNameMatches);
$atAndRuleName = $atAndRuleNameMatches[0];
self::assertStringNotContainsString($atAndRuleName, $result);
} catch (UnexpectedTokenException $exception) {
// Passed test
self::expectNotToPerformAssertions();
oliverklee marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* @return array<string, array<int, string>>
*/
Expand Down Expand Up @@ -516,7 +579,7 @@ public function provideInvalidNonConditionalAtRule(): array
*/
public function notRendersAtMediaRuleInNonConditionalAtRules(): void
{
$subject = new CssDocument('@media screen { p { color: red; } }');
$subject = self::createDebugSubject('@media screen { p { color: red; } }');

$result = $subject->renderNonConditionalAtRules();

Expand Down
46 changes: 35 additions & 11 deletions tests/Unit/CssInlinerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Pelago\Emogrifier\Tests\Support\Traits\AssertCss;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
use Symfony\Component\CssSelector\CssSelectorConverter;
use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
use TRegx\DataProvider\DataProviders;
Expand Down Expand Up @@ -1407,34 +1408,60 @@ static function (string $styleAttributeContent): Constraint {
}

/**
* @return string[][]
* @return array<string, array<int, string>>
*/
public function invalidDeclarationDataProvider(): array
public function provideInvalidDeclaration(): array
{
return [
'missing dash in property name' => ['font weight: bold;'],
'invalid character in property name' => ['-9webkit-text-size-adjust:none;'],
'missing :' => ['-webkit-text-size-adjust none'],
'missing value' => ['-webkit-text-size-adjust :'],
];
}

/**
* @return array<string, array<int, string>>
*/
public function provideDeclarationWithInvalidPropertyName(): array
{
return [
'invalid character in property name' => ['-9webkit-text-size-adjust:none;'],
];
}

/**
* @test
*
* @param string $cssDeclarationBlock the CSS declaration block (without the curly braces)
*
* @dataProvider invalidDeclarationDataProvider
* @dataProvider provideInvalidDeclaration
* @dataProvider provideDeclarationWithInvalidPropertyName
*/
public function inlineCssDropsInvalidCssDeclaration(string $cssDeclarationBlock): void
public function inlineCssNotInDebugModeDropsInvalidCssDeclaration(string $cssDeclarationBlock): void
{
$subject = $this->buildDebugSubject('<html></html>');
$subject = CssInliner::fromHtml('<html></html>');

$subject->inlineCss('html {' . $cssDeclarationBlock . '}');

self::assertStringContainsString('<html>', $subject->render());
}

/**
* @test
*
* @param string $cssDeclarationBlock the CSS declaration block (without the curly braces)
*
* @dataProvider provideInvalidDeclaration
*/
public function inlineCssInDebugModeForInvalidCssDeclarationThrowsException(string $cssDeclarationBlock): void
{
$this->expectException(UnexpectedTokenException::class);

$subject = $this->buildDebugSubject('<html></html>');

$subject->inlineCss('html {' . $cssDeclarationBlock . '}');
}

/**
* @test
*/
Expand Down Expand Up @@ -1557,10 +1584,7 @@ public function inlineCssRemovesStyleNodes(): void
*/
public function inlineCssInDebugModeForInvalidCssSelectorThrowsException(): void
{
// @see https://github.com/sabberworm/PHP-CSS-Parser/issues/347
self::markTestSkipped('This test is disabled as it currently is failing due to a bug in the CSS parser.');

$this->expectException(SyntaxErrorException::class);
$this->expectException(UnexpectedTokenException::class);

$subject = CssInliner::fromHtml(
'<html><style type="text/css">p{color:red;} <style data-x="1">html{cursor:text;}</style></html>'
Expand Down Expand Up @@ -3758,7 +3782,7 @@ public function copyUninlinableCssToStyleNodeHasNoSideEffects(): void
$copyUninlinableCssToStyleNode->setAccessible(true);

$domDocument = $subject->getDomDocument();
$cssDocument = new CssDocument('');
$cssDocument = new CssDocument('', true);

$copyUninlinableCssToStyleNode->invoke($subject, $cssDocument);
$expectedHtml = $subject->render();
Expand Down