Skip to content

Commit 8eba275

Browse files
committed
Better fault tolerance.
Rules with an error will now also be caught (in lenient mode) if there is no newline or semicolon till the next closing brace. Also, there is now a test for the bug reported in MyIntervals#63
1 parent 9dee32a commit 8eba275

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,20 @@ private function parseRuleSet($oRuleSet) {
264264
try {
265265
$oRule = $this->parseRule();
266266
} catch (UnexpectedTokenException $e) {
267-
$this->consumeUntil(array("\n", ";"), true);
268-
$this->consumeWhiteSpace();
269-
while ($this->comes(';')) {
270-
$this->consume(';');
267+
try {
268+
$sConsume = $this->consumeUntil(array("\n", ";", '}'), true);
269+
// We need to “unfind” the matches to the end of the ruleSet as this will be matched later
270+
if($this->streql($this->substr($sConsume, $this->strlen($sConsume)-1, 1), '}')) {
271+
$this->iCurrentPosition--;
272+
} else {
273+
$this->consumeWhiteSpace();
274+
while ($this->comes(';')) {
275+
$this->consume(';');
276+
}
277+
}
278+
} catch (UnexpectedTokenException $e) {
279+
// We’ve reached the end of the document. Just close the RuleSet.
280+
return;
271281
}
272282
}
273283
} else {

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function testFiles() {
2222
continue;
2323
}
2424
if (strpos($sFileName, '-') === 0) {
25-
//Either a file which SHOULD fail or a future test of a as-of-now missing feature
25+
//Either a file which SHOULD fail (at least in strict mode) or a future test of a as-of-now missing feature
2626
continue;
2727
}
2828
$oParser = new Parser(file_get_contents($sDirectory . DIRECTORY_SEPARATOR . $sFileName));

tests/Sabberworm/CSS/RuleSet/LenientParsingTest.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,33 @@ class LenientParsingTest extends \PHPUnit_Framework_TestCase {
1111
* @expectedException Sabberworm\CSS\Parsing\UnexpectedTokenException
1212
*/
1313
public function testFaultToleranceOff() {
14-
$sFile = dirname(__FILE__) . '/../../../files' . DIRECTORY_SEPARATOR . "fault-tolerance.css";
14+
$sFile = dirname(__FILE__) . '/../../../files' . DIRECTORY_SEPARATOR . "-fault-tolerance.css";
1515
$oParser = new Parser(file_get_contents($sFile), Settings::create()->beStrict());
1616
$oParser->parse();
1717
}
1818

1919
public function testFaultToleranceOn() {
20-
$sFile = dirname(__FILE__) . '/../../../files' . DIRECTORY_SEPARATOR . "fault-tolerance.css";
20+
$sFile = dirname(__FILE__) . '/../../../files' . DIRECTORY_SEPARATOR . "-fault-tolerance.css";
2121
$oParser = new Parser(file_get_contents($sFile), Settings::create()->withLenientParsing(true));
2222
$oResult = $oParser->parse();
23-
$this->assertSame('.test1 {}'."\n".'.test2 {hello: 2.2;hello: 200000000000.2;}'."\n", $oResult->__toString());
23+
$this->assertSame('.test1 {}'."\n".'.test2 {hello: 2.2;hello: 200000000000.2;}'."\n".'#test {}'."\n".'#test2 {help: none;}'."\n", $oResult->__toString());
24+
}
25+
26+
/**
27+
* @expectedException Sabberworm\CSS\Parsing\UnexpectedTokenException
28+
*/
29+
public function testEndToken() {
30+
$sFile = dirname(__FILE__) . '/../../../files' . DIRECTORY_SEPARATOR . "-end-token.css";
31+
$oParser = new Parser(file_get_contents($sFile), Settings::create()->beStrict());
32+
$oResult = $oParser->parse();
2433
}
2534

2635
public function testLocaleTrap() {
2736
setlocale(LC_ALL, "pt_PT", "no");
28-
$sFile = dirname(__FILE__) . '/../../../files' . DIRECTORY_SEPARATOR . "fault-tolerance.css";
37+
$sFile = dirname(__FILE__) . '/../../../files' . DIRECTORY_SEPARATOR . "-fault-tolerance.css";
2938
$oParser = new Parser(file_get_contents($sFile), Settings::create()->withLenientParsing(true));
3039
$oResult = $oParser->parse();
31-
$this->assertSame('.test1 {}'."\n".'.test2 {hello: 2.2;hello: 200000000000.2;}'."\n", $oResult->__toString());
40+
$this->assertSame('.test1 {}'."\n".'.test2 {hello: 2.2;hello: 200000000000.2;}'."\n".'#test {}'."\n".'#test2 {help: none;}'."\n", $oResult->__toString());
3241
}
3342

3443
public function testCaseInsensitivity() {

tests/files/fault-tolerance.css renamed to tests/files/-fault-tolerance.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@
66
*hello: 1;
77
hello: 2.2;
88
hello: 2000000000000.2;
9-
}
9+
}
10+
11+
#test {
12+
#hello: 1}
13+
14+
#test2 {
15+
help: none;

0 commit comments

Comments
 (0)