From d4744a6ed9ca70db291f97804734f10b201ab103 Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Mon, 17 Jun 2024 11:15:23 +0200 Subject: [PATCH 01/11] [FEATURE] Support for inserting an item in a CSSList Signed-off-by: Daniel Ziegenberg Co-authored-by: Frederic Massart --- src/CSSList/CSSList.php | 21 +++++++++++ tests/CSSList/DocumentTest.php | 69 ++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index 9ce207cf..0648378a 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -294,6 +294,27 @@ public function splice($iOffset, $iLength = null, $mReplacement = null) array_splice($this->aContents, $iOffset, $iLength, $mReplacement); } + /** + * Insert an item before or after its sibling. + * + * @param RuleSet|CSSList|Import|Charset $oItem The item. + * @param RuleSet|CSSList|Import|Charset $oSibling The sibling. + * @param string $sPosition The position. + * + * @return void + */ + public function insert($oItem, $oSibling, string $sPosition = 'before'): void + { + $iIndex = in_array($oSibling, $this->aContents); + if ($iIndex === false) { + $this->append($oItem); + } elseif ($sPosition === 'before') { + $this->replace($oSibling, array($oItem, $oSibling)); + } else { + $this->replace($oSibling, array($oSibling, $oItem)); + } + } + /** * Removes an item from the CSS list. * diff --git a/tests/CSSList/DocumentTest.php b/tests/CSSList/DocumentTest.php index 10f72e3e..1adad9d1 100644 --- a/tests/CSSList/DocumentTest.php +++ b/tests/CSSList/DocumentTest.php @@ -2,6 +2,7 @@ namespace Sabberworm\CSS\Tests\CSSList; +use Generator; use PHPUnit\Framework\TestCase; use Sabberworm\CSS\Comment\Commentable; use Sabberworm\CSS\CSSList\Document; @@ -85,4 +86,72 @@ public function setContentsReplacesContentsSetInPreviousCall(): void self::assertSame($contents2, $this->subject->getContents()); } + + /** + * @return Generator + */ + public static function insertDataProvider(): Generator + { + + $bogusOne = new DeclarationBlock(); + $bogusOne->setSelectors('.bogus-one'); + $bogusTwo = new DeclarationBlock(); + $bogusTwo->setSelectors('.bogus-two'); + + $oItem = new DeclarationBlock(); + $oItem->setSelectors('.item'); + + $oSibling = new DeclarationBlock(); + $oSibling->setSelectors('.sibling'); + + $oOrphan = new DeclarationBlock(); + $oOrphan->setSelectors('.forever-alone'); + + yield 'insert before' => [ + 'initialContent' => [$bogusOne, $oSibling, $bogusTwo], + 'oItem' => $oItem, + 'oSibling' => $oSibling, + 'position' => 'before', + 'expectedContent' => [$bogusOne, $oItem, $oSibling, $bogusTwo], + ]; + yield 'insert after' => [ + 'initialContent' => [$bogusOne, $oSibling, $bogusTwo], + 'oItem' => $oItem, + 'oSibling' => $oSibling, + 'position' => 'after', + 'expectedContent' => [$bogusOne, $oSibling, $oItem, $bogusTwo], + ]; + yield 'append if not found' => [ + 'initialContent' => [$bogusOne, $oSibling, $bogusTwo], + 'oItem' => $oItem, + 'oSibling' => $oOrphan, + 'position' => 'before', + 'expectedContent' => [$bogusOne, $oSibling, $bogusTwo, $oItem], + ]; + } + + /** + * @test + * + * @param array $contents + * + * @dataProvider insertDataProvider + */ + public function insertContent( + array $initialContent, + DeclarationBlock $oItem, + DeclarationBlock $oSibling, + string $sPosition, + array $expectedContent + ) { + + $this->subject->setContents($initialContent); + + self::assertCount(3, $this->subject->getContents()); + + $this->subject->insert($oItem, $oSibling, $sPosition); + + self::assertCount(4, $this->subject->getContents()); + self::assertSame($expectedContent, $this->subject->getContents()); + } } From 1a16a4914b3bbd19be01d813127ce12016b845c4 Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Mon, 17 Jun 2024 17:09:12 +0200 Subject: [PATCH 02/11] fixup! [FEATURE] Support for inserting an item in a CSSList --- src/CSSList/CSSList.php | 4 +--- tests/CSSList/DocumentTest.php | 9 ++++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index 0648378a..ac2f90d5 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -299,9 +299,7 @@ public function splice($iOffset, $iLength = null, $mReplacement = null) * * @param RuleSet|CSSList|Import|Charset $oItem The item. * @param RuleSet|CSSList|Import|Charset $oSibling The sibling. - * @param string $sPosition The position. - * - * @return void + * @param $sPosition The position. */ public function insert($oItem, $oSibling, string $sPosition = 'before'): void { diff --git a/tests/CSSList/DocumentTest.php b/tests/CSSList/DocumentTest.php index 1adad9d1..9dfc0b79 100644 --- a/tests/CSSList/DocumentTest.php +++ b/tests/CSSList/DocumentTest.php @@ -2,7 +2,6 @@ namespace Sabberworm\CSS\Tests\CSSList; -use Generator; use PHPUnit\Framework\TestCase; use Sabberworm\CSS\Comment\Commentable; use Sabberworm\CSS\CSSList\Document; @@ -90,7 +89,7 @@ public function setContentsReplacesContentsSetInPreviousCall(): void /** * @return Generator */ - public static function insertDataProvider(): Generator + public static function insertDataProvider(): \Generator { $bogusOne = new DeclarationBlock(); @@ -133,7 +132,11 @@ public static function insertDataProvider(): Generator /** * @test * - * @param array $contents + * @param $initialContent + * @param $oItem + * @param $oSibling + * @param $sPosition + * @param $expectedContent * * @dataProvider insertDataProvider */ From d82adae379c52cfa01fa9efed1f9a656570c04a1 Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Mon, 17 Jun 2024 17:14:23 +0200 Subject: [PATCH 03/11] fixup! [FEATURE] Support for inserting an item in a CSSList --- src/CSSList/CSSList.php | 9 +++------ tests/CSSList/DocumentTest.php | 20 +++++--------------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index ac2f90d5..036420ff 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -295,21 +295,18 @@ public function splice($iOffset, $iLength = null, $mReplacement = null) } /** - * Insert an item before or after its sibling. + * Insert an item before its sibling. * * @param RuleSet|CSSList|Import|Charset $oItem The item. * @param RuleSet|CSSList|Import|Charset $oSibling The sibling. - * @param $sPosition The position. */ - public function insert($oItem, $oSibling, string $sPosition = 'before'): void + public function insertBefore($oItem, $oSibling): void { $iIndex = in_array($oSibling, $this->aContents); if ($iIndex === false) { $this->append($oItem); - } elseif ($sPosition === 'before') { - $this->replace($oSibling, array($oItem, $oSibling)); } else { - $this->replace($oSibling, array($oSibling, $oItem)); + $this->replace($oSibling, array($oItem, $oSibling)); } } diff --git a/tests/CSSList/DocumentTest.php b/tests/CSSList/DocumentTest.php index 9dfc0b79..208cc8ce 100644 --- a/tests/CSSList/DocumentTest.php +++ b/tests/CSSList/DocumentTest.php @@ -87,9 +87,9 @@ public function setContentsReplacesContentsSetInPreviousCall(): void } /** - * @return Generator + * @return \Generator, DeclarationBlock, DeclarationBlock, array>> */ - public static function insertDataProvider(): \Generator + public static function insertBeforeDataProvider(): \Generator { $bogusOne = new DeclarationBlock(); @@ -110,21 +110,12 @@ public static function insertDataProvider(): \Generator 'initialContent' => [$bogusOne, $oSibling, $bogusTwo], 'oItem' => $oItem, 'oSibling' => $oSibling, - 'position' => 'before', 'expectedContent' => [$bogusOne, $oItem, $oSibling, $bogusTwo], ]; - yield 'insert after' => [ - 'initialContent' => [$bogusOne, $oSibling, $bogusTwo], - 'oItem' => $oItem, - 'oSibling' => $oSibling, - 'position' => 'after', - 'expectedContent' => [$bogusOne, $oSibling, $oItem, $bogusTwo], - ]; yield 'append if not found' => [ 'initialContent' => [$bogusOne, $oSibling, $bogusTwo], 'oItem' => $oItem, 'oSibling' => $oOrphan, - 'position' => 'before', 'expectedContent' => [$bogusOne, $oSibling, $bogusTwo, $oItem], ]; } @@ -138,13 +129,12 @@ public static function insertDataProvider(): \Generator * @param $sPosition * @param $expectedContent * - * @dataProvider insertDataProvider + * @dataProvider insertBeforeDataProvider */ - public function insertContent( + public function insertContentBefore( array $initialContent, DeclarationBlock $oItem, DeclarationBlock $oSibling, - string $sPosition, array $expectedContent ) { @@ -152,7 +142,7 @@ public function insertContent( self::assertCount(3, $this->subject->getContents()); - $this->subject->insert($oItem, $oSibling, $sPosition); + $this->subject->insertBefore($oItem, $oSibling); self::assertCount(4, $this->subject->getContents()); self::assertSame($expectedContent, $this->subject->getContents()); From 2c97591c6e43e5f4e141ca880056bfb59d0a94b6 Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Tue, 18 Jun 2024 16:55:00 +0200 Subject: [PATCH 04/11] fixup! [FEATURE] Support for inserting an item in a CSSList --- src/CSSList/CSSList.php | 11 +++++----- tests/CSSList/DocumentTest.php | 38 +++++++++++++++------------------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index 036420ff..273489dc 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -295,18 +295,17 @@ public function splice($iOffset, $iLength = null, $mReplacement = null) } /** - * Insert an item before its sibling. + * Inserts an item before its sibling. * - * @param RuleSet|CSSList|Import|Charset $oItem The item. - * @param RuleSet|CSSList|Import|Charset $oSibling The sibling. + * @param RuleSet|CSSList|Import|Charset $oItem + * @param RuleSet|CSSList|Import|Charset $oSibling */ public function insertBefore($oItem, $oSibling): void { - $iIndex = in_array($oSibling, $this->aContents); - if ($iIndex === false) { + if (in_array($oSibling, $this->aContents, true) === false) { $this->append($oItem); } else { - $this->replace($oSibling, array($oItem, $oSibling)); + $this->replace($oSibling, [$oItem, $oSibling]); } } diff --git a/tests/CSSList/DocumentTest.php b/tests/CSSList/DocumentTest.php index 208cc8ce..1edefe32 100644 --- a/tests/CSSList/DocumentTest.php +++ b/tests/CSSList/DocumentTest.php @@ -87,9 +87,9 @@ public function setContentsReplacesContentsSetInPreviousCall(): void } /** - * @return \Generator, DeclarationBlock, DeclarationBlock, array>> + * @return array>> */ - public static function insertBeforeDataProvider(): \Generator + public static function insertContentBeforeInsertsContentBeforeSibblingOrAppendsIfSibblingNotFoundDataProvider(): array { $bogusOne = new DeclarationBlock(); @@ -106,32 +106,28 @@ public static function insertBeforeDataProvider(): \Generator $oOrphan = new DeclarationBlock(); $oOrphan->setSelectors('.forever-alone'); - yield 'insert before' => [ - 'initialContent' => [$bogusOne, $oSibling, $bogusTwo], - 'oItem' => $oItem, - 'oSibling' => $oSibling, - 'expectedContent' => [$bogusOne, $oItem, $oSibling, $bogusTwo], - ]; - yield 'append if not found' => [ - 'initialContent' => [$bogusOne, $oSibling, $bogusTwo], - 'oItem' => $oItem, - 'oSibling' => $oOrphan, - 'expectedContent' => [$bogusOne, $oSibling, $bogusTwo, $oItem], - ]; + return [ + 'insert before' => [ + 'initialContent' => [$bogusOne, $oSibling, $bogusTwo], + 'oItem' => $oItem, + 'oSibling' => $oSibling, + 'expectedContent' => [$bogusOne, $oItem, $oSibling, $bogusTwo], + ], + 'append if not found' => [ + 'initialContent' => [$bogusOne, $oSibling, $bogusTwo], + 'oItem' => $oItem, + 'oSibling' => $oOrphan, + 'expectedContent' => [$bogusOne, $oSibling, $bogusTwo, $oItem], + ] + ]; } /** * @test * - * @param $initialContent - * @param $oItem - * @param $oSibling - * @param $sPosition - * @param $expectedContent - * * @dataProvider insertBeforeDataProvider */ - public function insertContentBefore( + public function insertContentBeforeInsertsContentBeforeSibblingOrAppendsIfSibblingNotFound( array $initialContent, DeclarationBlock $oItem, DeclarationBlock $oSibling, From 07e5bf27a10689a26beecc04bc4a9f5ae17dc892 Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Thu, 20 Jun 2024 00:21:59 +0200 Subject: [PATCH 05/11] fixup! [FEATURE] Support for inserting an item in a CSSList --- src/CSSList/CSSList.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index 273489dc..dd6fb9e8 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -295,17 +295,18 @@ public function splice($iOffset, $iLength = null, $mReplacement = null) } /** - * Inserts an item before its sibling. + * Inserts an item before its sibling. If the desired sibling cannot be found, + * the item is appended at the end. * * @param RuleSet|CSSList|Import|Charset $oItem * @param RuleSet|CSSList|Import|Charset $oSibling */ public function insertBefore($oItem, $oSibling): void { - if (in_array($oSibling, $this->aContents, true) === false) { - $this->append($oItem); - } else { + if (in_array($oSibling, $this->aContents, true)) { $this->replace($oSibling, [$oItem, $oSibling]); + } else { + $this->append($oItem); } } From edd609ee9b99257cff33e9982ce2f82d03e9602a Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Thu, 20 Jun 2024 00:23:34 +0200 Subject: [PATCH 06/11] fixup! [FEATURE] Support for inserting an item in a CSSList --- src/CSSList/CSSList.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/CSSList/CSSList.php b/src/CSSList/CSSList.php index dd6fb9e8..8f3644d6 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -298,15 +298,15 @@ public function splice($iOffset, $iLength = null, $mReplacement = null) * Inserts an item before its sibling. If the desired sibling cannot be found, * the item is appended at the end. * - * @param RuleSet|CSSList|Import|Charset $oItem - * @param RuleSet|CSSList|Import|Charset $oSibling + * @param RuleSet|CSSList|Import|Charset $item + * @param RuleSet|CSSList|Import|Charset $sibling */ - public function insertBefore($oItem, $oSibling): void + public function insertBefore($item, $sibling): void { - if (in_array($oSibling, $this->aContents, true)) { - $this->replace($oSibling, [$oItem, $oSibling]); + if (in_array($sibling, $this->aContents, true)) { + $this->replace($oSibling, [$item, $sibling]); } else { - $this->append($oItem); + $this->append($item); } } From f388f6b0067d76337ea4ed623854dbf9c035ba71 Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Thu, 20 Jun 2024 00:24:31 +0200 Subject: [PATCH 07/11] fixup! [FEATURE] Support for inserting an item in a CSSList --- 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 8f3644d6..5c37771f 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -295,7 +295,7 @@ public function splice($iOffset, $iLength = null, $mReplacement = null) } /** - * Inserts an item before its sibling. If the desired sibling cannot be found, + * 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 From 0401ce3db97dbf14e491e2d4a32c0bbbc373a3eb Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Thu, 20 Jun 2024 00:35:14 +0200 Subject: [PATCH 08/11] fixup! [FEATURE] Support for inserting an item in a CSSList --- 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 5c37771f..b107b6ab 100644 --- a/src/CSSList/CSSList.php +++ b/src/CSSList/CSSList.php @@ -304,7 +304,7 @@ public function splice($iOffset, $iLength = null, $mReplacement = null) public function insertBefore($item, $sibling): void { if (in_array($sibling, $this->aContents, true)) { - $this->replace($oSibling, [$item, $sibling]); + $this->replace($sibling, [$item, $sibling]); } else { $this->append($item); } From b2d209163b298a1c1fdc3121c341e04a04fa85ff Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Thu, 20 Jun 2024 00:37:10 +0200 Subject: [PATCH 09/11] fixup! [FEATURE] Support for inserting an item in a CSSList --- tests/CSSList/DocumentTest.php | 65 ++++++++++++++++------------------ 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/tests/CSSList/DocumentTest.php b/tests/CSSList/DocumentTest.php index 1edefe32..b30520d0 100644 --- a/tests/CSSList/DocumentTest.php +++ b/tests/CSSList/DocumentTest.php @@ -87,60 +87,55 @@ public function setContentsReplacesContentsSetInPreviousCall(): void } /** - * @return array>> + * @test */ - public static function insertContentBeforeInsertsContentBeforeSibblingOrAppendsIfSibblingNotFoundDataProvider(): array - { - + public function insertContentBeforeInsertsContentBeforeSibbling() { $bogusOne = new DeclarationBlock(); $bogusOne->setSelectors('.bogus-one'); $bogusTwo = new DeclarationBlock(); $bogusTwo->setSelectors('.bogus-two'); - $oItem = new DeclarationBlock(); - $oItem->setSelectors('.item'); + $item = new DeclarationBlock(); + $item->setSelectors('.item'); - $oSibling = new DeclarationBlock(); - $oSibling->setSelectors('.sibling'); + $sibling = new DeclarationBlock(); + $sibling->setSelectors('.sibling'); - $oOrphan = new DeclarationBlock(); - $oOrphan->setSelectors('.forever-alone'); + $this->subject->setContents([$bogusOne, $sibling, $bogusTwo]); - return [ - 'insert before' => [ - 'initialContent' => [$bogusOne, $oSibling, $bogusTwo], - 'oItem' => $oItem, - 'oSibling' => $oSibling, - 'expectedContent' => [$bogusOne, $oItem, $oSibling, $bogusTwo], - ], - 'append if not found' => [ - 'initialContent' => [$bogusOne, $oSibling, $bogusTwo], - 'oItem' => $oItem, - 'oSibling' => $oOrphan, - 'expectedContent' => [$bogusOne, $oSibling, $bogusTwo, $oItem], - ] - ]; + 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 - * - * @dataProvider insertBeforeDataProvider */ - public function insertContentBeforeInsertsContentBeforeSibblingOrAppendsIfSibblingNotFound( - array $initialContent, - DeclarationBlock $oItem, - DeclarationBlock $oSibling, - array $expectedContent - ) { + 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($initialContent); + $this->subject->setContents([$bogusOne, $sibling, $bogusTwo]); self::assertCount(3, $this->subject->getContents()); - $this->subject->insertBefore($oItem, $oSibling); + $this->subject->insertBefore($item, $orphan); self::assertCount(4, $this->subject->getContents()); - self::assertSame($expectedContent, $this->subject->getContents()); + self::assertSame([$bogusOne, $sibling, $bogusTwo, $item], $this->subject->getContents()); } } From 1b51cd6d7fcfeeae651603a65a59de44daef3e0b Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Thu, 20 Jun 2024 00:49:19 +0200 Subject: [PATCH 10/11] fixup! [FEATURE] Support for inserting an item in a CSSList --- tests/CSSList/DocumentTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/CSSList/DocumentTest.php b/tests/CSSList/DocumentTest.php index b30520d0..ec84f1e2 100644 --- a/tests/CSSList/DocumentTest.php +++ b/tests/CSSList/DocumentTest.php @@ -89,7 +89,8 @@ public function setContentsReplacesContentsSetInPreviousCall(): void /** * @test */ - public function insertContentBeforeInsertsContentBeforeSibbling() { + public function insertContentBeforeInsertsContentBeforeSibbling() + { $bogusOne = new DeclarationBlock(); $bogusOne->setSelectors('.bogus-one'); $bogusTwo = new DeclarationBlock(); @@ -114,7 +115,8 @@ public function insertContentBeforeInsertsContentBeforeSibbling() { /** * @test */ - public function insertContentBeforeAppendsIfSibblingNotFound() { + public function insertContentBeforeAppendsIfSibblingNotFound() + { $bogusOne = new DeclarationBlock(); $bogusOne->setSelectors('.bogus-one'); $bogusTwo = new DeclarationBlock(); From ed1c30b5917b62080418b07d0aba70ba07c9c4f1 Mon Sep 17 00:00:00 2001 From: Daniel Ziegenberg Date: Thu, 20 Jun 2024 01:02:37 +0200 Subject: [PATCH 11/11] fixup! [FEATURE] Support for inserting an item in a CSSList --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c01983fd..1844e90c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +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) - Add a class diagram to the README (#482) - Add support for the `dvh`, `lvh` and `svh` length units (#415) - Add more tests (#449)