Skip to content

Commit ef95a38

Browse files
committed
Ignore Exceptions during rule set and list render
if output format has `setIgnoreExceptions(true)` or `beLenient()`
1 parent 5d5c629 commit ef95a38

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

lib/Sabberworm/CSS/CSSList/CSSList.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,19 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
8080
$oNextLevel = $oOutputFormat->nextLevel();
8181
}
8282
foreach ($this->aContents as $oContent) {
83+
$sRendered = $oOutputFormat->safely(function() use ($oNextLevel, $oContent) {
84+
return $oContent->render($oNextLevel);
85+
});
86+
if($sRendered === null) {
87+
continue;
88+
}
8389
if($bIsFirst) {
8490
$bIsFirst = false;
8591
$sResult .= $oNextLevel->spaceBeforeBlocks();
8692
} else {
8793
$sResult .= $oNextLevel->spaceBetweenBlocks();
8894
}
89-
$sResult .= $oContent->render($oNextLevel);
95+
$sResult .= $sRendered;
9096
}
9197

9298
if(!$bIsFirst) {

lib/Sabberworm/CSS/OutputFormat.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sabberworm\CSS;
44

5+
use Sabberworm\CSS\Parsing\OutputException;
6+
57
class OutputFormat {
68
/**
79
* Value format
@@ -225,7 +227,7 @@ public function spaceBeforeOpeningBrace() {
225227
* Runs the given code, either swallowing or passing exceptions, depending on the bIgnoreExceptions setting.
226228
*/
227229
public function safely($cCode) {
228-
if($this->oFormat->bIgnoreExceptions) {
230+
if($this->oFormat->get('IgnoreExceptions')) {
229231
// If output exceptions are ignored, run the code with exception guards
230232
try {
231233
return $cCode();

lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Sabberworm\CSS\Value\Size;
1010
use Sabberworm\CSS\Value\Color;
1111
use Sabberworm\CSS\Value\URL;
12+
use Sabberworm\CSS\Parsing\OutputException;
1213

1314
/**
1415
* Declaration blocks are the parts of a css file which denote the rules belonging to a selector.
@@ -596,7 +597,7 @@ public function __toString() {
596597
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
597598
if(count($this->aSelectors) === 0) {
598599
// If all the selectors have been removed, this declaration block becomes invalid
599-
throw new \Sabberworm\CSS\Parsing\OutputException("Attempt to print declaration block with missing selector");
600+
throw new OutputException("Attempt to print declaration block with missing selector");
600601
}
601602
$sResult = $oOutputFormat->implode($oOutputFormat->spaceBeforeSelectorSeparator() . ',' . $oOutputFormat->spaceAfterSelectorSeparator(), $this->aSelectors) . $oOutputFormat->spaceBeforeOpeningBrace() . '{';
602603
$sResult .= parent::render($oOutputFormat);

lib/Sabberworm/CSS/RuleSet/RuleSet.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,19 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
9292
$bIsFirst = true;
9393
foreach ($this->aRules as $aRules) {
9494
foreach($aRules as $oRule) {
95+
$sRendered = $oOutputFormat->safely(function() use ($oRule, $oOutputFormat) {
96+
return $oRule->render($oOutputFormat->nextLevel());
97+
});
98+
if($sRendered === null) {
99+
continue;
100+
}
95101
if($bIsFirst) {
96102
$bIsFirst = false;
97103
$sResult .= $oOutputFormat->nextLevel()->spaceBeforeRules();
98104
} else {
99105
$sResult .= $oOutputFormat->nextLevel()->spaceBetweenRules();
100106
}
101-
$sResult .= $oRule->render($oOutputFormat->nextLevel());
107+
$sResult .= $sRendered;
102108
}
103109
}
104110

tests/Sabberworm/CSS/OutputFormatTest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,25 @@ public function testSpaceBeforeBraces() {
146146
@media screen{.main{background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', $this->oDocument->render(OutputFormat::create()->setSpaceBeforeOpeningBrace('')));
147147
}
148148

149-
// public function testIgnoreExceptions() {
150-
// $this->assertSame('.main, .test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;}
151-
// @media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}
152-
// }', $this->oDocument->render(OutputFormat::create()->setIgnoreExceptions('')));
153-
// }
149+
/**
150+
* @expectedException Sabberworm\CSS\Parsing\OutputException
151+
*/
152+
public function testIgnoreExceptionsOff() {
153+
$aBlocks = $this->oDocument->getAllDeclarationBlocks();
154+
$oFirstBlock = $aBlocks[0];
155+
$oFirstBlock->removeSelector('.main');
156+
$this->assertSame('.test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;}
157+
@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', $this->oDocument->render(OutputFormat::create()->setIgnoreExceptions(false)));
158+
$oFirstBlock->removeSelector('.test');
159+
$this->oDocument->render(OutputFormat::create()->setIgnoreExceptions(false));
160+
}
161+
162+
public function testIgnoreExceptionsOn() {
163+
$aBlocks = $this->oDocument->getAllDeclarationBlocks();
164+
$oFirstBlock = $aBlocks[0];
165+
$oFirstBlock->removeSelector('.main');
166+
$oFirstBlock->removeSelector('.test');
167+
$this->assertSame('@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', $this->oDocument->render(OutputFormat::create()->setIgnoreExceptions(true)));
168+
}
154169

155170
}

0 commit comments

Comments
 (0)