Skip to content

Commit 50d8e9d

Browse files
committed
WIP #136
1 parent 9fe8a86 commit 50d8e9d

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ Released on tbd.
99
- Fixed missing semicolon in `@page` rule (#135)
1010
- Fixed integer serialization of keyframe stops (#128)
1111
- Fixed ordering of rows and columns in `grid` and `grid-gap` (#137)
12-
- Fixed inclusion of CSS from stylesheets (#140)
12+
- Fixed inclusion of CSS from stylesheets (#116, #140)
1313
- Added further compactification of CSS tuples (#89, #93)
1414
- Added support for 8-digit hex color codes (#132)
15+
- Added more CSSOM possibilities and helpers (#6)
1516

1617
# 0.17.0
1718

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace AngleSharp.Css.Tests.Library
2+
{
3+
using AngleSharp.Dom;
4+
using AngleSharp.Html.Dom;
5+
using NUnit.Framework;
6+
using System.Threading.Tasks;
7+
8+
[TestFixture]
9+
public class ComputedStyleTests
10+
{
11+
[Test]
12+
[Ignore("Not implemented yet")]
13+
public async Task TransformEmToPx_Issue136()
14+
{
15+
// .With<IRenderDevice>()
16+
var config = Configuration.Default.WithCss();
17+
var context = BrowsingContext.New(config);
18+
var source = "<p>This is <span>only</span> a test.</p>";
19+
var cssSheet = "p { font-size: 1.5em }";
20+
var document = await context.OpenAsync(req => req.Content(source));
21+
var style = document.CreateElement<IHtmlStyleElement>();
22+
style.TextContent = cssSheet;
23+
document.Head.AppendChild(style);
24+
var span = document.QuerySelector("span");
25+
var fontSize = span.ComputeCurrentStyle().GetProperty("font-size");
26+
27+
Assert.AreEqual("24px", fontSize.Value);
28+
}
29+
}
30+
}

src/AngleSharp.Css/Extensions/CssOmExtensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace AngleSharp.Css.Dom
22
{
33
using AngleSharp.Css.Parser;
4+
using AngleSharp.Css.Values;
45
using AngleSharp.Text;
56
using System;
67
using System.Linq;
@@ -73,7 +74,15 @@ public static ICssValue GetValueOf(this ICssStyleRule rule, String propertyName)
7374
/// <returns>A new style declaration with the existing or computed values.</returns>
7475
public static ICssStyleDeclaration Compute(this ICssStyleDeclaration style, IRenderDevice device)
7576
{
76-
//TODO
77+
//var prop = style.GetProperty("font-size");
78+
79+
//if (prop is not null && prop.RawValue is Length length)
80+
//{
81+
// var px = length.ToPixel(device, RenderMode.Horizontal);
82+
// var prio = prop.IsImportant ? CssKeywords.Important : null;
83+
// style.SetProperty(prop.Name, $"{px}px", prio);
84+
//}
85+
7786
return style;
7887
}
7988
}

src/AngleSharp.Css/Extensions/StyleCollectionExtensions.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ public static IStyleCollection GetStyleCollection(this IWindow window)
4242
/// <returns>The style declaration containing all the declarations.</returns>
4343
public static ICssStyleDeclaration ComputeDeclarations(this IEnumerable<ICssStyleRule> rules, IElement element, String pseudoSelector = null)
4444
{
45-
var computedStyle = new CssStyleDeclaration(element.Owner?.Context);
45+
var ctx = element.Owner?.Context;
46+
var device = ctx?.GetService<IRenderDevice>();
47+
var computedStyle = new CssStyleDeclaration(ctx);
4648
var nodes = element.GetAncestors().OfType<IElement>();
4749

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

52-
if (pseudoElement != null)
54+
if (pseudoElement is not null)
5355
{
5456
element = pseudoElement;
5557
}
@@ -62,6 +64,11 @@ public static ICssStyleDeclaration ComputeDeclarations(this IEnumerable<ICssStyl
6264
computedStyle.UpdateDeclarations(rules.ComputeCascadedStyle(node));
6365
}
6466

67+
if (device is not null)
68+
{
69+
return computedStyle.Compute(device);
70+
}
71+
6572
return computedStyle;
6673
}
6774

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

93-
if (parent != null)
100+
if (parent is not null)
94101
{
95102
computedStyle.UpdateDeclarations(parent);
96103
}

0 commit comments

Comments
 (0)