Skip to content

Commit d9a7d90

Browse files
committed
Output correct size unit in lenient mode; fail if unit not valid
1 parent d9d0190 commit d9a7d90

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

lib/Sabberworm/CSS/Value/Size.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Sabberworm\CSS\Value;
44

55
use Sabberworm\CSS\Parsing\ParserState;
6+
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
67

78
class Size extends PrimitiveValue {
89

@@ -38,11 +39,21 @@ public static function parse(ParserState $oParserState, $bIsColorComponent = fal
3839

3940
$sUnit = null;
4041
$aSizeUnits = self::getSizeUnits();
41-
foreach($aSizeUnits as $iLength => &$aValues) {
42+
$sUnit = strtolower($oParserState->parseIdentifier());
43+
$oParserState->backtrack(strlen($sUnit));
44+
45+
foreach($aSizeUnits as $iLength => $aValues) {
46+
$iConsumeLength = $iLength;
4247
$sKey = strtolower($oParserState->peek($iLength));
4348
if(array_key_exists($sKey, $aValues)) {
49+
if ($sUnit !== $sKey) {
50+
if (!$oParserState->getSettings()->bLenientParsing) {
51+
throw new UnexpectedTokenException('Unit', $sUnit, 'identifier', $oParserState->currentLine());
52+
}
53+
$iConsumeLength = strlen($sUnit);
54+
}
4455
if (($sUnit = $aValues[$sKey]) !== null) {
45-
$oParserState->consume($iLength);
56+
$oParserState->consume($iConsumeLength);
4657
break;
4758
}
4859
}

tests/Sabberworm/CSS/ParserTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ function testManipulation() {
214214
$this->assertSame('#header {margin: 10px 2em 1cm 2%;color: red !important;frequency: 30Hz;}
215215
body {color: green;}', $oDoc->render());
216216
}
217-
217+
218218
function testRuleGetters() {
219219
$oDoc = $this->parsedStructureForFile('values');
220220
$aBlocks = $oDoc->getAllDeclarationBlocks();
@@ -319,7 +319,7 @@ function testNamespaces() {
319319
|test {gaga: 2;}';
320320
$this->assertSame($sExpected, $oDoc->render());
321321
}
322-
322+
323323
function testInnerColors() {
324324
$oDoc = $this->parsedStructureForFile('inner-color');
325325
$sExpected = 'test {background: -webkit-gradient(linear,0 0,0 bottom,from(#006cad),to(hsl(202,100%,49%)));}';
@@ -359,7 +359,7 @@ function testListValueRemoval() {
359359
$this->assertSame('@media screen {html {some: -test(val2);}}
360360
#unrelated {other: yes;}', $oDoc->render());
361361
}
362-
362+
363363
/**
364364
* @expectedException Sabberworm\CSS\Parsing\OutputException
365365
*/
@@ -766,4 +766,22 @@ function testLonelyImport() {
766766
$sExpected = "@import url(\"example.css\") only screen and (max-width: 600px);";
767767
$this->assertSame($sExpected, $oDoc->render());
768768
}
769+
770+
function testTurnUnitLenient() {
771+
$sText = ".foo {transform: rotate(1turn);}\n.bar {transform: rotate(1turns);}";
772+
$sExpected = ".foo {transform: rotate(1turn);}\n.bar {transform: rotate(1turn);}";
773+
774+
$oParser = new Parser($sText);
775+
$this->assertSame($sExpected, $oParser->parse()->render());
776+
}
777+
778+
function testTurnUnitStrict() {
779+
$sText = ".foo {transform: rotate(1turn);}\n.bar {transform: rotate(1turns);}";
780+
781+
$oParser = new Parser($sText, Settings::create()->beStrict());
782+
783+
// Line 2 contains the invalid unit and so should be reported.
784+
$this->setExpectedException( 'Sabberworm\CSS\Parsing\UnexpectedTokenException', 'Identifier expected. Got “turns” [line no: 2]' );
785+
$oParser->parse();
786+
}
769787
}

0 commit comments

Comments
 (0)