diff --git a/Source/HtmlRenderer/Core/Parse/DomParser.cs b/Source/HtmlRenderer/Core/Parse/DomParser.cs
index 7ffb57384..36d3ad0ae 100644
--- a/Source/HtmlRenderer/Core/Parse/DomParser.cs
+++ b/Source/HtmlRenderer/Core/Parse/DomParser.cs
@@ -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.
@@ -551,6 +552,31 @@ public VarResolution(bool success, string value)
public string Value;
}
+ ///
+ /// 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.
+ ///
+ 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();
+ var resolving = new HashSet();
+ var cyclic = new HashSet();
+ var computed = new Dictionary();
+
+ foreach (var name in box.CustomProperties.Keys)
+ {
+ string value;
+ if (TryResolveCustomProperty(box, name, resolvedCache, resolving, cyclic, out value))
+ computed[name] = value;
+ }
+
+ box.CustomProperties = computed;
+ }
+
///
/// Resolves every regular property deferred during the cascade's phases (see
/// ) now that this box's custom properties reflect their FINAL
diff --git a/Source/Test/HtmlRenderer.Test/Css/CustomPropertyTests.cs b/Source/Test/HtmlRenderer.Test/Css/CustomPropertyTests.cs
index 0bbd26443..491cc8c22 100644
--- a/Source/Test/HtmlRenderer.Test/Css/CustomPropertyTests.cs
+++ b/Source/Test/HtmlRenderer.Test/Css/CustomPropertyTests.cs
@@ -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;
///
/// 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.
///
[TestClass]
public sealed class CustomPropertyTests
@@ -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("", "p");
+ Assert.AreEqual("rgb(255, 0, 0)", p.Color);
+ }
+
+ [TestMethod]
+ public void CustomProperty_IsInheritedWithVarSubstitutedOnEachAncestor()
+ {
+ var p = FindBoxByTag(
+ "", "p");
+ Assert.AreEqual(30, p.ActualMarginLeft, 0.01);
+ }
+
+ [TestMethod]
+ public void CyclicCustomProperty_IsInheritedAsGuaranteedInvalid()
+ {
+ var p = FindBoxByTag("", "p");
+ Assert.AreEqual("rgb(0, 128, 0)", p.Color);
+ }
+
+ [TestMethod]
+ public void InheritedProperty_UsingVar_InheritsTheParentsResolvedValue()
+ {
+ var p = FindBoxByTag("", "p");
+ Assert.AreEqual("rgb(255, 0, 0)", p.Color);
+ }
+
+ private static CssBox FindBoxByTag(string body, string tag)
+ {
+ var (root, _) = LayoutHarness.Layout("" + body + "");
+ var box = LayoutHarness.Descendants(root).FirstOrDefault(b => b.HtmlTag != null && b.HtmlTag.Name == tag);
+ Assert.IsNotNull(box);
+ return box!;
+ }
}