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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# 1.4.2

Released on Friday, November 13 2026.

- Improved serializing a style declaration, which happens with every change of an element's `style` by script (about 7 times faster): numbers are no longer formatted with a long custom format string, property names are only escaped when they need it, a `url()` is serialized once, and shorthands are only tried when all of their longhands are declared
- Fixed `grid-gap` not setting `row-gap` and `column-gap`, whose legacy name it is, and its single value (`grid-gap: 10px`) leaving the column gap unset
- Fixed a shorthand kept as written (e.g. `overflow`) whose value refers to a custom property (`overflow: var(--overflow)`, as Elementor's containers do) leaving its longhands (`overflow-x`, `overflow-y`) at their initial value, as they were taken from the value before the substitution
- Fixed a unitless zero with a sign or fraction (`-0`, `+0`, `0.0`) not being a length, so that a style attribute written with one (e.g. `background-position: -80px -0`, as a style set to `-0px` is serialized) lost the declaration when it was parsed again
- Fixed computing a `background` whose final layer only has a color (e.g., `background: linear-gradient(...), var(--accent)`), which threw a `NullReferenceException` for the element and all of its descendants
- Fixed `place-content`, `place-items` and `place-self` with a single value only setting the align longhand (e.g., `place-content: center` left `justify-content` without a value)
- Fixed declarations in an `@media` or `@supports` rule nested in a style rule (e.g., `.a { @media (min-width: 500px) { display: grid } }`) being dropped: they apply to the elements of the style rule

# 1.4.1

Released on Sunday, November 8 2026.
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ AngleSharp.Css contains code written by (in order of first pull request / commit
* [Serhan Apaydın](https://github.com/monoblaine)
* [scasteran](https://github.com/scasteran-jw)
* [Sébastien Ros](https://github.com/sebastienros)
* [Michael Thyregod](https://github.com/michaelthyregod)

Without these awesome people AngleSharp.Css could not exist. Thanks to everyone for your contributions! :beers:

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.4.1",
"version": "1.4.2",
"preview": true,
"description": "The doclet for the AngleSharp.Css documentation.",
"keywords": [
Expand Down
16 changes: 16 additions & 0 deletions src/AngleSharp.Css.Tests/CssBoxAlignmentProperties.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace AngleSharp.Css.Tests
{
using AngleSharp.Css.Dom;
using AngleSharp.Dom;
using NUnit.Framework;
using static CssConstructionFunctions;

Expand Down Expand Up @@ -252,5 +253,20 @@ public void PlaceSelfInitialValue()
var style = rule!.Style;
Assert.That(style["place-self"], Is.Not.Null);
}

// a single value sets both longhands (justify-content had no value before)
[TestCase("place-content: center", "align-content", "center", "justify-content", "center")]
[TestCase("place-content: center space-between", "align-content", "center", "justify-content", "space-between")]
[TestCase("place-content: baseline", "align-content", "baseline", "justify-content", "start")]
[TestCase("place-items: center", "align-items", "center", "justify-items", "center")]
[TestCase("place-self: end", "align-self", "end", "justify-self", "end")]
public void PlaceShorthandSetsBothLonghands(string declaration, string align, string alignValue, string justify, string justifyValue)
{
var document = ParseDocumentOnScreen($"<!doctype html><style>div {{ display: grid; {declaration} }}</style><div></div>");
var style = document.DefaultView!.GetComputedStyle(document.QuerySelector("div")!);

Assert.AreEqual(alignValue, style.GetPropertyValue(align));
Assert.AreEqual(justifyValue, style.GetPropertyValue(justify));
}
}
}
12 changes: 12 additions & 0 deletions src/AngleSharp.Css.Tests/CssConstructionFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ internal static IHtmlDocument ParseDocument(String source, CssParserOptions opti
return parser.ParseDocument(source);
}

/// <summary>
/// Parses the document for a screen of 1280 x 800 pixels, e.g. to compute styles with
/// media queries or relative units.
/// </summary>
internal static IHtmlDocument ParseDocumentOnScreen(String source)
{
var device = new DefaultRenderDevice { DeviceWidth = 1280, DeviceHeight = 800, ViewPortWidth = 1280, ViewPortHeight = 800 };
var context = BrowsingContext.New(Configuration.Default.WithCss().WithRenderDevice(device));
var parser = context.GetService<IHtmlParser>();
return parser.ParseDocument(source);
}

internal static CssStyleRule ParseStyle(String source)
{
var parser = new CssParser();
Expand Down
23 changes: 22 additions & 1 deletion src/AngleSharp.Css.Tests/Declarations/CssBackgroundProperty.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AngleSharp.Css.Tests.Declarations
{
using AngleSharp.Dom;
using NUnit.Framework;
using static CssConstructionFunctions;

Expand Down Expand Up @@ -777,6 +778,26 @@ public void CssBackgroundPositionSlashSizeLegal()
Assert.IsTrue(property.HasValue);
Assert.AreEqual("center / cover", property.Value);
}

// the final layer may only have a color (the tractor store: a gradient over its accent color)
[TestCase("linear-gradient(180deg,#a8a8a8,#fff),#fff", "rgba(255, 255, 255, 1)")]
[TestCase("linear-gradient(180deg,#a8a8a8,#fff),var(--accent)", "rgba(0, 128, 0, 1)")]
public void CssBackgroundFinalLayerWithColorOnlyComputes(string background, string color)
{
var source = $"<!doctype html><style>div {{ --accent: green; background: {background} }}</style><div></div>";
var document = ParseDocumentOnScreen(source);
var style = document.DefaultView!.GetComputedStyle(document.QuerySelector("div")!);

Assert.AreEqual("linear-gradient(180deg, rgba(168, 168, 168, 1), rgba(255, 255, 255, 1)), none", style.GetPropertyValue("background-image"));
Assert.AreEqual(color, style.GetPropertyValue("background-color"));
}

[Test]
public void CssBackgroundFinalLayerWithColorOnlySerializesItsLayer()
{
var property = ParseDeclaration("background: url(a.png) no-repeat, red");
Assert.IsTrue(property.HasValue);
Assert.AreEqual("url(\"a.png\") no-repeat, rgba(255, 0, 0, 1)", property.Value);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#nullable disable
namespace AngleSharp.Css.Tests.Declarations
{
using AngleSharp.Css.Dom;
using AngleSharp.Dom;
using NUnit.Framework;
using static CssConstructionFunctions;

/// <summary>
/// A style changed by script is serialized into the style attribute with every change, which
/// is parsed again by whoever reads the attribute.
/// </summary>
[TestFixture]
public class StyleAttributeSerializationTests
{
[TestCase("-0", "-0")]
[TestCase("+0", "0")]
[TestCase("0.0", "0")]
[TestCase("-0.0", "-0")]
public void UnitlessZeroIsALength(string value, string expected)
{
var declarations = ParseDeclarations($"left: {value}; margin-top: {value}");

Assert.AreEqual(expected, declarations.GetPropertyValue("left"));
Assert.AreEqual(expected, declarations.GetPropertyValue("margin-top"));
}

[Test]
public void UnitlessNonZeroIsNoLength()
{
var declarations = ParseDeclarations("left: 1; margin-top: -0.5");

Assert.AreEqual(string.Empty, declarations.GetPropertyValue("left"));
Assert.AreEqual(string.Empty, declarations.GetPropertyValue("margin-top"));
}

[Test]
public void ABackgroundPositionWithANegativeZeroSurvivesTheStyleAttribute()
{
var document = ParseDocument("<div id=sprite></div>");
var sprite = document.GetElementById("sprite");

// what a game does to show another frame of a sprite
sprite.GetStyle().SetProperty("background-position", "-80px -0px");
var copy = document.CreateElement("div");
copy.SetAttribute("style", sprite.GetAttribute("style"));

Assert.AreEqual("-80px", copy.GetStyle().GetPropertyValue("background-position-x"));
Assert.AreEqual("-0", copy.GetStyle().GetPropertyValue("background-position-y"));
}

[TestCase("12.5px", "12.5px")]
[TestCase("160px", "160px")]
[TestCase("-374.900314795383px", "-374.900314795383px")]
[TestCase("0.30000000000000004px", "0.3px")]
[TestCase("0.0001px", "0.0001px")]
[TestCase("0.00001px", "0.00001px")]
[TestCase("1234.5678901234567px", "1234.56789012346px")]
[TestCase("100000000000000000px", "100000000000000000px")]
[TestCase("-2.5e3px", "-2500px")]
public void LengthsAreSerializedAsBefore(string value, string expected)
{
var declarations = ParseDeclarations($"left: {value}");

Assert.AreEqual(expected, declarations.GetPropertyValue("left"));
}

[Test]
public void TheStyleAttributeFollowsEveryChange()
{
var document = ParseDocument("<div id=sprite style='left: 160px; background-image: url(sprite.png); background-position: 0 0; width: 80px'></div>");
var sprite = document.GetElementById("sprite");
var style = sprite.GetStyle();

for (var i = 1; i < 4; i++)
{
style.SetProperty("left", $"{160 + i}px");
style.SetProperty("background-position", $"-{i * 80}px -0px");

Assert.AreEqual($"left: {160 + i}px; background-image: url(\"sprite.png\"); background-position: -{i * 80}px -0; width: 80px", sprite.GetAttribute("style"));
}
}
}
}
48 changes: 48 additions & 0 deletions src/AngleSharp.Css.Tests/Extensions/Nesting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,53 @@ public void InvalidNestedRuleIsDroppedUpToItsSemicolon(string css, string first,
Assert.AreEqual(first, sheet.Rules[0].CssText);
Assert.AreEqual(second, sheet.Rules[1].CssText);
}

// declarations directly in a nested conditional group rule apply to the elements of the
// style rule (the tractor store: .e_HomePage { @media (min-width: 500px) { display: grid } })
[TestCase(".foo { color: red; @media (min-width: 500px) { color: green } }", "rgba(0, 128, 0, 1)")]
[TestCase(".foo { color: red; @media (max-width: 499px) { color: blue } }", "rgba(255, 0, 0, 1)")]
[TestCase(".foo{color:red;@media(max-width:499px){color:blue}@media(min-width:500px){color:green}}", "rgba(0, 128, 0, 1)")]
[TestCase(".foo { @supports (display: grid) { color: green } }", "rgba(0, 128, 0, 1)")]
[TestCase(".bar { color: red } .foo { @media (min-width: 500px) { color: green } }", "rgba(0, 128, 0, 1)")]
public void NestedConditionalGroupRuleWithDeclarations(string css, string color)
{
var source = $"<!doctype html><head><style>{css}</style></head><body><div class='foo bar'>text";
var document = ParseDocumentOnScreen(source);
var style = document.DefaultView!.GetComputedStyle(document.QuerySelector("div")!);

Assert.AreEqual(color, style.GetColor());
}

[Test]
public void NestedConditionalGroupRuleWithNestedStyleRule()
{
var source = @"<!doctype html><head><style>.foo {
@media (min-width: 500px) {
.bar { color: green }
}
}</style></head><body><div class='foo'><span class='bar'>green</span></div><span class='bar'>not";
var document = ParseDocumentOnScreen(source);
var window = document.DefaultView!;
var elements = document.QuerySelectorAll(".bar");

Assert.AreEqual("rgba(0, 128, 0, 1)", window.GetComputedStyle(elements[0]).GetColor());
Assert.AreNotEqual("rgba(0, 128, 0, 1)", window.GetComputedStyle(elements[1]).GetColor());
}

[Test]
public void NestedConditionalGroupRuleInNestedStyleRule()
{
var source = @"<!doctype html><head><style>.foo {
.bar {
@media (min-width: 500px) { color: green }
}
}</style></head><body><div class='foo'><span class='bar'>green</span></div><span class='bar'>not";
var document = ParseDocumentOnScreen(source);
var window = document.DefaultView!;
var elements = document.QuerySelectorAll(".bar");

Assert.AreEqual("rgba(0, 128, 0, 1)", window.GetComputedStyle(elements[0]).GetColor());
Assert.AreNotEqual("rgba(0, 128, 0, 1)", window.GetComputedStyle(elements[1]).GetColor());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,24 @@ public async Task SubstitutedTokensDoNotMergeWithTheirNeighbors()

Assert.AreEqual("0px", style.GetPropertyValue("margin-left"));
}

[Test]
public async Task AShorthandKeptAsWrittenGivesItsLonghandsTheSubstitutedValue()
{
// as Elementor's containers do: the longhands stayed visible, so nothing was clipped
var style = await ComputeAsync("--overflow: hidden; overflow: var(--overflow)");

Assert.AreEqual("hidden", style.GetPropertyValue("overflow-x"));
Assert.AreEqual("hidden", style.GetPropertyValue("overflow-y"));
}

[Test]
public async Task AShorthandKeptAsWrittenGivesItsLonghandsTheFallbackValue()
{
var style = await ComputeAsync("overflow: var(--missing, clip auto)");

Assert.AreEqual("clip", style.GetPropertyValue("overflow-x"));
Assert.AreEqual("auto", style.GetPropertyValue("overflow-y"));
}
}
}
35 changes: 35 additions & 0 deletions src/AngleSharp.Css.Tests/Styling/GapShorthandComputedStyleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,40 @@ public void GridGapShorthandDecomposesRowFirstColumnSecond()
Assert.AreEqual("10px", style.GetPropertyValue("grid-row-gap"));
Assert.AreEqual("20px", style.GetPropertyValue("grid-column-gap"));
}

[Test]
public void GridGapShorthandSetsTheGapsOfTheStandard()
{
var document = ParseDocument("<div id=target style=\"grid-gap: 10px 20px;\"></div>");
var target = document.GetElementById("target");
var style = target.ComputeCurrentStyle();

Assert.AreEqual("10px", style.GetPropertyValue("row-gap"));
Assert.AreEqual("20px", style.GetPropertyValue("column-gap"));
}

[Test]
public void GridGapShorthandWithOneValueSetsBothGaps()
{
var document = ParseDocument("<style>#target { grid-gap: 10px }</style><div id=target></div>");
var target = document.GetElementById("target");
var style = target.ComputeCurrentStyle();

Assert.AreEqual("10px", style.GetPropertyValue("row-gap"));
Assert.AreEqual("10px", style.GetPropertyValue("column-gap"));
Assert.AreEqual("10px", style.GetPropertyValue("grid-row-gap"));
Assert.AreEqual("10px", style.GetPropertyValue("grid-column-gap"));
}

[Test]
public void GridGapAfterGapWins()
{
var document = ParseDocument("<style>#target { gap: 4px; grid-gap: 10px }</style><div id=target></div>");
var target = document.GetElementById("target");
var style = target.ComputeCurrentStyle();

Assert.AreEqual("10px", style.GetPropertyValue("row-gap"));
Assert.AreEqual("10px", style.GetPropertyValue("column-gap"));
}
}
}
Loading
Loading