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

.ToCss Throws InvalidCastException in CssGridTemplateValue.CssText #63

Closed The-Nutty closed 3 years ago

The-Nutty commented 4 years ago

Bug Report

Prerequisites

For more information, see the CONTRIBUTING guide.

Description

Unfortunately i don't have the full html just a stack trace, im not sure the html/css that has thrown this is valid but i still dont think it should throw. I would try fix this my self but im not really sure what a Constant represents in angle sharp.

System.InvalidCastException: Unable to cast object of type 'AngleSharp.Css.Values.Constant`1[System.Object]' to type 'AngleSharp.Css.Values.CssTupleValue'.
  at AngleSharp.Css.Values.CssGridTemplateValue.get_CssText()
  at AngleSharp.Css.Dom.CssProperty.get_Value()
  at AngleSharp.Css.Dom.CssProperty.ToCss(TextWriter writer, IStyleFormatter formatter)
  at AngleSharp.Css.CssStyleFormatter.AngleSharp.IStyleFormatter.BlockDeclarations(IEnumerable`1 declarations)
  at AngleSharp.Css.Dom.CssStyleDeclaration.ToCssBlock(IStyleFormatter formatter)
  at AngleSharp.Css.Dom.CssStyleDeclaration.ToCss(TextWriter writer, IStyleFormatter formatter)
  at AngleSharp.FormatExtensions.ToCss(IStyleFormattable style, IStyleFormatter formatter)
FlorianRappl commented 4 years ago

but i still dont think it should throw

Fully agreed - it should never throw (just a browser, which "tolerates" things).

I can look into this - thanks for reporting!

ikesnowy commented 3 years ago

Just got same error in my logs, here's an MWE, hope that can help.

var html = "<div style=\"grid-template-areas: none;\n  grid-template-columns: none;\n grid-template-rows: none;\"></div>";
var parser = new HtmlParser(
    new HtmlParserOptions(),
    BrowsingContext.New(
        new Configuration().WithCss(
            new CssParserOptions
            {
                IsIncludingUnknownDeclarations = true,
                IsIncludingUnknownRules = true,
                IsToleratingInvalidSelectors = true
            })));
var dom = parser.ParseDocument(html);
var div = dom.GetElementsByTagName("div")[0];
var sb = new StringBuilder();
using (var stringWriter = new StringWriter(sb))
    div.GetStyle().ToCss(stringWriter, CssStyleFormatter.Instance); // invalid cast exception
div.SetAttribute("style", sb.ToString());

exceptions:

Unhandled exception. System.InvalidCastException: Unable to cast object of type 'AngleSharp.Css.Values.Constant`1[System.Object]' to type 'AngleSharp.Css.Values.
CssTupleValue'.
   at AngleSharp.Css.Values.CssGridTemplateValue.get_CssText()
   at AngleSharp.Css.Dom.CssProperty.get_Value()
   at AngleSharp.Css.Dom.CssProperty.ToCss(TextWriter writer, IStyleFormatter formatter)
   at AngleSharp.Css.CssStyleFormatter.AngleSharp.IStyleFormatter.BlockDeclarations(IEnumerable`1 declarations)
   at AngleSharp.Css.Dom.CssStyleDeclaration.ToCssBlock(IStyleFormatter formatter)
   at AngleSharp.Css.Dom.CssStyleDeclaration.ToCss(TextWriter writer, IStyleFormatter formatter)