Skip to content

Commit 2c5cd6f

Browse files
committed
Add basic selector validation
1 parent 4956048 commit 2c5cd6f

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/Sabberworm/CSS/Property/Selector.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sabberworm\CSS\Property;
44

5+
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
6+
57
/**
68
* Class representing a single CSS selector. Selectors have to be split by the comma prior to being passed into this class.
79
*/
@@ -38,7 +40,15 @@ class Selector {
3840
private $sSelector;
3941
private $iSpecificity;
4042

43+
public static function isValid($sSelector) {
44+
return preg_match("/^[a-zA-Z0-9\x{00A0}-\x{FFFF}_\=\"\'\~\[\]\(\)\-\s\.:#\+\>]*$/u", $sSelector);
45+
}
46+
4147
public function __construct($sSelector, $bCalculateSpecificity = false) {
48+
if (!Selector::isValid($sSelector)) {
49+
preg_match("/[^a-zA-Z0-9\x{00A0}-\x{FFFF}_\=\"\'\~\[\]\(\)\-\s\.:#\+\>]/u", $sSelector, $matches);
50+
throw new UnexpectedTokenException("Selector did not match '/[^a-zA-Z0-9\x{00A0}-\x{FFFF}_\=\"\'\~\[\]\(\)\-\s\.:#\+\>]/u'. ({$matches[0]} found).", $sSelector, "custom");
51+
}
4252
$this->setSelector($sSelector);
4353
if ($bCalculateSpecificity) {
4454
$this->getSpecificity();

lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Sabberworm\CSS\Parsing\ParserState;
66
use Sabberworm\CSS\Parsing\OutputException;
7+
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
78
use Sabberworm\CSS\Property\Selector;
89
use Sabberworm\CSS\Rule\Rule;
910
use Sabberworm\CSS\Value\RuleValueList;
@@ -28,7 +29,15 @@ public function __construct($iLineNo = 0) {
2829
public static function parse(ParserState $oParserState) {
2930
$aComments = array();
3031
$oResult = new DeclarationBlock($oParserState->currentLine());
31-
$oResult->setSelector($oParserState->consumeUntil('{', false, true, $aComments));
32+
try {
33+
$oResult->setSelector($oParserState->consumeUntil('{', false, true, $aComments));
34+
} catch (UnexpectedTokenException $e) {
35+
if($oParserState->getSettings()->bLenientParsing) {
36+
return NULL;
37+
} else {
38+
throw $e;
39+
}
40+
}
3241
$oResult->setComments($aComments);
3342
RuleSet::parseRuleSet($oParserState, $oResult);
3443
return $oResult;

0 commit comments

Comments
 (0)