Skip to content

Commit

Permalink
Merge pull request #176 from SebastianStehle/devel
Browse files Browse the repository at this point in the history
Get declarations
  • Loading branch information
FlorianRappl authored Sep 20, 2024
2 parents ff33ad7 + 4c9f16a commit 459b48c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/AngleSharp.Css.Tests/Extensions/Nesting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ public void SimpleSelectorNestingImplicit()
Assert.AreEqual("22.4px", style.GetFontSize());
}

[Test]
public void SimpleSelectorNestingImplicitDeclarations()
{
var source = @"<!doctype html><head><style>.foo {
color: green;
.bar {
font-size: 1.4rem;
}
}</style></head><body class='foo'><div class='bar'>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()
{
Expand Down
20 changes: 18 additions & 2 deletions src/AngleSharp.Css/Extensions/StyleCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,26 @@ public static IStyleCollection GetStyleCollection(this IWindow window)
/// <param name="pseudoSelector">The optional pseudo selector to use.</param>
/// <returns>The style declaration containing all the declarations.</returns>
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);
}

/// <summary>
/// Gets the declarations for the given element in the context of
/// the specified styling rules.
/// </summary>
/// <param name="styles">The styles to use.</param>
/// <param name="element">The element that is questioned.</param>
/// <param name="pseudoSelector">The optional pseudo selector to use.</param>
/// <returns>The style declaration containing all the declarations.</returns>
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<IElement>();

if (!String.IsNullOrEmpty(pseudoSelector))
Expand All @@ -65,7 +81,7 @@ public static ICssStyleDeclaration ComputeDeclarations(this IStyleCollection sty
computedStyle.UpdateDeclarations(styles.ComputeCascadedStyle(node));
}

return computedStyle.Compute(context);
return computedStyle;
}

/// <summary>
Expand Down

0 comments on commit 459b48c

Please sign in to comment.