-
Notifications
You must be signed in to change notification settings - Fork 145
Selector validation #162
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
Merged
sabberworm
merged 15 commits into
MyIntervals:master
from
raxbg:improvement/selector_validation
Jul 13, 2019
Merged
Selector validation #162
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2c5cd6f
Add basic selector validation
raxbg 218a207
Add | and * to the list of valid selector characters
raxbg 8792d00
Add ^ and $ to the list of valid selector characters (all special cha…
raxbg 15f1fdb
Skip parsing of the next ruleset upon finding an invalid selector ins…
raxbg 4f8da97
Update closing bracket parsing in a way which allows us to better mat…
raxbg a06e5bb
Add unit test for invalid selectors
raxbg 4ea4fd6
Add percentage matching since keyframe steps are treated as regular s…
raxbg 134f4e6
Improved selector parsing + better handling for '}'
raxbg 5b3f780
More tests for invalid selectors
raxbg 425c78e
Improvement: Handle escaped characters when validating selectors
raxbg 75f7b13
Match selectors with comments or quoted strings
raxbg 385d559
Merge branch 'master' of github.com:sabberworm/PHP-CSS-Parser into im…
raxbg 397ca39
Add forgotten test file
raxbg 2943d9f
Move selector validation outside of the Selector's constructor
raxbg a27e301
Simplify the CSS validating regex
raxbg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
namespace Sabberworm\CSS\Property; | ||
|
||
use Sabberworm\CSS\Parsing\UnexpectedTokenException; | ||
|
||
/** | ||
* Class representing a single CSS selector. Selectors have to be split by the comma prior to being passed into this class. | ||
*/ | ||
|
@@ -35,10 +37,21 @@ class Selector { | |
)) | ||
/ix'; | ||
|
||
const SELECTOR_VALIDATION_RX = '/ | ||
^((?:[a-zA-Z0-9\x{00A0}-\x{FFFF}_\^\$\|\*\=\"\'\~\[\]\(\)\-\s\.:#\+\>]*(?:\\\\.)?(?:\'.*?\')?(?:\".*?\")?)*|\s*?[\+-]?\d+\%\s*)$ | ||
/ux'; | ||
|
||
private $sSelector; | ||
private $iSpecificity; | ||
|
||
public static function isValid($sSelector) { | ||
return preg_match(self::SELECTOR_VALIDATION_RX, $sSelector); | ||
} | ||
|
||
public function __construct($sSelector, $bCalculateSpecificity = false) { | ||
if (!Selector::isValid($sSelector)) { | ||
throw new UnexpectedTokenException("Selector did not match '" . self::SELECTOR_VALIDATION_RX . "'.", $sSelector, "custom"); | ||
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’m not sure it is the job of the |
||
} | ||
$this->setSelector($sSelector); | ||
if ($bCalculateSpecificity) { | ||
$this->getSpecificity(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
@media only screen and (max-width: 1215px) { | ||
.breadcrumb{ | ||
padding-left:10px; | ||
} | ||
.super-menu > li:first-of-type{ | ||
border-left-width:0; | ||
} | ||
.super-menu > li:last-of-type{ | ||
border-right-width:0; | ||
} | ||
html[dir="rtl"] .super-menu > li:first-of-type{ | ||
border-left-width:1px; | ||
border-right-width:0; | ||
} | ||
html[dir="rtl"] .super-menu > li:last-of-type{ | ||
border-left-width:0; | ||
} | ||
html[dir="rtl"] .super-menu.menu-floated > li:first-of-type | ||
border-right-width:0; | ||
} | ||
} | ||
|
||
|
||
.super-menu.menu-floated{ | ||
border-right-width:1px; | ||
border-left-width:1px; | ||
border-color:rgb(90, 66, 66); | ||
border-style:dotted; | ||
} | ||
|
||
body { | ||
background-color: red; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
@keyframes mymove { | ||
from { top: 0px; } | ||
} | ||
|
||
#test { | ||
color: white; | ||
background: green; | ||
} | ||
|
||
body | ||
background: black; | ||
} | ||
|
||
#test { | ||
display: block; | ||
background: red; | ||
color: white; | ||
} | ||
#test { | ||
display: block; | ||
background: white; | ||
color: black; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#\# { | ||
color: red; | ||
} | ||
|
||
.col-sm-1\/5 { | ||
width: 20%; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
.some[selectors-may='contain-a-{'] { | ||
|
||
|
||
} | ||
|
||
.this-selector /* should remain-} */ .valid { | ||
width:100px; | ||
} | ||
|
||
@media only screen and (min-width: 200px) { | ||
.test { | ||
prop: val; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This regex is too complicated to understand. If it can’t be simplified, maybe a regex isn’t the right tool for the job. But since the end goal for selectors still is a complete parser (which will make the regex obsolete), I’ll allow it for now. But maybe you could split the regex to multiple lines using concatenation and comment each line so we better understand what it does.
Also, some of the escapes are unnecessary. Inside character classes
[]
, you only need to escape]
and-
(in some cases) (and maybe[
to be symmetrical), but^
,$
,|
,*
,=
,"
,~
,+
,(
,)
can be left literal ('
was already literal since\'
is a string escape, not a regex escape).