From 47c9cdac155806c4d8a138886f532c5dbfc8af6d Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Thu, 20 Jun 2024 09:44:18 +0200 Subject: [PATCH 1/2] [FEATURE] Add support for inserting an item in a CSSList (#545) --- CHANGELOG.md | 1 + src/CSSList/CSSList.php | 16 ++++++++++ tests/CSSList/DocumentTest.php | 55 ++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86ceb2ca..a68b8526 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ## x.y.z ### Added +- Add support for inserting an item in a CSS list (#545) ### Changed diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index 603f662b..3780dc0b 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -296,6 +296,22 @@ public function splice($iOffset, $iLength = null, $mReplacement = null) array_splice($this->aContents, $iOffset, $iLength, $mReplacement); } + /** + * Inserts an item in the CSS list before its sibling. If the desired sibling cannot be found, + * the item is appended at the end. + * + * @param RuleSet|CSSList|Import|Charset $item + * @param RuleSet|CSSList|Import|Charset $sibling + */ + public function insertBefore($item, $sibling): void + { + if (in_array($sibling, $this->aContents, true)) { + $this->replace($sibling, [$item, $sibling]); + } else { + $this->append($item); + } + } + /** * Removes an item from the CSS list. * diff --git a/tests/CSSList/DocumentTest.php b/tests/CSSList/DocumentTest.php index 23f6ef85..d4903b7f 100644 --- a/tests/CSSList/DocumentTest.php +++ b/tests/CSSList/DocumentTest.php @@ -85,4 +85,59 @@ public function setContentsReplacesContentsSetInPreviousCall() self::assertSame($contents2, $this->subject->getContents()); } + + /** + * @test + */ + public function insertContentBeforeInsertsContentBeforeSibbling() + { + $bogusOne = new DeclarationBlock(); + $bogusOne->setSelectors('.bogus-one'); + $bogusTwo = new DeclarationBlock(); + $bogusTwo->setSelectors('.bogus-two'); + + $item = new DeclarationBlock(); + $item->setSelectors('.item'); + + $sibling = new DeclarationBlock(); + $sibling->setSelectors('.sibling'); + + $this->subject->setContents([$bogusOne, $sibling, $bogusTwo]); + + self::assertCount(3, $this->subject->getContents()); + + $this->subject->insertBefore($item, $sibling); + + self::assertCount(4, $this->subject->getContents()); + self::assertSame([$bogusOne, $item, $sibling, $bogusTwo], $this->subject->getContents()); + } + + /** + * @test + */ + public function insertContentBeforeAppendsIfSibblingNotFound() + { + $bogusOne = new DeclarationBlock(); + $bogusOne->setSelectors('.bogus-one'); + $bogusTwo = new DeclarationBlock(); + $bogusTwo->setSelectors('.bogus-two'); + + $item = new DeclarationBlock(); + $item->setSelectors('.item'); + + $sibling = new DeclarationBlock(); + $sibling->setSelectors('.sibling'); + + $orphan = new DeclarationBlock(); + $orphan->setSelectors('.forever-alone'); + + $this->subject->setContents([$bogusOne, $sibling, $bogusTwo]); + + self::assertCount(3, $this->subject->getContents()); + + $this->subject->insertBefore($item, $orphan); + + self::assertCount(4, $this->subject->getContents()); + self::assertSame([$bogusOne, $sibling, $bogusTwo, $item], $this->subject->getContents()); + } } From fb6de10997ff62fe9b296c03632704a9f5275277 Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Fri, 28 Jun 2024 01:14:37 +0100 Subject: [PATCH 2/2] Remove return value specifier for PHP5. --- src/CSSList/CSSList.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index 3780dc0b..4bb37589 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -303,7 +303,7 @@ public function splice($iOffset, $iLength = null, $mReplacement = null) * @param RuleSet|CSSList|Import|Charset $item * @param RuleSet|CSSList|Import|Charset $sibling */ - public function insertBefore($item, $sibling): void + public function insertBefore($item, $sibling) { if (in_array($sibling, $this->aContents, true)) { $this->replace($sibling, [$item, $sibling]);