Skip to content

GetInnerText Fails with Percent Styles #1692

@jtleaming

Description

@jtleaming

Describe the bug

When calling GetInnerText and exception is thrown if the html element has a percentage style on it.

Example:
This is from a minimal reproduce case.
Testing this component:

<div class="my-component" style="width: 5%">
    This component is defined in the <strong>CompLib</strong> library.
</div>

With this test:

    [Fact]
    public void Test1()
    {
        var context = new TestContext();
        var subject = context.RenderComponent<Component1>();
        var text = subject.Find(".my-component").GetInnerText();
        Assert.Contains("This", text);
    }

Results in this output:

System.ArgumentException
A non null render device with a font size is required to calculate em or rem units.
   at AngleSharp.Css.Values.CssLengthValue.CheckForValidRenderDimensions(IRenderDimensions renderDimensions, RenderMode mode)
   at AngleSharp.Css.Values.CssLengthValue.ToPixel(IRenderDimensions renderDimensions, RenderMode mode)
   at AngleSharp.Css.Values.CssLengthValue.ToPixel(IRenderDimensions renderDimensions)
   at AngleSharp.Css.Values.CssLengthValue.AngleSharp.Css.Dom.ICssValue.Compute(ICssComputeContext context)
   at AngleSharp.Css.Dom.CssProperty.Compute(ICssComputeContext context)
   at AngleSharp.Css.Dom.CssOmExtensions.Compute(ICssStyleDeclaration style, ICssComputeContext context)
   at AngleSharp.Css.StyleCollectionExtensions.ComputeDeclarations(IStyleCollection styles, IElement element, String pseudoSelector)
   at AngleSharp.Dom.WindowExtensions.GetComputedStyle(IWindow window, IElement element, String pseudo)
   at AngleSharp.Dom.CssApiExtensions.ComputeCurrentStyle(IElement element)
   at AngleSharp.Dom.ElementExtensions.InnerTextCollection(INode node, StringBuilder sb, Dictionary`2 requiredLineBreakCounts, ICssStyleDeclaration parentStyle)
   at AngleSharp.Dom.ElementExtensions.ItcInCssBox(ICssStyleDeclaration elementStyle, ICssStyleDeclaration parentStyle, INode node, StringBuilder sb, Dictionary`2 requiredLineBreakCounts)
   at AngleSharp.Dom.ElementExtensions.InnerTextCollection(INode node, StringBuilder sb, Dictionary`2 requiredLineBreakCounts, ICssStyleDeclaration parentStyle)
   at AngleSharp.Dom.ElementExtensions.GetInnerText(IElement element)

Expected behavior:

The text value gets returned.

Version info:

  • bUnit version: 1.38.5
  • .NET Runtime and Blazor version: .net9
  • OS type and version: windows

Additional context:
I filed this issue with AngleSharp at first here, as this was coming down to an issue with an AngleSharp calculation. I know what is happening, but I don't know why. AngleSharp is trying to convert the percentage value on the component to a pixel value. And in order to do that it needs to use the device width value. But in the context of BUnit the device width/height are both set to 0. Before hitting the exception the object looks like this

{
  "ViewPortWidth" : 0,
  "ViewPortHeight" : 0,
  "IsInterlaced" : false,
  "IsScripting" : true,
  "IsGrid" : false,
  "DeviceWidth" : 0,
  "DeviceHeight" : 0,
  "Resolution" : 96,
  "Frequency" : 60,
  "ColorBits" : 32,
  "MonochromeBits" : 16,
  "Category" : 0,
  "RenderWidth" : 0,
  "RenderHeight" : 0,
  "FontSize" : 16
}

It's worth pointing out that the above test passes just fine if there is no % value in the styles. I tried both with width and font-size percentages and got the same results.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds designMore design is needed before this issue should result in a feature being implemented.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions