PHP-CSS-Parser icon indicating copy to clipboard operation
PHP-CSS-Parser copied to clipboard

Add additional validation for size unit

Open pierlon opened this issue 4 years ago • 6 comments

This PR adds additional validation when parsing a size unit.

With this change, when parsing in strict mode an UnexpectedTokenException error will be thrown when an invalid size unit occurs after a number. When in lenient mode, however, if a valid size unit identifier is detected, it will be kept and the proceeding set of characters will be stripped. For example:

Dimension value Strict parsing Lenient parsing
Before 1radsss 1 rad sss 1 rad sss
After 1radsss UnexpectedTokenException Value is removed

Also, there is no such unit named turns, but there is one called turn (ref), which I suppose is what was meant here :smile:.

pierlon avatar May 06 '20 19:05 pierlon

That failing test seems to be unrelated to the change here.

pierlon avatar May 06 '20 20:05 pierlon

Yeah, if I try validating:

@keyframes spin {
  to {
    transform: rotate(1turns);
  }
}

Then it fails with Unknown dimension: 1turns

westonruter avatar May 11 '20 00:05 westonruter

@sabberworm Anything else you'd like to see here?

westonruter avatar May 13 '20 21:05 westonruter

I’m just wondering if we really need to hard-code all possible units. To be future-proof, couldn’t we just parse everything as a unit that looks like a unit?

I don’t know if there are contexts where it’s ambiguous whether a token is a unit or not but I’d think everything that starts with a number is a CSSSize and every token that follows such a number without white-space in between is a unit. Or am I missing something?

I know this implies a less strict validation but the parser was always meant as a parser that checks syntax, not semantics.

sabberworm avatar May 14 '20 06:05 sabberworm

I don’t know if there are contexts where it’s ambiguous whether a token is a unit or not but I’d think everything that starts with a number is a CSSSize and every token that follows such a number without white-space in between is a unit.

Yes, you're correct. In this case the parser would be looking for either a dimension (number immediately followed by a unit identifier) or a percentage ( number immediately followed by a percent sign).

Removing the hard-coded units along with the validation of the size unit does seem like the best option here, and if that's the case, it will be a breaking change and a major release would be warranted.

pierlon avatar May 29 '20 19:05 pierlon

:warning: Rebasing with latest changes from master.

pierlon avatar Nov 02 '20 01:11 pierlon