From 2ebf59e8bfbf6cfc1653a5f0ed743b95062c62a4 Mon Sep 17 00:00:00 2001 From: Raphael Schweikert Date: Mon, 1 Jun 2020 11:03:20 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20use=20eval?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Sabberworm/CSS/CSSList/CSSBlockList.php | 31 +++++++++++++++++++-- lib/Sabberworm/CSS/CSSList/Document.php | 3 -- tests/Sabberworm/CSS/ParserTest.php | 7 +++++ 3 files changed, 35 insertions(+), 6 deletions(-) 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 265c9ce1..fee661c4 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 9e8b9642..ad20b0f3 100644 --- a/tests/Sabberworm/CSS/ParserTest.php +++ b/tests/Sabberworm/CSS/ParserTest.php @@ -146,6 +146,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() {