From 734116199c53d7bfa5085da58ecedb0356ddb36c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20D=C3=BCrrenberger?= Date: Tue, 10 Nov 2026 12:35:58 +0100 Subject: [PATCH] Apply style rules inside true @supports blocks --- Source/HtmlRenderer/Core/CssData.cs | 14 +++- .../ConditionGroupingRuleIntegrationTests.cs | 73 +++++++++++++------ 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/Source/HtmlRenderer/Core/CssData.cs b/Source/HtmlRenderer/Core/CssData.cs index d7ae1ca52..5957e3d9f 100644 --- a/Source/HtmlRenderer/Core/CssData.cs +++ b/Source/HtmlRenderer/Core/CssData.cs @@ -253,7 +253,7 @@ private void EnsureIndex() /// /// Walks a rule list in true document order, recursing into @media blocks, @layer - /// blocks and CSS-Nesting child rules (any nesting depth) in place, and buckets every style rule + /// blocks, @supports blocks whose condition is true and CSS-Nesting child rules (any nesting depth) in place, and buckets every style rule /// found - assigning each an ever-increasing and recording /// the chain of enclosing media conditions it's nested under, if any. /// @@ -327,9 +327,15 @@ private static void IndexRules( // rule inside a layer would silently stop applying. IndexRules(layerRule.Rules, isUserAgent, enclosingMedia, tagIndex, classIndex, idIndex, universal, keys, ref order); } - // Everything else (@layer statements, @property, @font-palette-values, @supports, - // @container, ...) contributes no style rules to the cascade, so falling through is the - // correct handling rather than an oversight. + else if (rule is ISupportsRule supportsRule) + { + // css-conditional-3 #at-supports: the condition doesn't depend on the device, so it + // is decided once here instead of per box like @media. + if (supportsRule.Condition.Check()) + IndexRules(supportsRule.Rules, isUserAgent, enclosingMedia, tagIndex, classIndex, idIndex, universal, keys, ref order); + } + // Everything else (@layer statements, @property, @font-palette-values, @container, ...) + // contributes no style rules to the cascade. } } diff --git a/Source/Test/HtmlRenderer.Test/Css/Selectors/ConditionGroupingRuleIntegrationTests.cs b/Source/Test/HtmlRenderer.Test/Css/Selectors/ConditionGroupingRuleIntegrationTests.cs index 79575aae8..4a0180bea 100644 --- a/Source/Test/HtmlRenderer.Test/Css/Selectors/ConditionGroupingRuleIntegrationTests.cs +++ b/Source/Test/HtmlRenderer.Test/Css/Selectors/ConditionGroupingRuleIntegrationTests.cs @@ -5,21 +5,9 @@ namespace HtmlRenderer.Test.Css.Selectors; /// -/// Ported from PeachPDF.Tests/CSS/ConditionGroupingRuleIntegrationTests.cs. @supports and -/// @container parse and have working condition-evaluation code -/// (, -/// , -/// ), but -/// TheArtOfDev.HtmlRenderer.Core.CssData.cs:330-332 explicitly documents that the real per-box -/// cascade NEVER applies their inner rules, regardless of whether the condition is true or false - this -/// is a deliberate, still-open scope gap (unlike PeachPDF, which genuinely evaluates and applies both). -/// -/// Only the four cases that assert the inner rule DOES apply are ported, as d - they -/// would genuinely fail against the real engine today. The mirror-image "does NOT apply" cases (and the -/// AND/OR combination case, which also expects the block does not apply) are NOT ported: they'd pass, -/// but only vacuously (inner rules never apply here regardless of the condition), so they're not -/// meaningful regression coverage. Supports_SvgOnlyProperty_AppliesToAnInlineSvgElement is not -/// ported either - HTML-Renderer has no SVG rendering pipeline. +/// Ported from PeachPDF.Tests/CSS/ConditionGroupingRuleIntegrationTests.cs. +/// Supports_SvgOnlyProperty_AppliesToAnInlineSvgElement is not ported, HTML-Renderer has no SVG +/// rendering pipeline. The cascade doesn't apply @container's inner rules yet. /// [TestClass] public sealed class ConditionGroupingRuleIntegrationTests @@ -27,8 +15,6 @@ public sealed class ConditionGroupingRuleIntegrationTests private static readonly RColor Blue = RColor.FromArgb(0, 0, 255); [TestMethod] - [Ignore("not yet spec compliant - CssData.cs:330-332 documents that the real per-box cascade never " + - "applies @supports'/@container's inner rules, regardless of the condition. See class doc comment.")] public void Supports_InnerRules_Apply_WhenConditionIsSupported() { var html = Html( @@ -39,8 +25,18 @@ public void Supports_InnerRules_Apply_WhenConditionIsSupported() } [TestMethod] - [Ignore("not yet spec compliant - CssData.cs:330-332 documents that the real per-box cascade never " + - "applies @supports'/@container's inner rules, regardless of the condition. See class doc comment.")] + public void SupportsNot_Applies_WhenDeclarationIsInvalid() + { + var html = Html( + "@supports not (display: no-such-value) { p { color: #0000ff; } }", + "

