Skip to content

Commit

Permalink
Merge tag '5.0.9' into drop-eval
Browse files Browse the repository at this point in the history
* tag '5.0.9':
  Don’t use eval
  • Loading branch information
sabberworm committed Jun 1, 2020
2 parents 4c4ab98 + 8950480 commit d8453c3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
31 changes: 28 additions & 3 deletions lib/Sabberworm/CSS/CSSList/CSSBlockList.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,34 @@ protected function allSelectors(&$aResult, $sSpecificitySearch = null) {
if ($sSpecificitySearch === null) {
$aResult[] = $oSelector;
} else {
$sComparison = "\$bRes = {$oSelector->getSpecificity()} $sSpecificitySearch;";
eval($sComparison);
if ($bRes) {
$sComparator = '===';
$aSpecificitySearch = explode(' ', $sSpecificitySearch);
$iTargetSpecificity = $aSpecificitySearch[0];
if(count($aSpecificitySearch) > 1) {
$sComparator = $aSpecificitySearch[0];
$iTargetSpecificity = $aSpecificitySearch[1];
}
$iTargetSpecificity = (int)$iTargetSpecificity;
$iSelectorSpecificity = $oSelector->getSpecificity();
$bMatches = false;
switch($sComparator) {
case '<=':
$bMatches = $iSelectorSpecificity <= $iTargetSpecificity;
break;
case '<':
$bMatches = $iSelectorSpecificity < $iTargetSpecificity;
break;
case '>=':
$bMatches = $iSelectorSpecificity >= $iTargetSpecificity;
break;
case '>':
$bMatches = $iSelectorSpecificity > $iTargetSpecificity;
break;
default:
$bMatches = $iSelectorSpecificity === $iTargetSpecificity;
break;
}
if ($bMatches) {
$aResult[] = $oSelector;
}
}
Expand Down
3 changes: 0 additions & 3 deletions lib/Sabberworm/CSS/CSSList/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ public function getAllValues($mElement = null, $bSearchInFunctionArguments = fal
* @example getSelectorsBySpecificity('>= 100')
*/
public function getSelectorsBySpecificity($sSpecificitySearch = null) {
if (is_numeric($sSpecificitySearch) || is_numeric($sSpecificitySearch[0])) {
$sSpecificitySearch = "== $sSpecificitySearch";
}
$aResult = array();
$this->allSelectors($aResult, $sSpecificitySearch);
return $aResult;
Expand Down
7 changes: 7 additions & 0 deletions tests/Sabberworm/CSS/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ function testSpecificity() {
}
}
$this->assertEquals(array(new Selector('#test .help', true)), $oDoc->getSelectorsBySpecificity('> 100'));
$this->assertEquals(array(new Selector('#test .help', true), new Selector('#file', true)), $oDoc->getSelectorsBySpecificity('>= 100'));
$this->assertEquals(array(new Selector('#file', true)), $oDoc->getSelectorsBySpecificity('=== 100'));
$this->assertEquals(array(new Selector('#file', true)), $oDoc->getSelectorsBySpecificity('== 100'));
$this->assertEquals(array(new Selector('#file', true), new Selector('.help:hover', true), new Selector('li.green', true), new Selector('ol li::before', true)), $oDoc->getSelectorsBySpecificity('<= 100'));
$this->assertEquals(array(new Selector('.help:hover', true), new Selector('li.green', true), new Selector('ol li::before', true)), $oDoc->getSelectorsBySpecificity('< 100'));
$this->assertEquals(array(new Selector('li.green', true)), $oDoc->getSelectorsBySpecificity('11'));
$this->assertEquals(array(new Selector('ol li::before', true)), $oDoc->getSelectorsBySpecificity(3));
}

function testManipulation() {
Expand Down

0 comments on commit d8453c3

Please sign in to comment.