From 3b244c0e16cb8482a0f359c9ef46c74878c41722 Mon Sep 17 00:00:00 2001 From: Mathew May Date: Wed, 9 Dec 2020 13:52:59 +0800 Subject: [PATCH 1/3] MDL-70300 lib: Upgrade php css parser to 8.3.1 --- lib/php-css-parser/CSSList/CSSBlockList.php | 31 +++++++++++++++++++-- lib/php-css-parser/CSSList/CSSList.php | 17 ++--------- lib/php-css-parser/CSSList/Document.php | 3 -- lib/php-css-parser/Rule/Rule.php | 1 + 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/lib/php-css-parser/CSSList/CSSBlockList.php b/lib/php-css-parser/CSSList/CSSBlockList.php index 17c68142eb7..15742423deb 100644 --- a/lib/php-css-parser/CSSList/CSSBlockList.php +++ b/lib/php-css-parser/CSSList/CSSBlockList.php @@ -69,9 +69,34 @@ abstract class CSSBlockList extends CSSList { 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/php-css-parser/CSSList/CSSList.php b/lib/php-css-parser/CSSList/CSSList.php index 11e1dcc8259..d883df82460 100644 --- a/lib/php-css-parser/CSSList/CSSList.php +++ b/lib/php-css-parser/CSSList/CSSList.php @@ -61,6 +61,7 @@ abstract class CSSList implements Renderable, Commentable { $oListItem->setComments($comments); $oList->append($oListItem); } + $oParserState->consumeWhiteSpace(); } if(!$bIsRoot && !$bLenientParsing) { throw new SourceException("Unexpected end of document", $oParserState->currentLine()); @@ -211,20 +212,6 @@ abstract class CSSList implements Renderable, Commentable { array_splice($this->aContents, $iOffset, $iLength, $mReplacement); } - /** - * Insert an item before its sibling. - * - * @param mixed $oItem The item. - * @param mixed $oSibling The sibling. - */ - public function insert($oItem, $oSibling) { - $iIndex = array_search($oSibling, $this->aContents); - if ($iIndex === false) { - return $this->append($oItem); - } - array_splice($this->aContents, $iIndex, 0, array($oItem)); - } - /** * Removes an item from the CSS list. * @param RuleSet|Import|Charset|CSSList $oItemToRemove May be a RuleSet (most likely a DeclarationBlock), a Import, a Charset or another CSSList (most likely a MediaQuery) @@ -327,7 +314,7 @@ abstract class CSSList implements Renderable, Commentable { return $sResult; } - + /** * Return true if the list can not be further outdented. Only important when rendering. */ diff --git a/lib/php-css-parser/CSSList/Document.php b/lib/php-css-parser/CSSList/Document.php index 873df755f7f..1658aee8dbb 100644 --- a/lib/php-css-parser/CSSList/Document.php +++ b/lib/php-css-parser/CSSList/Document.php @@ -72,9 +72,6 @@ class Document extends CSSBlockList { * @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/lib/php-css-parser/Rule/Rule.php b/lib/php-css-parser/Rule/Rule.php index 4480948f488..3fa031bd639 100644 --- a/lib/php-css-parser/Rule/Rule.php +++ b/lib/php-css-parser/Rule/Rule.php @@ -56,6 +56,7 @@ class Rule implements Renderable, Commentable { while ($oParserState->comes(';')) { $oParserState->consume(';'); } + $oParserState->consumeWhiteSpace(); return $oRule; } From f2cc1a8a348b59cd5ac3cebc9c1895d23f59d61b Mon Sep 17 00:00:00 2001 From: Mathew May Date: Wed, 9 Dec 2020 13:59:50 +0800 Subject: [PATCH 2/3] MDL-70300 lib: Apply php css parser patches --- lib/php-css-parser/CSSList/CSSList.php | 17 +++++++++++++++-- lib/php-css-parser/Rule/Rule.php | 1 - 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/php-css-parser/CSSList/CSSList.php b/lib/php-css-parser/CSSList/CSSList.php index d883df82460..11e1dcc8259 100644 --- a/lib/php-css-parser/CSSList/CSSList.php +++ b/lib/php-css-parser/CSSList/CSSList.php @@ -61,7 +61,6 @@ abstract class CSSList implements Renderable, Commentable { $oListItem->setComments($comments); $oList->append($oListItem); } - $oParserState->consumeWhiteSpace(); } if(!$bIsRoot && !$bLenientParsing) { throw new SourceException("Unexpected end of document", $oParserState->currentLine()); @@ -212,6 +211,20 @@ abstract class CSSList implements Renderable, Commentable { array_splice($this->aContents, $iOffset, $iLength, $mReplacement); } + /** + * Insert an item before its sibling. + * + * @param mixed $oItem The item. + * @param mixed $oSibling The sibling. + */ + public function insert($oItem, $oSibling) { + $iIndex = array_search($oSibling, $this->aContents); + if ($iIndex === false) { + return $this->append($oItem); + } + array_splice($this->aContents, $iIndex, 0, array($oItem)); + } + /** * Removes an item from the CSS list. * @param RuleSet|Import|Charset|CSSList $oItemToRemove May be a RuleSet (most likely a DeclarationBlock), a Import, a Charset or another CSSList (most likely a MediaQuery) @@ -314,7 +327,7 @@ abstract class CSSList implements Renderable, Commentable { return $sResult; } - + /** * Return true if the list can not be further outdented. Only important when rendering. */ diff --git a/lib/php-css-parser/Rule/Rule.php b/lib/php-css-parser/Rule/Rule.php index 3fa031bd639..4480948f488 100644 --- a/lib/php-css-parser/Rule/Rule.php +++ b/lib/php-css-parser/Rule/Rule.php @@ -56,7 +56,6 @@ class Rule implements Renderable, Commentable { while ($oParserState->comes(';')) { $oParserState->consume(';'); } - $oParserState->consumeWhiteSpace(); return $oRule; } From ca64b0af460813079e361b72a1b1219c5dcd1824 Mon Sep 17 00:00:00 2001 From: Mathew May Date: Fri, 8 Jan 2021 14:33:49 +0800 Subject: [PATCH 3/3] MDL-70300 core_lib: Update thirdpartylibs php-css-parser --- lib/php-css-parser/moodle_readme.txt | 13 +++++-------- lib/thirdpartylibs.xml | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/php-css-parser/moodle_readme.txt b/lib/php-css-parser/moodle_readme.txt index 07f3b4d33ed..4040005a941 100644 --- a/lib/php-css-parser/moodle_readme.txt +++ b/lib/php-css-parser/moodle_readme.txt @@ -1,12 +1,9 @@ PHP CSS Parser -------------- -Downloaded from: https://github.com/sabberworm/PHP-CSS-Parser/releases/tag/8.3.0 - Import procedure: - -- Copy all the files from the folder 'lib/Sabberworm/CSS/' in this directory. - -- Apply the patch in Sabberworm/PHP-CSS-Parser#115 - -- Apply the patch in sabberworm/PHP-CSS-Parser/issues/173 (if this has not already been resolved upstream). +1. Download the latest release from https://github.com/sabberworm/PHP-CSS-Parser/releases +2. Copy all the files from the folder 'lib/Sabberworm/CSS/' in this directory. +3. Apply the following patches from the following pull requests if they have not yet been merged upstream: + a. https://github.com/sabberworm/PHP-CSS-Parser/pull/115 + b. https://github.com/sabberworm/PHP-CSS-Parser/pull/173 diff --git a/lib/thirdpartylibs.xml b/lib/thirdpartylibs.xml index 32ca880ea1d..2e8a7fe6ea3 100644 --- a/lib/thirdpartylibs.xml +++ b/lib/thirdpartylibs.xml @@ -217,7 +217,7 @@ php-css-parser PHP-CSS-Parser MIT - 8.3.0 + 8.3.1 rtlcss