Parsing z-index values outside of int range throws an OverflowException
For example, any z-index values greater than INT_MAX throws, such as this:
.foo {
z-index: 99999999999999999;
}
Would also apply to values smaller than INT_MIN.
Stack trace
Unhandled exception. System.OverflowException: Value was either too large or too small for an Int32.
at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, ReadOnlySpan`1 value, TypeCode type)
at System.Int32.Parse(String s, IFormatProvider provider)
at ExCSS.NumberToken.get_IntegerValue()
at ExCSS.ValueExtensions.ToInteger(IEnumerable`1 value)
at ExCSS.StructValueConverter`1.Convert(IEnumerable`1 value)
at ExCSS.OrValueConverter.Convert(IEnumerable`1 value)
at ExCSS.OrValueConverter.Convert(IEnumerable`1 value)
at ExCSS.OrValueConverter.Convert(IEnumerable`1 value)
at ExCSS.Property.TrySetValue(TokenValue newTokenValue)
at ExCSS.StylesheetComposer.CreateDeclarationWith(Func`2 createProperty, Token& token)
at ExCSS.StylesheetComposer.FillDeclarations(StyleDeclaration style)
at ExCSS.StylesheetComposer.CreateStyle(Token current)
at ExCSS.StylesheetComposer.CreateRule(Token token)
at ExCSS.StylesheetComposer.CreateRules(Stylesheet sheet)
at ExCSS.StylesheetParser.Parse(TextSource source)
at ExCSS.StylesheetParser.Parse(String content)
at [redacted]
The standard doesn't actually limit z-index values to any particular range, though realistically speaking every browser clamps them to Int32.
I'm also encountering this, it's in a rather large 3rd party css file that I don't have direct control over. I'm seeing z-index values of 999999999999999.
I can work-around by replacing those with smaller numbers before parsing, but it would be nice if the library tolerated this.
It's an interesting issue since CSS doesn't allow the value, but browsers handle the incorrect syntax. I'm looking at options on how to handle it since, technically, one could overflow an int64 value as well, but it's never fractional.
I haven't peeked at your code yet, but something like "if length >= 10 and first character > 2". to speed it up maybe a TryParse and only check that on failure?
The most recent version fixes the parsing by first trying to parse the value as an integer (as it did before). If that fails, the string representing the numeric value is scanned to check that all characters are digits. If so, the value is set to int.MaxValue. While not ideal, it does allow the parser to continue without exception. Any non-digit characters will throw an explicit exception. This introduces an edge case where the value is negative and smaller than in.MinValue. That scenario isn't currently handled, but as an edge case should not affect standard parsing behavior.
that sounds great!