Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jun 15, 2023
1 parent 9fe8a86 commit 50d8e9d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ Released on tbd.
- Fixed missing semicolon in `@page` rule (#135)
- Fixed integer serialization of keyframe stops (#128)
- Fixed ordering of rows and columns in `grid` and `grid-gap` (#137)
- Fixed inclusion of CSS from stylesheets (#140)
- Fixed inclusion of CSS from stylesheets (#116, #140)
- Added further compactification of CSS tuples (#89, #93)
- Added support for 8-digit hex color codes (#132)
- Added more CSSOM possibilities and helpers (#6)

# 0.17.0

Expand Down
30 changes: 30 additions & 0 deletions src/AngleSharp.Css.Tests/Library/ComputedStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace AngleSharp.Css.Tests.Library
{
using AngleSharp.Dom;
using AngleSharp.Html.Dom;
using NUnit.Framework;
using System.Threading.Tasks;

[TestFixture]
public class ComputedStyleTests
{
[Test]
[Ignore("Not implemented yet")]
public async Task TransformEmToPx_Issue136()
{
// .With<IRenderDevice>()
var config = Configuration.Default.WithCss();
var context = BrowsingContext.New(config);
var source = "<p>This is <span>only</span> a test.</p>";
var cssSheet = "p { font-size: 1.5em }";
var document = await context.OpenAsync(req => req.Content(source));
var style = document.CreateElement<IHtmlStyleElement>();
style.TextContent = cssSheet;
document.Head.AppendChild(style);
var span = document.QuerySelector("span");
var fontSize = span.ComputeCurrentStyle().GetProperty("font-size");

Assert.AreEqual("24px", fontSize.Value);
}
}
}
11 changes: 10 additions & 1 deletion src/AngleSharp.Css/Extensions/CssOmExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace AngleSharp.Css.Dom
{
using AngleSharp.Css.Parser;
using AngleSharp.Css.Values;
using AngleSharp.Text;
using System;
using System.Linq;
Expand Down Expand Up @@ -73,7 +74,15 @@ public static ICssValue GetValueOf(this ICssStyleRule rule, String propertyName)
/// <returns>A new style declaration with the existing or computed values.</returns>
public static ICssStyleDeclaration Compute(this ICssStyleDeclaration style, IRenderDevice device)
{
//TODO
//var prop = style.GetProperty("font-size");

//if (prop is not null && prop.RawValue is Length length)
//{
// var px = length.ToPixel(device, RenderMode.Horizontal);
// var prio = prop.IsImportant ? CssKeywords.Important : null;
// style.SetProperty(prop.Name, $"{px}px", prio);
//}

return style;
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/AngleSharp.Css/Extensions/StyleCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ public static IStyleCollection GetStyleCollection(this IWindow window)
/// <returns>The style declaration containing all the declarations.</returns>
public static ICssStyleDeclaration ComputeDeclarations(this IEnumerable<ICssStyleRule> rules, IElement element, String pseudoSelector = null)
{
var computedStyle = new CssStyleDeclaration(element.Owner?.Context);
var ctx = element.Owner?.Context;
var device = ctx?.GetService<IRenderDevice>();
var computedStyle = new CssStyleDeclaration(ctx);
var nodes = element.GetAncestors().OfType<IElement>();

if (!String.IsNullOrEmpty(pseudoSelector))
{
var pseudoElement = element?.Pseudo(pseudoSelector.TrimStart(':'));

if (pseudoElement != null)
if (pseudoElement is not null)
{
element = pseudoElement;
}
Expand All @@ -62,6 +64,11 @@ public static ICssStyleDeclaration ComputeDeclarations(this IEnumerable<ICssStyl
computedStyle.UpdateDeclarations(rules.ComputeCascadedStyle(node));
}

if (device is not null)
{
return computedStyle.Compute(device);
}

return computedStyle;
}

Expand Down Expand Up @@ -90,7 +97,7 @@ public static ICssStyleDeclaration ComputeCascadedStyle(this IEnumerable<ICssSty
computedStyle.SetDeclarations(element.GetStyle());
}

if (parent != null)
if (parent is not null)
{
computedStyle.UpdateDeclarations(parent);
}
Expand Down

0 comments on commit 50d8e9d

Please sign in to comment.