PHP-CSS-Parser
PHP-CSS-Parser copied to clipboard
Add additional validation for size unit
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:.
That failing test seems to be unrelated to the change here.
Yeah, if I try validating:
@keyframes spin {
to {
transform: rotate(1turns);
}
}
Then it fails with Unknown dimension: 1turns
@sabberworm Anything else you'd like to see here?
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.
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.
:warning: Rebasing with latest changes from master
.