AngleSharp / AngleSharp.Css

:angel: Library to enable support for cascading stylesheets in AngleSharp.
https://anglesharp.github.io
MIT License
72 stars 34 forks source link

System.FormatException thown parsing CSS #39

Closed The-Nutty closed 5 years ago

The-Nutty commented 5 years ago

Bug Report

Prerequisites

For more information, see the CONTRIBUTING guide.

Description

[Description of the bug] When parsing CSS with some very odd table styles a System.FormatException is thrown. I think this is caused by the letter-spacing:-.px in the following CSS block (see steps to reproduce) Note this is from an email hence the unusual styles and seemingly useless table.

<table border="0" cellpadding="0" cellspacing="0" style="background-color: #0098;border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;" width="45%">
    <tbody>
    <tr>
        <td align="center" style="color:#FFFFFF; font-family:Helvetica, Arial, sans-serif; font-size:16px; font-weight:bold; letter-spacing:-.px; line-height:100%; padding-top:7px; padding-right:8px; padding-bottom:7px; padding-left:8px;mso-line-height-rule: exactly;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;" valign="middle">
            <a href="test.com" style="color: #FFFFFF;text-decoration: none;mso-line-height-rule: exactly;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;font-weight: normal;" target="_blank">Click Here</a>
        </td>
    </tr>
    </tbody>
</table>

Steps to Reproduce

Create a C# project with AngleShart and AngleSharp.Css installed and run the following code

            var context = BrowsingContext.New(Configuration.Default.WithCss());
            var htmlParser = context.GetService<IHtmlParser>();
            var doc = htmlParser.ParseDocument("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"background-color: #0098;border-collapse: collapse;mso-table-lspace: 0pt;mso-table-rspace: 0pt;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;\" width=\"45%\">\r\n\r\n    <tbody>\r\n\r\n    <tr>\r\n\r\n        <td align=\"center\" style=\"color:#FFFFFF; font-family:Helvetica, Arial, sans-serif; font-size:16px; font-weight:bold; letter-spacing:-.px; line-height:100%; padding-top:7px; padding-right:8px; padding-bottom:7px; padding-left:8px;mso-line-height-rule: exactly;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;\" valign=\"middle\">\r\n\r\n            <a href=\"test.com\" style=\"color: #FFFFFF;text-decoration: none;mso-line-height-rule: exactly;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;font-weight: normal;\" target=\"_blank\">Click Here</a>\r\n\r\n        </td>\r\n\r\n    </tr>\r\n\r\n    </tbody>\r\n\r\n</table>\r\n");

Expected behavior: [What you expected to happen] Should parse the HTML without throwing. Actual behavior: [What actually happened] Throws with the following stack trace.

System.FormatException
  HResult=0x80131537
  Message=Input string was not in a correct format.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Number.ParseDouble(ReadOnlySpan`1 value, NumberStyles options, NumberFormatInfo numfmt)
   at AngleSharp.Css.Parser.UnitParser.GetLength(Unit test)
   at AngleSharp.Css.Parser.UnitParser.ParseLength(StringSource source)
   at AngleSharp.Css.Converters.StructValueConverter`1.Convert(StringSource source)
   at AngleSharp.Css.Converters.OrValueConverter.Convert(StringSource source)
   at AngleSharp.Css.Converters.OrValueConverter.Convert(StringSource source)
   at AngleSharp.Css.Converters.OrValueConverter.Convert(StringSource source)
   at AngleSharp.Css.Converters.ValueConverterExtensions.Convert(IValueConverter converter, String value)
   at AngleSharp.Css.Dom.CssProperty.set_Value(String value)
   at AngleSharp.Css.Dom.CssStyleDeclaration.SetProperty(String propertyName, String propertyValue, String priority)
   at AngleSharp.Css.Parser.CssBuilder.CreateDeclarationWith(ICssProperties properties, CssToken& token)
   at AngleSharp.Css.Parser.CssBuilder.FillDeclarations(CssStyleDeclaration style, CssToken token)
   at AngleSharp.Css.Parser.CssParser.Parse[T](String source, Func`3 create)
   at AngleSharp.Css.Dom.CssStyleDeclaration.Update(String value)
   at AngleSharp.Css.Dom.ElementCssInlineStyleExtensions.CreateStyle(IElement element, String source)
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValueLocked(TKey key, CreateValueCallback createValueCallback)
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValue(TKey key, CreateValueCallback createValueCallback)
   at AngleSharp.Css.StyleAttributeObserver.AngleSharp.Dom.IAttributeObserver.NotifyChange(IElement host, String name, String value)
   at AngleSharp.Dom.Element.SetupElement()
   at AngleSharp.Html.Parser.HtmlDomBuilder.CloseCurrentNode()
   at AngleSharp.Html.Parser.HtmlDomBuilder.InCellEndTagCell(HtmlToken token)
   at AngleSharp.Html.Parser.HtmlDomBuilder.InCell(HtmlToken token)
   at AngleSharp.Html.Parser.HtmlDomBuilder.Parse(HtmlParserOptions options)
   at AngleSharp.Html.Parser.HtmlParser.Parse(HtmlDocument document)
   at Testing.Program.<Main>d__6.MoveNext() in D:\other code\C#\Testing\Testing\Program.cs:line 137
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Testing.Program.<Main>(String[] args)

Environment details: [OS, .NET Runtime, ...] .NET core 2

FlorianRappl commented 5 years ago

Have you tried with the 0.13 beta? Or are you using AngleSharp.Css 0.12?

See https://github.com/AngleSharp/AngleSharp.Css/issues/33 (potential dup, already solved in 0.13 (beta)).

Thanks for the report!

The-Nutty commented 5 years ago

@FlorianRappl Ah thanks, yes works without issue on the 0.13 beta.

What is the expected timeline for 0.13's release?

Thanks

FlorianRappl commented 5 years ago

Should come quite soon. Not sure if its still in July, but then beginning of August.

I'll try to make room for it soon. From the remaining 2 items in AngleSharp.Css at least 1 should be covered in 0.13. Let's see.

Thanks for confirming the dup :beers:!