From 50d8e9d028642e95fc2563410a7d7d3fe65873db Mon Sep 17 00:00:00 2001 From: Florian Rappl Date: Fri, 16 Jun 2023 00:20:11 +0200 Subject: [PATCH] WIP #136 --- CHANGELOG.md | 3 +- .../Library/ComputedStyle.cs | 30 +++++++++++++++++++ .../Extensions/CssOmExtensions.cs | 11 ++++++- .../Extensions/StyleCollectionExtensions.cs | 13 ++++++-- 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 src/AngleSharp.Css.Tests/Library/ComputedStyle.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index c4d2d6e7..52309a98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/AngleSharp.Css.Tests/Library/ComputedStyle.cs b/src/AngleSharp.Css.Tests/Library/ComputedStyle.cs new file mode 100644 index 00000000..db479616 --- /dev/null +++ b/src/AngleSharp.Css.Tests/Library/ComputedStyle.cs @@ -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() + var config = Configuration.Default.WithCss(); + var context = BrowsingContext.New(config); + var source = "

This is only a test.

"; + var cssSheet = "p { font-size: 1.5em }"; + var document = await context.OpenAsync(req => req.Content(source)); + var style = document.CreateElement(); + style.TextContent = cssSheet; + document.Head.AppendChild(style); + var span = document.QuerySelector("span"); + var fontSize = span.ComputeCurrentStyle().GetProperty("font-size"); + + Assert.AreEqual("24px", fontSize.Value); + } + } +} diff --git a/src/AngleSharp.Css/Extensions/CssOmExtensions.cs b/src/AngleSharp.Css/Extensions/CssOmExtensions.cs index e9fce611..8ad0d928 100644 --- a/src/AngleSharp.Css/Extensions/CssOmExtensions.cs +++ b/src/AngleSharp.Css/Extensions/CssOmExtensions.cs @@ -1,6 +1,7 @@ namespace AngleSharp.Css.Dom { using AngleSharp.Css.Parser; + using AngleSharp.Css.Values; using AngleSharp.Text; using System; using System.Linq; @@ -73,7 +74,15 @@ public static ICssValue GetValueOf(this ICssStyleRule rule, String propertyName) /// A new style declaration with the existing or computed values. 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; } } diff --git a/src/AngleSharp.Css/Extensions/StyleCollectionExtensions.cs b/src/AngleSharp.Css/Extensions/StyleCollectionExtensions.cs index 91570714..d637e6f4 100644 --- a/src/AngleSharp.Css/Extensions/StyleCollectionExtensions.cs +++ b/src/AngleSharp.Css/Extensions/StyleCollectionExtensions.cs @@ -42,14 +42,16 @@ public static IStyleCollection GetStyleCollection(this IWindow window) /// The style declaration containing all the declarations. public static ICssStyleDeclaration ComputeDeclarations(this IEnumerable rules, IElement element, String pseudoSelector = null) { - var computedStyle = new CssStyleDeclaration(element.Owner?.Context); + var ctx = element.Owner?.Context; + var device = ctx?.GetService(); + var computedStyle = new CssStyleDeclaration(ctx); var nodes = element.GetAncestors().OfType(); if (!String.IsNullOrEmpty(pseudoSelector)) { var pseudoElement = element?.Pseudo(pseudoSelector.TrimStart(':')); - if (pseudoElement != null) + if (pseudoElement is not null) { element = pseudoElement; } @@ -62,6 +64,11 @@ public static ICssStyleDeclaration ComputeDeclarations(this IEnumerable