Skip to content

Commit 1f94eb4

Browse files
committed
Merge remote-tracking branch 'origin/pr/77'
* origin/pr/77: Add of the removeSelector from Block (previously in the todo list)
2 parents 4c4ab98 + 48e4380 commit 1f94eb4

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ There are a few convenience methods on Document to ease finding, manipulating an
106106

107107
## To-Do
108108

109-
* More convenience methods [like `selectorsWithElement($sId/Class/TagName)`, `removeSelector($oSelector)`, `attributesOfType($sType)`, `removeAttributesOfType($sType)`]
109+
* More convenience methods [like `selectorsWithElement($sId/Class/TagName)`, `attributesOfType($sType)`, `removeAttributesOfType($sType)`]
110110
* Options for output (compact, verbose, etc.)
111111
* Real multibyte support. Currently only multibyte charsets whose first 255 code points take up only one byte and are identical with ASCII are supported (yes, UTF-8 fits this description).
112112
* Named color support (using `Color` instead of an anonymous string literal)

lib/Sabberworm/CSS/CSSList/CSSList.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ public function remove($oItemToRemove) {
3333
$iKey = array_search($oItemToRemove, $this->aContents, true);
3434
if ($iKey !== false) {
3535
unset($this->aContents[$iKey]);
36+
return true;
3637
}
38+
return false;
3739
}
3840

41+
/**
42+
* Removes a declaration block from the CSS list if it matches all given selectors.
43+
* @param array|string $mSelector The selectors to match.
44+
* @param boolean $bRemoveAll Whether to stop at the first declaration block found or remove all blocks
45+
*/
3946
public function removeDeclarationBlockBySelector($mSelector, $bRemoveAll = false) {
4047
if ($mSelector instanceof DeclarationBlock) {
4148
$mSelector = $mSelector->getSelectors();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Sabberworm\CSS\Parsing;
4+
5+
/**
6+
* Thrown if the CSS parsers attempts to print something invalid
7+
*/
8+
class OutputException extends \Exception {
9+
}

lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ public function setSelectors($mSelector) {
3636
}
3737
}
3838

39+
// remove one of the selector of the block
40+
public function removeSelector($mSelector) {
41+
if($mSelector instanceof Selector) {
42+
$mSelector = $mSelector->getSelector();
43+
}
44+
foreach($this->aSelectors as $iKey => $oSelector) {
45+
if($oSelector->getSelector() === $mSelector) {
46+
unset($this->aSelectors[$iKey]);
47+
return true;
48+
}
49+
}
50+
return false;
51+
}
52+
3953
/**
4054
* @deprecated use getSelectors()
4155
*/
@@ -576,6 +590,10 @@ public function createFontShorthand() {
576590
}
577591

578592
public function __toString() {
593+
if(count($this->aSelectors) === 0) {
594+
// If all the selectors have been removed, this declaration block becomes invalid
595+
throw new \Sabberworm\CSS\Parsing\OutputException("Attempt to print declaration block with missing selector");
596+
}
579597
$sResult = implode(', ', $this->aSelectors) . ' {';
580598
$sResult .= parent::__toString();
581599
$sResult .= '}' . "\n";

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class ParserTest extends \PHPUnit_Framework_TestCase {
1111

1212
function testFiles() {
13-
13+
1414
$sDirectory = dirname(__FILE__) . '/../../files';
1515
if ($rHandle = opendir($sDirectory)) {
1616
/* This is the correct way to loop over the directory. */
@@ -342,6 +342,23 @@ function testListValueRemoval() {
342342
$this->assertSame('@media screen {html {some: -test(val2);}
343343
}#unrelated {other: yes;}' . "\n", $oDoc->__toString());
344344
}
345+
346+
/**
347+
* @expectedException Sabberworm\CSS\Parsing\OutputException
348+
*/
349+
function testSelectorRemoval() {
350+
$oDoc = $this->parsedStructureForFile('1readme');
351+
$aBlocks = $oDoc->getAllDeclarationBlocks();
352+
$oBlock1 = $aBlocks[0];
353+
$this->assertSame(true, $oBlock1->removeSelector('html'));
354+
$sExpected = '@charset "utf-8";@font-face {font-family: "CrassRoots";src: url("../media/cr.ttf");}body {font-size: 1.6em;}
355+
';
356+
$this->assertSame($sExpected, $oDoc->__toString());
357+
$this->assertSame(false, $oBlock1->removeSelector('html'));
358+
$this->assertSame(true, $oBlock1->removeSelector('body'));
359+
// This tries to output a declaration block without a selector and throws.
360+
$oDoc->__toString();
361+
}
345362

346363
function testComments() {
347364
$oDoc = $this->parsedStructureForFile('comments');

0 commit comments

Comments
 (0)