Skip to content

Keyframe selector validation #168

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
merged 3 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion lib/Sabberworm/CSS/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static function parseListItem(ParserState $oParserState, CSSList $oList)
}
}
} else {
return DeclarationBlock::parse($oParserState);
return DeclarationBlock::parse($oParserState, $oList);
Copy link
Collaborator

Choose a reason for hiding this comment

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

}
}

Expand Down
23 changes: 23 additions & 0 deletions lib/Sabberworm/CSS/Property/KeyframeSelector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Sabberworm\CSS\Property;

use Sabberworm\CSS\Parsing\UnexpectedTokenException;

class KeyframeSelector extends Selector {

//Regexes for specificity calculations

const SELECTOR_VALIDATION_RX = '/
^(
(?:
[a-zA-Z0-9\x{00A0}-\x{FFFF}_^$|*="\'~\[\]()\-\s\.:#+>]* # any sequence of valid unescaped characters
(?:\\\\.)? # a single escaped character
(?:([\'"]).*?(?<!\\\\)\2)? # a quoted text like [id="example"]
)*
)|
(\d+%) # keyframe animation progress percentage (e.g. 50%)
$
/ux';

}
2 changes: 1 addition & 1 deletion lib/Sabberworm/CSS/Property/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Selector {
private $iSpecificity;

public static function isValid($sSelector) {
return preg_match(self::SELECTOR_VALIDATION_RX, $sSelector);
return preg_match(static::SELECTOR_VALIDATION_RX, $sSelector);
}

public function __construct($sSelector, $bCalculateSpecificity = false) {
Expand Down
25 changes: 17 additions & 8 deletions lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
use Sabberworm\CSS\Parsing\OutputException;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
use Sabberworm\CSS\Property\Selector;
use Sabberworm\CSS\Property\KeyframeSelector;
use Sabberworm\CSS\Rule\Rule;
use Sabberworm\CSS\Value\RuleValueList;
use Sabberworm\CSS\Value\Value;
use Sabberworm\CSS\Value\Size;
use Sabberworm\CSS\Value\Color;
use Sabberworm\CSS\Value\URL;
use Sabberworm\CSS\CSSList\KeyFrame;

/**
* Declaration blocks are the parts of a css file which denote the rules belonging to a selector.
Expand All @@ -26,7 +28,7 @@ public function __construct($iLineNo = 0) {
$this->aSelectors = array();
}

public static function parse(ParserState $oParserState) {
public static function parse(ParserState $oParserState, $oList = NULL) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

$aComments = array();
$oResult = new DeclarationBlock($oParserState->currentLine());
try {
Expand All @@ -42,7 +44,7 @@ public static function parse(ParserState $oParserState) {
}
}
} while (!in_array($oParserState->peek(), array('{', '}')) || $sStringWrapperChar !== false);
$oResult->setSelector(implode('', $aSelectorParts));
$oResult->setSelector(implode('', $aSelectorParts), $oList);
if ($oParserState->comes('{')) {
$oParserState->consume(1);
}
Expand All @@ -62,18 +64,25 @@ public static function parse(ParserState $oParserState) {
}


public function setSelectors($mSelector) {
public function setSelectors($mSelector, $oList = NULL) {
if (is_array($mSelector)) {
$this->aSelectors = $mSelector;
} else {
$this->aSelectors = explode(',', $mSelector);
}
foreach ($this->aSelectors as $iKey => $mSelector) {
if (!($mSelector instanceof Selector)) {
if (!Selector::isValid($mSelector)) {
throw new UnexpectedTokenException("Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.", $mSelector, "custom");
if ($oList === NULL || !($oList instanceof KeyFrame)) {
if (!Selector::isValid($mSelector)) {
throw new UnexpectedTokenException("Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.", $mSelector, "custom");
}
$this->aSelectors[$iKey] = new Selector($mSelector);
} else {
if (!KeyframeSelector::isValid($mSelector)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

throw new UnexpectedTokenException("Selector did not match '" . KeyframeSelector::SELECTOR_VALIDATION_RX . "'.", $mSelector, "custom");
}
$this->aSelectors[$iKey] = new KeyframeSelector($mSelector);
}
$this->aSelectors[$iKey] = new Selector($mSelector);
}
}
}
Expand Down Expand Up @@ -102,8 +111,8 @@ public function getSelector() {
/**
* @deprecated use setSelectors()
*/
public function setSelector($mSelector) {
$this->setSelectors($mSelector);
public function setSelector($mSelector, $oList = NULL) {
$this->setSelectors($mSelector, $oList);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Sabberworm/CSS/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,14 @@ function testSelectorIgnoresInFile() {
$this->assertSame($sExpected, $oDoc->render());
}

function testKeyframeSelectors() {
$oDoc = $this->parsedStructureForFile('keyframe-selector-validation', Settings::create()->withMultibyteSupport(true));
$sExpected = '@-webkit-keyframes zoom {0% {-webkit-transform: scale(1,1);}
50% {-webkit-transform: scale(1.2,1.2);}
100% {-webkit-transform: scale(1,1);}}';
$this->assertSame($sExpected, $oDoc->render());
}

/**
* @expectedException Sabberworm\CSS\Parsing\UnexpectedTokenException
*/
Expand Down
11 changes: 11 additions & 0 deletions tests/files/keyframe-selector-validation.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@-webkit-keyframes zoom {
0% {
-webkit-transform: scale(1,1);
}
50% {
-webkit-transform: scale(1.2,1.2);
}
100% {
-webkit-transform: scale(1,1);
}
}