Skip to content

Commit dc99bbd

Browse files
authored
[BUGFIX] Allow order-insensitve removeDeclarationBlockBySelector (#1406)
Also internally avoid comparison with `==`. Resolves #1330
1 parent 51a3c0a commit dc99bbd

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Please also have a look at our
2323

2424
### Fixed
2525

26+
- Allow `removeDeclarationBlockBySelector()` to be order-insensitve (#1406)
2627
- Fix parsing of `calc` expressions when a newline immediately precedes or
2728
follows a `+` or `-` operator (#1399)
2829
- Use typesafe versions of PHP functions (#1379, #1380, #1382, #1383, #1384)

config/phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: '#^Loose comparison via "\=\=" is not allowed\.$#'
5-
identifier: equal.notAllowed
6-
count: 1
7-
path: ../src/CSSList/CSSList.php
8-
93
-
104
message: '#^Parameter \#2 \$found of class Sabberworm\\CSS\\Parsing\\UnexpectedTokenException constructor expects string, Sabberworm\\CSS\\Value\\CSSFunction\|Sabberworm\\CSS\\Value\\CSSString\|Sabberworm\\CSS\\Value\\LineName\|Sabberworm\\CSS\\Value\\Size\|Sabberworm\\CSS\\Value\\URL given\.$#'
115
identifier: argument.type

src/CSSList/CSSList.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ public function removeDeclarationBlockBySelector($selectors, bool $removeAll = f
370370
if (!($item instanceof DeclarationBlock)) {
371371
continue;
372372
}
373-
if ($item->getSelectors() == $selectors) {
373+
if (self::selectorsMatch($item->getSelectors(), $selectors)) {
374374
unset($this->contents[$key]);
375375
if (!$removeAll) {
376376
return;
@@ -427,4 +427,34 @@ public function getContents(): array
427427
{
428428
return $this->contents;
429429
}
430+
431+
/**
432+
* @param list<Selector> $selectors1
433+
* @param list<Selector> $selectors2
434+
*/
435+
private static function selectorsMatch(array $selectors1, array $selectors2): bool
436+
{
437+
$selectorStrings1 = self::getSelectorStrings($selectors1);
438+
$selectorStrings2 = self::getSelectorStrings($selectors2);
439+
440+
\sort($selectorStrings1);
441+
\sort($selectorStrings2);
442+
443+
return $selectorStrings1 === $selectorStrings2;
444+
}
445+
446+
/**
447+
* @param list<Selector> $selectors
448+
*
449+
* @return list<string>
450+
*/
451+
private static function getSelectorStrings(array $selectors): array
452+
{
453+
return \array_map(
454+
static function (Selector $selector): string {
455+
return $selector->getSelector();
456+
},
457+
$selectors
458+
);
459+
}
430460
}

tests/Unit/CSSList/CSSListTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,22 @@ public function removeDeclarationBlockBySelectorRemovesDeclarationBlockWithOutso
239239
self::assertSame([], $subject->getContents());
240240
}
241241

242+
/**
243+
* @test
244+
*/
245+
public function removeDeclarationBlockBySelectorRemovesDeclarationBlockWithSelectorsInReverseOrder(): void
246+
{
247+
$subject = new ConcreteCSSList();
248+
$declarationBlock = new DeclarationBlock();
249+
$declarationBlock->setSelectors(['html', 'body']);
250+
$subject->setContents([$declarationBlock]);
251+
self::assertNotSame([], $subject->getContents()); // make sure contents are set
252+
253+
$subject->removeDeclarationBlockBySelector([new Selector('body'), new Selector('html')]);
254+
255+
self::assertSame([], $subject->getContents());
256+
}
257+
242258
/**
243259
* @test
244260
*/

0 commit comments

Comments
 (0)