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

Get declarations #176

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading