Skip to content

Commit d708c1d

Browse files
ziegenbergFrederic Massart
and
Frederic Massart
committed
[FEATURE] Support for inserting an item in a CSSList
Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at> Co-authored-by: Frederic Massart <fred@moodle.com>
1 parent 4e598e9 commit d708c1d

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
@@ -296,6 +296,27 @@ public function splice($iOffset, $iLength = null, $mReplacement = null)
296296
array_splice($this->aContents, $iOffset, $iLength, $mReplacement);
297297
}
298298

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

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()
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+
$this->assertCount(3, $this->subject->getContents());
151+
152+
$this->subject->insert($oItem, $oSibling, $sPosition);
153+
154+
$this->assertCount(4, $this->subject->getContents());
155+
$this->assertSame($expectedContent, $this->subject->getContents());
156+
}
88157
}

0 commit comments

Comments
 (0)