anglesharp

AngleSharp Css Query For Element Style Using Path


Given only a css stylesheet, is it possible to parse and query the stylesheet using some sort of a path query to retrieve a computed style similar to what is returned from IElement.ComputeCurrentStyle()?

var css = @"
<style>
   table { font-family:foobar }
   td { font-weight:bold }
</style>
";

var config = Configuration.Default.WithDefaultLoader().WithCss();
var context = BrowsingContext.New(config);
IDocument document = await context.OpenAsync(req => req.Content(css));
ICssStyleSheet sheet = document.GetStyleSheets().OfType<ICssStyleSheet>().First()

Is it possible to do something like sheet.GetComputedStyle("body>table>tbody>tr>td") to retrieve a ICssStyleDeclaration?


Solution

  • Got a response to this on the github project issue tracker https://github.com/AngleSharp/AngleSharp.Css/issues/64

    Well to get a computed style you need to have a connection to the DOM. After all, things like selectors can only be computed in context of an available DOM structure. I guess what you are after is to "fake" the DOM, i.e., if you see some selector (like body>table>tbody>tr>td) create the minimal DOM to be able to get an element that satisfies the selector. Then use this element to get the computed style, right? Out of the box this is not possible, but I think it should be doable to write this.