From b920ac2202833117a37d0d262b24aab4b0bf21b7 Mon Sep 17 00:00:00 2001 From: raxbg Date: Thu, 5 Sep 2019 19:05:24 +0300 Subject: [PATCH 1/4] Add test with invalid rules --- tests/Sabberworm/CSS/ParserTest.php | 9 +++++++++ tests/files/invalid-rule.css | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/files/invalid-rule.css diff --git a/tests/Sabberworm/CSS/ParserTest.php b/tests/Sabberworm/CSS/ParserTest.php index a6d95359..73c79020 100644 --- a/tests/Sabberworm/CSS/ParserTest.php +++ b/tests/Sabberworm/CSS/ParserTest.php @@ -474,6 +474,15 @@ function testSelectorIgnoresInFile() { $this->assertSame($sExpected, $oDoc->render()); } + function testInvalidRulesInFile() { + $oDoc = $this->parsedStructureForFile('invalid-rule', Settings::create()->withMultibyteSupport(true)); + $sExpected = 'fusion-max-sh-shbp {} +@media only screen and (max-width: 800px) {.has-sidebar #content {order: 1;} + .has-sidebar #sidebar {order: 2;margin-top: 50px;} + .has-sidebar #sidebar-2 {order: 3;margin-top: 50px;}}'; + $this->assertSame($sExpected, $oDoc->render()); + } + /** * @expectedException Sabberworm\CSS\Parsing\UnexpectedTokenException */ diff --git a/tests/files/invalid-rule.css b/tests/files/invalid-rule.css new file mode 100644 index 00000000..bec2b65c --- /dev/null +++ b/tests/files/invalid-rule.css @@ -0,0 +1,21 @@ + fusion-max-sh-shbp { + .fusion-mobile-nav-holder .wpml-ls-item .menu-text,.fusion-mobile-nav-holder .wpml-ls-item > a,.wpml-ls-item .menu-text, .wpml-ls-item .sub-menu a > span { + justify-content: flex-start; + } + } + + @media only screen and (max-width: 800px) { + .has-sidebar #content { + order:1; + } + + .has-sidebar #sidebar { + order: 2; + margin-top: 50px; + } + + .has-sidebar #sidebar-2 { + order: 3; + margin-top: 50px; + } + } From de740d2d26cd4c35848b3c8c2b73e4fab9fd9fad Mon Sep 17 00:00:00 2001 From: raxbg Date: Thu, 5 Sep 2019 19:06:13 +0300 Subject: [PATCH 2/4] Improve handling of invalid rules --- lib/Sabberworm/CSS/RuleSet/RuleSet.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/Sabberworm/CSS/RuleSet/RuleSet.php b/lib/Sabberworm/CSS/RuleSet/RuleSet.php index e5d5e415..671728c3 100644 --- a/lib/Sabberworm/CSS/RuleSet/RuleSet.php +++ b/lib/Sabberworm/CSS/RuleSet/RuleSet.php @@ -35,9 +35,11 @@ public static function parseRuleSet(ParserState $oParserState, RuleSet $oRuleSet $oRule = Rule::parse($oParserState); } catch (UnexpectedTokenException $e) { try { - $sConsume = $oParserState->consumeUntil(array("\n", ";", '}'), true); + $sConsume = $oParserState->consumeUntil(array("\n", ";", '{', '}'), true); // We need to “unfind” the matches to the end of the ruleSet as this will be matched later - if($oParserState->streql(substr($sConsume, -1), '}')) { + if($oParserState->streql(substr($sConsume, -1), '{')) { // We need to skip the entire block + $oParserState->consumeUntil('}', true); + } else if($oParserState->streql(substr($sConsume, -1), '}')) { $oParserState->backtrack(1); } else { while ($oParserState->comes(';')) { From 7485b837ade29997673a48f759a1123968c202fe Mon Sep 17 00:00:00 2001 From: Ivailo Hristov Date: Sat, 17 Sep 2022 23:02:19 +0300 Subject: [PATCH 3/4] Fixes for CI --- src/RuleSet/RuleSet.php | 4 ++-- tests/ParserTest.php | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/RuleSet/RuleSet.php b/src/RuleSet/RuleSet.php index 46807b95..bd852eaf 100644 --- a/src/RuleSet/RuleSet.php +++ b/src/RuleSet/RuleSet.php @@ -67,7 +67,7 @@ public static function parseRuleSet(ParserState $oParserState, RuleSet $oRuleSet try { $sConsume = $oParserState->consumeUntil(["\n", ";", '{', '}'], true); // We need to “unfind” the matches to the end of the ruleSet as this will be matched later - if($oParserState->streql(substr($sConsume, -1), '{')) { // We need to skip the entire block + if ($oParserState->streql(substr($sConsume, -1), '{')) { // We need to skip the entire block $oParserState->consumeUntil('}', true); } elseif ($oParserState->streql(substr($sConsume, -1), '}')) { $oParserState->backtrack(1); @@ -254,7 +254,7 @@ public function removeRule($mRule) if ( !$mRule || $sName === $mRule || (strrpos($mRule, '-') === strlen($mRule) - strlen('-') - && (strpos($sName, $mRule) === 0 || $sName === substr($mRule, 0, -1))) + && (strpos($sName, $mRule) === 0 || $sName === substr($mRule, 0, -1))) ) { unset($this->aRules[$sName]); } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index db7c48b1..30b57755 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -764,14 +764,17 @@ public function selectorEscapesInFile() self::assertSame($sExpected, $oDoc->render()); } + /** + * @test + */ public function invalidRulesInFile() { $oDoc = $this->parsedStructureForFile('invalid-rule', Settings::create()->withMultibyteSupport(true)); $sExpected = 'fusion-max-sh-shbp {} @media only screen and (max-width: 800px) {.has-sidebar #content {order: 1;} - .has-sidebar #sidebar {order: 2;margin-top: 50px;} - .has-sidebar #sidebar-2 {order: 3;margin-top: 50px;}}'; - $this->assertSame($sExpected, $oDoc->render()); + .has-sidebar #sidebar {order: 2;margin-top: 50px;} + .has-sidebar #sidebar-2 {order: 3;margin-top: 50px;}}'; + self::assertSame($sExpected, $oDoc->render()); } /** From 5751c62110791d985003d7dd4a5cad539e3839bd Mon Sep 17 00:00:00 2001 From: raxbg Date: Wed, 10 Jul 2024 15:42:41 +0300 Subject: [PATCH 4/4] Fix quotes --- src/RuleSet/RuleSet.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RuleSet/RuleSet.php b/src/RuleSet/RuleSet.php index 44cd1920..39a889c9 100644 --- a/src/RuleSet/RuleSet.php +++ b/src/RuleSet/RuleSet.php @@ -63,7 +63,7 @@ public static function parseRuleSet(ParserState $oParserState, RuleSet $oRuleSet $oRule = Rule::parse($oParserState); } catch (UnexpectedTokenException $e) { try { - $sConsume = $oParserState->consumeUntil(["\n", ";", '{', '}'], true); + $sConsume = $oParserState->consumeUntil(["\n", ';', '{', '}'], true); // We need to “unfind” the matches to the end of the ruleSet as this will be matched later if ($oParserState->streql(\substr($sConsume, -1), '{')) { // We need to skip the entire block $oParserState->consumeUntil('}', true);