Skip to content

Commit 459b48c

Browse files
authored
Merge pull request #176 from SebastianStehle/devel
Get declarations
2 parents ff33ad7 + 4c9f16a commit 459b48c

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/AngleSharp.Css.Tests/Extensions/Nesting.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ public void SimpleSelectorNestingImplicit()
2525
Assert.AreEqual("22.4px", style.GetFontSize());
2626
}
2727

28+
[Test]
29+
public void SimpleSelectorNestingImplicitDeclarations()
30+
{
31+
var source = @"<!doctype html><head><style>.foo {
32+
color: green;
33+
.bar {
34+
font-size: 1.4rem;
35+
}
36+
}</style></head><body class='foo'><div class='bar'>Larger and green";
37+
var document = ParseDocument(source);
38+
var window = document.DefaultView;
39+
var element = document.QuerySelector(".bar");
40+
var styleCollection = window.GetStyleCollection();
41+
var style = styleCollection.GetDeclarations(element);
42+
43+
Assert.AreEqual("1.4rem", style.GetFontSize());
44+
}
45+
2846
[Test]
2947
public void SimpleSelectorNestingExplicit()
3048
{

src/AngleSharp.Css/Extensions/StyleCollectionExtensions.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,26 @@ public static IStyleCollection GetStyleCollection(this IWindow window)
4242
/// <param name="pseudoSelector">The optional pseudo selector to use.</param>
4343
/// <returns>The style declaration containing all the declarations.</returns>
4444
public static ICssStyleDeclaration ComputeDeclarations(this IStyleCollection styles, IElement element, String pseudoSelector = null)
45+
{
46+
var ctx = element.Owner?.Context;
47+
var declarations = GetDeclarations(styles, element, pseudoSelector);
48+
var context = new CssComputeContext(styles.Device, ctx, declarations);
49+
50+
return declarations.Compute(context);
51+
}
52+
53+
/// <summary>
54+
/// Gets the declarations for the given element in the context of
55+
/// the specified styling rules.
56+
/// </summary>
57+
/// <param name="styles">The styles to use.</param>
58+
/// <param name="element">The element that is questioned.</param>
59+
/// <param name="pseudoSelector">The optional pseudo selector to use.</param>
60+
/// <returns>The style declaration containing all the declarations.</returns>
61+
public static ICssStyleDeclaration GetDeclarations(this IStyleCollection styles, IElement element, String pseudoSelector = null)
4562
{
4663
var ctx = element.Owner?.Context;
4764
var computedStyle = new CssStyleDeclaration(ctx);
48-
var context = new CssComputeContext(styles.Device, ctx, computedStyle);
4965
var nodes = element.GetAncestors().OfType<IElement>();
5066

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

68-
return computedStyle.Compute(context);
84+
return computedStyle;
6985
}
7086

7187
/// <summary>

0 commit comments

Comments
 (0)