Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Source/HtmlRenderer/Core/Parse/DomParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ private void CascadeApplyStyles(CssBox box, CssData cssData, MediaQueryContext m
AssignCssBlocks(box, uaRules, true, afterInlineImportantSnapshot, afterInlineImportantCustomSnapshot, pendingVarProperties);

// 9. Resolve var() references now that every custom property's final cascaded value is known
ResolveCustomProperties(box);
ResolveDeferredVarProperties(box, pendingVarProperties);

// cascade text decoration only to boxes that actually have text so it will be handled correctly.
Expand Down Expand Up @@ -551,6 +552,31 @@ public VarResolution(bool success, string value)
public string Value;
}

/// <summary>
/// Replaces the box's custom properties with their computed values, so children inherit them
/// with var() already substituted against this box (css-variables-1 §2). Cyclic or otherwise
/// invalid ones become the guaranteed-invalid value, i.e. absent.
/// </summary>
private static void ResolveCustomProperties(CssBox box)
{
if (box.CustomProperties == null || !box.CustomProperties.Values.Any(v => v.IndexOf("var(", StringComparison.OrdinalIgnoreCase) >= 0))
return;

var resolvedCache = new Dictionary<string, string>();
var resolving = new HashSet<string>();
var cyclic = new HashSet<string>();
var computed = new Dictionary<string, string>();

foreach (var name in box.CustomProperties.Keys)
{
string value;
if (TryResolveCustomProperty(box, name, resolvedCache, resolving, cyclic, out value))
computed[name] = value;
}

box.CustomProperties = computed;
}

/// <summary>
/// Resolves every regular property deferred during the cascade's phases (see
/// <see cref="AssignCssBlock"/>) now that this box's custom properties reflect their FINAL
Expand Down
44 changes: 43 additions & 1 deletion Source/Test/HtmlRenderer.Test/Css/CustomPropertyTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System.Linq;
using HtmlRenderer.Test.CssEngineSupport;
using HtmlRenderer.Test.TestSupport;
using TheArtOfDev.HtmlRenderer.Core.CssEngine;
using TheArtOfDev.HtmlRenderer.Core.Dom;

namespace HtmlRenderer.Test.Css;

/// <summary>
/// Ported from PeachPDF.Tests/CSS/PropertyTests/CustomPropertyTests.cs. Verifies that the CSS module
/// correctly parses custom properties (--foo) and tolerates the var() function inside any property's
/// value. Pure CSSOM parse tests - no layout/cascade resolution of var() is exercised here.
/// value. The inheritance tests at the end run the cascade, the rest are pure CSSOM parse tests.
/// </summary>
[TestClass]
public sealed class CustomPropertyTests
Expand Down Expand Up @@ -129,4 +131,44 @@ public void NegativeLength_StillParsesCorrectly()
Assert.IsTrue(property.HasValue);
Assert.AreEqual("-5px", property.Value);
}

// css-variables-1 §2: the computed value of a custom property has its var() substituted, and that
// is what inherits.
[TestMethod]
public void CustomProperty_IsInheritedWithVarSubstitutedOnTheParent()
{
var p = FindBoxByTag("<div style='--a: #ff0000; --b: var(--a)'><p style='--a: #0000ff; color: var(--b)'>a</p></div>", "p");
Assert.AreEqual("rgb(255, 0, 0)", p.Color);
}

[TestMethod]
public void CustomProperty_IsInheritedWithVarSubstitutedOnEachAncestor()
{
var p = FindBoxByTag(
"<div style='--foo: 10px'><div style='--bar: calc(var(--foo) + 10px)'>" +
"<p style='--foo: calc(var(--bar) + 10px); margin-left: var(--foo)'>a</p></div></div>", "p");
Assert.AreEqual(30, p.ActualMarginLeft, 0.01);
}

[TestMethod]
public void CyclicCustomProperty_IsInheritedAsGuaranteedInvalid()
{
var p = FindBoxByTag("<div style='--a: var(--b); --b: var(--a)'><p style='--b: #ff0000; color: var(--a, #008000)'>a</p></div>", "p");
Assert.AreEqual("rgb(0, 128, 0)", p.Color);
}

[TestMethod]
public void InheritedProperty_UsingVar_InheritsTheParentsResolvedValue()
{
var p = FindBoxByTag("<div style='--a: #ff0000; color: var(--a)'><p style='--a: #0000ff'>a</p></div>", "p");
Assert.AreEqual("rgb(255, 0, 0)", p.Color);
}

private static CssBox FindBoxByTag(string body, string tag)
{
var (root, _) = LayoutHarness.Layout("<!DOCTYPE html><html><body>" + body + "</body></html>");
var box = LayoutHarness.Descendants(root).FirstOrDefault(b => b.HtmlTag != null && b.HtmlTag.Name == tag);
Assert.IsNotNull(box);
return box!;
}
}
Loading