Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional validation for size unit #193

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions lib/Sabberworm/CSS/Value/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Sabberworm\CSS\Value;

use Sabberworm\CSS\Parsing\ParserState;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;

class Size extends PrimitiveValue {

const ABSOLUTE_SIZE_UNITS = 'px/cm/mm/mozmm/in/pt/pc/vh/vw/vmin/vmax/rem'; //vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport)
const RELATIVE_SIZE_UNITS = '%/em/ex/ch/fr';
const NON_SIZE_UNITS = 'deg/grad/rad/s/ms/turns/Hz/kHz';
const NON_SIZE_UNITS = 'deg/grad/rad/s/ms/turn/Hz/kHz';

private static $SIZE_UNITS = null;

Expand Down Expand Up @@ -38,15 +39,20 @@ public static function parse(ParserState $oParserState, $bIsColorComponent = fal

$sUnit = null;
$aSizeUnits = self::getSizeUnits();
foreach($aSizeUnits as $iLength => &$aValues) {
$sKey = strtolower($oParserState->peek($iLength));
if(array_key_exists($sKey, $aValues)) {
if (($sUnit = $aValues[$sKey]) !== null) {
$oParserState->consume($iLength);
break;
}
$iMaxSizeUnitLength = max(array_keys($aSizeUnits));

if ( preg_match( '/^(%|[a-zA-Z0-9]+)/', $oParserState->peek($iMaxSizeUnitLength), $matches ) ) {
$sUnit = strtolower($matches[0]);
$iUnitLength = strlen($sUnit);

if (isset($aSizeUnits[$iUnitLength][$sUnit])) {
$sUnit = $aSizeUnits[$iUnitLength][$sUnit];
$oParserState->consume($iUnitLength);
} else {
throw new UnexpectedTokenException('Unit', $sUnit, 'identifier', $oParserState->currentLine());
}
}

return new Size(floatval($sSize), $sUnit, $bIsColorComponent, $oParserState->currentLine());
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Sabberworm/CSS/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,4 +787,22 @@ function testLonelyImport() {
$sExpected = "@import url(\"example.css\") only screen and (max-width: 600px);";
$this->assertSame($sExpected, $oDoc->render());
}

function testTurnUnitLenient() {
$sText = ".foo {transform: rotate(1turn);}\n.bar {transform: rotate(1turns);}";
$sExpected = ".foo {transform: rotate(1turn);}\n.bar {}";

$oParser = new Parser($sText);
$this->assertSame($sExpected, $oParser->parse()->render());
}

function testTurnUnitStrict() {
$sText = ".foo {transform: rotate(1turn);}\n.bar {transform: rotate(1turns);}";

$oParser = new Parser($sText, Settings::create()->beStrict());

// Line 2 contains the invalid unit and so should be reported.
$this->setExpectedException( 'Sabberworm\CSS\Parsing\UnexpectedTokenException', 'Identifier expected. Got “turns” [line no: 2]' );
$oParser->parse();
}
}