Skip to content

Commit d4744a6

Browse files
ziegenbergFrederic Massart
and
Frederic Massart
committed
[FEATURE] Support for inserting an item in a CSSList
Signed-off-by: Daniel Ziegenberg <[email protected]> Co-authored-by: Frederic Massart <[email protected]>
1 parent 1b4dc29 commit d4744a6

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/CSSList/CSSList.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,27 @@ public function splice($iOffset, $iLength = null, $mReplacement = null)
294294
array_splice($this->aContents, $iOffset, $iLength, $mReplacement);
295295
}
296296

297+
/**
298+
* Insert an item before or after its sibling.
299+
*
300+
* @param RuleSet|CSSList|Import|Charset $oItem The item.
301+
* @param RuleSet|CSSList|Import|Charset $oSibling The sibling.
302+
* @param string $sPosition The position.
303+
*
304+
* @return void
305+
*/
306+
public function insert($oItem, $oSibling, string $sPosition = 'before'): void
307+
{
308+
$iIndex = in_array($oSibling, $this->aContents);
309+
if ($iIndex === false) {
310+
$this->append($oItem);
311+
} elseif ($sPosition === 'before') {
312+
$this->replace($oSibling, array($oItem, $oSibling));
313+
} else {
314+
$this->replace($oSibling, array($oSibling, $oItem));
315+
}
316+
}
317+
297318
/**
298319
* Removes an item from the CSS list.
299320
*

tests/CSSList/DocumentTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sabberworm\CSS\Tests\CSSList;
44

5+
use Generator;
56
use PHPUnit\Framework\TestCase;
67
use Sabberworm\CSS\Comment\Commentable;
78
use Sabberworm\CSS\CSSList\Document;
@@ -85,4 +86,72 @@ public function setContentsReplacesContentsSetInPreviousCall(): void
8586

8687
self::assertSame($contents2, $this->subject->getContents());
8788
}
89+
90+
/**
91+
* @return Generator
92+
*/
93+
public static function insertDataProvider(): Generator
94+
{
95+
96+
$bogusOne = new DeclarationBlock();
97+
$bogusOne->setSelectors('.bogus-one');
98+
$bogusTwo = new DeclarationBlock();
99+
$bogusTwo->setSelectors('.bogus-two');
100+
101+
$oItem = new DeclarationBlock();
102+
$oItem->setSelectors('.item');
103+
104+
$oSibling = new DeclarationBlock();
105+
$oSibling->setSelectors('.sibling');
106+
107+
$oOrphan = new DeclarationBlock();
108+
$oOrphan->setSelectors('.forever-alone');
109+
110+
yield 'insert before' => [
111+
'initialContent' => [$bogusOne, $oSibling, $bogusTwo],
112+
'oItem' => $oItem,
113+
'oSibling' => $oSibling,
114+
'position' => 'before',
115+
'expectedContent' => [$bogusOne, $oItem, $oSibling, $bogusTwo],
116+
];
117+
yield 'insert after' => [
118+
'initialContent' => [$bogusOne, $oSibling, $bogusTwo],
119+
'oItem' => $oItem,
120+
'oSibling' => $oSibling,
121+
'position' => 'after',
122+
'expectedContent' => [$bogusOne, $oSibling, $oItem, $bogusTwo],
123+
];
124+
yield 'append if not found' => [
125+
'initialContent' => [$bogusOne, $oSibling, $bogusTwo],
126+
'oItem' => $oItem,
127+
'oSibling' => $oOrphan,
128+
'position' => 'before',
129+
'expectedContent' => [$bogusOne, $oSibling, $bogusTwo, $oItem],
130+
];
131+
}
132+
133+
/**
134+
* @test
135+
*
136+
* @param array $contents
137+
*
138+
* @dataProvider insertDataProvider
139+
*/
140+
public function insertContent(
141+
array $initialContent,
142+
DeclarationBlock $oItem,
143+
DeclarationBlock $oSibling,
144+
string $sPosition,
145+
array $expectedContent
146+
) {
147+
148+
$this->subject->setContents($initialContent);
149+
150+
self::assertCount(3, $this->subject->getContents());
151+
152+
$this->subject->insert($oItem, $oSibling, $sPosition);
153+
154+
self::assertCount(4, $this->subject->getContents());
155+
self::assertSame($expectedContent, $this->subject->getContents());
156+
}
88157
}

0 commit comments

Comments
 (0)