Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Em css properties do not give enough information to calculate value. #136

Closed
jbrayfaithlife opened this issue Mar 20, 2023 · 3 comments
Closed
Labels
Milestone

Comments

@jbrayfaithlife
Copy link

jbrayfaithlife commented Mar 20, 2023

Bug Report

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.Css for CSS support)
  • [/] Did you perform a search in the issues?

For more information, see the CONTRIBUTING guide.

Description

Computed Styles do not translate em units. This does not match the behavior of the javascript function window.getComputedStyles. In the case of font-size this becomes particularly problematic because the CssProperty does not correctly set the IsInherited property. This means that in the case of an element that has inherited an em font-size, there is no way to calculate what the "true" font-size is.

Edit: upon further review of the IsInherited property, I now understand that to mean that it is indicated whether the property has a declaration of inherit, in which case this is not the flag we would need to set to solve this problem. Either we may calculate the em units properly or indicate that the css property returned does not originate on the element itself so that the value could be calculated.

Steps to Reproduce

var config = Configuration.Default.WithCss();
var context= BrowsingContext.New(config);
var source = "<p>This is <span>only</span> a test.</p>";
var cssSheet = "p { font-size: 1.5em }";
var document = await context.OpenAsync(req => req.Content(source));
var style = document.CreateElement<IHtmlStyleElement>();
style.TextContent = cssSheet;
document.Head.AppendChild(style);
var span = document.QuerySelector("span");
var fontSize = span.ComputeCurrentStyle().GetProperty("font-size");
fontSize.IsInherited.Dump();
fontSize.Value.Dump();

Expected behavior: Either IsInherited == true, or Value == 24px. Ideally both.

Actual behavior: IsInherited == false AND Value == 1.5em

Environment details: Windows, .NET 6.0.14

Possible Solution

First, when a cssProperty is computed from the parent of the element in question, IsInherited should be set to true.

Ideally Length units would utilize the ToPixels method to translate relative units to pixels, as this is the behavior of the javascript method that is being mirrored.

@jbrayfaithlife
Copy link
Author

I was going to take a crack at solving this problem, but I am unable to assign myself to the bug.

@jbrayfaithlife jbrayfaithlife changed the title CssProperty IsInherited not set correctly in computed styles Em css properties do not give enough information to calculate value. Mar 20, 2023
@FlorianRappl FlorianRappl added this to the v1.0 milestone Jun 14, 2023
FlorianRappl added a commit that referenced this issue Jun 15, 2023
@ddunkin
Copy link

ddunkin commented Jun 16, 2023

We've worked around this by computing the values as part of rendering. https://github.com/Faithlife/AngleSharp.Css/tree/render

@FlorianRappl
Copy link
Contributor

Landed in devel.

FlorianRappl added a commit that referenced this issue Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants