From 50929e2e9a4d9f58c1f0a9ca47121fb199b4478f Mon Sep 17 00:00:00 2001 From: raxbg Date: Mon, 27 Feb 2023 18:31:07 +0200 Subject: [PATCH 1/3] Pseudo classes cannot have a space after the : char --- src/Property/Selector.php | 4 ++-- tests/ParserTest.php | 5 +++++ tests/fixtures/invalid-selectors-3.css | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/invalid-selectors-3.css diff --git a/src/Property/Selector.php b/src/Property/Selector.php index cf07a8f5..be2814f9 100644 --- a/src/Property/Selector.php +++ b/src/Property/Selector.php @@ -46,7 +46,7 @@ class Selector /ix'; /** - * regexp for specificity calculations + * regexp for validity verifications * * @var string * @@ -79,7 +79,7 @@ class Selector */ public static function isValid($sSelector) { - return \preg_match(static::SELECTOR_VALIDATION_RX, $sSelector); + return \preg_match(static::SELECTOR_VALIDATION_RX, $sSelector) && \strpos($sSelector, ': ') === false; } /** diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 2c1c7287..91a613d3 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -787,6 +787,11 @@ public function invalidSelectorsInFile(): void html[dir="rtl"] .super-menu > li:last-of-type {border-left-width: 0;}} body {background-color: red;}'; self::assertSame($sExpected, $oDoc->render()); + + $oDoc = self::parsedStructureForFile('invalid-selectors-3', Settings::create()->withMultibyteSupport(true)); + $sExpected = 'body, div :hover {color: green;} +div {color: blue;}'; + self::assertSame($sExpected, $oDoc->render()); } /** diff --git a/tests/fixtures/invalid-selectors-3.css b/tests/fixtures/invalid-selectors-3.css new file mode 100644 index 00000000..1b47dce0 --- /dev/null +++ b/tests/fixtures/invalid-selectors-3.css @@ -0,0 +1,15 @@ +body, div: hover { + color: green; +} + +body, div : hover { + color: green; +} + +body, div :hover { + color: green; +} + +div { + color: blue; +} From d43033bd0936d19b0716bc3389a22f03d7f6fd00 Mon Sep 17 00:00:00 2001 From: raxbg Date: Mon, 27 Feb 2023 18:42:28 +0200 Subject: [PATCH 2/3] Rename a fixture file --- tests/ParserTest.php | 2 +- .../{invalid-selectors-3.css => invalid-selectors-4.css} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/fixtures/{invalid-selectors-3.css => invalid-selectors-4.css} (100%) diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 91a613d3..0072dac9 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -788,7 +788,7 @@ public function invalidSelectorsInFile(): void body {background-color: red;}'; self::assertSame($sExpected, $oDoc->render()); - $oDoc = self::parsedStructureForFile('invalid-selectors-3', Settings::create()->withMultibyteSupport(true)); + $oDoc = self::parsedStructureForFile('invalid-selectors-4', Settings::create()->withMultibyteSupport(true)); $sExpected = 'body, div :hover {color: green;} div {color: blue;}'; self::assertSame($sExpected, $oDoc->render()); diff --git a/tests/fixtures/invalid-selectors-3.css b/tests/fixtures/invalid-selectors-4.css similarity index 100% rename from tests/fixtures/invalid-selectors-3.css rename to tests/fixtures/invalid-selectors-4.css From 25c2c0386c6f169591f423be88a71ec528419740 Mon Sep 17 00:00:00 2001 From: raxbg Date: Wed, 10 Jul 2024 11:20:17 +0300 Subject: [PATCH 3/3] Improve to handle escaped colons and ones in quoted strings --- src/Property/Selector.php | 4 +++- tests/ParserTest.php | 5 ++++- tests/fixtures/invalid-selectors-4.css | 12 ++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Property/Selector.php b/src/Property/Selector.php index be2814f9..130a18ac 100644 --- a/src/Property/Selector.php +++ b/src/Property/Selector.php @@ -79,7 +79,9 @@ class Selector */ public static function isValid($sSelector) { - return \preg_match(static::SELECTOR_VALIDATION_RX, $sSelector) && \strpos($sSelector, ': ') === false; + return \preg_match(static::SELECTOR_VALIDATION_RX, $sSelector) + // trim quoted texts and validate colons on the remainder + && !\preg_match('/(?withMultibyteSupport(true)); $sExpected = 'body, div :hover {color: green;} -div {color: blue;}'; +div {color: blue;} +a[href*=": \" "] {color: blue;} +a[href*=\': \" \'] {color: blue;} +#some_id_that_ends_in_a\: > a {color: blue;}'; self::assertSame($sExpected, $oDoc->render()); } diff --git a/tests/fixtures/invalid-selectors-4.css b/tests/fixtures/invalid-selectors-4.css index 1b47dce0..787a301f 100644 --- a/tests/fixtures/invalid-selectors-4.css +++ b/tests/fixtures/invalid-selectors-4.css @@ -13,3 +13,15 @@ body, div :hover { div { color: blue; } + +a[href*=": \" "] { + color: blue; +} + +a[href*=': \" '] { + color: blue; +} + +#some_id_that_ends_in_a\: > a { + color: blue; +}