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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ dotnet test src/AngleSharp.Css.Tests/AngleSharp.Css.Tests.csproj
- Existing code mixes nullable contexts (`#nullable enable` and `#nullable disable`); do not normalize unrelated files.
- Stay trim- and AOT-clean. The library sets `IsAotCompatible` on its .NETCoreApp targets, so the trim and AOT analyzers run on every build and a warning breaks it. Reflection over a runtime type (`GetType().GetProperties()`, `Assembly.GetTypes()`) is what they reject: read members off an annotated generic parameter instead (`Css<T, TProperties>` via AngleSharp's `ToDictionary<T>`), or use a fixed table. The analyzers only see the library itself; publishing a small `PublishAot` console app against it is the end-to-end check.
- In `src/AngleSharp.Css/ValueConverters.cs`, avoid static field initializers that reference converters declared later in the same file. Forward references can capture `null` during type initialization and only fail at runtime.
- Untrusted input can nest arbitrarily deep, and a stack overflow ends the process. `CssBuilder` parses nested blocks recursively and carries a depth that stops at `CssParserOptions.MaxNestingDepth`; a new recursive parsing path needs the same. The limit is a mitigation, not a fix: the recursion is still there, so a raised limit can still overflow the stack. `src/AngleSharp.Css.Tests/Parsing/DeepNesting.cs` holds the tests, which parse on a 256 KB stack.
- Untrusted input can nest arbitrarily deep, and a stack overflow ends the process. `CssBuilder` parses nested blocks recursively and carries a depth that stops at `CssParserOptions.MaxNestingDepth`, and the value and condition parsers (`CalcParser`, `ConditionParser`, `MediumParser`) stop at `CssParserOptions.MaxValueNestingDepth`, made current by `ValueNesting` while a parser builds; a new recursive parsing path needs the same. The limits are a mitigation, not a fix: the recursion is still there, so a raised limit can still overflow the stack. `src/AngleSharp.Css.Tests/Parsing/DeepNesting.cs` holds the tests, which parse on a 256 KB stack.

## 8) Documentation and onboarding references

Expand Down
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# 1.3.2

Released on Tuesday, November 3 2026.

- Improved the parser with a nesting limit for values and conditions (`MaxValueNestingDepth`) (#260) @michaelthyregod
- Improved indexing rules with deeply nested `:is()`, `:where()`, or `:matches()` (#261) @michaelthyregod
- Improved reading inherited properties on deep trees (e.g. `visibility` in `IsVisible` and `GetInnerText`) (#263) @michaelthyregod
- Fixed `min()`, `max()`, and `clamp()` being rejected in line widths such as `border-width` and the `border` shorthand
- Fixed `only` and the names of unknown schemes being rejected in `color-scheme`
- Fixed the default style of form controls to use the text color (not inherited, except for file inputs), font (`13.3333px Arial`, with a `normal` line height), padding, margins, and `box-sizing` of browsers, and added the one of `progress` and `meter`
- Fixed `font-size` keywords being relative to the parent's size, instead of absolute sizes matching browsers (`x-small` 10px, `small` 13px, `large` 18px, ...)
- Fixed `min()`, `max()` and `clamp()` being rejected in grid tracks and `background-size`/`mask-size`
- Fixed computing a `box-shadow` or `text-shadow` without a color throwing
- Added the default border of `iframe` and its `frameborder` attribute
- Added the presentational hints `cellspacing`, `cellpadding`, `border`, `align`, `valign`, `nowrap`, `background`, `hspace`, `vspace`, `size` and `face` of fonts and rules, `type` of lists, and the margin attributes of the body
- Added the quirks mode sheet of documents without a doctype (tables not inheriting font and alignment, the bottom margin of forms) and lengths without units and colors without a hash there
- Added `-webkit-center`, `-webkit-left` and `-webkit-right` for `text-align` (used for `<center>`) and `-webkit-baseline-middle` for `vertical-align`

# 1.3.1

Released on Sunday, November 1 2026.
Expand Down
2 changes: 1 addition & 1 deletion src/AngleSharp.Css.Docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@anglesharp/css",
"version": "1.3.1",
"version": "1.3.2",
"preview": true,
"description": "The doclet for the AngleSharp.Css documentation.",
"keywords": [
Expand Down
13 changes: 13 additions & 0 deletions src/AngleSharp.Css.Tests/CssColorProperties.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
namespace AngleSharp.Css.Tests
{
using AngleSharp.Css.Dom;
Expand Down Expand Up @@ -58,6 +59,18 @@ public void CaretColorRgb()
Assert.That(property.HasValue, Is.True);
}

[TestCase("color-scheme: only dark", "only dark")]
[TestCase("color-scheme: dark only", "dark only")]
[TestCase("color-scheme: only light", "only light")]
[TestCase("color-scheme: light dark only", "light dark only")]
[TestCase("color-scheme: foo dark", "foo dark")]
public void CssColorSchemeAcceptsOnlyAndUnknownSchemes(String snippet, String expected)
{
var property = ParseDeclaration(snippet);
Assert.That(property.HasValue, Is.True);
Assert.That(property.Value, Is.EqualTo(expected));
}

[Test]
public void ColorSchemeNormal()
{
Expand Down
12 changes: 12 additions & 0 deletions src/AngleSharp.Css.Tests/Declarations/CssBorderProperty.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AngleSharp.Css.Dom;
using AngleSharp.Css.Parser;
using System;

namespace AngleSharp.Css.Tests.Declarations
{
Expand Down Expand Up @@ -365,6 +366,17 @@ public void CssBorderLeftWidthThickLegal()
Assert.AreEqual("5px", property.Value);
}

[TestCase("border-width: max(1px, 2px)")]
[TestCase("border-top-width: min(1px, 2px)")]
[TestCase("border-width: clamp(1px, 2px, 3px)")]
[TestCase("border: max(1px, .0625rem) solid red")]
[TestCase("outline-width: max(1px, 2px)")]
public void CssLineWidthAcceptsComparisonFunctions(String snippet)
{
var property = ParseDeclaration(snippet);
Assert.IsTrue(property.HasValue);
}

[Test]
public void CssBorderWidthMediumLegal()
{
Expand Down
130 changes: 130 additions & 0 deletions src/AngleSharp.Css.Tests/Parsing/DeepNesting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace AngleSharp.Css.Tests.Parsing
using AngleSharp.Css.Dom;
using AngleSharp.Css.Dom.Events;
using AngleSharp.Css.Parser;
using AngleSharp.Dom;
using AngleSharp.Html.Parser;
using NUnit.Framework;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -168,5 +170,133 @@ public void NestedStyleRuleBeyondTheLimitIsDroppedAndTheDeclarationsContinue()
Assert.AreEqual("rgba(0, 128, 0, 1)", b.Style.GetPropertyValue("color"));
Assert.AreEqual(0, b.Rules.Length);
}

[TestCase("calc(", "(1px + ", ")", ")")]
[TestCase("", "calc(", ")", "")]
[TestCase("", "min(1px, ", ")", "")]
public void DeeplyNestedMathIsInvalid(String prefix, String open, String close, String suffix)
{
var value = prefix + Repeat(open, Deep) + "1px" + Repeat(close, Deep) + suffix;
var sheet = ParseOnSmallStack("a{width:" + value + "}");
Assert.AreEqual("", ((ICssStyleRule)sheet.Rules[0]).Style.GetPropertyValue("width"));
}

[Test]
public void MathNestedUpToTheLimitIsValid()
{
// calc() itself is the first level
var limit = CssParserOptions.DefaultMaxValueNestingDepth;
var style = OnSmallStack(() => ParseDeclarations("width: " + NestedCalc(limit - 1)));
Assert.AreNotEqual("", style.GetPropertyValue("width"));
style = OnSmallStack(() => ParseDeclarations("width: " + NestedCalc(limit)));
Assert.AreEqual("", style.GetPropertyValue("width"));
}

[Test]
public void DeeplyNestedMathSetThroughTheCssomIsInvalid()
{
var document = ParseDocument("<div></div>");
var style = document.QuerySelector("div").GetStyle();
OnSmallStack(() => { style.SetProperty("width", "calc(" + Repeat("(1px + ", Deep) + "1px" + Repeat(")", Deep) + ")"); return style; });
Assert.AreEqual("", style.GetPropertyValue("width"));
}

[Test]
public void DeeplyNestedSupportsConditionIsInvalid()
{
var sheet = ParseOnSmallStack("@supports " + Repeat("(", Deep) + "display: block" + Repeat(")", Deep) + " { a { color: red } } b { color: blue }");
Assert.AreEqual(1, sheet.Rules.Length);
Assert.AreEqual("b", ((ICssStyleRule)sheet.Rules[0]).SelectorText);
}

[Test]
public void SupportsConditionNestedUpToTheLimitIsValid()
{
var limit = CssParserOptions.DefaultMaxValueNestingDepth;
var valid = ParseOnSmallStack("@supports " + Repeat("(", limit) + "display: block" + Repeat(")", limit) + " { a { color: red } }");
var invalid = ParseOnSmallStack("@supports " + Repeat("(", limit + 1) + "display: block" + Repeat(")", limit + 1) + " { a { color: red } }");
Assert.AreEqual(1, valid.Rules.Length);
Assert.AreEqual(0, invalid.Rules.Length);
}

[Test]
public void DeeplyNestedMediaConditionDoesNotOverflow()
{
var sheet = ParseOnSmallStack("@media " + Repeat("(", Deep) + "min-width: 1px" + Repeat(")", Deep) + " { a { color: red } } b { color: blue }");
Assert.AreEqual("b", ((ICssStyleRule)sheet.Rules[sheet.Rules.Length - 1]).SelectorText);
}

[Test]
public void ModeratelyNestedMediaConditionIsStillValid()
{
var sheet = ParseStyleSheet("@media " + Repeat("(", 10) + "min-width: 1px" + Repeat(")", 10) + " { a { color: red } }");
Assert.AreEqual("(min-width: 1px)", ((ICssMediaRule)sheet.Rules[0]).ConditionText);
}

private static String NestedCalc(Int32 parentheses) =>
"calc(" + Repeat("(", parentheses) + "1px" + Repeat(")", parentheses) + ")";

private static String WidthOf(ICssStyleSheet sheet) =>
((ICssStyleRule)sheet.Rules[0]).Style.GetPropertyValue("width");

[Test]
public void ValueNestingLimitIsConfigurable()
{
// calc() itself is the first level
var options = new CssParserOptions { MaxValueNestingDepth = 3 };
Assert.AreNotEqual("", WidthOf(ParseStyleSheet("a{width:" + NestedCalc(2) + "}", options)));
Assert.AreEqual("", WidthOf(ParseStyleSheet("a{width:" + NestedCalc(3) + "}", options)));
}

[Test]
public void ValueNestingLimitAboveTheDefaultIsHonored()
{
var options = new CssParserOptions { MaxValueNestingDepth = 100 };
Assert.AreEqual("", WidthOf(ParseStyleSheet("a{width:" + NestedCalc(99) + "}")));
Assert.AreNotEqual("", WidthOf(ParseStyleSheet("a{width:" + NestedCalc(99) + "}", options)));
}

[Test]
public void ValueNestingLimitAppliesToSupportsConditions()
{
var options = new CssParserOptions { MaxValueNestingDepth = 3 };
var valid = ParseStyleSheet("@supports " + Repeat("(", 3) + "display: block" + Repeat(")", 3) + " { a { color: red } }", options);
var invalid = ParseStyleSheet("@supports " + Repeat("(", 4) + "display: block" + Repeat(")", 4) + " { a { color: red } }", options);
Assert.AreEqual(1, valid.Rules.Length);
Assert.AreEqual(0, invalid.Rules.Length);
}

[Test]
public void ValueNestingLimitOfTheBrowsingContextAppliesToItsStyleSheets()
{
var config = Configuration.Default.WithCss(new CssParserOptions { MaxValueNestingDepth = 3 });
var document = BrowsingContext.New(config).GetService<IHtmlParser>().ParseDocument("<style>a{width:" + NestedCalc(3) + "}</style>");
Assert.AreEqual("", WidthOf((ICssStyleSheet)document.StyleSheets[0]));
}

[Test]
public void ValueNestingLimitDoesNotOutliveItsParser()
{
ParseStyleSheet("a{width:" + NestedCalc(3) + "}", new CssParserOptions { MaxValueNestingDepth = 3 });
// a value set without a parser right after, on the same thread, gets the default again
var style = ParseDocument("<div></div>").QuerySelector("div").GetStyle();
style.SetProperty("width", NestedCalc(10));
Assert.AreNotEqual("", style.GetPropertyValue("width"));
}

[Test]
public void ValuesSetThroughTheCssomUseTheDefaultLimit()
{
var config = Configuration.Default.WithCss(new CssParserOptions { MaxValueNestingDepth = 3 });
var document = BrowsingContext.New(config).GetService<IHtmlParser>().ParseDocument("<div></div><div></div>");
var divs = document.QuerySelectorAll("div");
// an invalid value is ignored by SetProperty, so each case gets its own element
var accepted = divs[0].GetStyle();
accepted.SetProperty("width", NestedCalc(10));
Assert.AreNotEqual("", accepted.GetPropertyValue("width"));
var rejected = divs[1].GetStyle();
rejected.SetProperty("width", NestedCalc(CssParserOptions.DefaultMaxValueNestingDepth));
Assert.AreEqual("", rejected.GetPropertyValue("width"));
}
}
}
5 changes: 4 additions & 1 deletion src/AngleSharp.Css.Tests/Styling/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public void ObtainDefaultSheet()
// 72, not 70: the UA stylesheet now also generates quotes for `q::before` and
// `q::after`.
// 73, not 72: the UA stylesheet now also gives `::placeholder` its default look.
Assert.AreEqual(73, sheet.Rules.Length);
// 80, not 73: the UA stylesheet now also has rules of its own for text areas, selects,
// list boxes, file, search and date/time inputs, progress bars and meters.
// 81, not 80: the UA stylesheet now also gives `iframe` its default border.
Assert.AreEqual(81, sheet.Rules.Length);
}
}
}
Loading
Loading