From 4c9f16a578ae82fa0d0a7dedf39c6be8034a7290 Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Thu, 19 Sep 2024 15:37:42 +0200 Subject: [PATCH] Get declarations --- .../Extensions/Nesting.cs | 18 +++++++++++++++++ .../Extensions/StyleCollectionExtensions.cs | 20 +++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/AngleSharp.Css.Tests/Extensions/Nesting.cs b/src/AngleSharp.Css.Tests/Extensions/Nesting.cs index 0a5b0c8..577f386 100644 --- a/src/AngleSharp.Css.Tests/Extensions/Nesting.cs +++ b/src/AngleSharp.Css.Tests/Extensions/Nesting.cs @@ -25,6 +25,24 @@ public void SimpleSelectorNestingImplicit() Assert.AreEqual("22.4px", style.GetFontSize()); } + [Test] + public void SimpleSelectorNestingImplicitDeclarations() + { + var source = @"
Larger and green"; + var document = ParseDocument(source); + var window = document.DefaultView; + var element = document.QuerySelector(".bar"); + var styleCollection = window.GetStyleCollection(); + var style = styleCollection.GetDeclarations(element); + + Assert.AreEqual("1.4rem", style.GetFontSize()); + } + [Test] public void SimpleSelectorNestingExplicit() { diff --git a/src/AngleSharp.Css/Extensions/StyleCollectionExtensions.cs b/src/AngleSharp.Css/Extensions/StyleCollectionExtensions.cs index f07ca5b..09e3362 100644 --- a/src/AngleSharp.Css/Extensions/StyleCollectionExtensions.cs +++ b/src/AngleSharp.Css/Extensions/StyleCollectionExtensions.cs @@ -42,10 +42,26 @@ public static IStyleCollection GetStyleCollection(this IWindow window) /// The optional pseudo selector to use. /// The style declaration containing all the declarations. public static ICssStyleDeclaration ComputeDeclarations(this IStyleCollection styles, IElement element, String pseudoSelector = null) + { + var ctx = element.Owner?.Context; + var declarations = GetDeclarations(styles, element, pseudoSelector); + var context = new CssComputeContext(styles.Device, ctx, declarations); + + return declarations.Compute(context); + } + + /// + /// Gets the declarations for the given element in the context of + /// the specified styling rules. + /// + /// The styles to use. + /// The element that is questioned. + /// The optional pseudo selector to use. + /// The style declaration containing all the declarations. + public static ICssStyleDeclaration GetDeclarations(this IStyleCollection styles, IElement element, String pseudoSelector = null) { var ctx = element.Owner?.Context; var computedStyle = new CssStyleDeclaration(ctx); - var context = new CssComputeContext(styles.Device, ctx, computedStyle); var nodes = element.GetAncestors().OfType(); if (!String.IsNullOrEmpty(pseudoSelector)) @@ -65,7 +81,7 @@ public static ICssStyleDeclaration ComputeDeclarations(this IStyleCollection sty computedStyle.UpdateDeclarations(styles.ComputeCascadedStyle(node)); } - return computedStyle.Compute(context); + return computedStyle; } ///