text

"); + var box = FindBoxByTag(html, "p"); + Assert.AreEqual(Blue, box.ActualColor); + } + + [TestMethod] + [Ignore("not yet spec compliant - DeclarationCondition.Check() reports animation-name as supported, " + + "see SupportsTests.SupportsAnimationNameRule_NoLongerFalsePositive")] public void SupportsNot_Fallback_Applies_WhenFeatureIsUnsupported() { var html = Html( @@ -54,8 +50,6 @@ public void SupportsNot_Fallback_Applies_WhenFeatureIsUnsupported() // block and its not-guarded fallback wrapping the same declaration, both present in one // stylesheet. Real evaluation must let exactly one of them win, never both and never neither. [TestMethod] - [Ignore("not yet spec compliant - CssData.cs:330-332 documents that the real per-box cascade never " + - "applies @supports'/@container's inner rules, regardless of the condition. See class doc comment.")] public void SupportsEnhancedBlockAndNotGuardedFallback_ExactlyOneApplies() { var html = Html( @@ -68,8 +62,41 @@ public void SupportsEnhancedBlockAndNotGuardedFallback_ExactlyOneApplies() } [TestMethod] - [Ignore("not yet spec compliant - CssData.cs:330-332 documents that the real per-box cascade never " + - "applies @supports'/@container's inner rules, regardless of the condition. See class doc comment.")] + public void Supports_InnerRules_DoNotApply_WhenConditionIsNotSupported() + { + var html = Html( + "p { color: #ff0000; } @supports (display: no-such-value) { p { color: #0000ff; } }", + "

text

"); + var box = FindBoxByTag(html, "p"); + Assert.AreEqual(RColor.FromArgb(255, 0, 0), box.ActualColor); + } + + [TestMethod] + public void Supports_NestedWithMedia_AppliesOnlyWhenAllConditionsHold() + { + var html = Html( + "p { color: #ff0000; } " + + "@supports (display: block) { @media screen { @supports (display: block) { p { color: #0000ff; } } } } " + + "@media screen { @supports (display: no-such-value) { p { color: #00ff00; } } } " + + "@supports (display: block) { @media print { p { color: #00ff00; } } }", + "

text

"); + var box = FindBoxByTag(html, "p"); + Assert.AreEqual(Blue, box.ActualColor); + } + + [TestMethod] + public void Supports_InsideLayer_AppliesOnlyWhenConditionIsSupported() + { + var html = Html( + "@layer base { @supports (display: block) { p { color: #0000ff; } } " + + "@supports (display: no-such-value) { p { color: #00ff00; } } }", + "

text

"); + var box = FindBoxByTag(html, "p"); + Assert.AreEqual(Blue, box.ActualColor); + } + + [TestMethod] + [Ignore("not yet spec compliant - the cascade never applies @container's inner rules")] public void Container_InnerRules_Apply_WhenAnEligibleAncestorContainerMatches() { var html = Html(