This commit is contained in:
Florian Rappl 2023-06-16 00:20:11 +02:00
Родитель 9fe8a86bef
Коммит 50d8e9d028
4 изменённых файлов: 52 добавлений и 5 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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);
}
}
}

Просмотреть файл

@ -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 @@ namespace AngleSharp.Css.Dom
/// <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;
}
}

Просмотреть файл

@ -42,14 +42,16 @@ namespace AngleSharp.Css
/// <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;
}
@ -62,6 +64,11 @@ namespace AngleSharp.Css
computedStyle.UpdateDeclarations(rules.ComputeCascadedStyle(node));
}
if (device is not null)
{
return computedStyle.Compute(device);
}
return computedStyle;
}
@ -90,7 +97,7 @@ namespace AngleSharp.Css
computedStyle.SetDeclarations(element.GetStyle());
}
if (parent != null)
if (parent is not null)
{
computedStyle.UpdateDeclarations(parent);
}