diff --git a/lib/Sabberworm/CSS/CSSList/CSSBlockList.php b/lib/Sabberworm/CSS/CSSList/CSSBlockList.php index 55a594f5..233eb541 100644 --- a/lib/Sabberworm/CSS/CSSList/CSSBlockList.php +++ b/lib/Sabberworm/CSS/CSSList/CSSBlockList.php @@ -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; } } diff --git a/lib/Sabberworm/CSS/CSSList/Document.php b/lib/Sabberworm/CSS/CSSList/Document.php index 0349886f..869bab05 100644 --- a/lib/Sabberworm/CSS/CSSList/Document.php +++ b/lib/Sabberworm/CSS/CSSList/Document.php @@ -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; diff --git a/tests/Sabberworm/CSS/ParserTest.php b/tests/Sabberworm/CSS/ParserTest.php index e5db6b88..2e40a205 100644 --- a/tests/Sabberworm/CSS/ParserTest.php +++ b/tests/Sabberworm/CSS/ParserTest.php @@ -150,6 +150,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() {