From 057a00968bdf53775f4f4b3356e40f1c3efa9a5d Mon Sep 17 00:00:00 2001 From: Kieran Brahney Date: Sun, 16 May 2021 18:45:26 +0100 Subject: [PATCH] Remove from CSSList via key --- lib/Sabberworm/CSS/CSSList/CSSList.php | 9 ++++++++- tests/Sabberworm/CSS/ParserTest.php | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/Sabberworm/CSS/CSSList/CSSList.php b/lib/Sabberworm/CSS/CSSList/CSSList.php index 7a4ef22c..c32f8823 100644 --- a/lib/Sabberworm/CSS/CSSList/CSSList.php +++ b/lib/Sabberworm/CSS/CSSList/CSSList.php @@ -246,11 +246,18 @@ public function splice($iOffset, $iLength = null, $mReplacement = null) /** * 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) + * + * @param RuleSet|Import|Charset|CSSList|int $oItemToRemove May be a RuleSet (most likely a DeclarationBlock), a Import, a Charset or another CSSList (most likely a MediaQuery) * @return bool Whether the item was removed. */ public function remove($oItemToRemove) { + if (is_numeric($oItemToRemove) && array_key_exists($oItemToRemove, $this->aContents)) { + unset($this->aContents[$oItemToRemove]); + + return true; + } + $iKey = array_search($oItemToRemove, $this->aContents, true); if ($iKey !== false) { unset($this->aContents[$iKey]); diff --git a/tests/Sabberworm/CSS/ParserTest.php b/tests/Sabberworm/CSS/ParserTest.php index c96abb65..e3218654 100644 --- a/tests/Sabberworm/CSS/ParserTest.php +++ b/tests/Sabberworm/CSS/ParserTest.php @@ -389,6 +389,18 @@ public function testListValueRemoval() #unrelated {other: yes;}', $oDoc->render()); } + public function testListValueRemovalByKey() + { + $oDoc = $this->parsedStructureForFile('atrules'); + foreach ($oDoc->getContents() as $key => $oItem) { + if ($oItem instanceof AtRule) { + $oDoc->remove($key); + } + } + + $this->assertSame('html, body {font-size: -.6em;}', $oDoc->render()); + } + /** * @expectedException Sabberworm\CSS\Parsing\OutputException */