Skip to content

Parsing exception when Culture is not western #187

Description

@nadjibus

Hi. When trying to parse a css file while the current culture is not western (en, fr...) an exception is thrown:

ExCSS.ParseException: Unrecognized integer value '-1.'
   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.OrValueConverter.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 Program.<Main>$(String[] args) in D:\CssRtlTest\Program.cs:line 10
   at Program.<Main>(String[] args)

Code to reproduce:

// Switching Culture to Arabic ...
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ar");

// Using Bootstrap 5 CSS...
var bootstrap5 = await new HttpClient().GetStringAsync("https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"); 

// Exception: ExCSS.ParseException: Unrecognized integer value '-1.'
var stylesheet = new StylesheetParser().Parse(bootstrap5);
  
Console.WriteLine("Parsing successful!");
Console.ReadLine();

Workaround:
Temporary switch culture to Invariant before parsing:

Stylesheet stylesheet;

// Save original culture
var currentCulture = CultureInfo.CurrentCulture;

try
{
    // Temporary switching Culture to Invariant...
    CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

    // Parse CSS...
    stylesheet = new StylesheetParser().Parse(bootstrap5);
}
finally
{
    // Switch back to original culture
    CultureInfo.CurrentCulture = currentCulture;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions