Skip to content

StyleDeclarationExtensions.GetColor throws when there is no color assigned #183

@Shayan-To

Description

@Shayan-To

Prerequisites

  • Can you reproduce the problem in a MWE?
  • Are you running the latest version of AngleSharp.Css?
  • Did you check the FAQs to see if that helps you?
  • Are you reporting to the correct repository? (there are multiple AngleSharp libraries, e.g., AngleSharp.Xml for Xml support)
  • Did you perform a search in the issues?

Description

I want to extract text and style from an html document, but when I try to use element.ComputeCurrentStyle().GetColor(), it throws an exception.

Steps to Reproduce

    using var angle = AngleSharp.BrowsingContext.New();
    var parser = angle.GetService<IHtmlParser>()!;

    using var doc = await parser.ParseDocumentAsync("<div>hi</div>");

    Console.WriteLine(doc.QuerySelector("div").ComputeCurrentStyle().GetColor());

Expected Behavior

Outputs null or a default color.

Actual Behavior

AngleSharp.Css.Dom.CssStyleDeclaration
System.InvalidOperationException: Sequence contains no elements
   at System.Linq.ThrowHelper.ThrowNoElementsException()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at AngleSharp.BrowsingContextExtensions.GetFactory[TFactory](IBrowsingContext context)
   at AngleSharp.Css.Dom.CssStyleDeclaration.TryCreateShorthand(String shorthandName, IEnumerable`1 serialized, List`1 usedProperties, Boolean force)
   at AngleSharp.Css.Dom.CssStyleDeclaration.GetPropertyShorthand(String name)
   at AngleSharp.Css.Dom.CssStyleDeclaration.GetProperty(String name)
   at AngleSharp.Css.Dom.CssStyleDeclaration.GetPropertyValue(String propertyName)
   at AngleSharp.Css.Dom.StyleDeclarationExtensions.GetColor(ICssStyleDeclaration style)

Possible Solution / Known Workarounds

Is there a way to assign a default color to the document so it can fall back to it when no color is assigned to the elements?
Is there a way to have some browser default styles be loaded? Even manually?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions