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

NullReferenceException when computing style #159

Closed meziantou closed 7 months ago

meziantou commented 7 months ago

Prerequisites

Description

AngleSharp fails to compute style for <div style=""></div> with the latest preview 1.0.0-beta.122

System.NullReferenceException: Object reference not set to an instance of an object.
   at AngleSharp.Css.Dom.CssStyleDeclaration.ChangeDeclarations(IEnumerable`1 decls, Predicate`1 defaultSkip, Func`3 removeExisting)
   at AngleSharp.Css.Dom.CssStyleDeclaration.SetDeclarations(IEnumerable`1 decls)
   at AngleSharp.Css.StyleCollectionExtensions.ComputeCascadedStyle(IStyleCollection styles, IElement element, ICssStyleDeclaration parent)
   at AngleSharp.Css.StyleCollectionExtensions.ComputeDeclarations(IStyleCollection styles, IElement element, String pseudoSelector)
   at AngleSharp.Dom.WindowExtensions.GetComputedStyle(IWindow window, IElement element, String pseudo)
   at Program.<Main>$(String[] args) in D:\source\ConsoleApp4\ConsoleApp4\Program.cs:line 16
   at Program.<Main>(String[] args)

Steps to Reproduce

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AngleSharp" Version="1.1.1-beta.386" />
    <PackageReference Include="AngleSharp.Css" Version="1.0.0-beta.122" />
  </ItemGroup>

</Project>
using AngleSharp.Css;
using AngleSharp;
using AngleSharp.Io;
using AngleSharp.Dom;

var configuration = new AngleSharp.Configuration()
            .WithDefaultLoader()
            .WithRenderDevice(new DefaultRenderDevice() { ViewPortHeight = 100, ViewPortWidth = 100 });

var context = BrowsingContext.New(configuration);
using var response = VirtualResponse.Create(request => request.Content("""
    <div style="transform-origin: bottom"></div>
    """));
var document = await context.OpenAsync(response, CancellationToken.None);
var element = document.QuerySelector("div");
var style = document.DefaultView.GetComputedStyle(element); // throw NRE

Expected Behavior

No exception

Actual Behavior

It throws a NullReferenceException

Possible Solution / Known Workarounds

No response